app/soc/views/models/role.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sat, 24 Jan 2009 17:09:02 +0000
changeset 964 63005fcf12df
parent 963 24c0a9da6360
child 970 8b5611d5b053
permissions -rw-r--r--
Coding style fix in views/models/role.py. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
837
bc1c951bf3a0 Add missing blank line in soc.views.helper.params module. Fix docstring typo in soc.views.models.role module. Add missing dots at the end of sentences in soc.logic.cleaning and soc.view.models.user_self modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 755
diff changeset
    17
"""Views for Role profiles.
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    22
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
  ]
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    26
from django import http
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
from django.utils.translation import ugettext_lazy
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
from soc.logic import dicts
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    30
from soc.logic.models import request as request_logic
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
    31
from soc.logic.models import user as user_logic
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
    32
from soc.logic.helper import notifications as notifications_helper
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    33
from soc.logic.helper import request as request_helper
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    34
from soc.views.helper import decorators
620
e74e0b74625f Put the redirects in the views/helper/redirects.py module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 616
diff changeset
    35
from soc.views.helper import redirects
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    36
from soc.views.helper import responses
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
from soc.views.models import base
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
    38
from soc.views.models import request as request_view
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
import soc.models.request
646
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 624
diff changeset
    41
import soc.views.helper.lists
860e17e5118f Remove cyclic imports by moving response method of out_of_band.Error class to soc.views.helper.responses module as errorResponse function. Apply changes to affected files.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 624
diff changeset
    42
import soc.views.helper.responses
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
import soc.views.helper.widgets
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
657
c781de4f6d39 Rename role.RoleView to role.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 656
diff changeset
    46
class View(base.View):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 495
diff changeset
    47
  """Views for all entities that inherit from Role.
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
  All views that only Role entities have are defined in this subclass.
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
  """
518
d9d31d316a74 Changes the list template to use an instruction_text property that sets the text above the list. This makes the create_invite template unneeded. The instruction_text comes in handy when defining special lists.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 512
diff changeset
    51
  
538
4d209757c835 Make it more obvious that a format string constant is a format string by
Todd Larsen <tlarsen@google.com>
parents: 528
diff changeset
    52
  DEF_INVITE_INSTRUCTION_MSG_FMT = ugettext_lazy(
520
b0c623819b68 Split a too long line
Sverre Rabbelier <srabbelier@gmail.com>
parents: 518
diff changeset
    53
      'Please use this form to invite someone to become a %(name)s.')
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
616
580b1c21b808 Fixed requests not working anymore
Sverre Rabbelier <srabbelier@gmail.com>
parents: 572
diff changeset
    55
  def __init__(self, params=None):
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
    """
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    57
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
    Args:
656
a76f1b443ea4 Cleanups in the views module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 654
diff changeset
    59
      params: This dictionary should be filled with the parameters
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
    """
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
660
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    62
    new_params = {}
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
    63
    # TODO(ljvderijk) parameterize these patterns
963
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    64
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    65
    patterns = params.get('extra_django_patterns')
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    66
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    67
    if not patterns:
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    68
      patterns = []
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    69
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    70
    if params.get('allow_requests_and_invites'):
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    71
      # add patterns concerning requests and invites
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    72
      patterns += [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    73
          'soc.views.models.%(module_name)s.invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    74
          'Create invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    75
          (r'^%(url_name)s/(?P<access_type>accept_invite)/%(scope)s/%(lnp)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    76
          'soc.views.models.%(module_name)s.accept_invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    77
          'Accept invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    78
          (r'^%(url_name)s/(?P<access_type>process_request)/%(scope)s/%(lnp)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    79
          'soc.views.models.%(module_name)s.process_request',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    80
          'Process request for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    81
          (r'^%(url_name)s/(?P<access_type>request)/%(scope)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    82
          'soc.views.models.%(module_name)s.request',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    83
          'Create a Request to become %(name)s')]
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    84
    elif params.get('allow_invites'):
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    85
      # add patterns concerning only invites
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    86
      patterns += [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    87
          'soc.views.models.%(module_name)s.invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    88
          'Create invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    89
          (r'^%(url_name)s/(?P<access_type>accept_invite)/%(scope)s/%(lnp)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    90
          'soc.views.models.%(module_name)s.accept_invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    91
          'Accept invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    92
          (r'^%(url_name)s/(?P<access_type>process_request)/%(scope)s/%(lnp)s$',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    93
          'soc.views.models.%(module_name)s.process_request',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    94
          'Process request for %(name)s')]
660
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    95
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    96
    new_params['extra_django_patterns'] = patterns
663
c2dcae606699 Make base.View.create lookup the scope if appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 660
diff changeset
    97
    new_params['scope_redirect'] = redirects.getInviteRedirect
660
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    98
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    99
    params = dicts.merge(params, new_params)
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
   100
656
a76f1b443ea4 Cleanups in the views module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 654
diff changeset
   101
    super(View, self).__init__(params=params)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   102
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   103
  @decorators.merge_params
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   104
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   105
  def invite(self, request, access_type,
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   106
                   page_name=None, params=None, **kwargs):
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   107
    """Creates the page on which an invite can be send out.
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   108
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   109
    Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   110
      request: the standard Django HTTP request object
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   111
      access_type : the name of the access type which should be checked
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   112
      context: dictionary containing the context for this view
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   113
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   114
      kwargs: the Key Fields for the specified entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   115
    """
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   116
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   117
    # get the context for this webpage
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   118
    context = responses.getUniversalContext(request)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   119
    context['page_name'] = page_name
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   120
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   121
    if request.method == 'POST':
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   122
      return self.invitePost(request, context, params, **kwargs)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   123
    else:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   124
      # request.method == 'GET'
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   125
      return self.inviteGet(request, context, params, **kwargs)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   126
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   127
  def inviteGet(self, request, context, params, **kwargs):
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   128
    """Handles the GET request concerning the view that creates an invite
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   129
    for attaining a certain Role.
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   130
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   131
    Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   132
      request: the standard Django HTTP request object
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   133
      context: dictionary containing the context for this view
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   134
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   135
      kwargs: the Key Fields for the specified entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   136
    """
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   137
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   138
    # set the role to the right name
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   139
    fields = {'role': '%(module_name)s' % (params)}
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   140
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   141
    # get the request view parameters and initialize the create form
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   142
    request_params = request_view.view.getParams()
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   143
    form = request_params['invite_form'](initial=fields)
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   144
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   145
    # construct the appropriate response
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   146
    return super(View, self)._constructResponse(request, entity=None,
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   147
        context=context, form=form, params=params)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   148
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   149
  def invitePost(self, request, context, params, **kwargs):
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   150
    """Handles the POST request concerning the view that creates an invite
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   151
    for attaining a certain Role.
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   152
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   153
    Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   154
      request: the standard Django HTTP request object
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   155
      page_name: the page name displayed in templates as page and header title
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   156
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   157
      kwargs: the Key Fields for the specified entity
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
    """
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   159
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   160
    # get the request view parameters and populate the form using POST data
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   161
    request_params = request_view.view.getParams()
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   162
    form = request_params['invite_form'](request.POST)
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   163
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   164
    if not form.is_valid():
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   165
      # return the invalid form response
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   166
      return self._constructResponse(request, entity=None, context=context,
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   167
          form=form, params=params)
708
89f1a8c2ea59 Some cleanups in role.py and site.py and create()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 668
diff changeset
   168
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   169
    # collect the cleaned data from the valid form
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   170
    key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   171
    
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   172
    # get the group entity for which this request is via the scope_path
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   173
    group = self._getGroupEntityFromScopePath(params['group_logic'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   174
         kwargs['scope_path'])
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   175
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   176
    # get the request scope path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   177
    request_scope_path = self._getRequestScopePathFromGroup(group)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   178
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   179
    # create the fields for the new request entity
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   180
    request_fields = {'link_id': form_fields['link_id'].link_id,
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   181
        'scope': group,
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   182
        'scope_path': request_scope_path,
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   183
        'role': params['module_name'],
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   184
        'role_verbose': params['name'],
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   185
        'state': 'group_accepted'}
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   186
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   187
    # extract the key_name for the new request entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   188
    key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   189
    key_name = request_logic.logic.getKeyNameForFields(key_fields)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   190
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   191
    # create the request entity
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   192
    entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, 
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   193
        key_name)
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   194
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   195
    # send out an invite notification
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   196
    notifications_helper.sendInviteNotification(entity)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   197
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   198
    # TODO(ljvderijk) redirect to a more useful place like the group homepage
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   199
    return http.HttpResponseRedirect('/')
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   200
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   201
  def _getGroupEntityFromScopePath(self, group_logic, scope_path):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   202
    """Returns a group entity by using the given scope_path that's in kwargs.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   203
    
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   204
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   205
      group_logic: logic for the group which should be retrieved
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   206
      kwargs: the Key Fields for the specified entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   207
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   208
    group_key_fields = scope_path.rsplit('/',1)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   209
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   210
    if len(group_key_fields) == 1:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   211
      # there is only a link_id
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   212
      fields = {'link_id' : group_key_fields[0]}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   213
    else:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   214
      # there is a scope_path and link_id
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   215
      fields = {'scope_path' : group_key_fields[0],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   216
                'link_id' : group_key_fields[1]}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   217
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   218
    group = group_logic.getForFields(fields, unique=True)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   219
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   220
    return group
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   221
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   222
  def _getRequestScopePathFromGroup(self, group_entity):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   223
    """Returns the scope_path that should be put in a request for a given group.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   224
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   225
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   226
      group_entity: The group entity for which the request scope_path should
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   227
                    be returned.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   228
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   229
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   230
    if group_entity.scope_path:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   231
      request_scope_path = '%s/%s' % [
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   232
          group_entity.scope_path, group_entity.link_id]
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   233
    else:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   234
      request_scope_path = group_entity.link_id
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   235
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   236
    return request_scope_path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   237
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   238
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   239
  @decorators.merge_params
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   240
  @decorators.check_access
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   241
  def acceptInvite(self, request, access_type,
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   242
                   page_name=None, params=None, **kwargs):
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   243
    """Creates the page process an invite into a Role.
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   244
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   245
    Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   246
      request: the standard Django HTTP request object
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   247
      access_type : the name of the access type which should be checked
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   248
      page_name: the page name displayed in templates as page and header title
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   249
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   250
      kwargs: the Key Fields for the specified entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   251
    """
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   252
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   253
     # get the context for this webpage
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   254
    context = responses.getUniversalContext(request)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   255
    context['page_name'] = page_name
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   256
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   257
    if request.method == 'POST':
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   258
      return self.acceptInvitePost(request, context, params, **kwargs)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   259
    else:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   260
      # request.method == 'GET'
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   261
      return self.acceptInviteGet(request, context, params, **kwargs)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   262
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   263
  def acceptInviteGet(self, request, context, params, **kwargs):
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   264
    """Handles the GET request concerning the creation of a Role via an
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   265
    invite.
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   266
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   267
    Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   268
      request: the standard Django HTTP request object
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   269
      context: dictionary containing the context for this view
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   270
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   271
      kwargs: the Key Fields for the specified entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   272
    """
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   273
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   274
    # create the form using the scope_path and link_id from kwargs 
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   275
    # as initial value
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   276
    fields = {'link_id': kwargs['link_id'],
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   277
              'scope_path': kwargs['scope_path']}
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   278
    form = params['invited_create_form'](initial=fields)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   279
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   280
    # construct the appropriate response
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   281
    return super(View, self)._constructResponse(request, entity=None,
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   282
        context=context, form=form, params=params)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   283
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   284
  def acceptInvitePost(self, request, context, params, **kwargs):
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   285
    """Handles the POST request concerning the creation of a Role via an
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   286
    invite.
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   287
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   288
    Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   289
      request: the standard Django HTTP request object
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   290
      context: dictionary containing the context for this view
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   291
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   292
      kwargs: the Key Fields for the specified entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   293
    """
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   294
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   295
    # populate the form using the POST data
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   296
    form = params['invited_create_form'](request.POST)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   297
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   298
    if not form.is_valid():
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   299
      # return the invalid form response
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   300
      return self._constructResponse(request, entity=None, context=context,
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   301
          form=form, params=params)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   302
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   303
    # collect the cleaned data from the valid form
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   304
    key_name, fields = soc.views.helper.forms.collectCleanedFields(form)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   305
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   306
    # call the post process method
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   307
    self._acceptInvitePost(fields, request, context, params, **kwargs)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   308
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   309
    group_logic = params['group_logic']
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   310
    group_entity = group_logic.getFromKeyName(fields['scope_path'])
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   311
    fields['scope'] = group_entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   312
    
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   313
    # make sure that this role becomes active once more in case this user
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   314
    # has been reinvited
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   315
    fields ['state'] = 'active'
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   316
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   317
    # get the key_name for the new entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   318
    key_fields =  self._logic.getKeyFieldsFromDict(fields)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   319
    key_name = self._logic.getKeyNameForFields(key_fields)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   320
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   321
    # create new Role entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   322
    entity = self._logic.updateOrCreateFromKeyName(fields, key_name)
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   323
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   324
    # mark the request as completed
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   325
    request_helper.completeRequestForRole(entity, params['module_name'])
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   326
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   327
    # redirect to the roles overview page
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   328
    return http.HttpResponseRedirect('/user/roles')
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   329
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   330
  def _acceptInvitePost(self, fields, request, context, params, **kwargs):
959
b6e8750c9407 Some style guide fixes, docstring fixes and removal of unused imports in different modules. Rename acceptInvite view variable to accept_invite in soc.views.models.host module to comply to our style guide.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 952
diff changeset
   331
    """Used to post-process data after the fields have been cleaned.
858
e79e7a22326f Add an export() view, and implement it as text/text for Document.
Todd Larsen <tlarsen@google.com>
parents: 837
diff changeset
   332
944
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   333
      Args:
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   334
      fields : the cleaned fields from the role form
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   335
      request: the standard Django HTTP request object
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   336
      context: dictionary containing the context for this view
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   337
      params: a dict with params for this View
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   338
      kwargs: the Key Fields for the specified entity
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   339
    """
5ea2bd9e3fa6 Refactored invite system from club_admin to role to make the system work with host as well.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 908
diff changeset
   340
    pass
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   341
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   342
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   343
  @decorators.merge_params
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   344
  @decorators.check_access
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   345
  def request(self, request, access_type,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   346
                   page_name=None, params=None, **kwargs):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   347
    """Handles the GET request concerning the view that creates a request
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   348
    for attaining a certain Role.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   349
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   350
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   351
      request: the standard Django HTTP request object
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   352
      page_name: the page name displayed in templates as page and header title
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   353
      params: a dict with params for this View
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   354
      kwargs: the Key Fields for the specified entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   355
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   356
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   357
    # get the context for this webpage
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   358
    context = responses.getUniversalContext(request)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   359
    context['page_name'] = page_name
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   360
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   361
    if request.method == 'POST':
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   362
      return self.requestPost(request, context, params, **kwargs)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   363
    else:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   364
      # request.method == 'GET'
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   365
      return self.requestGet(request, context, params, **kwargs)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   366
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   367
  def requestGet(self, request, context, params, **kwargs):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   368
    """Handles the GET request concerning the creation of a request
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   369
    to attain a role.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   370
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   371
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   372
      request: the standard Django HTTP request object
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   373
      context: dictionary containing the context for this view
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   374
      params: a dict with params for this View
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   375
      kwargs: the Key Fields for the specified entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   376
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   377
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   378
    # set right fields for the request form
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   379
    user_entity = user_logic.logic.getForCurrentAccount()
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   380
    fields = {'link_id' : user_entity.link_id,
964
63005fcf12df Coding style fix in views/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 963
diff changeset
   381
              'role' : params['module_name'],
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   382
              'group_id' : kwargs['scope_path']}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   383
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   384
    # get the request view parameters and initialize the create form
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   385
    request_params = request_view.view.getParams()
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   386
    form = request_params['request_form'](initial=fields)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   387
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   388
    # construct the appropriate response
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   389
    return super(View, self)._constructResponse(request, entity=None,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   390
        context=context, form=form, params=params)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   391
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   392
  def requestPost(self, request, context, params, **kwargs):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   393
    """Handles the POST request concerning the creation of a request
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   394
    to attain a role.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   395
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   396
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   397
      request: the standard Django HTTP request object
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   398
      context: dictionary containing the context for this view
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   399
      params: a dict with params for this View
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   400
      kwargs: the Key Fields for the specified entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   401
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   402
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   403
    # get the request view parameters and populate the form using POST data
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   404
    request_params = request_view.view.getParams()
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   405
    form = request_params['invite_form'](request.POST)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   406
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   407
    if not form.is_valid():
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   408
      # return the invalid form response
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   409
      return self._constructResponse(request, entity=None, context=context,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   410
          form=form, params=params)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   411
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   412
    # get the group entity for which this request is via the scope_path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   413
    group = self._getGroupEntityFromScopePath(params['group_logic'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   414
         kwargs['scope_path'])
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   415
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   416
    # get the request scope path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   417
    request_scope_path = self._getRequestScopePathFromGroup(group)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   418
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   419
    # defensively set the fields we need for this request and set state to new
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   420
    user_entity = user_logic.logic.getForCurrentAccount()
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   421
    request_fields = {'link_id' : user_entity.link_id,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   422
        'scope' : group,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   423
        'scope_path' : request_scope_path,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   424
        'role' : params['module_name'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   425
        'role_verbose' : params['name'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   426
        'state' : 'new'}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   427
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   428
    # extract the key_name for the new request entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   429
    key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   430
    key_name = request_logic.logic.getKeyNameForFields(key_fields)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   431
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   432
    # create the request entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   433
    entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, key_name)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   434
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   435
    # TODO(ljvderijk) send out a message to alert the users able to process this request
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   436
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   437
    # redirect to roles overview
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   438
    return http.HttpResponseRedirect('/user/roles')
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   439
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   440
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   441
  @decorators.merge_params
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   442
  @decorators.check_access
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   443
  def processRequest(self, request, access_type,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   444
                   page_name=None, params=None, **kwargs):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   445
    """Creates the page upon which a request can be processed.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   446
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   447
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   448
      request: the standard Django HTTP request object
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   449
      access_type : the name of the access type which should be checked
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   450
      page_name: the page name displayed in templates as page and header title
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   451
      params: a dict with params for this View
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   452
      kwargs: the Key Fields for the specified entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   453
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   454
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   455
    # get the context for this webpage
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   456
    context = responses.getUniversalContext(request)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   457
    context['page_name'] = page_name
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   458
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   459
    # get the request entity using the information from kwargs
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   460
    fields = {'link_id': kwargs['link_id'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   461
        'scope_path': kwargs['scope_path'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   462
        'role': params['module_name']}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   463
    request_entity = request_logic.logic.getForFields(fields, unique=True)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   464
    
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   465
    get_dict = request.GET
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   466
    
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   467
    if 'status' in get_dict.keys():
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   468
      if get_dict['status'] in ['group_accepted', 'rejected', 'ignored']:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   469
        # update the request_entity and redirect away from this page
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   470
        request_state = get_dict['status']
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   471
        request_logic.logic.updateModelProperties(request_entity, {
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   472
            'state': get_dict['status']})
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   473
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   474
        if request_state == 'group_accepted':
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   475
          notifications_helper.sendInviteNotification(request_entity)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   476
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   477
        # TODO(ljvderijk) redirect to group requests overview
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   478
        return http.HttpResponseRedirect('/')
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   479
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   480
    # put the entity in the context
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   481
    context['entity'] = request_entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   482
    context['module_name'] = params['module_name']
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   483
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   484
    #display the request processing page using the appropriate template
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   485
    template = request_view.view.getParams()['request_processing_template']
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   486
963
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
   487
    return responses.respond(request, template, context=context)