app/soc/views/models/club.py
changeset 869 cd17698846ce
parent 858 e79e7a22326f
child 877 b2575da44c36
equal deleted inserted replaced
868:8d3c1ee6eba7 869:cd17698846ce
    23   ]
    23   ]
    24 
    24 
    25 
    25 
    26 from google.appengine.api import users
    26 from google.appengine.api import users
    27 
    27 
       
    28 from django import http
    28 from django import forms
    29 from django import forms
    29 
    30 
    30 from soc.logic import dicts
    31 from soc.logic import dicts
    31 from soc.logic.models import user as user_logic
    32 from soc.logic.models import user as user_logic
    32 from soc.logic.models import group_app as group_app_logic
    33 from soc.logic.models import group_app as group_app_logic
    33 from soc.logic.models import club as club_logic
    34 from soc.logic.models import club as club_logic
       
    35 from soc.views import out_of_band
    34 from soc.views.helper import access
    36 from soc.views.helper import access
       
    37 from soc.views.helper import dynaform
    35 from soc.views.helper import widgets
    38 from soc.views.helper import widgets
       
    39 from soc.views.helper import responses
    36 from soc.views.models import base
    40 from soc.views.models import base
    37 
    41 
    38 import soc.logic.models.club
    42 import soc.logic.models.club
       
    43 import soc.views.helper
    39 
    44 
    40 
    45 
    41 class View(base.View):
    46 class View(base.View):
    42   """View methods for the Club model.
    47   """View methods for the Club model.
    43   """
    48   """
    49     Params:
    54     Params:
    50       params: a dict with params for this View
    55       params: a dict with params for this View
    51     """
    56     """
    52 
    57 
    53     rights = {}
    58     rights = {}
    54     rights['create'] = [access.checkIsClubAppAccepted]
    59     rights['create'] = [access.checkIsHost]
    55     rights['edit'] = [access.checkIsClubAdminForClub]
    60     rights['edit'] = [access.checkIsClubAdminForClub]
       
    61     rights['applicant'] = [access.checkIsClubAppAccepted]
    56 
    62 
    57     new_params = {}
    63     new_params = {}
    58     new_params['logic'] = soc.logic.models.club.logic
    64     new_params['logic'] = soc.logic.models.club.logic
    59     new_params['rights'] = rights
    65     new_params['rights'] = rights
    60 
       
    61     new_params['name'] = "Club"
    66     new_params['name'] = "Club"
       
    67     
       
    68     patterns = []
       
    69 
       
    70     page_name = "Club Creation via Accepted Application"
       
    71     patterns += [(r'^%(url_name)s/(?P<access_type>applicant)/%(key_fields)s$',
       
    72                   'soc.views.models.%(module_name)s.applicant', page_name)]
       
    73     
       
    74     new_params['extra_django_patterns'] = patterns
    62 
    75 
    63     new_params['extra_dynaexclude'] = ['founder', 'home']
    76     new_params['extra_dynaexclude'] = ['founder', 'home']
    64     new_params['edit_extra_dynafields'] = {
    77     new_params['edit_extra_dynafields'] = {
    65         'founded_by': forms.CharField(widget=widgets.ReadOnlyInput(),
    78         'founded_by': forms.CharField(widget=widgets.ReadOnlyInput(),
    66                                    required=False),
    79                                    required=False),
    67         }
    80         }
    68 
    81 
    69     new_params['edit_redirect'] = '/notification/list'
       
    70 
       
    71     params = dicts.merge(params, new_params)
    82     params = dicts.merge(params, new_params)
    72 
    83 
    73     super(View, self).__init__(params=params)
    84     super(View, self).__init__(params=params)
    74 
    85     
    75   def create(self, request, access_type,
    86     # create and store the special form for applicants
    76              page_name=None, params=None, **kwargs):
    87     updated_fields = {
    77     """See base.View.create()
    88         'link_id': forms.CharField(widget=widgets.ReadOnlyInput(),
    78     """
    89             required=False)}
    79 
    90         
    80     if 'link_id' not in kwargs:
    91     applicant_create_form = dynaform.extendDynaForm(
    81       return super(View, self).create(request, access_type, page_name,
    92         dynaform = self._params['create_form'],
    82                                       params=params, **kwargs)
    93         dynafields = updated_fields)
    83 
    94     
    84     # Find their application
    95     params['applicant_create_form'] = applicant_create_form
       
    96 
       
    97 
       
    98   def applicant(self, request, access_type,
       
    99                   page_name=None, params=None, **kwargs):
       
   100     """Handles the creation of a club via an approved club application.
       
   101     
       
   102     Args:
       
   103       request: the standard Django HTTP request object
       
   104       page_name: the page name displayed in templates as page and header title
       
   105       params: a dict with params for this View
       
   106       kwargs: the Key Fields for the specified entity
       
   107     """
       
   108     
       
   109     
       
   110     # merge the params
       
   111     params = dicts.merge(params, self._params)
       
   112     
       
   113     # check if the current user has access to this page
       
   114     try:
       
   115       access.checkAccess(access_type, request, rights=params['rights'])
       
   116     except out_of_band.Error, error:
       
   117       return responses.errorResponse(error, request)
       
   118     
       
   119     # get the context for this webpage
       
   120     context = responses.getUniversalContext(request)
       
   121     context['page_name'] = page_name
       
   122     
       
   123     
       
   124     if request.method == 'POST':
       
   125       return self.applicantPost(request, context, params, **kwargs)
       
   126     else:
       
   127       # request.method == 'GET'
       
   128       return self.applicantGet(request, context, params, **kwargs)
       
   129 
       
   130 
       
   131   def applicantGet(self, request, context, params, **kwargs):
       
   132     """Handles the GET request concerning the creation of a club via an 
       
   133     approved club application.
       
   134     
       
   135     Args:
       
   136       request: the standard Django HTTP request object
       
   137       context: dictionary containing the context for this view
       
   138       params: a dict with params for this View
       
   139       kwargs: the Key Fields for the specified entity
       
   140     """
       
   141     
       
   142     # find the application
    85     key_fields = group_app_logic.logic.getKeyFieldsFromDict(kwargs)
   143     key_fields = group_app_logic.logic.getKeyFieldsFromDict(kwargs)
    86     application = group_app_logic.logic.getFromFields(**key_fields)
   144     application = group_app_logic.logic.getFromFields(**key_fields)
    87 
   145     
    88     # Extract the application fields
   146     # extract the application fields
    89     field_names = application.properties().keys()
   147     field_names = application.properties().keys()
    90     fields = dict( [(i, getattr(application, i)) for i in field_names] )
   148     fields = dict( [(i, getattr(application, i)) for i in field_names] )
    91 
   149     
    92     empty = dict( [(i, None) for i in self._logic.getKeyFieldNames()] )
   150     # create the form using the fields from the application as the initial value
    93 
   151     form = params['applicant_create_form'](initial=fields)
    94     return super(View, self).edit(request, access_type, page_name,
   152     
    95                                   params=params, seed=fields, **empty)
   153     # construct the appropriate response
       
   154     return super(View, self)._constructResponse(request, entity=None, 
       
   155         context=context, form=form, params=params)
       
   156 
       
   157 
       
   158   def applicantPost(self, request, context, params, **kwargs):
       
   159     """Handles the POST request concerning the creation of a club via an 
       
   160     approved club application.
       
   161     
       
   162     Args:
       
   163       request: the standard Django HTTP request object
       
   164       context: dictionary containing the context for this view
       
   165       params: a dict with params for this View
       
   166       kwargs: the Key Fields for the specified entity
       
   167     """
       
   168     
       
   169     # populate the form using the POST data
       
   170     form = params['applicant_create_form'](request.POST)
       
   171     
       
   172     if not form.is_valid():
       
   173       # return the invalid form response
       
   174       return self._constructResponse(request, entity=None, context=context,
       
   175           form=form, params=params)
       
   176     
       
   177     # collect the cleaned data from the valid form
       
   178     key_name, fields = soc.views.helper.forms.collectCleanedFields(form)
       
   179     
       
   180     # fill in the founder of the club
       
   181     account = users.get_current_user()
       
   182     user = user_logic.logic.getForFields({'account': account}, unique=True)
       
   183     fields['founder'] = user
       
   184     
       
   185     if not key_name:
       
   186       key_fields =  self._logic.getKeyFieldsFromDict(fields)
       
   187       key_name = self._logic.getKeyNameForFields(key_fields)
       
   188     
       
   189     # create the club entity
       
   190     entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
       
   191     
       
   192     # redirect to notifications list to see the admin invite
       
   193     return http.HttpResponseRedirect('/notification/list')
       
   194 
    96 
   195 
    97   def _editGet(self, request, entity, form):
   196   def _editGet(self, request, entity, form):
    98     """See base.View._editGet().
   197     """See base.View._editGet().
    99     """
   198     """
   100 
   199 
   101     # fill in the founded_by with data from the entity
   200     # fill in the founded_by with data from the entity
   102     form.fields['founded_by'].initial = entity.founder.name
   201     form.fields['founded_by'].initial = entity.founder.name
   103     super(View, self)._editGet(request, entity, form)
   202     super(View, self)._editGet(request, entity, form)
       
   203 
   104 
   204 
   105   def _editPost(self, request, entity, fields):
   205   def _editPost(self, request, entity, fields):
   106     """See base.View._editPost().
   206     """See base.View._editPost().
   107     """
   207     """
   108 
   208 
   112       user = user_logic.logic.getForFields({'account': account}, unique=True)
   212       user = user_logic.logic.getForFields({'account': account}, unique=True)
   113       fields['founder'] = user
   213       fields['founder'] = user
   114 
   214 
   115     super(View, self)._editPost(request, entity, fields)
   215     super(View, self)._editPost(request, entity, fields)
   116 
   216 
       
   217 
   117 view = View()
   218 view = View()
   118 
   219 
       
   220 applicant = view.applicant
   119 create = view.create
   221 create = view.create
   120 delete = view.delete
   222 delete = view.delete
   121 edit = view.edit
   223 edit = view.edit
   122 list = view.list
   224 list = view.list
   123 public = view.public
   225 public = view.public