app/soc/views/models/host.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 16 Nov 2008 23:02:31 +0000
changeset 493 6976c4d8d0ac
parent 492 4abdeedfc08e
child 494 5e9c656a1b68
permissions -rw-r--r--
Refactor the sidebar to use Django templates With this change the html for the sidebar is no longer generated by Melange, instead it's delegated to Django (which is what it does best anyway). The downside is that it is no longer possible to have arbitrary deeply nested menu's.

#!/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 Host profiles.
"""

__authors__ = [
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
  ]


from django.utils.translation import ugettext_lazy

from soc.logic import dicts
from soc.views import helper
from soc.views.models import base
from soc.views.sitemap import sidebar

import soc.models.host
import soc.logic.models.host
import soc.views.helper


class CreateForm(helper.forms.BaseForm):
  """Django form displayed when creating a Host.
  """

  class Meta:
    """Inner Meta class that defines some behavior for the form.
    """

    #: db.Model subclass for which the form will gather information
    model = soc.models.host.Host

    #: list of model fields which will *not* be gathered by the form
    exclude = ['inheritance_line']

  def clean_empty(self, field):
    data = self.cleaned_data.get(field)
    if not data or data == u'':
      return None

    return data

  def clean_home_page(self):
    return self.clean_empty('home_page')

  def clean_blog(self):
    return self.clean_empty('blog')

  def clean_photo_url(self):
    return self.clean_empty('photo_url')


class EditForm(CreateForm):
  """Django form displayed when editing a Host.
  """

  pass

class View(base.View):
  """View methods for the Host 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.host.logic

    params = {}
    rights = {}

    params['name'] = "Host"
    params['name_short'] = "Host"
    params['name_plural'] = "Hosts"

    params['edit_form'] = EditForm
    params['create_form'] = CreateForm

    # TODO(tlarsen) Add support for Django style template lookup
    params['edit_template'] = 'soc/models/edit.html'
    params['public_template'] = 'soc/host/public.html'
    params['list_template'] = 'soc/models/list.html'

    params['lists_template'] = {
      'list_main': 'soc/list/list_main.html',
      'list_pagination': 'soc/list/list_pagination.html',
      'list_row': 'soc/host/list/host_row.html',
      'list_heading': 'soc/host/list/host_heading.html',
    }

    params['delete_redirect'] = '/host/list'
    params['invite_redirect'] = '/host/list'

    params['save_message'] = [ugettext_lazy('Profile saved.')]

    params['edit_params'] = {
        self.DEF_SUBMIT_MSG_PARAM_NAME: self.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):
    """See base.View._editPost().
    """

    fields['sponsor_ln'] = fields['sponsor'].link_name
    fields['user_ln'] = fields['user'].link_name


view = View()

create = view.create
delete = view.delete
edit = view.edit
list = view.list
public = view.public

sidebar.SIDEBAR.append(view.getSidebarLinks())