25 |
25 |
26 |
26 |
27 from google.appengine.api import users |
27 from google.appengine.api import users |
28 |
28 |
29 from django import forms |
29 from django import forms |
|
30 from django import http |
|
31 from django.core import urlresolvers |
30 from django.utils.translation import ugettext_lazy |
32 from django.utils.translation import ugettext_lazy |
31 |
33 |
|
34 from soc.logic import cleaning |
32 from soc.logic import dicts |
35 from soc.logic import dicts |
33 from soc.logic.models import sponsor as sponsor_logic |
36 from soc.logic.models import sponsor as sponsor_logic |
34 from soc.logic.models import user as user_logic |
37 from soc.logic.models import user as user_logic |
35 from soc.views import helper |
38 from soc.views import helper |
36 from soc.views import out_of_band |
39 from soc.views import out_of_band |
37 from soc.views.helper import access |
40 from soc.views.helper import access |
38 from soc.views.helper import decorators |
41 from soc.views.helper import decorators |
39 from soc.views.helper import redirects |
42 from soc.views.helper import redirects |
|
43 from soc.views.helper import responses |
|
44 from soc.views.helper import widgets |
40 from soc.views.models import base |
45 from soc.views.models import base |
41 |
46 |
42 import soc.models.request |
47 import soc.models.request |
43 import soc.logic.models.request |
48 import soc.logic.models.request |
44 import soc.logic.dicts |
49 import soc.logic.dicts |
45 import soc.views.helper |
50 import soc.views.helper |
46 import soc.views.helper.lists |
51 import soc.views.helper.lists |
47 import soc.views.helper.responses |
52 import soc.views.helper.responses |
48 import soc.views.helper.widgets |
|
49 |
|
50 |
|
51 class CreateForm(helper.forms.BaseForm): |
|
52 """Django form displayed when Developer creates a Request. |
|
53 """ |
|
54 |
|
55 class Meta: |
|
56 """Inner Meta class that defines some behavior for the form. |
|
57 """ |
|
58 model = soc.models.request.Request |
|
59 |
|
60 #: list of model fields which will *not* be gathered by the form |
|
61 exclude = ['scope', 'scope_path', 'link_id', 'role', 'declined'] |
|
62 |
|
63 role = forms.CharField(widget=helper.widgets.ReadOnlyInput()) |
|
64 |
|
65 user = forms.CharField( |
|
66 label=soc.models.request.Request.link_id.verbose_name, |
|
67 help_text=soc.models.request.Request.link_id.help_text, |
|
68 widget=helper.widgets.ReadOnlyInput()) |
|
69 |
|
70 group = forms.CharField( |
|
71 label=soc.models.request.Request.scope.verbose_name, |
|
72 help_text=soc.models.request.Request.scope.help_text, |
|
73 widget=helper.widgets.ReadOnlyInput()) |
|
74 |
|
75 def clean_user(self): |
|
76 self.cleaned_data['requester'] = user_logic.logic.getForFields( |
|
77 {'link_id': self.cleaned_data['user']}, unique=True) |
|
78 return self.cleaned_data['user'] |
|
79 |
|
80 def clean_group(self): |
|
81 self.cleaned_data['to'] = sponsor_logic.logic.getFromFields( |
|
82 link_id=self.cleaned_data['group']) |
|
83 return self.cleaned_data['group'] |
|
84 |
|
85 |
|
86 class EditForm(CreateForm): |
|
87 """Django form displayed when Developer edits a Request. |
|
88 """ |
|
89 |
|
90 pass |
|
91 |
53 |
92 |
54 |
93 class View(base.View): |
55 class View(base.View): |
94 """View methods for the Docs model. |
56 """View methods for the Request model. |
95 """ |
57 """ |
96 |
58 |
97 def __init__(self, params=None): |
59 def __init__(self, params=None): |
98 """Defines the fields and methods required for the base View class |
60 """Defines the fields and methods required for the base View class |
99 to provide the user with list, public, create, edit and delete views. |
61 to provide the user with list, public, create, edit and delete views. |
102 params: a dict with params for this View |
64 params: a dict with params for this View |
103 """ |
65 """ |
104 |
66 |
105 rights = {} |
67 rights = {} |
106 rights['listSelf'] = [access.checkAgreesToSiteToS] |
68 rights['listSelf'] = [access.checkAgreesToSiteToS] |
107 rights['create'] = [access.checkCanInvite] |
69 rights['create'] = [access.allow] # TODO(ljvderijk) Set to deny once host has been converted |
|
70 rights['edit'] = [access.checkIsDeveloper] |
|
71 rights['process_invite'] = [access.checkIsMyUncompletedRequest] |
|
72 rights['list'] = [access.checkIsDeveloper] |
|
73 rights['delete'] = [access.checkIsDeveloper] |
108 |
74 |
109 new_params = {} |
75 new_params = {} |
110 new_params['rights'] = rights |
76 new_params['rights'] = rights |
111 new_params['logic'] = soc.logic.models.request.logic |
77 new_params['logic'] = soc.logic.models.request.logic |
112 |
78 |
113 new_params['name'] = "Request" |
79 new_params['name'] = "Request" |
114 |
80 |
115 new_params['edit_form'] = EditForm |
|
116 new_params['create_form'] = CreateForm |
|
117 |
|
118 new_params['sidebar_defaults'] = [('/%s/list', 'List %(name_plural)s', 'list')] |
81 new_params['sidebar_defaults'] = [('/%s/list', 'List %(name_plural)s', 'list')] |
119 |
82 |
120 new_params['save_message'] = [ugettext_lazy('Request saved.')] |
83 new_params['save_message'] = [ugettext_lazy('Request saved.')] |
|
84 |
|
85 new_params['extra_dynaexclude'] = ['group_accepted', 'user_accepted', |
|
86 'role_verbose', 'completed'] |
|
87 |
|
88 # TODO(ljvderijk) add clean field that checks to see if the user already has |
|
89 # the role that's been entered in the create form fields |
|
90 new_params['create_extra_dynafields'] = { |
|
91 'role' : forms.CharField(widget=widgets.ReadOnlyInput(), |
|
92 required=True), |
|
93 'clean_link_id': cleaning.clean_existing_user('link_id') |
|
94 } |
|
95 |
|
96 patterns = [(r'^%(url_name)s/(?P<access_type>invite)/%(lnp)s$', |
|
97 'soc.views.models.%(module_name)s.invite', |
|
98 'Create invite for %(name_plural)s'), |
|
99 (r'^%(url_name)s/(?P<access_type>process_invite)/%(key_fields)s$', |
|
100 'soc.views.models.%(module_name)s.processInvite', |
|
101 'Process Invite to for a Role')] |
|
102 |
|
103 new_params['extra_django_patterns'] = patterns |
|
104 |
|
105 new_params['invite_processing_template'] = 'soc/request/process_invite.html' |
121 |
106 |
122 params = dicts.merge(params, new_params) |
107 params = dicts.merge(params, new_params) |
123 |
108 |
124 super(View, self).__init__(params=params) |
109 super(View, self).__init__(params=params) |
|
110 |
|
111 |
|
112 @decorators.merge_params |
|
113 @decorators.check_access |
|
114 def processInvite(self, request, access_type, |
|
115 page_name=None, params=None, **kwargs): |
|
116 """Creates the page upon which an invite can be processed. |
|
117 |
|
118 Args: |
|
119 request: the standard Django HTTP request object |
|
120 access_type : the name of the access type which should be checked |
|
121 context: dictionary containing the context for this view |
|
122 params: a dict with params for this View |
|
123 kwargs: the Key Fields for the specified entity |
|
124 """ |
|
125 |
|
126 # get the context for this webpage |
|
127 context = responses.getUniversalContext(request) |
|
128 context['page_name'] = page_name |
|
129 |
|
130 request_logic = params['logic'] |
|
131 |
|
132 # get the request entity using the information from kwargs |
|
133 fields = {'link_id' : kwargs['link_id'], |
|
134 'scope_path' : kwargs['scope_path'], |
|
135 'role' : kwargs['role'], |
|
136 'group_accepted' : True, |
|
137 'user_accepted' : False, |
|
138 'completed' : False} |
|
139 request_entity = request_logic.getForFields(fields, unique=True) |
|
140 |
|
141 get_dict = request.GET |
|
142 |
|
143 if 'status' in get_dict.keys(): |
|
144 if get_dict['status'] == 'rejected': |
|
145 # this invite has been rejected mark accepted as False and mark completed as True |
|
146 request_logic.updateModelProperties(request_entity, { |
|
147 'user_accepted' : False, 'completed' : True}) |
|
148 |
|
149 # redirect to user role overview |
|
150 return http.HttpResponseRedirect('/user/roles') |
|
151 |
|
152 # put the entity in the context |
|
153 context['entity'] = request_entity |
|
154 context['module_name'] = params['module_name'] |
|
155 |
|
156 #display the invite processing page using the appropriate template |
|
157 template = params['invite_processing_template'] |
|
158 |
|
159 return responses.respond(request, template, context=context) |
|
160 |
125 |
161 |
126 @decorators.merge_params |
162 @decorators.merge_params |
127 @decorators.check_access |
163 @decorators.check_access |
128 def listSelf(self, request, access_type, |
164 def listSelf(self, request, access_type, |
129 page_name=None, params=None, **kwargs): |
165 page_name=None, params=None, **kwargs): |
138 |
174 |
139 # get the current user |
175 # get the current user |
140 properties = {'account': users.get_current_user()} |
176 properties = {'account': users.get_current_user()} |
141 user_entity = user_logic.logic.getForFields(properties, unique=True) |
177 user_entity = user_logic.logic.getForFields(properties, unique=True) |
142 |
178 |
143 # construct the Unhandled Requests list |
179 # construct the Unhandled Invites list |
144 |
180 |
145 # only select the requests for this user that haven't been handled yet |
181 # only select the Invites for this user that haven't been handled yet |
146 filter = {'link_id': user_entity.link_id, |
182 filter = {'link_id': user_entity.link_id, |
147 'group_accepted' : True} |
183 'group_accepted' : True, |
|
184 'user_accepted' : False, |
|
185 'completed' : False} |
148 |
186 |
149 uh_params = params.copy() |
187 uh_params = params.copy() |
150 uh_params['list_action'] = (redirects.inviteAcceptedRedirect, None) |
188 uh_params['list_action'] = (redirects.inviteProcessRedirect, None) |
151 uh_params['list_description'] = ugettext_lazy( |
189 uh_params['list_description'] = ugettext_lazy( |
152 "An overview of your unhandled requests.") |
190 "An overview of your unhandled invites.") |
153 |
191 |
154 uh_list = helper.lists.getListContent( |
192 uh_list = helper.lists.getListContent( |
155 request, uh_params, filter, 0) |
193 request, uh_params, filter, 0) |
156 |
194 |
157 # construct the Open Requests list |
195 # construct the Open Requests list |
158 |
196 |
159 # only select the requests for the user |
197 # only select the requests from the user |
160 # that haven't been accepted by an admin yet |
198 # that haven't been accepted by an admin yet |
161 filter = {'link_id' : user_entity.link_id, |
199 filter = {'link_id' : user_entity.link_id, |
162 'group_accepted' : False} |
200 'group_accepted' : False, |
|
201 'completed' : False} |
163 |
202 |
164 ar_params = params.copy() |
203 ar_params = params.copy() |
165 ar_params['list_description'] = ugettext_lazy( |
204 ar_params['list_description'] = ugettext_lazy( |
166 "List of your pending requests.") |
205 "List of your pending requests.") |
167 |
206 |
172 contents = [uh_list, ar_list] |
211 contents = [uh_list, ar_list] |
173 |
212 |
174 # call the _list method from base to display the list |
213 # call the _list method from base to display the list |
175 return self._list(request, params, contents, page_name) |
214 return self._list(request, params, contents, page_name) |
176 |
215 |
177 def _editSeed(self, request, seed): |
|
178 """See base.View._editGet(). |
|
179 """ |
|
180 |
|
181 # fill in the email field with the data from the entity |
|
182 seed['user'] = seed['link_id'] |
|
183 seed['group'] = seed['scope_path'] |
|
184 |
|
185 def _editGet(self, request, entity, form): |
|
186 """See base.View._editGet(). |
|
187 """ |
|
188 |
|
189 # fill in the email field with the data from the entity |
|
190 form.fields['user'].initial = entity.link_id |
|
191 form.fields['group'].initial = entity.scope_path |
|
192 |
|
193 super(View, self)._editGet(request, entity, form) |
|
194 |
216 |
195 def _editPost(self, request, entity, fields): |
217 def _editPost(self, request, entity, fields): |
196 """See base.View._editPost(). |
218 """See base.View._editPost(). |
197 """ |
219 """ |
198 |
220 |
199 # fill in the account field with the user created from email |
221 # TODO(ljvderijk) remove this once host has been rewritten |
200 fields['link_id'] = fields['requester'].link_id |
222 callback, args, kwargs = urlresolvers.resolve(request.path) |
201 fields['scope_path'] = fields['to'].link_id |
223 |
202 fields['scope'] = fields['to'] |
224 # fill in the fields via kwargs |
|
225 fields['link_id'] = kwargs['link_id'] |
|
226 fields['scope_path'] = kwargs['scope_path'] |
|
227 fields['role'] = kwargs['role'] |
|
228 fields['role_verbose'] = 'Some Role' |
|
229 fields['group_accepted'] = True |
203 |
230 |
204 super(View, self)._editPost(request, entity, fields) |
231 super(View, self)._editPost(request, entity, fields) |
205 |
232 |
206 |
233 |
207 view = View() |
234 view = View() |