app/soc/views/models/group.py
changeset 1153 4804f7f5a7c0
parent 1127 69a9134c5c7e
child 1218 569a3fe9cb88
equal deleted inserted replaced
1152:b82caf7bb17c 1153:4804f7f5a7c0
    24 
    24 
    25 
    25 
    26 from google.appengine.api import users
    26 from google.appengine.api import users
    27 
    27 
    28 from django import forms
    28 from django import forms
       
    29 from django import http
    29 from django.utils.translation import ugettext
    30 from django.utils.translation import ugettext
    30 
    31 
    31 from soc.logic import dicts
    32 from soc.logic import dicts
    32 from soc.logic.models import user as user_logic
    33 from soc.logic.models import user as user_logic
    33 from soc.views.helper import decorators
    34 from soc.views.helper import decorators
    34 from soc.views.helper import lists as list_helper
    35 from soc.views.helper import lists as list_helper
    35 from soc.views.helper import redirects
    36 from soc.views.helper import redirects
       
    37 from soc.views.helper import responses
    36 from soc.views.helper import widgets
    38 from soc.views.helper import widgets
    37 from soc.views.models import presence
    39 from soc.views.models import presence
    38 from soc.views.models import document as document_view
    40 from soc.views.models import document as document_view
    39 from soc.views.models.request import view as request_view
    41 from soc.views.models.request import view as request_view
    40 from soc.views.sitemap import sidebar
    42 from soc.views.sitemap import sidebar
    41 
    43 
       
    44 import soc.views.helper
    42 
    45 
    43 class View(presence.View):
    46 class View(presence.View):
    44   """View methods for the Group model.
    47   """View methods for the Group model.
    45   """
    48   """
    46 
    49 
    47   # TODO(ljvderijk) add sidebar entry for listRequests to each group
       
    48 
    50 
    49   def __init__(self, params=None):
    51   def __init__(self, params=None):
    50     """Defines the fields and methods required for the base View class
    52     """Defines the fields and methods required for the base View class
    51     to provide the user with list, public, create, edit and delete views.
    53     to provide the user with list, public, create, edit and delete views.
    52 
    54 
    72         'List of requests for %(name)s'),
    74         'List of requests for %(name)s'),
    73         (r'^%(url_name)s/(?P<access_type>list_roles)/%(key_fields)s$',
    75         (r'^%(url_name)s/(?P<access_type>list_roles)/%(key_fields)s$',
    74         'soc.views.models.%(module_name)s.list_roles',
    76         'soc.views.models.%(module_name)s.list_roles',
    75         'List of roles for %(name)s')]
    77         'List of roles for %(name)s')]
    76 
    78 
       
    79     if params.get('group_applicant_url'):
       
    80       # add the applicant pattern
       
    81       patterns += [
       
    82           (r'^%(url_name)s/(?P<access_type>applicant)/%(key_fields)s$',
       
    83           'soc.views.models.%(module_name)s.applicant', 
       
    84           "%(name)s Creation via Accepted Application"),]
       
    85 
    77     new_params['extra_django_patterns'] = patterns
    86     new_params['extra_django_patterns'] = patterns
    78 
    87 
    79     # TODO(tlarsen): Add support for Django style template lookup
    88     # TODO(tlarsen): Add support for Django style template lookup
    80     new_params['public_template'] = 'soc/group/public.html'
    89     new_params['public_template'] = 'soc/group/public.html'
    81 
    90 
   104       # only if we are creating a new entity we should fill in founder
   113       # only if we are creating a new entity we should fill in founder
   105       user = user_logic.logic.getForCurrentAccount()
   114       user = user_logic.logic.getForCurrentAccount()
   106       fields['founder'] = user
   115       fields['founder'] = user
   107 
   116 
   108     super(View, self)._editPost(request, entity, fields)
   117     super(View, self)._editPost(request, entity, fields)
       
   118 
       
   119 
       
   120   @decorators.merge_params
       
   121   @decorators.check_access
       
   122   def applicant(self, request, access_type,
       
   123                 page_name=None, params=None, **kwargs):
       
   124     """Handles the creation of a group via an approved group application.
       
   125 
       
   126     Args:
       
   127       request: the standard Django HTTP request object
       
   128       access_type : the name of the access type which should be checked
       
   129       page_name: the page name displayed in templates as page and header title
       
   130       params: a dict with params for this View
       
   131       kwargs: the Key Fields for the specified entity
       
   132     """
       
   133 
       
   134     # get the context for this webpage
       
   135     context = responses.getUniversalContext(request)
       
   136     context['page_name'] = page_name
       
   137 
       
   138     if request.method == 'POST':
       
   139       return self.applicantPost(request, context, params, **kwargs)
       
   140     else:
       
   141       # request.method == 'GET'
       
   142       return self.applicantGet(request, context, params, **kwargs)
       
   143 
       
   144   def applicantGet(self, request, context, params, **kwargs):
       
   145     """Handles the GET request concerning the creation of a group via an
       
   146     approved group application.
       
   147 
       
   148     Args:
       
   149       request: the standard Django HTTP request object
       
   150       context: dictionary containing the context for this view
       
   151       params: a dict with params for this View
       
   152       kwargs: the Key Fields for the specified entity
       
   153     """
       
   154 
       
   155     # find the application
       
   156     application_logic = params['application_logic']
       
   157     key_fields = application_logic.logic.getKeyFieldsFromFields(kwargs)
       
   158     application = application_logic.logic.getFromKeyFields(key_fields)
       
   159 
       
   160     # extract the application fields
       
   161     field_names = application.properties().keys()
       
   162     fields = dict( [(i, getattr(application, i)) for i in field_names] )
       
   163 
       
   164     # create the form using the fields from the application as the initial value
       
   165     form = params['applicant_create_form'](initial=fields)
       
   166 
       
   167     # construct the appropriate response
       
   168     return super(View, self)._constructResponse(request, entity=None,
       
   169         context=context, form=form, params=params)
       
   170 
       
   171   def applicantPost(self, request, context, params, **kwargs):
       
   172     """Handles the POST request concerning the creation of a group via an
       
   173     approved group application.
       
   174 
       
   175     Args:
       
   176       request: the standard Django HTTP request object
       
   177       context: dictionary containing the context for this view
       
   178       params: a dict with params for this View
       
   179       kwargs: the Key Fields for the specified entity
       
   180     """
       
   181 
       
   182     # populate the form using the POST data
       
   183     form = params['applicant_create_form'](request.POST)
       
   184 
       
   185     if not form.is_valid():
       
   186       # return the invalid form response
       
   187       return self._constructResponse(request, entity=None, context=context,
       
   188           form=form, params=params)
       
   189 
       
   190     # collect the cleaned data from the valid form
       
   191     key_name, fields = soc.views.helper.forms.collectCleanedFields(form)
       
   192 
       
   193     # do post processing
       
   194     self._applicantPost(request, context, fields)
       
   195 
       
   196     if not key_name:
       
   197       key_fields =  self._logic.getKeyFieldsFromFields(fields)
       
   198       key_name = self._logic.getKeyNameFromFields(key_fields)
       
   199 
       
   200     # create the group entity
       
   201     entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
       
   202 
       
   203     # redirect to notifications list to see the admin invite
       
   204     return http.HttpResponseRedirect('/notification/list')
       
   205 
       
   206   def _applicantPost(self, request, context, fields):
       
   207     """Performs any required processing on the entity to post its edit page.
       
   208 
       
   209     Args:
       
   210       request: the django request object
       
   211       context: the context for the webpage
       
   212       fields: the new field values
       
   213     """
       
   214 
       
   215      # fill in the founder of the group
       
   216     user = user_logic.logic.getForCurrentAccount()
       
   217     fields['founder'] = user
       
   218 
       
   219     # If scope_logic is not defined, this entity has no scope
       
   220     if not self._params['scope_logic']:
       
   221       return
       
   222 
       
   223     # If this entity is unscoped, do not try to retrieve a scope
       
   224     if 'scope_path' not in fields:
       
   225       return
       
   226 
       
   227     scope = self._params['scope_logic'].logic.getFromKeyName(
       
   228         fields['scope_path'])
       
   229     fields['scope'] = scope
       
   230 
   109 
   231 
   110   @decorators.merge_params
   232   @decorators.merge_params
   111   @decorators.check_access
   233   @decorators.check_access
   112   def listRequests(self, request, access_type,
   234   def listRequests(self, request, access_type,
   113                    page_name=None, params=None, **kwargs):
   235                    page_name=None, params=None, **kwargs):