~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/tutorial/exercise_service.py

Merge enforce-naming-constraints. Users, subjects, semesters, projects, groups exercises and worksheets now have restricted name character sets.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import ivle.database
22
22
from ivle.database import Exercise, TestSuite, TestCase, \
23
23
                          TestSuiteVar, TestCasePart
 
24
from ivle.webapp.base.forms import VALID_URL_NAME
24
25
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
25
26
                                   require_permission)
26
27
from ivle.webapp.errors import NotFound, BadRequest
47
48
    
48
49
    @named_operation('save')
49
50
    def add_exercise(self, req, identifier, name, description, partial, solution, include, num_rows):
50
 
    
 
51
        if not VALID_URL_NAME.match(identifier):
 
52
            raise BadRequest(
 
53
                "Exercise names must consist of a lowercase alphanumeric "
 
54
                "character followed by any number of lowercase alphanumerics, "
 
55
                "., +, - or _.")
 
56
 
 
57
        if req.store.find(Exercise, id=unicode(identifier)).one():
 
58
            raise BadRequest("An exercise with that URL name already exists.")
 
59
 
51
60
        new_exercise = Exercise()
52
61
        new_exercise.id = unicode(identifier)
53
62
        new_exercise.name = unicode(name)