|
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 """Site-wide Melange home page views. |
|
18 |
|
19 public: how the general public sees the site home page of a Melange |
|
20 site |
|
21 |
|
22 edit: site settings view for logged-in Developers |
|
23 """ |
|
24 |
|
25 __authors__ = [ |
|
26 '"Pawel Solyga" <pawel.solyga@gmail.com>', |
|
27 ] |
|
28 |
|
29 |
|
30 from google.appengine.api import users |
|
31 from google.appengine.ext import db |
|
32 |
|
33 from django import forms |
|
34 from django import http |
|
35 from django import shortcuts |
|
36 |
|
37 from soc.logic import models |
|
38 from soc.logic import out_of_band |
|
39 from soc.logic import validate |
|
40 from soc.logic.models import document |
|
41 from soc.logic.site import id_user |
|
42 from soc.views import simple |
|
43 from soc.views import helper |
|
44 from soc.views.helper import access |
|
45 |
|
46 import soc.logic.models.site_settings |
|
47 import soc.models.document |
|
48 import soc.models.site_settings |
|
49 import soc.views.helper.forms |
|
50 import soc.views.helper.responses |
|
51 import soc.views.helper.templates |
|
52 import soc.views.helper.widgets |
|
53 import soc.views.out_of_band |
|
54 |
|
55 |
|
56 class DocumentForm(helper.forms.DbModelForm): |
|
57 content = forms.fields.CharField(widget=helper.widgets.TinyMCE( |
|
58 attrs={'rows':10, 'cols':40})) |
|
59 |
|
60 class Meta: |
|
61 """Inner Meta class that defines some behavior for the form. |
|
62 """ |
|
63 #: db.Model subclass for which the form will gather information |
|
64 model = soc.models.document.Document |
|
65 |
|
66 #: list of model fields which will *not* be gathered by the form |
|
67 exclude = ['partial_path', 'link_name', |
|
68 'author', 'modified', 'created', 'inheritance_line'] |
|
69 |
|
70 |
|
71 class SiteSettingsForm(helper.forms.DbModelForm): |
|
72 """Django form displayed when creating or editing Site Settings. |
|
73 """ |
|
74 class Meta: |
|
75 """Inner Meta class that defines some behavior for the form. |
|
76 """ |
|
77 #: db.Model subclass for which the form will gather information |
|
78 model = soc.models.site_settings.SiteSettings |
|
79 |
|
80 #: list of model fields which will *not* be gathered by the form |
|
81 exclude = ['inheritance_line', 'home'] |
|
82 |
|
83 def clean_feed_url(self): |
|
84 feed_url = self.cleaned_data.get('feed_url') |
|
85 |
|
86 if feed_url == '': |
|
87 # feed url not supplied (which is OK), so do not try to validate it |
|
88 return None |
|
89 |
|
90 if not validate.isFeedURLValid(feed_url): |
|
91 raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.') |
|
92 |
|
93 return feed_url |
|
94 |
|
95 |
|
96 DEF_SITE_HOME_EDIT_TMPL = 'soc/site/settings/edit.html' |
|
97 |
|
98 def edit(request, template=DEF_SITE_HOME_EDIT_TMPL): |
|
99 """View for Developer to edit content of Melange site home page. |
|
100 |
|
101 Args: |
|
102 request: the standard django request object. |
|
103 template: the template path to use for rendering the template. |
|
104 |
|
105 Returns: |
|
106 A subclass of django.http.HttpResponse with generated template. |
|
107 """ |
|
108 |
|
109 try: |
|
110 access.checkIsDeveloper(request) |
|
111 except soc.views.out_of_band.AccessViolationResponse, alt_response: |
|
112 return alt_response.response() |
|
113 |
|
114 # create default template context for use with any templates |
|
115 context = helper.responses.getUniversalContext(request) |
|
116 |
|
117 settings_form = None |
|
118 document_form = None |
|
119 |
|
120 if request.method == 'POST': |
|
121 document_form = DocumentForm(request.POST) |
|
122 settings_form = SiteSettingsForm(request.POST) |
|
123 |
|
124 if document_form.is_valid() and settings_form.is_valid(): |
|
125 link_name = models.site_settings.logic.DEF_SITE_HOME_DOC_LINK_NAME |
|
126 partial_path=models.site_settings.logic.DEF_SITE_SETTINGS_PATH |
|
127 logged_in_id = users.get_current_user() |
|
128 author = models.user.logic.getFromFields(email=logged_in_id.email()) |
|
129 |
|
130 properties = { |
|
131 'title': document_form.cleaned_data.get('title'), |
|
132 'short_name': document_form.cleaned_data.get('short_name'), |
|
133 'abstract': document_form.cleaned_data.get('abstract'), |
|
134 'content': document_form.cleaned_data.get('content'), |
|
135 'link_name': link_name, |
|
136 'partial_path': partial_path, |
|
137 'id': logged_in_id, |
|
138 'author': author, |
|
139 } |
|
140 |
|
141 site_doc = document.logic.updateOrCreateFromFields( |
|
142 properties, partial_path=partial_path, link_name=link_name) |
|
143 |
|
144 feed_url = settings_form.cleaned_data.get('feed_url') |
|
145 |
|
146 site_settings = models.site_settings.logic.updateOrCreateFromFields( |
|
147 {'feed_url': feed_url, 'home': site_doc}, |
|
148 path=models.site_settings.logic.DEF_SITE_SETTINGS_PATH) |
|
149 |
|
150 context['notice'] = 'Site Settings saved.' |
|
151 else: # request.method == 'GET' |
|
152 # try to fetch SiteSettings entity by unique key_name |
|
153 site_settings = models.site_settings.logic.getFromFields( |
|
154 path=models.site_settings.logic.DEF_SITE_SETTINGS_PATH) |
|
155 |
|
156 if site_settings: |
|
157 # populate form with the existing SiteSettings entity |
|
158 settings_form = SiteSettingsForm(instance=site_settings) |
|
159 |
|
160 # check if ReferenceProperty to home Document is valid |
|
161 try: |
|
162 site_doc = site_settings.home |
|
163 except db.Error: |
|
164 site_doc = None |
|
165 |
|
166 else: |
|
167 # no SiteSettings entity exists for this key_name, so show a blank form |
|
168 settings_form = SiteSettingsForm() |
|
169 site_doc = None |
|
170 |
|
171 if site_doc: |
|
172 # populate form with the existing Document entity |
|
173 document_form = DocumentForm(instance=site_doc) |
|
174 else: |
|
175 # no Document entity exists for this key_name, so show a blank form |
|
176 document_form = DocumentForm() |
|
177 |
|
178 context.update({'document_form': document_form, |
|
179 'settings_form': settings_form }) |
|
180 |
|
181 return helper.responses.respond(request, template, context) |