|
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 """Developer views for editing and examining Sponsor profiles. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
22 ] |
|
23 |
|
24 |
|
25 from google.appengine.api import users |
|
26 |
|
27 from django import http |
|
28 from django import newforms as forms |
|
29 |
|
30 from soc.logic import validate |
|
31 from soc.logic import out_of_band |
|
32 from soc.logic import sponsor |
|
33 from soc.logic.site import id_user |
|
34 from soc.views import simple |
|
35 from soc.views.helpers import custom_widgets |
|
36 from soc.views.helpers import forms_helpers |
|
37 from soc.views.helpers import response_helpers |
|
38 from soc.views.helpers import request_helpers |
|
39 from soc.views.user import profile |
|
40 |
|
41 import soc.models.sponsor |
|
42 |
|
43 |
|
44 class CreateForm(forms_helpers.DbModelForm): |
|
45 """Django form displayed when creating a Sponsor. |
|
46 """ |
|
47 class Meta: |
|
48 """Inner Meta class that defines some behavior for the form. |
|
49 """ |
|
50 #: db.Model subclass for which the form will gather information |
|
51 model = soc.models.sponsor.Sponsor |
|
52 |
|
53 #: list of model fields which will *not* be gathered by the form |
|
54 exclude = ['founder', 'inheritance_line'] |
|
55 |
|
56 # TODO(pawel.solyga): write validation functions for other fields |
|
57 def clean_link_name(self): |
|
58 link_name = self.cleaned_data.get('link_name') |
|
59 if not validate.isLinkNameFormatValid(link_name): |
|
60 raise forms.ValidationError("This link name is in wrong format.") |
|
61 if sponsor.doesLinkNameExist(link_name): |
|
62 raise forms.ValidationError("This link name is already in use.") |
|
63 return link_name |
|
64 |
|
65 |
|
66 class EditForm(CreateForm): |
|
67 """Django form displayed when editing a Sponsor. |
|
68 """ |
|
69 link_name = forms.CharField(widget=custom_widgets.ReadOnlyInput()) |
|
70 |
|
71 def clean_link_name(self): |
|
72 link_name = self.cleaned_data.get('link_name') |
|
73 if not validate.isLinkNameFormatValid(link_name): |
|
74 raise forms.ValidationError("This link name is in wrong format.") |
|
75 return link_name |
|
76 |
|
77 |
|
78 DEF_SITE_SPONSOR_PROFILE_EDIT_TMPL = 'soc/group/profile/edit.html' |
|
79 DEF_SPONSOR_NO_LINKNAME_CHANGE_MSG = 'Sponsor link name cannot be changed.' |
|
80 DEF_CREATE_NEW_SPONSOR_MSG = ' You can create a new sponsor by visiting' \ |
|
81 ' <a href="/site/sponsor/profile">Create ' \ |
|
82 'a New Sponsor</a> page.' |
|
83 |
|
84 def edit(request, linkname=None, template=DEF_SITE_SPONSOR_PROFILE_EDIT_TMPL): |
|
85 """View for a Developer to modify the properties of a Sponsor Model entity. |
|
86 |
|
87 Args: |
|
88 request: the standard django request object |
|
89 linkname: the Sponsor's site-unique "linkname" extracted from the URL |
|
90 template: the "sibling" template (or a search list of such templates) |
|
91 from which to construct the public.html template name (or names) |
|
92 |
|
93 Returns: |
|
94 A subclass of django.http.HttpResponse which either contains the form to |
|
95 be filled out, or a redirect to the correct view in the interface. |
|
96 """ |
|
97 # create default template context for use with any templates |
|
98 context = response_helpers.getUniversalContext(request) |
|
99 |
|
100 alt_response = simple.getAltResponseIfNotDeveloper(request, |
|
101 context=context) |
|
102 if alt_response: |
|
103 return alt_response |
|
104 |
|
105 logged_in_id = users.get_current_user() |
|
106 user = id_user.getUserFromId(logged_in_id) |
|
107 sponsor_form = None |
|
108 existing_sponsor = None |
|
109 |
|
110 # try to fetch Sponsor entity corresponding to linkname if one exists |
|
111 try: |
|
112 existing_sponsor = soc.logic.sponsor.getSponsorIfLinkName(linkname) |
|
113 except out_of_band.ErrorResponse, error: |
|
114 # show custom 404 page when link name doesn't exist in Datastore |
|
115 error.message = error.message + DEF_CREATE_NEW_SPONSOR_MSG |
|
116 return simple.errorResponse(request, error, template, context) |
|
117 |
|
118 if request.method == 'POST': |
|
119 if existing_sponsor: |
|
120 sponsor_form = EditForm(request.POST) |
|
121 else: |
|
122 sponsor_form = CreateForm(request.POST) |
|
123 |
|
124 if sponsor_form.is_valid(): |
|
125 if linkname: |
|
126 # Form doesn't allow to change linkname but somebody might want to |
|
127 # abuse that manually, so we check if form linkname is the same as |
|
128 # url linkname |
|
129 if sponsor_form.cleaned_data.get('link_name') != linkname: |
|
130 msg = DEF_SPONSOR_NO_LINKNAME_CHANGE_MSG |
|
131 error = out_of_band.ErrorResponse(msg) |
|
132 return simple.errorResponse(request, error, template, context) |
|
133 |
|
134 fields = {} |
|
135 |
|
136 # Ask for all the fields and pull them out |
|
137 for field in sponsor_form.cleaned_data: |
|
138 value = sponsor_form.cleaned_data.get(field) |
|
139 fields[field] = value |
|
140 |
|
141 fields['founder'] = user |
|
142 |
|
143 form_ln = fields['link_name'] |
|
144 form_sponsor = sponsor.updateOrCreateSponsorFromLinkName(form_ln, |
|
145 **fields) |
|
146 |
|
147 if not form_sponsor: |
|
148 return http.HttpResponseRedirect('/') |
|
149 |
|
150 # redirect to new /site/sponsor/profile/form_link_name?s=0 |
|
151 # (causes 'Profile saved' message to be displayed) |
|
152 return response_helpers.redirectToChangedSuffix( |
|
153 request, None, form_ln, |
|
154 params=profile.SUBMIT_PROFILE_SAVED_PARAMS) |
|
155 |
|
156 else: # request.method == 'GET' |
|
157 if existing_sponsor: |
|
158 # is 'Profile saved' parameter present, but referrer was not ourself? |
|
159 # (e.g. someone bookmarked the GET that followed the POST submit) |
|
160 if (request.GET.get(profile.SUBMIT_MSG_PARAM_NAME) |
|
161 and (not request_helpers.isReferrerSelf(request, suffix=linkname))): |
|
162 # redirect to aggressively remove 'Profile saved' query parameter |
|
163 return http.HttpResponseRedirect(request.path) |
|
164 |
|
165 # referrer was us, so select which submit message to display |
|
166 # (may display no message if ?s=0 parameter is not present) |
|
167 context['submit_message'] = ( |
|
168 request_helpers.getSingleIndexedParamValue( |
|
169 request, profile.SUBMIT_MSG_PARAM_NAME, |
|
170 values=profile.SUBMIT_MESSAGES)) |
|
171 |
|
172 # populate form with the existing Sponsor entity |
|
173 sponsor_form = EditForm(instance=existing_sponsor) |
|
174 else: |
|
175 if request.GET.get(profile.SUBMIT_MSG_PARAM_NAME): |
|
176 # redirect to aggressively remove 'Profile saved' query parameter |
|
177 return http.HttpResponseRedirect(request.path) |
|
178 |
|
179 # no Sponsor entity exists for this link name, so show a blank form |
|
180 sponsor_form = CreateForm() |
|
181 |
|
182 context.update({'form': sponsor_form, |
|
183 'existing_group': existing_sponsor, |
|
184 'group_type': 'Sponsor'}) |
|
185 |
|
186 return response_helpers.respond(request, template, context) |