app/soc/views/models/group_app.py
changeset 1138 18ef39338211
parent 858 e79e7a22326f
child 1139 7a6f94ffcc87
equal deleted inserted replaced
1137:91b6bfe09579 1138:18ef39338211
    17 """Views for Group App.
    17 """Views for Group App.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    21     '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22     '"Lennard de Rijk" <ljvderijk@gmail.com>',
    22   ]
    23   ]
    23 
    24 
    24 
    25 
    25 from django import forms
    26 from django import forms
    26 
    27 from django.utils.translation import ugettext
       
    28 
       
    29 from soc.logic import accounts
       
    30 from soc.logic import cleaning
    27 from soc.logic import dicts
    31 from soc.logic import dicts
       
    32 from soc.logic.helper import notifications
    28 from soc.logic.models import group_app as group_app_logic
    33 from soc.logic.models import group_app as group_app_logic
       
    34 from soc.logic.models import user as user_logic
       
    35 from soc.views.helper import decorators
       
    36 from soc.views.helper import lists as list_helper
       
    37 from soc.views.helper import redirects
       
    38 from soc.views.helper import responses
    29 from soc.views.models import base
    39 from soc.views.models import base
    30 
    40 
    31 import soc.logic.models.group_app
    41 import soc.logic.models.group_app
    32 
    42 
    33 
    43 
    47     new_params['logic'] = soc.logic.models.group_app.logic
    57     new_params['logic'] = soc.logic.models.group_app.logic
    48 
    58 
    49     new_params['name'] = "Group Application"
    59     new_params['name'] = "Group Application"
    50     new_params['name_short'] = "Group App"
    60     new_params['name_short'] = "Group App"
    51 
    61 
    52     params = dicts.merge(params, new_params)
    62     # use the twoline templates for these questionnaires
       
    63     new_params['create_template'] = 'soc/models/twoline_edit.html'
       
    64     new_params['edit_template'] = 'soc/models/twoline_edit.html'
       
    65 
       
    66     patterns = [(r'^%(url_name)s/(?P<access_type>review)$',
       
    67         'soc.views.models.%(module_name)s.review_overview',
       
    68         'Review %(name_plural)s'),
       
    69         (r'^%(url_name)s/(?P<access_type>review)/%(lnp)s$',
       
    70           'soc.views.models.%(module_name)s.review',
       
    71           'Review %(name_short)s')]
       
    72 
       
    73     new_params['extra_django_patterns'] = patterns
       
    74 
       
    75     new_params['sidebar_additional'] = [
       
    76         ('/%(url_name)s/review' % params,
       
    77          'Review %(name_plural)s' % params, 'review')]
       
    78 
       
    79     new_params['extra_dynaexclude'] = ['applicant', 'backup_admin', 'status',
       
    80         'created_on', 'last_modified_on']
       
    81 
       
    82     new_params['create_extra_dynafields'] = {
       
    83         'backup_admin_link_id': forms.CharField(
       
    84               label=params['logic'].getModel().backup_admin.verbose_name
       
    85               ),
       
    86         'clean_backup_admin_link_id': 
       
    87             cleaning.clean_users_not_same('backup_admin_link_id'),
       
    88         }
       
    89 
       
    90     new_params['edit_extra_dynafields'] = {
       
    91         'clean_link_id' : cleaning.clean_link_id('link_id'),
       
    92         }
       
    93 
       
    94     params = dicts.merge(params, new_params, sub_merge=True)
    53 
    95 
    54     super(View, self).__init__(params=params)
    96     super(View, self).__init__(params=params)
    55 
    97 
       
    98 
       
    99   def _editGet(self, request, entity, form):
       
   100     """See base.View._editGet().
       
   101     """
       
   102 
       
   103     form.fields['backup_admin_link_id'].initial = entity.backup_admin.link_id
       
   104 
       
   105     super(View, self)._editGet(request, entity, form)
       
   106 
       
   107   def _editPost(self, request, entity, fields):
       
   108     """See base.View._editPost().
       
   109     """
       
   110 
       
   111     fields['backup_admin'] = fields['backup_admin_link_id']
       
   112 
       
   113     if not entity:
       
   114       fields['applicant'] = user_logic.logic.getForCurrentAccount()
       
   115 
       
   116     # the application has either been created or edited so
       
   117     # the status needs to be set accordingly
       
   118     fields['status'] = 'needs review'
       
   119 
       
   120     super(View, self)._editPost(request, entity, fields)
       
   121 
       
   122 
       
   123   def _public(self, request, entity, context):
       
   124     """See base._public().
       
   125     """
       
   126 
       
   127     context['entity_type_url'] = self._params['url_name']
       
   128 
       
   129     super(View, self)._public(request, entity, context)
       
   130 
       
   131 
       
   132   @decorators.merge_params
       
   133   @decorators.check_access
       
   134   def list(self, request, access_type,
       
   135            page_name=None, params=None, filter=None):
       
   136     """Lists all notifications that the current logged in user has stored.
       
   137 
       
   138     for parameters see base.list()
       
   139     """
       
   140 
       
   141     # get the current user
       
   142     user_entity = user_logic.logic.getForCurrentAccount()
       
   143 
       
   144     is_developer = accounts.isDeveloper(user=user_entity)
       
   145 
       
   146     filter = {
       
   147         'status': 'needs review',
       
   148         }
       
   149 
       
   150     if not is_developer:
       
   151       # only select the applications for this user so construct a filter
       
   152       filter['applicant'] = user_entity
       
   153 
       
   154     # get all the pending applications
       
   155 
       
   156     pa_params = params.copy() # pending applications
       
   157 
       
   158     if is_developer:
       
   159       pa_params['list_description'] = ugettext(
       
   160           "An overview of all pending %(name_plural)s.") % params
       
   161     else:
       
   162       pa_params['list_description'] = ugettext(
       
   163           "An overview of your pending %(name_plural)s.") % params
       
   164 
       
   165     pa_list = list_helper.getListContent(
       
   166         request, pa_params, filter, 0)
       
   167 
       
   168     # get all the reviewed applications now
       
   169 
       
   170     # re-use the old filter, but set to only reviewed and accepted
       
   171     filter['status'] = 'accepted'
       
   172 
       
   173     aa_params = params.copy() # accepted applications
       
   174 
       
   175     if is_developer:
       
   176       aa_params['list_description'] = ugettext(
       
   177           "An overview of all accepted %(name_plural)s.") % params
       
   178     else:
       
   179       aa_params['list_description'] = ugettext(
       
   180           "An overview of your accepted %(name_plural)s.") % params
       
   181 
       
   182     aa_params['url_name'] = params['group_url_name']
       
   183     aa_params['list_action'] = (redirects.getApplicantRedirect, aa_params)
       
   184 
       
   185     aa_list = list_helper.getListContent(
       
   186         request, aa_params, filter, 1)
       
   187 
       
   188     # get all the reviewed applications that were denied
       
   189 
       
   190     # re use the old filter, but this time only for denied apps
       
   191     filter['status'] = 'rejected'
       
   192 
       
   193     da_params = params.copy() # denied applications
       
   194 
       
   195     if is_developer:
       
   196       da_params['list_description'] = ugettext(
       
   197           "An overview of all denied %(name_plural)s.") % params
       
   198     else:
       
   199       da_params['list_description'] = ugettext(
       
   200           "An overview of your denied %(name_plural)s.") % params
       
   201 
       
   202     da_list = list_helper.getListContent(
       
   203         request, da_params, filter, 2)
       
   204 
       
   205     contents = [pa_list, aa_list, da_list]
       
   206 
       
   207     if is_developer:
       
   208       # re use the old filter, but this time only for ignored apps
       
   209       filter['status'] = 'ignored'
       
   210 
       
   211       ia_params = params.copy() # ignored applications
       
   212 
       
   213       ia_params['list_description'] = ugettext(
       
   214           "An overview of all ignored %(name_plural)s.") % params
       
   215 
       
   216       ia_list = list_helper.getListContent(
       
   217           request, ia_params, filter, 3)
       
   218 
       
   219       contents += [ia_list]
       
   220 
       
   221     # call the _list method from base to display the list
       
   222     return self._list(request, params, contents, page_name)
       
   223 
       
   224 
       
   225   @decorators.merge_params
       
   226   @decorators.check_access
       
   227   def review(self, request, access_type,
       
   228              page_name=None, params=None, **kwargs):
       
   229     """Handles the view containing the review of an application.
       
   230 
       
   231     accepted (true or false) in the GET data will mark
       
   232     the application accordingly.
       
   233 
       
   234 
       
   235     For params see base.View.public().
       
   236     """
       
   237 
       
   238     # create default template context for use with any templates
       
   239     context = responses.getUniversalContext(request)
       
   240     context['page_name'] = page_name
       
   241     entity = None
       
   242 
       
   243     try:
       
   244       key_fields = self._logic.getKeyFieldsFromFields(kwargs)
       
   245       entity = self._logic.getFromKeyFieldsOr404(key_fields)
       
   246     except out_of_band.Error, error:
       
   247       return helper.responses.errorResponse(
       
   248           error, request, template=params['error_public'], context=context)
       
   249 
       
   250     get_dict = request.GET
       
   251 
       
   252     # check to see if we can make a decision for this application
       
   253     if 'status' in get_dict.keys():
       
   254       status_value = get_dict['status']
       
   255 
       
   256       if status_value in ['accepted', 'rejected', 'ignored']:
       
   257         # this application has been properly reviewed update the status
       
   258         fields = {'status' : status_value}
       
   259 
       
   260         self._logic.updateEntityProperties(entity, fields)
       
   261 
       
   262         if status_value == 'accepted':
       
   263           # the application has been accepted send out a notification
       
   264           notifications.sendNewGroupNotification(entity, params)
       
   265 
       
   266         return self.reviewOverview(request, access_type,
       
   267             page_name=page_name, params=params, **kwargs)
       
   268 
       
   269     # the application has not been reviewed so show the information
       
   270     # using the appropriate review template
       
   271     params['public_template'] = params['review_template']
       
   272 
       
   273     return super(View, self).public(request, access_type,
       
   274         page_name=page_name, params=params, **kwargs)
       
   275 
       
   276 
       
   277   @decorators.merge_params
       
   278   @decorators.check_access
       
   279   def reviewOverview(self, request, access_type,
       
   280              page_name=None, params=None, **kwargs):
       
   281     """Displays multiple lists of applications that are in a different
       
   282     status of the application process.
       
   283     """
       
   284 
       
   285     params = dicts.merge(params, self._params)
       
   286 
       
   287     # only select the requests that haven't been reviewed yet
       
   288     filter = {'status' : 'needs review'}
       
   289 
       
   290     ur_params = params.copy()
       
   291     ur_params['list_description'] = ugettext('A list of all unhandled '
       
   292         '%(name_plural)s.') % params
       
   293     ur_params ['list_action'] = (redirects.getReviewRedirect, params)
       
   294 
       
   295     ur_list = list_helper.getListContent(
       
   296         request, ur_params, filter, 0)
       
   297 
       
   298     # only select the requests that haven't been turned into a group yet
       
   299     filter['status'] = 'accepted'
       
   300 
       
   301     uh_params = params.copy()
       
   302     uh_params['list_description'] = ugettext('A list of all %(name_plural)s '
       
   303         'that have been accepted but not completed yet') % params
       
   304     uh_params ['list_action'] = (redirects.getReviewRedirect, params)
       
   305 
       
   306     uh_list = list_helper.getListContent(
       
   307         request, uh_params, filter, 1)
       
   308 
       
   309     # only select the requests the have been rejected
       
   310     filter ['status'] = 'rejected'
       
   311 
       
   312     den_params = params.copy()
       
   313     den_params['list_description'] = ugettext('A list of all %(name_plural)s '
       
   314         'that have been rejected') % params
       
   315     den_params ['list_action'] = (redirects.getReviewRedirect, params)
       
   316 
       
   317     den_list = list_helper.getListContent(
       
   318         request, den_params, filter, 2)
       
   319 
       
   320     # only select the request that have been ignored
       
   321     filter ['status'] = 'ignored'
       
   322 
       
   323     ign_params = params.copy()
       
   324     ign_params['list_description'] = ugettext('A list of all %(name_plural)s '
       
   325         'that have been ignored') % params
       
   326     ign_params ['list_action'] = (redirects.getReviewRedirect, params)
       
   327 
       
   328     ign_list = list_helper.getListContent(
       
   329         request, ign_params, filter, 3)
       
   330 
       
   331     # fill contents with all the needed lists
       
   332     contents = [ur_list, uh_list, den_list, ign_list]
       
   333 
       
   334     # call the _list method from base to display the list
       
   335     return self._list(request, params, contents, page_name)
       
   336