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