[3526] | 1 | import grok |
---|
[3878] | 2 | from zope.component import getUtility, getMultiAdapter |
---|
| 3 | from zope.component.interfaces import Invalid |
---|
| 4 | from zope.exceptions import DuplicationError |
---|
[3526] | 5 | from waeup.basecontainer import BaseContainer |
---|
[3828] | 6 | from waeup.interfaces import IFacultyContainer |
---|
[3878] | 7 | from waeup.viewlets import MainArea, Index, Add |
---|
[3526] | 8 | from interfaces import IFaculty |
---|
[3527] | 9 | from faculty import Faculty |
---|
[3526] | 10 | |
---|
[3878] | 11 | from zope.schema import ValidationError |
---|
[3828] | 12 | |
---|
[3878] | 13 | class FacultyContainer(grok.Container): |
---|
| 14 | |
---|
[3828] | 15 | grok.implements(IFacultyContainer) |
---|
[3526] | 16 | |
---|
| 17 | name = u'Faculties' |
---|
[3828] | 18 | |
---|
[3878] | 19 | def getId(self, faculty): |
---|
| 20 | """We need a unified id for each faculty, which acts as key. |
---|
| 21 | """ |
---|
| 22 | id = faculty.name |
---|
| 23 | id = id.replace(' ', '') |
---|
| 24 | return id.lower() |
---|
| 25 | |
---|
| 26 | def addFaculty(self, faculty): |
---|
| 27 | if not IFaculty.providedBy(faculty): |
---|
| 28 | raise TypeError('FacultyContainers contain only IFaculty instances') |
---|
| 29 | id = self.getId(faculty) |
---|
| 30 | self[id] = faculty |
---|
| 31 | |
---|
| 32 | # We register FacultyContainer as a utility. This way we can |
---|
| 33 | # call getUtility(IFacultyContainer) from anywhere in the code and |
---|
| 34 | # get an instance of FacultyContainer. |
---|
[3828] | 35 | grok.global_utility(FacultyContainer, provides=IFacultyContainer) |
---|
[3878] | 36 | |
---|
| 37 | class Content(grok.Viewlet): |
---|
| 38 | grok.viewletmanager(MainArea) |
---|
| 39 | grok.context(IFacultyContainer) |
---|
| 40 | grok.view(Index) |
---|
| 41 | |
---|
| 42 | def getFaculties(self): |
---|
| 43 | """Convinience method to create a sorted list of faculties. |
---|
| 44 | |
---|
| 45 | It provides a list of dicts with entries for all data needed by |
---|
| 46 | usual list templates. |
---|
| 47 | """ |
---|
| 48 | result = [] |
---|
| 49 | for key, val in self.context.items(): |
---|
| 50 | result.append(dict(id=key, name=val.name)) |
---|
| 51 | return result |
---|
| 52 | |
---|
| 53 | class AddFacultyForm(grok.AddForm): |
---|
| 54 | grok.context(IFacultyContainer) |
---|
| 55 | form_fields = grok.AutoFields(IFaculty) |
---|
| 56 | |
---|
| 57 | @grok.action('Add faculty') |
---|
| 58 | def addFaculty(self, **data): |
---|
| 59 | faculty = getUtility(IFaculty) |
---|
| 60 | self.applyData(faculty, **data) |
---|
| 61 | try: |
---|
| 62 | self.context.addFaculty(faculty) |
---|
| 63 | except DuplicationError: |
---|
| 64 | self.status = Invalid('The name chosen already exists ' |
---|
| 65 | 'in the database') |
---|
| 66 | return |
---|
| 67 | self.redirect(self.url(self.context)) |
---|
| 68 | |
---|
| 69 | class AddFaculty(grok.Viewlet): |
---|
| 70 | """A viewlet that wraps the `AddFacultyForm`. |
---|
| 71 | """ |
---|
| 72 | grok.viewletmanager(MainArea) |
---|
| 73 | grok.context(IFacultyContainer) |
---|
| 74 | grok.view(Add) |
---|
| 75 | grok.require('waeup.manageUniversity') |
---|
| 76 | |
---|
| 77 | def update(self, **data): |
---|
| 78 | self.form = getMultiAdapter((self.context, self.request), |
---|
| 79 | name='addfacultyform') |
---|
| 80 | result = self.form.update_form() |
---|
| 81 | if self.request.method == 'POST': |
---|
| 82 | #app = get_application(self.context) |
---|
| 83 | #self.__parent__.redirect(self.__parent__.url(obj=app)) |
---|
| 84 | pass |
---|
| 85 | return |
---|
| 86 | |
---|
| 87 | def render(self): |
---|
| 88 | result = self.form.render() |
---|
| 89 | try: |
---|
| 90 | # strip all except the form part... |
---|
| 91 | result = re.match('^.+\(<form[^\>]+>.*</form>).+$', result, |
---|
| 92 | re.DOTALL).groups()[0] |
---|
| 93 | except: |
---|
| 94 | # except there is no such part... |
---|
| 95 | pass |
---|
| 96 | return result |
---|