# HG changeset patch # User Sverre Rabbelier # Date 1224545072 0 # Node ID 8f07048d84ef527373fdc80d5d49828d56978f1f # Parent b82852e6963eb2d476d1e0b719d0310de062f145 Added a generic version of the Document views Patch by: Sverre Rabbelier Reviewed by: to-be-reviewed diff -r b82852e6963e -r 8f07048d84ef app/soc/models/work.py --- a/app/soc/models/work.py Mon Oct 20 23:24:21 2008 +0000 +++ b/app/soc/models/work.py Mon Oct 20 23:24:32 2008 +0000 @@ -41,6 +41,9 @@ Reviews of that Work. This relation is implemented as the 'reviews' back-reference Query of the Review model 'reviewed' reference. """ + + key_fields = ['partial_path', 'link_name'] + #: Required 1:1 relationship indicating the User who initially authored the #: Work (this relationship is needed to keep track of lifetime document #: creation limits, used to prevent spamming, etc.). diff -r b82852e6963e -r 8f07048d84ef app/soc/views/models/docs.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/soc/views/models/docs.py Mon Oct 20 23:24:32 2008 +0000 @@ -0,0 +1,152 @@ +#!/usr/bin/python2.5 +# +# Copyright 2008 the Melange authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Views for Sponsor profiles. +""" + +__authors__ = [ + '"Sverre Rabbelier" ', + '"Pawel Solyga" ', + ] + + +from google.appengine.api import users + +from django import forms +from django.utils.translation import ugettext_lazy + +from soc.logic import dicts +from soc.logic import validate +from soc.views import helper +from soc.views.helper import widgets +from soc.views.models import base + +import soc.models.document +import soc.logic.models.document +import soc.logic.dicts +import soc.views.helper +import soc.views.helper.widgets + +class CreateForm(helper.forms.DbModelForm): + """Django form displayed when Developer creates a Document. + """ + + content = forms.fields.CharField(widget=helper.widgets.TinyMCE( + attrs={'rows':10, 'cols':40})) + + class Meta: + model = soc.models.document.Document + + #: list of model fields which will *not* be gathered by the form + exclude = ['inheritance_line', 'author', 'created', 'modified'] + + def clean_partial_path(self): + partial_path = self.cleaned_data.get('partial_path') + # TODO(tlarsen): combine path and link_name and check for uniqueness + return partial_path + + def clean_link_name(self): + link_name = self.cleaned_data.get('link_name') + # TODO(tlarsen): combine path and link_name and check for uniqueness + return link_name + + +class EditForm(CreateForm): + """Django form displayed a Document is edited. + """ + + doc_key_name = forms.fields.CharField(widget=forms.HiddenInput) + created_by = forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(), + required=False) + + +class View(base.View): + """View methods for the Docs model + """ + + def __init__(self, original_params=None, original_rights=None): + """Defines the fields and methods required for the base View class + to provide the user with list, public, create, edit and delete views. + + Params: + original_params: a dict with params for this View + original_rights: a dict with right definitions for this View + """ + + self._logic = soc.logic.models.document.logic + + params = {} + rights = {} + + params['name'] = "Document" + params['name_short'] = "Docs" + params['name_plural'] = "Documents" + + params['edit_form'] = EditForm + params['create_form'] = CreateForm + + # TODO(tlarsen) Add support for Django style template lookup + params['edit_template'] = 'soc/docs/edit.html' + params['public_template'] = 'soc/docs/public.html' + params['list_template'] = 'soc/docs/list/all.html' + + params['lists_template'] = { + 'list_main': 'soc/list/list_main.html', + 'list_pagination': 'soc/list/list_pagination.html', + 'list_row': 'soc/docs/list/docs_row.html', + 'list_heading': 'soc/docs/list/docs_heading.html', + } + + params['delete_redirect'] = '/docs/list' + params['create_redirect'] = 'soc/docs/edit.html' + + params['save_message'] = [ugettext_lazy('Profile saved.')] + + params['edit_params'] = { + base.View.DEF_SUBMIT_MSG_PARAM_NAME: base.View.DEF_SUBMIT_MSG_PROFILE_SAVED, + } + + rights['list'] = [helper.access.checkIsDeveloper] + rights['delete'] = [helper.access.checkIsDeveloper] + + params = dicts.merge(original_params, params) + rights = dicts.merge(original_rights, rights) + + base.View.__init__(self, rights=rights, params=params) + + def _editPost(self, request, entity, fields): + """ + """ + + id = users.get_current_user() + user = soc.logic.models.user.logic.getFromFields(email=id.email()) + fields['author'] = user + + def _editGet(self, request, entity, form): + """ + """ + + form.fields['created_by'].initial = entity.author.link_name + form.fields['doc_key_name'].initial = entity.key().name(), + + +view = View() + +create = view.create +edit = view.edit +delete = view.delete +list = view.list +public = view.public