app/soc/views/models/club.py
changeset 1153 4804f7f5a7c0
parent 1144 f89bbc9b20a6
child 1163 d8c50be19232
equal deleted inserted replaced
1152:b82caf7bb17c 1153:4804f7f5a7c0
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    22     '"Lennard de Rijk" <ljvderijk@gmail.com>',
    22     '"Lennard de Rijk" <ljvderijk@gmail.com>',
    23   ]
    23   ]
    24 
    24 
    25 
    25 
    26 from django import http
       
    27 from django import forms
    26 from django import forms
    28 
    27 
    29 from soc.logic import cleaning
    28 from soc.logic import cleaning
    30 from soc.logic import dicts
    29 from soc.logic import dicts
    31 from soc.logic.models import user as user_logic
    30 from soc.logic.models import user as user_logic
    40 from soc.views.helper import responses
    39 from soc.views.helper import responses
    41 from soc.views.helper import widgets
    40 from soc.views.helper import widgets
    42 from soc.views.models import group
    41 from soc.views.models import group
    43 
    42 
    44 import soc.logic.models.club
    43 import soc.logic.models.club
    45 import soc.views.helper
       
    46 
    44 
    47 
    45 
    48 class View(group.View):
    46 class View(group.View):
    49   """View methods for the Club model.
    47   """View methods for the Club model.
    50   """
    48   """
    75     new_params['url_name'] = "club"
    73     new_params['url_name'] = "club"
    76     new_params['sidebar_grouping'] = 'Clubs'
    74     new_params['sidebar_grouping'] = 'Clubs'
    77 
    75 
    78     patterns = []
    76     patterns = []
    79 
    77 
    80     patterns += [(r'^%(url_name)s/(?P<access_type>applicant)/%(key_fields)s$',
    78     patterns += [(r'^%(url_name)s/(?P<access_type>apply_member)$',
    81         'soc.views.models.%(module_name)s.applicant', 
       
    82         "%(name)s Creation via Accepted Application"),
       
    83         (r'^%(url_name)s/(?P<access_type>apply_member)$',
       
    84         'soc.views.models.%(module_name)s.apply_member', 
    79         'soc.views.models.%(module_name)s.apply_member', 
    85         "List of all %(name_plural)s you can apply to"),]
    80         "List of all %(name_plural)s you can apply to"),]
    86 
    81 
    87     new_params['extra_django_patterns'] = patterns
    82     new_params['extra_django_patterns'] = patterns
       
    83 
       
    84     new_params['application_logic'] = club_app_logic
       
    85     new_params['group_applicant_url'] = True
    88 
    86 
    89     new_params['sidebar_additional'] = [
    87     new_params['sidebar_additional'] = [
    90         ('/' + new_params['url_name'] + '/apply_member', 'Join a Club', 'apply_member'),]
    88         ('/' + new_params['url_name'] + '/apply_member', 'Join a Club', 'apply_member'),]
    91 
    89 
    92     new_params['create_extra_dynafields'] = {
    90     new_params['create_extra_dynafields'] = {
   110     applicant_create_form = dynaform.extendDynaForm(
   108     applicant_create_form = dynaform.extendDynaForm(
   111         dynaform = self._params['create_form'],
   109         dynaform = self._params['create_form'],
   112         dynafields = updated_fields)
   110         dynafields = updated_fields)
   113 
   111 
   114     params['applicant_create_form'] = applicant_create_form
   112     params['applicant_create_form'] = applicant_create_form
   115 
       
   116   @decorators.merge_params
       
   117   @decorators.check_access
       
   118   def applicant(self, request, access_type,
       
   119                 page_name=None, params=None, **kwargs):
       
   120     """Handles the creation of a club via an approved club application.
       
   121 
       
   122     Args:
       
   123       request: the standard Django HTTP request object
       
   124       access_type : the name of the access type which should be checked
       
   125       page_name: the page name displayed in templates as page and header title
       
   126       params: a dict with params for this View
       
   127       kwargs: the Key Fields for the specified entity
       
   128     """
       
   129 
       
   130     # get the context for this webpage
       
   131     context = responses.getUniversalContext(request)
       
   132     context['page_name'] = page_name
       
   133 
       
   134     if request.method == 'POST':
       
   135       return self.applicantPost(request, context, params, **kwargs)
       
   136     else:
       
   137       # request.method == 'GET'
       
   138       return self.applicantGet(request, context, params, **kwargs)
       
   139 
       
   140   def applicantGet(self, request, context, params, **kwargs):
       
   141     """Handles the GET request concerning the creation of a club via an
       
   142     approved club application.
       
   143 
       
   144     Args:
       
   145       request: the standard Django HTTP request object
       
   146       context: dictionary containing the context for this view
       
   147       params: a dict with params for this View
       
   148       kwargs: the Key Fields for the specified entity
       
   149     """
       
   150 
       
   151     # find the application
       
   152     key_fields = club_app_logic.logic.getKeyFieldsFromFields(kwargs)
       
   153     application = club_app_logic.logic.getFromKeyFields(key_fields)
       
   154 
       
   155     # extract the application fields
       
   156     field_names = application.properties().keys()
       
   157     fields = dict( [(i, getattr(application, i)) for i in field_names] )
       
   158 
       
   159     # create the form using the fields from the application as the initial value
       
   160     form = params['applicant_create_form'](initial=fields)
       
   161 
       
   162     # construct the appropriate response
       
   163     return super(View, self)._constructResponse(request, entity=None,
       
   164         context=context, form=form, params=params)
       
   165 
       
   166   def applicantPost(self, request, context, params, **kwargs):
       
   167     """Handles the POST request concerning the creation of a club via an
       
   168     approved club application.
       
   169 
       
   170     Args:
       
   171       request: the standard Django HTTP request object
       
   172       context: dictionary containing the context for this view
       
   173       params: a dict with params for this View
       
   174       kwargs: the Key Fields for the specified entity
       
   175     """
       
   176 
       
   177     # populate the form using the POST data
       
   178     form = params['applicant_create_form'](request.POST)
       
   179 
       
   180     if not form.is_valid():
       
   181       # return the invalid form response
       
   182       return self._constructResponse(request, entity=None, context=context,
       
   183           form=form, params=params)
       
   184 
       
   185     # collect the cleaned data from the valid form
       
   186     key_name, fields = soc.views.helper.forms.collectCleanedFields(form)
       
   187 
       
   188     # fill in the founder of the club
       
   189     user = user_logic.logic.getForCurrentAccount()
       
   190     fields['founder'] = user
       
   191 
       
   192     if not key_name:
       
   193       key_fields =  self._logic.getKeyFieldsFromFields(fields)
       
   194       key_name = self._logic.getKeyNameFromFields(key_fields)
       
   195 
       
   196     # create the club entity
       
   197     entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
       
   198 
       
   199     # redirect to notifications list to see the admin invite
       
   200     return http.HttpResponseRedirect('/notification/list')
       
   201 
       
   202 
   113 
   203   @decorators.merge_params
   114   @decorators.merge_params
   204   @decorators.check_access
   115   @decorators.check_access
   205   def applyMember(self, request, access_type,
   116   def applyMember(self, request, access_type,
   206                   page_name=None, params=None, **kwargs):
   117                   page_name=None, params=None, **kwargs):