|
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 Host profiles. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
22 ] |
|
23 |
|
24 from google.appengine.api import users |
|
25 |
|
26 from django import forms |
|
27 from django.utils.translation import ugettext_lazy |
|
28 |
|
29 from soc.logic import dicts |
|
30 from soc.logic import validate |
|
31 from soc.logic.models import user as user_logic |
|
32 from soc.views import helper |
|
33 from soc.views.models import base |
|
34 |
|
35 import soc.models.user |
|
36 import soc.logic.models.user |
|
37 import soc.views.helper |
|
38 |
|
39 |
|
40 class CreateForm(helper.forms.BaseForm): |
|
41 """Django form displayed when creating a User. |
|
42 """ |
|
43 |
|
44 id = forms.EmailField( |
|
45 label=soc.models.user.User.id.verbose_name, |
|
46 help_text=soc.models.user.User.id.help_text) |
|
47 |
|
48 link_name = forms.CharField( |
|
49 label=soc.models.user.User.link_name.verbose_name, |
|
50 help_text=soc.models.user.User.link_name.help_text) |
|
51 |
|
52 nick_name = forms.CharField( |
|
53 label=soc.models.user.User.nick_name.verbose_name) |
|
54 |
|
55 is_developer = forms.BooleanField(required=False, |
|
56 label=soc.models.user.User.is_developer.verbose_name, |
|
57 help_text=soc.models.user.User.is_developer.help_text) |
|
58 |
|
59 class Meta: |
|
60 model = None |
|
61 |
|
62 def clean_link_name(self): |
|
63 link_name = self.cleaned_data.get('link_name') |
|
64 if not validate.isLinkNameFormatValid(link_name): |
|
65 raise forms.ValidationError("This link name is in wrong format.") |
|
66 |
|
67 properties = {'link_name': link_name} |
|
68 user = soc.logic.models.user.logic.getForFields(properties, unique=True) |
|
69 |
|
70 if user and user.link_name != link_name: |
|
71 raise forms.ValidationError("This link name is already in use.") |
|
72 |
|
73 return link_name |
|
74 |
|
75 def clean_id(self): |
|
76 form_id = users.User(email=self.cleaned_data.get('id')) |
|
77 key_name = self.data.get('key_name') |
|
78 if key_name: |
|
79 user = user_logic.logic.getFromKeyName(key_name) |
|
80 old_email = user.id.email() |
|
81 else: |
|
82 old_email = None |
|
83 |
|
84 new_email = form_id.email() |
|
85 |
|
86 if new_email != old_email and user_logic.logic.getFromFields(email=new_email): |
|
87 raise forms.ValidationError("This account is already in use.") |
|
88 |
|
89 return form_id |
|
90 |
|
91 |
|
92 class EditForm(CreateForm): |
|
93 """Django form displayed when editing a User. |
|
94 """ |
|
95 |
|
96 key_name = forms.CharField(widget=forms.HiddenInput) |
|
97 |
|
98 class View(base.View): |
|
99 """View methods for the User model |
|
100 """ |
|
101 |
|
102 def __init__(self, original_params=None, original_rights=None): |
|
103 """Defines the fields and methods required for the base View class |
|
104 to provide the user with list, public, create, edit and delete views. |
|
105 |
|
106 Params: |
|
107 original_params: a dict with params for this View |
|
108 original_rights: a dict with right definitions for this View |
|
109 """ |
|
110 |
|
111 self._logic = soc.logic.models.user.logic |
|
112 |
|
113 params = {} |
|
114 rights = {} |
|
115 |
|
116 params['name'] = "User" |
|
117 params['name_short'] = "User" |
|
118 params['name_plural'] = "Users" |
|
119 |
|
120 params['edit_form'] = EditForm |
|
121 params['create_form'] = CreateForm |
|
122 |
|
123 # TODO(tlarsen) Add support for Django style template lookup |
|
124 params['edit_template'] = 'soc/models/edit.html' |
|
125 params['public_template'] = 'soc/user/public.html' |
|
126 params['list_template'] = 'soc/models/list.html' |
|
127 |
|
128 params['lists_template'] = { |
|
129 'list_main': 'soc/list/list_main.html', |
|
130 'list_pagination': 'soc/list/list_pagination.html', |
|
131 'list_row': 'soc/user/list/user_row.html', |
|
132 'list_heading': 'soc/user/list/user_heading.html', |
|
133 } |
|
134 |
|
135 params['delete_redirect'] = '/user/list' |
|
136 params['create_redirect'] = '/user/create' |
|
137 |
|
138 params['save_message'] = [ugettext_lazy('Profile saved.')] |
|
139 |
|
140 params['edit_params'] = { |
|
141 self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED, |
|
142 } |
|
143 |
|
144 rights['list'] = [helper.access.checkIsDeveloper] |
|
145 rights['delete'] = [helper.access.checkIsDeveloper] |
|
146 |
|
147 params = dicts.merge(original_params, params) |
|
148 rights = dicts.merge(original_rights, rights) |
|
149 |
|
150 base.View.__init__(self, rights=rights, params=params) |
|
151 |
|
152 def self(self, request, page=None, params=None, **kwargs): |
|
153 """ |
|
154 """ |
|
155 |
|
156 params = dicts.merge(params, {'edit_template': 'soc/user/edit_self.html'}) |
|
157 |
|
158 id = users.get_current_user() |
|
159 email = id.email() |
|
160 properties = {'id': id} |
|
161 |
|
162 entity = self._logic.getForFields(properties, unique=True) |
|
163 keys = self._logic.getKeyFieldNames() |
|
164 values = self._logic.getKeyValues(entity) |
|
165 key_fields = dicts.zip(keys, values) |
|
166 |
|
167 return self.edit(request, page, params=params, **key_fields) |
|
168 |
|
169 def _editPost(self, request, entity, fields): |
|
170 """See base.View._editPost(). |
|
171 """ |
|
172 |
|
173 fields['email'] = fields['id'].email() |
|
174 |
|
175 |
|
176 view = View() |
|
177 |
|
178 create = view.create |
|
179 delete = view.delete |
|
180 edit = view.edit |
|
181 list = view.list |
|
182 public = view.public |
|
183 self = view.self |