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

« back to all changes in this revision

Viewing changes to ivle/webapp/admin/subject.py

  • Committer: William Grant
  • Date: 2009-03-03 06:57:03 UTC
  • Revision ID: grantw@unimelb.edu.au-20090303065703-7iemguctczu20zw4
Refuse a +enrol if the user is already enrolled. This stops overwriting
of privileges, and is probably a good idea.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
        if user:
66
66
            return user
67
67
        else:
68
 
            raise formencode.Invalid('That user does not exist', value, state)
 
68
            raise formencode.Invalid('User does not exist', value, state)
 
69
 
 
70
 
 
71
class NoEnrolmentValidator(formencode.FancyValidator):
 
72
    """A FormEncode validator that ensures absence of an enrolment.
 
73
 
 
74
    The state must have an 'offering' attribute.
 
75
    """
 
76
    def _to_python(self, value, state):
 
77
        if state.offering.get_enrolment(value):
 
78
            raise formencode.Invalid('User already enrolled', value, state)
 
79
        return value
69
80
 
70
81
 
71
82
class EnrolSchema(formencode.Schema):
72
 
    user = UserValidator()
 
83
    user = formencode.All(NoEnrolmentValidator(), UserValidator())
73
84
 
74
85
 
75
86
class EnrolView(XHTMLView):
98
109
            data = dict(req.get_fieldstorage())
99
110
            try:
100
111
                validator = EnrolSchema()
 
112
                req.offering = self.context # XXX: Getting into state.
101
113
                data = validator.to_python(data, state=req)
102
114
                self.context.enrol(data['user'])
103
115
                req.store.commit()