app/soc/views/models/role.py
changeset 495 87afae6e4c51
child 499 d22e4fe8e64b
equal deleted inserted replaced
494:5e9c656a1b68 495:87afae6e4c51
       
     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" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api import users
       
    26 
       
    27 from django import forms
       
    28 from django import http
       
    29 from django.utils.translation import ugettext_lazy
       
    30 
       
    31 from soc.models import request as request_model
       
    32 from soc.logic import dicts
       
    33 from soc.logic.models import request as request_logic
       
    34 from soc.logic.models import user as user_logic
       
    35 from soc.views import helper
       
    36 from soc.views.models import base
       
    37 from soc.views.models import user as user_view
       
    38 
       
    39 import soc.models.request
       
    40 import soc.views.helper.widgets
       
    41 
       
    42 
       
    43 class RequestForm(helper.forms.BaseForm):
       
    44   """Django form displayed when creating a new Invite
       
    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.request.Request
       
    53 
       
    54     #: exclude pretty much everything, model=None would also remove the help text etc. 
       
    55     exclude = ['inheritance_line', 'requester', 'to', 'role', 'accepted', 'declined']
       
    56 
       
    57   requester = forms.CharField(widget=helper.widgets.ReadOnlyInput())
       
    58 
       
    59   role = forms.CharField(widget=helper.widgets.ReadOnlyInput())
       
    60 
       
    61   to = forms.CharField(widget=helper.widgets.ReadOnlyInput())
       
    62 
       
    63 
       
    64 class RoleView(base.View):
       
    65   """Views for all entities that inherit from Role
       
    66 
       
    67   All views that only Role entities have are defined in this subclass.
       
    68   """
       
    69 
       
    70   def __init__(self, original_params=None, original_rights=None):
       
    71     """
       
    72 
       
    73     Args:
       
    74       rights: This dictionary should be filled with the access check
       
    75         functions that should be called, it will be modified in-place.
       
    76       params: This dictionary should be filled with the parameters
       
    77     """
       
    78 
       
    79     params = {}
       
    80     rights = {}
       
    81 
       
    82     params = dicts.merge(original_params, params)
       
    83     rights = dicts.merge(original_rights, rights)
       
    84 
       
    85     base.View.__init__(self, rights=rights, params=params)
       
    86 
       
    87   def invite(self, request, page_name=None, params=None, **kwargs):
       
    88     """Displays the request promotion to Role page.
       
    89     """
       
    90 
       
    91     new_params = {}
       
    92 
       
    93     new_params['list_template'] = 'soc/models/create_invite.html'
       
    94     new_params['list_redirect_action'] = 'request/create/%s/%s' % (
       
    95         self._params['name_short'].lower(),
       
    96         kwargs['link_name'])
       
    97     new_params['list_redirect_entity'] = self._params['name']
       
    98     new_params['name'] = self._params['name']
       
    99     new_params['name_short'] = self._params['name_short']
       
   100     new_params['name_plural'] = self._params['name_plural']
       
   101 
       
   102     params = dicts.merge(params, new_params)
       
   103 
       
   104     try:
       
   105       self.checkAccess('invite', request)
       
   106     except soc.views.out_of_band.AccessViolationResponse, alt_response:
       
   107       return alt_response.response()
       
   108 
       
   109     return user_view.list(request, page_name=page_name, params=params)
       
   110 
       
   111   def promote(self, request, page_name=None, **kwargs):
       
   112     """Displays the promote to Role page.
       
   113 
       
   114     Args:
       
   115       request: the standard Django HTTP request object
       
   116       page: a soc.logic.site.page.Page object which is abstraction
       
   117         that combines a Django view with sidebar menu info
       
   118       kwargs: the Key Fields for the specified entity
       
   119     """
       
   120 
       
   121     properties = {
       
   122         'accepted': True,
       
   123         }
       
   124 
       
   125     entity = request_logic.logic.updateOrCreateFromFields(properties, **kwargs)
       
   126 
       
   127     # TODO(SRabbelier) finish this
       
   128 
       
   129   def accept(self, request, page_name=None, params=None, **kwargs):
       
   130     """Displays the accept a Role request page.
       
   131 
       
   132     Args:
       
   133       request: the standard Django HTTP request object
       
   134       page: a soc.logic.site.page.Page object which is abstraction
       
   135         that combines a Django view with sidebar menu info
       
   136       kwargs: the Key Fields for the specified entity
       
   137     """
       
   138 
       
   139     entity = request_logic.logic.getFromFields(**kwargs)
       
   140 
       
   141     if entity.declined:
       
   142       properties = {
       
   143           'declined': False,
       
   144           }
       
   145 
       
   146       request_logic.logic.updateModelProperties(entity, **properties)
       
   147 
       
   148     if not entity.accepted:
       
   149       raise Error("The request has not yet been accepted")
       
   150 
       
   151     id = users.get_current_user()
       
   152     user = models.user.logic.getFromFields(email=id.email())
       
   153 
       
   154     if entity.user != user:
       
   155       raise Error("The request is being accepted by the wrong person")
       
   156 
       
   157     if entity.role != params['name'].lower():
       
   158       raise Error("The wrong module is handling the request")
       
   159 
       
   160     redirect = params['accept_redirect']
       
   161     suffix = self._logic.getKeySuffix(entity)
       
   162 
       
   163     return helper.responses.redirectToChangedSuffix(
       
   164         request, suffix, suffix)
       
   165 
       
   166   def decline(self, request, page_name=None, **kwargs):
       
   167     """Displays the decline a Role request page.
       
   168 
       
   169     Args:
       
   170       request: the standard Django HTTP request object
       
   171       page: a soc.logic.site.page.Page object which is abstraction
       
   172         that combines a Django view with sidebar menu info
       
   173       kwargs: the Key Fields for the specified entity
       
   174     """
       
   175 
       
   176     properties = {
       
   177         'declined': True,
       
   178         }
       
   179 
       
   180     request_logic.logic.updateOrCreateFromFields(properties, **kwargs)
       
   181 
       
   182     redirect = self._params['decline_redirect']
       
   183     suffix = self._logic.getKeySuffix(entity)
       
   184 
       
   185     return helper.responses.redirectToChangedSuffix(
       
   186         request, suffix, suffix)
       
   187 
       
   188   def getDjangoURLPatterns(self):
       
   189     """see base.View.getDjangoURLPatterns()
       
   190     """
       
   191 
       
   192     params = {}
       
   193     default_patterns = self._params['django_patterns_defaults']
       
   194     default_patterns += [
       
   195         (r'^%(name_lower)s/invite/%(lnp)s$',
       
   196             'soc.views.models.%s.invite', 'Invite %(name)s')]
       
   197 
       
   198     params['django_patterns_defaults'] = default_patterns
       
   199     patterns = super(RoleView, self).getDjangoURLPatterns(params)
       
   200 
       
   201     return patterns
       
   202