|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Views for Requests. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
|
23 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
24 ] |
|
25 |
|
26 |
|
27 from google.appengine.api import users |
|
28 |
|
29 from django import forms |
|
30 from django.utils.translation import ugettext_lazy |
|
31 |
|
32 from soc.logic import dicts |
|
33 from soc.logic import validate |
|
34 from soc.logic.models import sponsor as sponsor_logic |
|
35 from soc.logic.models import user as user_logic |
|
36 from soc.views import helper |
|
37 from soc.views.helper import widgets |
|
38 from soc.views.models import base |
|
39 |
|
40 import soc.models.request |
|
41 import soc.logic.models.request |
|
42 import soc.logic.dicts |
|
43 import soc.views.helper |
|
44 import soc.views.helper.widgets |
|
45 |
|
46 class CreateForm(helper.forms.BaseForm): |
|
47 """Django form displayed when Developer creates a Request. |
|
48 """ |
|
49 |
|
50 class Meta: |
|
51 model = soc.models.request.Request |
|
52 |
|
53 #: list of model fields which will *not* be gathered by the form |
|
54 exclude = ['inheritance_line', 'requester', 'to', 'role', 'declined'] |
|
55 |
|
56 role = forms.CharField(widget=helper.widgets.ReadOnlyInput()) |
|
57 |
|
58 user = forms.CharField( |
|
59 label=soc.models.request.Request.requester.verbose_name, |
|
60 help_text=soc.models.request.Request.requester.help_text, |
|
61 widget=helper.widgets.ReadOnlyInput()) |
|
62 |
|
63 group = forms.CharField( |
|
64 label=soc.models.request.Request.to.verbose_name, |
|
65 help_text=soc.models.request.Request.to.help_text, |
|
66 widget=helper.widgets.ReadOnlyInput()) |
|
67 |
|
68 def clean_user(self): |
|
69 self.cleaned_data['requester'] = user_logic.logic.getForFields( |
|
70 {'link_name': self.cleaned_data['user']}, unique=True) |
|
71 return self.cleaned_data['user'] |
|
72 |
|
73 def clean_group(self): |
|
74 self.cleaned_data['to'] = sponsor_logic.logic.getFromFields( |
|
75 link_name=self.cleaned_data['group']) |
|
76 return self.cleaned_data['group'] |
|
77 |
|
78 class EditForm(CreateForm): |
|
79 """Django form displayed when Developer edits a Request. |
|
80 """ |
|
81 |
|
82 pass |
|
83 |
|
84 |
|
85 class View(base.View): |
|
86 """View methods for the Docs model |
|
87 """ |
|
88 |
|
89 def __init__(self, original_params=None, original_rights=None): |
|
90 """Defines the fields and methods required for the base View class |
|
91 to provide the user with list, public, create, edit and delete views. |
|
92 |
|
93 Params: |
|
94 original_params: a dict with params for this View |
|
95 original_rights: a dict with right definitions for this View |
|
96 """ |
|
97 |
|
98 self._logic = soc.logic.models.request.logic |
|
99 |
|
100 params = {} |
|
101 rights = {} |
|
102 |
|
103 params['name'] = "Request" |
|
104 params['name_short'] = "Request" |
|
105 params['name_plural'] = "Requests" |
|
106 |
|
107 params['edit_form'] = EditForm |
|
108 params['create_form'] = CreateForm |
|
109 |
|
110 # TODO(tlarsen) Add support for Django style template lookup |
|
111 params['edit_template'] = 'soc/models/edit.html' |
|
112 params['public_template'] = 'soc/request/public.html' |
|
113 params['list_template'] = 'soc/models/list.html' |
|
114 |
|
115 params['lists_template'] = { |
|
116 'list_main': 'soc/list/list_main.html', |
|
117 'list_pagination': 'soc/list/list_pagination.html', |
|
118 'list_row': 'soc/request/list/request_row.html', |
|
119 'list_heading': 'soc/request/list/request_heading.html', |
|
120 } |
|
121 |
|
122 params['sidebar_defaults'] = [('/%s/list', 'List %(plural)s')] |
|
123 |
|
124 params['delete_redirect'] = '/request/list' |
|
125 params['create_redirect'] = '/request' |
|
126 |
|
127 params['save_message'] = [ugettext_lazy('Request saved.')] |
|
128 |
|
129 params['edit_params'] = { |
|
130 self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED, |
|
131 } |
|
132 |
|
133 rights['list'] = [helper.access.checkIsDeveloper] |
|
134 rights['delete'] = [helper.access.checkIsDeveloper] |
|
135 |
|
136 params = dicts.merge(original_params, params) |
|
137 rights = dicts.merge(original_rights, rights) |
|
138 |
|
139 base.View.__init__(self, rights=rights, params=params) |
|
140 |
|
141 def _editSeed(self, request, seed): |
|
142 """See base.View._editGet(). |
|
143 """ |
|
144 |
|
145 # fill in the email field with the data from the entity |
|
146 seed['user'] = seed['user_ln'] |
|
147 seed['group'] = seed['group_ln'] |
|
148 |
|
149 def _editGet(self, request, entity, form): |
|
150 """See base.View._editGet(). |
|
151 """ |
|
152 |
|
153 # fill in the email field with the data from the entity |
|
154 form.fields['user'].initial = entity.requester.link_name |
|
155 form.fields['group'].initial = entity.to.link_name |
|
156 |
|
157 def _editPost(self, request, entity, fields): |
|
158 """See base.View._editPost(). |
|
159 """ |
|
160 |
|
161 # fill in the account field with the user created from email |
|
162 fields['user_ln'] = fields['requester'].link_name |
|
163 fields['group_ln'] = fields['to'].link_name |
|
164 |
|
165 |
|
166 view = View() |
|
167 |
|
168 create = view.create |
|
169 edit = view.edit |
|
170 delete = view.delete |
|
171 list = view.list |
|
172 public = view.public |