|
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 Models with a "presence" on a Melange site. |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
22 ] |
|
23 |
|
24 |
|
25 from google.appengine.ext import db |
|
26 from google.appengine.api import users |
|
27 |
|
28 from django import forms |
|
29 from django.utils.translation import ugettext_lazy |
|
30 |
|
31 from soc.logic import dicts |
|
32 from soc.logic import validate |
|
33 from soc.logic.models import document as document_logic |
|
34 from soc.views import helper |
|
35 from soc.views.helper import widgets |
|
36 from soc.views.models import base |
|
37 |
|
38 import soc.models.presence |
|
39 import soc.logic.models.presence |
|
40 import soc.logic.dicts |
|
41 import soc.views.helper |
|
42 import soc.views.helper.widgets |
|
43 |
|
44 |
|
45 class SettingsValidationForm(helper.forms.BaseForm): |
|
46 """Django form displayed when creating or editing Settings. |
|
47 |
|
48 This form includes validation functions for Settings fields. |
|
49 """ |
|
50 |
|
51 # TODO(tlarsen): scope_path will be a hard-coded read-only |
|
52 # field for some (most?) User Roles |
|
53 doc_scope_path = forms.CharField(required=False, |
|
54 label=ugettext_lazy('Document scope path'), |
|
55 help_text=soc.models.work.Work.scope_path.help_text) |
|
56 |
|
57 # TODO(tlarsen): actually, using these two text fields to specify |
|
58 # the Document is pretty cheesy; this needs to be some much better |
|
59 # Role-scoped Document selector that we don't have yet |
|
60 doc_link_id = forms.CharField(required=False, |
|
61 label=ugettext_lazy('Document link ID'), |
|
62 help_text=soc.models.work.Work.link_id.help_text) |
|
63 |
|
64 def clean_feed_url(self): |
|
65 feed_url = self.cleaned_data.get('feed_url') |
|
66 |
|
67 if feed_url == '': |
|
68 # feed url not supplied (which is OK), so do not try to validate it |
|
69 return None |
|
70 |
|
71 if not validate.isFeedURLValid(feed_url): |
|
72 raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.') |
|
73 |
|
74 return feed_url |
|
75 |
|
76 |
|
77 class CreateForm(SettingsValidationForm): |
|
78 """Django form displayed when creating or editing Settings. |
|
79 """ |
|
80 |
|
81 class Meta: |
|
82 """Inner Meta class that defines some behavior for the form. |
|
83 """ |
|
84 #: db.Model subclass for which the form will gather information |
|
85 model = soc.models.presence.Presence |
|
86 |
|
87 #: list of model fields which will *not* be gathered by the form |
|
88 exclude = ['inheritance_line', 'home'] |
|
89 |
|
90 |
|
91 class EditForm(CreateForm): |
|
92 """Django form displayed a Document is edited. |
|
93 """ |
|
94 |
|
95 pass |
|
96 |
|
97 |
|
98 class View(base.View): |
|
99 """View methods for the Document model. |
|
100 """ |
|
101 |
|
102 def __init__(self, original_params=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 """ |
|
109 |
|
110 params = {} |
|
111 |
|
112 params['name'] = "Home Settings" |
|
113 params['name_short'] = "Home Settings" |
|
114 params['name_plural'] = "Home Settings" |
|
115 params['url_name'] = "home/settings" |
|
116 params['module_name'] = "presence" |
|
117 |
|
118 params['edit_form'] = EditForm |
|
119 params['create_form'] = CreateForm |
|
120 |
|
121 # TODO(tlarsen) Add support for Django style template lookup |
|
122 params['edit_template'] = 'soc/models/edit.html' |
|
123 params['public_template'] = 'soc/presence/public.html' |
|
124 params['list_template'] = 'soc/models/list.html' |
|
125 |
|
126 params['lists_template'] = { |
|
127 'list_main': 'soc/list/list_main.html', |
|
128 'list_pagination': 'soc/list/list_pagination.html', |
|
129 'list_row': 'soc/presence/list/home_row.html', |
|
130 'list_heading': 'soc/presence/list/home_heading.html', |
|
131 } |
|
132 |
|
133 params['delete_redirect'] = '/' + params['url_name'] + '/list' |
|
134 |
|
135 params = dicts.merge(original_params, params) |
|
136 |
|
137 base.View.__init__(self, params=params) |
|
138 |
|
139 self._logic = soc.logic.models.presence.logic |
|
140 |
|
141 def _public(self, request, entity, context): |
|
142 """ |
|
143 """ |
|
144 |
|
145 if not entity: |
|
146 return |
|
147 |
|
148 try: |
|
149 home_doc = entity.home |
|
150 except db.Error: |
|
151 home_doc = None |
|
152 |
|
153 if home_doc: |
|
154 home_doc.content = helper.templates.unescape(home_doc.content) |
|
155 context['home_document'] = home_doc |
|
156 |
|
157 def _editGet(self, request, entity, form): |
|
158 """See base.View._editGet(). |
|
159 """ |
|
160 |
|
161 try: |
|
162 if entity.home: |
|
163 form.fields['doc_scope_path'].initial = entity.home.scope_path |
|
164 form.fields['doc_link_id'].initial = entity.home.link_id |
|
165 except db.Error: |
|
166 pass |
|
167 |
|
168 def _editPost(self, request, entity, fields): |
|
169 """See base.View._editPost(). |
|
170 """ |
|
171 |
|
172 doc_scope_path = fields['doc_scope_path'] |
|
173 doc_link_id = fields['doc_link_id'] |
|
174 |
|
175 # TODO notify the user if home_doc is not found |
|
176 home_doc = document_logic.logic.getFromFields( |
|
177 scope_path=doc_scope_path, link_id=doc_link_id) |
|
178 |
|
179 fields['home'] = home_doc |
|
180 |
|
181 |
|
182 view = View() |
|
183 |
|
184 create = view.create |
|
185 edit = view.edit |
|
186 delete = view.delete |
|
187 list = view.list |
|
188 public = view.public |