app/soc/views/models/role.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sun, 25 Jan 2009 16:50:24 +0000
changeset 990 fd1e6afb2d62
parent 986 e9611a2288ca
child 1067 ecf3df87b81c
permissions -rw-r--r--
Added two needed redirects in role.py. To make this work properly group_view was added to params in club_admin and club_member. 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
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 964
diff changeset
    27
from django.utils.translation import ugettext
495
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
  
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 964
diff changeset
    52
  DEF_INVITE_INSTRUCTION_MSG_FMT = ugettext(
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 = {}
963
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    63
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    64
    patterns = params.get('extra_django_patterns')
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    65
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    66
    if not patterns:
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    67
      patterns = []
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    68
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    69
    if params.get('allow_requests_and_invites'):
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    70
      # add patterns concerning requests and invites
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    71
      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
    72
          'soc.views.models.%(module_name)s.invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    73
          'Create invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    74
          (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
    75
          'soc.views.models.%(module_name)s.accept_invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    76
          'Accept invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    77
          (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
    78
          'soc.views.models.%(module_name)s.process_request',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    79
          'Process request for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    80
          (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
    81
          'soc.views.models.%(module_name)s.request',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    82
          'Create a Request to become %(name)s')]
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    83
    elif params.get('allow_invites'):
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    84
      # add patterns concerning only invites
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    85
      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
    86
          'soc.views.models.%(module_name)s.invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    87
          'Create invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    88
          (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
    89
          'soc.views.models.%(module_name)s.accept_invite',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    90
          'Accept invite for %(name)s'),
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    91
          (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
    92
          'soc.views.models.%(module_name)s.process_request',
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
    93
          'Process request for %(name)s')]
660
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    94
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    95
    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
    96
    new_params['scope_redirect'] = redirects.getInviteRedirect
660
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    97
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    98
    params = dicts.merge(params, new_params)
5a381b290691 Introduced django_extra_patterns
Sverre Rabbelier <srabbelier@gmail.com>
parents: 657
diff changeset
    99
656
a76f1b443ea4 Cleanups in the views module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 654
diff changeset
   100
    super(View, self).__init__(params=params)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   101
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
   102
  @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
   103
  @decorators.check_access
710
edb5dbb1dea7 Add explicit access_types from the url
Sverre Rabbelier <srabbelier@gmail.com>
parents: 708
diff changeset
   104
  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
   105
                   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
   106
    """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
   107
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
    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
   109
      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
   110
      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
   111
      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
   112
      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
   113
      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
   114
    """
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
    # 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
   117
    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
   118
    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
   119
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
    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
   121
      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
   122
    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
   123
      # 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
   124
      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
   125
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
  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
   127
    """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
   128
    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
   129
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
    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
   131
      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
   132
      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
   133
      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
   134
      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
   135
    """
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
    # 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
   138
    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
   139
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
    # 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
   141
    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
   142
    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
   143
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
    # 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
   145
    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
   146
        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
   147
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
  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
   149
    """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
   150
    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
   151
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
    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
   153
      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
   154
      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
   155
      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
   156
      kwargs: the Key Fields for the specified entity
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
    """
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   158
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
   159
    # 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
   160
    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
   161
    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
   162
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
    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
   164
      # 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
   165
      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
   166
          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
   167
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
   168
    # 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
   169
    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
   170
    
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
    # get the group entity for which this request is via the scope_path
978
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 970
diff changeset
   172
    group = self._logic.getGroupEntityFromScopePath(params['group_logic'],
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   173
         kwargs['scope_path'])
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   174
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   175
    # get the request scope path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   176
    request_scope_path = self._getRequestScopePathFromGroup(group)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
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
   178
    # 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
   179
    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
   180
        '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
   181
        '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
   182
        '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
   183
        '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
   184
        '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
   185
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
    # 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
   187
    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
   188
    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
   189
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
    # 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
   191
    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
   192
        key_name)
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   193
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   194
    # send out an invite notification
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   195
    notifications_helper.sendInviteNotification(entity)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   196
990
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   197
    group_view = params.get('group_view')
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   198
    if not group_view:
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   199
      return http.HttpResponseRedirect('/')
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   200
    else:
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   201
      # redirect to the requests list
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   202
      return http.HttpResponseRedirect(
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   203
          redirects.getListRequestsRedirect(group, group_view.getParams()))
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
   204
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   205
  def _getRequestScopePathFromGroup(self, group_entity):
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   206
    """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
   207
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   208
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   209
      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
   210
                    be returned.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   211
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   212
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   213
    if group_entity.scope_path:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   214
      request_scope_path = '%s/%s' % [
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   215
          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
   216
    else:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   217
      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
   218
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   219
    return request_scope_path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   220
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   221
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
   222
  @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
   223
  @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
   224
  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
   225
                   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
   226
    """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
   227
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
   228
    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
   229
      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
   230
      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
   231
      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
   232
      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
   233
      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
   234
    """
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
   235
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
   236
     # 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
   237
    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
   238
    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
   239
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
    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
   241
      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
   242
    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
   243
      # 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
   244
      return self.acceptInviteGet(request, context, params, **kwargs)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   245
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
   246
  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
   247
    """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
   248
    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
   249
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
    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
   251
      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
   252
      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
   253
      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
   254
      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
   255
    """
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
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
   257
    # 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
   258
    # 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
   259
    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
   260
              '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
   261
    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
   262
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
    # 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
   264
    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
   265
        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
   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
  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
   268
    """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
   269
    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
   270
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
    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
   272
      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
   273
      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
   274
      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
   275
      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
   276
    """
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
   277
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
    # 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
   279
    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
   280
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
    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
   282
      # 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
   283
      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
   284
          form=form, params=params)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   285
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
   286
    # 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
   287
    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
   288
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
    # 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
   290
    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
   291
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
    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
   293
    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
   294
    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
   295
    
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
    # 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
   297
    # 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
   298
    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
   299
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
    # 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
   301
    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
   302
    key_name = self._logic.getKeyNameForFields(key_fields)
495
87afae6e4c51 Added basic 'invite' functionality
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   303
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
   304
    # 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
   305
    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
   306
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
    # 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
   308
    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
   309
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
    # 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
   311
    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
   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
  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
   314
    """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
   315
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
   316
      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
   317
      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
   318
      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
   319
      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
   320
      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
   321
      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
   322
    """
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
    pass
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   324
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   325
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   326
  @decorators.merge_params
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   327
  @decorators.check_access
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   328
  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
   329
                   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
   330
    """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
   331
    for attaining a certain Role.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   332
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   333
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   334
      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
   335
      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
   336
      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
   337
      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
   338
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   339
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   340
    # 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
   341
    context = responses.getUniversalContext(request)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   342
    context['page_name'] = page_name
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   343
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   344
    if request.method == 'POST':
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   345
      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
   346
    else:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   347
      # request.method == 'GET'
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   348
      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
   349
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   350
  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
   351
    """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
   352
    to attain a role.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   353
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   354
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   355
      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
   356
      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
   357
      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
   358
      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
   359
    """
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
    # 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
   362
    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
   363
    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
   364
              'role' : params['module_name'],
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   365
              'group_id' : kwargs['scope_path']}
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
    # 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
   368
    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
   369
    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
   370
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   371
    # construct the appropriate response
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   372
    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
   373
        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
   374
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   375
  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
   376
    """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
   377
    to attain a role.
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   378
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   379
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   380
      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
   381
      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
   382
      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
   383
      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
   384
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   385
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   386
    # 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
   387
    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
   388
    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
   389
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   390
    if not form.is_valid():
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   391
      # return the invalid form response
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   392
      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
   393
          form=form, params=params)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   394
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   395
    # get the group entity for which this request is via the scope_path
978
e05b09b53486 Moved getGroupEntityFromScopePath to logic/models/role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 970
diff changeset
   396
    group = self._logic.getGroupEntityFromScopePath(params['group_logic'],
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   397
         kwargs['scope_path'])
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   398
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   399
    # get the request scope path
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   400
    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
   401
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   402
    # 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
   403
    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
   404
    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
   405
        'scope' : group,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   406
        'scope_path' : request_scope_path,
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   407
        'role' : params['module_name'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   408
        'role_verbose' : params['name'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   409
        'state' : 'new'}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   410
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   411
    # 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
   412
    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
   413
    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
   414
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   415
    # create the request entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   416
    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
   417
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   418
    # 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
   419
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   420
    # redirect to roles overview
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   421
    return http.HttpResponseRedirect('/user/roles')
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   422
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   423
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   424
  @decorators.merge_params
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   425
  @decorators.check_access
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   426
  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
   427
                   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
   428
    """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
   429
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   430
    Args:
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   431
      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
   432
      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
   433
      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
   434
      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
   435
      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
   436
    """
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   437
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   438
    # 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
   439
    context = responses.getUniversalContext(request)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   440
    context['page_name'] = page_name
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   441
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   442
    # 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
   443
    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
   444
        'scope_path': kwargs['scope_path'],
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   445
        'role': params['module_name']}
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   446
    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
   447
    
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   448
    get_dict = request.GET
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   449
    
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   450
    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
   451
      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
   452
        # 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
   453
        request_state = get_dict['status']
986
e9611a2288ca Rename ModelProperties to EntityProperties
Sverre Rabbelier <srabbelier@gmail.com>
parents: 978
diff changeset
   454
        request_logic.logic.updateEntityProperties(request_entity, {
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   455
            'state': get_dict['status']})
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   456
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   457
        if request_state == 'group_accepted':
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   458
          notifications_helper.sendInviteNotification(request_entity)
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   459
990
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   460
        group_view = params.get('group_view')
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   461
        if not group_view:
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   462
          return http.HttpResponseRedirect('/')
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   463
        else:
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   464
          # redirect to the requests list
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   465
          return http.HttpResponseRedirect(
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   466
              redirects.getListRequestsRedirect(request_entity.scope, 
fd1e6afb2d62 Added two needed redirects in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 986
diff changeset
   467
                  group_view.getParams()))
962
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   468
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   469
    # 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
   470
    context['entity'] = request_entity
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   471
    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
   472
606871fda11c Added request creation and processing to role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 959
diff changeset
   473
    #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
   474
    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
   475
963
24c0a9da6360 Parameterized patterns in role.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 962
diff changeset
   476
    return responses.respond(request, template, context=context)