app/soc/views/models/document.py
changeset 477 8a8b1bd035c4
parent 476 3b0662786f95
child 481 94834a1e6c01
equal deleted inserted replaced
476:3b0662786f95 477:8a8b1bd035c4
       
     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 Documents.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22     '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    23     '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    24   ]
       
    25 
       
    26 
       
    27 from google.appengine.api import users
       
    28 
       
    29 from django import forms
       
    30 from django.utils.translation import ugettext_lazy
       
    31 
       
    32 from soc.logic import dicts
       
    33 from soc.logic import validate
       
    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.document
       
    39 import soc.logic.models.document
       
    40 import soc.logic.dicts
       
    41 import soc.views.helper
       
    42 import soc.views.helper.widgets
       
    43 
       
    44 class CreateForm(helper.forms.BaseForm):
       
    45   """Django form displayed when Developer creates a Document.
       
    46   """
       
    47 
       
    48   content = forms.fields.CharField(widget=helper.widgets.TinyMCE(
       
    49       attrs={'rows':10, 'cols':40}))
       
    50 
       
    51   class Meta:
       
    52     model = soc.models.document.Document
       
    53 
       
    54     #: list of model fields which will *not* be gathered by the form
       
    55     exclude = ['inheritance_line', 'author', 'created', 'modified']
       
    56 
       
    57   def clean_partial_path(self):
       
    58     partial_path = self.cleaned_data.get('partial_path')
       
    59     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
    60     if not validate.isPartialPathFormatValid(partial_path):
       
    61       raise forms.ValidationError("This partial path is in wrong format.")
       
    62     return partial_path
       
    63 
       
    64   def clean_link_name(self):
       
    65     link_name = self.cleaned_data.get('link_name')
       
    66     # TODO(tlarsen): combine path and link_name and check for uniqueness
       
    67     if not validate.isLinkNameFormatValid(link_name):
       
    68       raise forms.ValidationError("This link name is in wrong format.")
       
    69     return link_name
       
    70 
       
    71 
       
    72 class EditForm(CreateForm):
       
    73   """Django form displayed a Document is edited.
       
    74   """
       
    75 
       
    76   doc_key_name = forms.fields.CharField(widget=forms.HiddenInput)
       
    77   created_by = forms.fields.CharField(widget=helper.widgets.ReadOnlyInput(),
       
    78                                       required=False)
       
    79 
       
    80 
       
    81 class View(base.View):
       
    82   """View methods for the Document model
       
    83   """
       
    84 
       
    85   def __init__(self, original_params=None, original_rights=None):
       
    86     """Defines the fields and methods required for the base View class
       
    87     to provide the user with list, public, create, edit and delete views.
       
    88 
       
    89     Params:
       
    90       original_params: a dict with params for this View
       
    91       original_rights: a dict with right definitions for this View
       
    92     """
       
    93 
       
    94     self._logic = soc.logic.models.document.logic
       
    95 
       
    96     params = {}
       
    97     rights = {}
       
    98 
       
    99     params['name'] = "Document"
       
   100     params['name_short'] = "Document"
       
   101     params['name_plural'] = "Documents"
       
   102 
       
   103     params['edit_form'] = EditForm
       
   104     params['create_form'] = CreateForm
       
   105 
       
   106     # TODO(tlarsen) Add support for Django style template lookup
       
   107     params['edit_template'] = 'soc/models/edit.html'
       
   108     params['public_template'] = 'soc/document/public.html'
       
   109     params['list_template'] = 'soc/models/list.html'
       
   110 
       
   111     params['lists_template'] = {
       
   112       'list_main': 'soc/list/list_main.html',
       
   113       'list_pagination': 'soc/list/list_pagination.html',
       
   114       'list_row': 'soc/document/list/docs_row.html',
       
   115       'list_heading': 'soc/document/list/docs_heading.html',
       
   116     }
       
   117 
       
   118     params['delete_redirect'] = '/document/list'
       
   119     params['create_redirect'] = 'soc/models/edit.html'
       
   120 
       
   121     params['save_message'] = [ugettext_lazy('Profile saved.')]
       
   122 
       
   123     params['edit_params'] = {
       
   124         self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED,
       
   125         }
       
   126 
       
   127     rights['list'] = [helper.access.checkIsDeveloper]
       
   128     rights['delete'] = [helper.access.checkIsDeveloper]
       
   129 
       
   130     params = dicts.merge(original_params, params)
       
   131     rights = dicts.merge(original_rights, rights)
       
   132 
       
   133     base.View.__init__(self, rights=rights, params=params)
       
   134 
       
   135   def _editPost(self, request, entity, fields):
       
   136     """See base.View._editPost().
       
   137     """
       
   138 
       
   139     id = users.get_current_user()
       
   140     user = soc.logic.models.user.logic.getForFields({'id': id}, unique=True)
       
   141     fields['author'] = user
       
   142 
       
   143   def _editGet(self, request, entity, form):
       
   144     """See base.View._editGet().
       
   145     """
       
   146 
       
   147     form.fields['created_by'].initial = entity.author.link_name
       
   148     form.fields['doc_key_name'].initial = entity.key().name(),
       
   149 
       
   150 
       
   151 view = View()
       
   152 
       
   153 create = view.create
       
   154 edit = view.edit
       
   155 delete = view.delete
       
   156 list = view.list
       
   157 public = view.public