app/soc/views/models/sponsor.py
changeset 363 d35ffa6ca643
child 387 c55195361cb6
equal deleted inserted replaced
362:d904f4a76f6f 363:d35ffa6ca643
       
     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 validate 
       
    32 from soc.views import helper
       
    33 from soc.views.helper import widgets
       
    34 from soc.views.models import base
       
    35 
       
    36 import soc.models.sponsor
       
    37 import soc.logic.models.sponsor
       
    38 import soc.logic.dicts
       
    39 import soc.views.helper
       
    40 import soc.views.helper.widgets
       
    41 
       
    42 
       
    43 class CreateForm(helper.forms.DbModelForm):
       
    44   """Django form displayed when creating a Sponsor.
       
    45   """
       
    46   class Meta:
       
    47     """Inner Meta class that defines some behavior for the form.
       
    48     """
       
    49     #: db.Model subclass for which the form will gather information
       
    50     model = soc.models.sponsor.Sponsor
       
    51     
       
    52     #: list of model fields which will *not* be gathered by the form
       
    53     exclude = ['founder', 'inheritance_line']
       
    54   
       
    55   # TODO(pawel.solyga): write validation functions for other fields
       
    56   def clean_link_name(self):
       
    57     link_name = self.cleaned_data.get('link_name')
       
    58     if not validate.isLinkNameFormatValid(link_name):
       
    59       raise forms.ValidationError("This link name is in wrong format.")
       
    60     if models.sponsor.logic.getFromFields(link_name=link_name):
       
    61       raise forms.ValidationError("This link name is already in use.")
       
    62     return link_name
       
    63 
       
    64 
       
    65 class EditForm(CreateForm):
       
    66   """Django form displayed when editing a Sponsor.
       
    67   """
       
    68   link_name = forms.CharField(widget=helper.widgets.ReadOnlyInput())
       
    69   founded_by = forms.CharField(widget=helper.widgets.ReadOnlyInput(),
       
    70                                required=False)
       
    71 
       
    72   def clean_link_name(self):
       
    73     link_name = self.cleaned_data.get('link_name')
       
    74     if not validate.isLinkNameFormatValid(link_name):
       
    75       raise forms.ValidationError("This link name is in wrong format.")
       
    76     return link_name
       
    77 
       
    78 
       
    79 class View(base.View):
       
    80   """View methods for the Sponsor model
       
    81   """
       
    82 
       
    83   def __init__(self, original_params=None, original_rights=None):
       
    84     """Defines the fields and methods required for the base View class
       
    85     to provide the user with list, public, create, edit and delete views.
       
    86 
       
    87     Params:
       
    88       original_params: a dict with params for this View 
       
    89       original_rights: a dict with right definitions for this View
       
    90     """
       
    91 
       
    92     self.DEF_SUBMIT_MSG_PARAM_NAME = 's'
       
    93     self.SUBMIT_MSG_PROFILE_SAVED = 0
       
    94     #TODO(TLarsen) Better way to do this?
       
    95     
       
    96     self._logic = soc.logic.models.sponsor.logic
       
    97     
       
    98     params = {}
       
    99     rights = {}
       
   100 
       
   101     params['name'] = "Sponsor"
       
   102     params['name_plural'] = "Sponsors"
       
   103        
       
   104     params['edit_form'] = EditForm
       
   105     params['create_form'] = CreateForm
       
   106 
       
   107     # TODO(SRabbelier) Add support for Django style template lookup
       
   108     params['create_template'] = 'soc/site/sponsor/profile/edit.html'
       
   109     params['public_template'] = 'soc/group/profile/public.html'
       
   110 
       
   111     params['list_template'] = 'soc/group/list/all.html'
       
   112 
       
   113     params['lists_template'] = {
       
   114       'list_main': 'soc/list/list_main.html',
       
   115       'list_pagination': 'soc/list/list_pagination.html',
       
   116       'list_row': 'soc/group/list/group_row.html',
       
   117       'list_heading': 'soc/group/list/group_heading.html',
       
   118     }
       
   119     
       
   120     params['delete_redirect'] = '/site/sponsor/list'
       
   121     params['create_redirect'] = '/site/sponsor/profile'
       
   122     
       
   123     params['save_message'] = [ ugettext_lazy('Profile saved.') ]
       
   124     
       
   125     params['edit_params'] = {
       
   126         self.DEF_SUBMIT_MSG_PARAM_NAME:self.SUBMIT_MSG_PROFILE_SAVED,
       
   127         }
       
   128     
       
   129     rights['list'] = [helper.access.checkIsDeveloper]
       
   130     rights['delete'] = [helper.access.checkIsDeveloper]
       
   131 
       
   132     params = soc.logic.dicts.mergeDicts(original_params, params)
       
   133     rights = soc.logic.dicts.mergeDicts(original_rights, rights)
       
   134     
       
   135     base.View.__init__(self, rights=rights, params=params)
       
   136 
       
   137   def _editPost(self, request, entity, fields):
       
   138     """
       
   139     """
       
   140 
       
   141     id = users.get_current_user()
       
   142     user = soc.logic.models.user.logic.getFromFields(email=id.email())
       
   143     fields['founder'] = user
       
   144 
       
   145 
       
   146 view = View()
       
   147 public = view.public
       
   148 list = view.list
       
   149 delete = view.delete
       
   150 edit = view.edit