app/soc/views/models/host.py
changeset 404 44223e50e1fc
parent 402 021e86368600
child 406 cc603a815cad
equal deleted inserted replaced
403:d3e545a8bd26 404:44223e50e1fc
       
     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 Host profiles.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Sverre Rabbelier" <sverer@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api import users
       
    26 
       
    27 from django import forms
       
    28 from django.utils.translation import ugettext_lazy
       
    29 
       
    30 from soc.logic import dicts
       
    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.host
       
    37 import soc.logic.models.host
       
    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 Host.
       
    45   """
       
    46 
       
    47   class Meta:
       
    48     """Inner Meta class that defines some behavior for the form.
       
    49     """
       
    50 
       
    51     #: db.Model subclass for which the form will gather information
       
    52     model = soc.models.host.Host
       
    53 
       
    54     #: list of model fields which will *not* be gathered by the form
       
    55     exclude = ['inheritance_line']
       
    56 
       
    57   def clean_empty(self, field):
       
    58     data = self.cleaned_data.get(field)
       
    59     if not data or data == u'':
       
    60       return None
       
    61 
       
    62     return data
       
    63 
       
    64   def clean_home_page(self):
       
    65     return self.clean_empty('home_page')
       
    66 
       
    67   def clean_blog(self):
       
    68     return self.clean_empty('blog')
       
    69 
       
    70   def clean_photo_url(self):
       
    71     return self.clean_empty('photo_url')
       
    72 
       
    73 
       
    74 class EditForm(CreateForm):
       
    75   """Django form displayed when editing a Host.
       
    76   """
       
    77 
       
    78 class View(base.View):
       
    79   """View methods for the Host model
       
    80   """
       
    81 
       
    82   def __init__(self, original_params=None, original_rights=None):
       
    83     """Defines the fields and methods required for the base View class
       
    84     to provide the user with list, public, create, edit and delete views.
       
    85 
       
    86     Params:
       
    87       original_params: a dict with params for this View
       
    88       original_rights: a dict with right definitions for this View
       
    89     """
       
    90 
       
    91     self._logic = soc.logic.models.host.logic
       
    92 
       
    93     params = {}
       
    94     rights = {}
       
    95 
       
    96     params['name'] = "Host"
       
    97     params['name_short'] = "Host"
       
    98     params['name_plural'] = "Hosts"
       
    99 
       
   100     params['edit_form'] = EditForm
       
   101     params['create_form'] = CreateForm
       
   102 
       
   103     # TODO(tlarsen) Add support for Django style template lookup
       
   104     params['edit_template'] = 'soc/models/edit.html'
       
   105     params['public_template'] = 'soc/models/public.html'
       
   106     params['list_template'] = 'soc/models/all.html'
       
   107 
       
   108     params['lists_template'] = {
       
   109       'list_main': 'soc/list/list_main.html',
       
   110       'list_pagination': 'soc/list/list_pagination.html',
       
   111       'list_row': 'soc/host/group_row.html',
       
   112       'list_heading': 'soc/host/group_heading.html',
       
   113     }
       
   114 
       
   115     params['delete_redirect'] = '/site/host/list'
       
   116     params['create_redirect'] = '/site/host/profile'
       
   117 
       
   118     params['save_message'] = [ugettext_lazy('Profile saved.')]
       
   119 
       
   120     params['edit_params'] = {
       
   121         self.DEF_SUBMIT_MSG_PARAM_NAME: self.DEF_SUBMIT_MSG_PROFILE_SAVED,
       
   122         }
       
   123 
       
   124     rights['list'] = [helper.access.checkIsDeveloper]
       
   125     rights['delete'] = [helper.access.checkIsDeveloper]
       
   126 
       
   127     params = dicts.merge(original_params, params)
       
   128     rights = dicts.merge(original_rights, rights)
       
   129 
       
   130     base.View.__init__(self, rights=rights, params=params)
       
   131 
       
   132   def _editPost(self, request, entity, fields):
       
   133     """
       
   134     """
       
   135 
       
   136     fields['sponsor_ln'] = fields['sponsor'].link_name
       
   137     fields['user_ln'] = fields['user'].link_name
       
   138 
       
   139 
       
   140 view = View()
       
   141 
       
   142 create = view.create
       
   143 delete = view.delete
       
   144 edit = view.edit
       
   145 list = view.list
       
   146 public = view.public