3 # Copyright 2008 the Melange authors.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 """Views for Home Settings.
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
25 from google
.appengine
.ext
import db
26 from google
.appengine
.api
import users
28 from django
import forms
29 from django
.utils
.translation
import ugettext_lazy
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
38 import soc
.models
.home_settings
39 import soc
.logic
.models
.home_settings
40 import soc
.logic
.dicts
41 import soc
.views
.helper
42 import soc
.views
.helper
.widgets
45 class SettingsValidationForm(helper
.forms
.BaseForm
):
46 """Django form displayed when creating or editing Settings.
48 This form includes validation functions for Settings fields.
51 # TODO(tlarsen): partial_path will be a hard-coded read-only
52 # field for some (most?) User Roles
53 doc_partial_path
= forms
.CharField(required
=False,
54 label
=soc
.models
.work
.Work
.partial_path
.verbose_name
,
55 help_text
=soc
.models
.work
.Work
.partial_path
.help_text
)
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_name
= forms
.CharField(required
=False,
61 label
=soc
.models
.work
.Work
.link_name
.verbose_name
,
62 help_text
=soc
.models
.work
.Work
.link_name
.help_text
)
64 def clean_feed_url(self
):
65 feed_url
= self
.cleaned_data
.get('feed_url')
68 # feed url not supplied (which is OK), so do not try to validate it
71 if not validate
.isFeedURLValid(feed_url
):
72 raise forms
.ValidationError('This URL is not a valid ATOM or RSS feed.')
77 class CreateForm(SettingsValidationForm
):
78 """Django form displayed when creating or editing Settings.
82 """Inner Meta class that defines some behavior for the form.
84 #: db.Model subclass for which the form will gather information
85 model
= soc
.models
.home_settings
.HomeSettings
87 #: list of model fields which will *not* be gathered by the form
88 exclude
= ['inheritance_line', 'home']
91 class EditForm(CreateForm
):
92 """Django form displayed a Document is edited.
98 class View(base
.View
):
99 """View methods for the Docs model
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.
107 original_params: a dict with params for this View
108 original_rights: a dict with right definitions for this View
114 params
['name'] = "Home Settings"
115 params
['name_short'] = "Home"
116 params
['name_plural'] = "Home Settings"
118 params
['edit_form'] = EditForm
119 params
['create_form'] = CreateForm
121 # TODO(tlarsen) Add support for Django style template lookup
122 params
['edit_template'] = 'soc/models/edit.html'
123 params
['public_template'] = 'soc/home_settings/public.html'
124 params
['list_template'] = 'soc/models/list.html'
126 params
['lists_template'] = {
127 'list_main': 'soc/list/list_main.html',
128 'list_pagination': 'soc/list/list_pagination.html',
129 'list_row': 'soc/home_settings/list/home_row.html',
130 'list_heading': 'soc/home_settings/list/home_heading.html',
133 params
['delete_redirect'] = '/home/list'
134 params
['create_redirect'] = '/home/edit'
136 params
['save_message'] = [ugettext_lazy('Profile saved.')]
138 params
['edit_params'] = {
139 self
.DEF_SUBMIT_MSG_PARAM_NAME
: self
.DEF_SUBMIT_MSG_PROFILE_SAVED
,
142 rights
['list'] = [helper
.access
.checkIsDeveloper
]
143 rights
['delete'] = [helper
.access
.checkIsDeveloper
]
145 params
= dicts
.merge(original_params
, params
)
146 rights
= dicts
.merge(original_rights
, rights
)
148 base
.View
.__init
__(self
, rights
=rights
, params
=params
)
150 self
._logic
= soc
.logic
.models
.home_settings
.logic
152 def _public(self
, request
, entity
, context
):
160 home_doc
= entity
.home
165 home_doc
.content
= helper
.templates
.unescape(home_doc
.content
)
166 context
['home_document'] = home_doc
168 def _editGet(self
, request
, entity
, form
):
169 """See base.View._editGet().
174 form
.fields
['doc_partial_path'].initial
= entity
.home
.partial_path
175 form
.fields
['doc_link_name'].initial
= entity
.home
.link_name
179 def _editPost(self
, request
, entity
, fields
):
180 """See base.View._editPost().
183 doc_partial_path
= fields
['doc_partial_path']
184 doc_link_name
= fields
['doc_link_name']
186 # TODO notify the user if home_doc is not found
187 home_doc
= document_logic
.logic
.getFromFields(
188 partial_path
=doc_partial_path
, link_name
=doc_link_name
)
190 fields
['home'] = home_doc