Requests can now not be created when you already have a similar request or already have the role the request is for.
authorLennard de Rijk <ljvderijk@gmail.com>
Fri, 30 Jan 2009 17:23:19 +0000
changeset 1084 9c4221f7b747
parent 1083 b8018d7a9f23
child 1085 0afbdd0905ef
Requests can now not be created when you already have a similar request or already have the role the request is for. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
app/soc/templates/soc/request/create.html
app/soc/views/models/request.py
app/soc/views/models/role.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/request/create.html	Fri Jan 30 17:23:19 2009 +0000
@@ -0,0 +1,58 @@
+{% extends "soc/base.html" %}
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+{% load forms_helpers %}
+
+{% block scripts %}
+	<script type="text/javascript" src="/tiny_mce/tiny_mce_src.js"></script>
+{% endblock %}
+{% block header_title %}
+{{ page_name }}
+{% endblock %}
+
+{% block body %}
+<p>
+<p>
+{% block instructions %}
+{{instruction_message}}
+{% endblock %}
+{% if error_message %}
+ <div class="error">
+ {{error_message|safe}}
+ </div>
+{% endif %}
+</p>
+<form method="POST">
+  <table>
+    {% block form_table %}
+      {% as_table form %}
+    {% endblock form_table %}
+  </table>
+  <table>
+  <tr>
+   <td colspan="4">&nbsp;</td>
+  </tr>
+  <tr>
+    {% block submit_buttons %}
+   <td> 
+    <input style="font-weight: bold" type="submit" value="Submit"/></span>
+   </td>
+   <td>
+   <input type="button" value="Back to Previous Page" onClick="javascript: history.go(-1)">
+   </td>
+ </table>
+</form>
+</p>
+{% endblock submit_buttons %}
+{% endblock body %}
--- a/app/soc/views/models/request.py	Fri Jan 30 11:46:08 2009 +0000
+++ b/app/soc/views/models/request.py	Fri Jan 30 17:23:19 2009 +0000
@@ -82,16 +82,15 @@
 
     new_params['sidebar_defaults'] = [('/%s/list', 'List %(name_plural)s', 'list')]
 
+    new_params['create_template'] = ['soc/request/create.html']
     new_params['save_message'] = [ugettext('Request saved.')]
     
     new_params['extra_dynaexclude'] = ['state', 'role_verbose']
-    
-    # TODO(ljvderijk) add clean field that checks to see if the user already has
-    # the role that's been entered in the create form fields
+
     new_params['create_extra_dynafields'] = {
         'role': forms.CharField(widget=widgets.ReadOnlyInput(),
                                    required=True),
-        'clean_link_id': cleaning.clean_existing_user('link_id')
+        'clean_link_id': cleaning.clean_existing_user('link_id'),
         }
 
     new_params['edit_extra_dynafields'] = {
--- a/app/soc/views/models/role.py	Fri Jan 30 11:46:08 2009 +0000
+++ b/app/soc/views/models/role.py	Fri Jan 30 17:23:19 2009 +0000
@@ -48,10 +48,24 @@
 
   All views that only Role entities have are defined in this subclass.
   """
-  
+
   DEF_INVITE_INSTRUCTION_MSG_FMT = ugettext(
       'Please use this form to invite someone to become a %(name)s.')
 
+  DEF_REQUEST_INSTRUCTION_MSG_FMT = ugettext(
+      'Please use this form to request to become a %(name)s')
+
+  DEF_INVITE_ERROR_MSG_FMT = ugettext(
+      'This user can not receive an invite to become a %(name)s. <br/>'
+      'Please make sure there is no outstanding invite or request and '
+      'be sure that this user is not a %(name)s.')
+
+  DEF_REQUEST_ERROR_MSG_FMT = ugettext(
+      'You can not request to become a %(name)s. <br/>'
+      'Please make sure there is no outstanding invite or request and '
+      'be sure that you are not a %(name)s already.')
+
+
   def __init__(self, params=None):
     """
 
@@ -126,6 +140,8 @@
     # get the context for this webpage
     context = responses.getUniversalContext(request)
     context['page_name'] = page_name
+    context['instruction_message'] = (self.DEF_INVITE_INSTRUCTION_MSG_FMT %
+        params)
 
     if request.method == 'POST':
       return self.invitePost(request, context, params, **kwargs)
@@ -153,7 +169,7 @@
 
     # construct the appropriate response
     return super(View, self)._constructResponse(request, entity=None,
-        context=context, form=form, params=params)
+        context=context, form=form, params=request_params)
 
   def invitePost(self, request, context, params, **kwargs):
     """Handles the POST request concerning the view that creates an invite
@@ -177,7 +193,7 @@
 
     # collect the cleaned data from the valid form
     key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form)
-    
+
     # get the group entity for which this request is via the scope_path
     group = self._logic.getGroupEntityFromScopePath(params['group_logic'],
          kwargs['scope_path'])
@@ -193,6 +209,12 @@
         'role_verbose': params['name'],
         'state': 'group_accepted'}
 
+    if not self._isValidNewRequest(request_fields, params):
+      # not a valid invite
+      context['error_message'] = self.DEF_INVITE_ERROR_MSG_FMT % (
+          params)
+      return self.inviteGet(request, context, params, **kwargs)
+
     # extract the key_name for the new request entity
     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
     key_name = request_logic.logic.getKeyNameForFields(key_fields)
@@ -302,7 +324,7 @@
     group_logic = params['group_logic']
     group_entity = group_logic.getFromKeyName(fields['scope_path'])
     fields['scope'] = group_entity
-    
+
     # make sure that this role becomes active once more in case this user
     # has been reinvited
     fields ['state'] = 'active'
@@ -403,6 +425,8 @@
     # get the context for this webpage
     context = responses.getUniversalContext(request)
     context['page_name'] = page_name
+    context['instruction_message'] = (self.DEF_REQUEST_INSTRUCTION_MSG_FMT %
+        params)
 
     if request.method == 'POST':
       return self.requestPost(request, context, params, **kwargs)
@@ -433,7 +457,7 @@
 
     # construct the appropriate response
     return super(View, self)._constructResponse(request, entity=None,
-        context=context, form=form, params=params)
+        context=context, form=form, params=request_params)
 
   def requestPost(self, request, context, params, **kwargs):
     """Handles the POST request concerning the creation of a request
@@ -471,6 +495,12 @@
         'role_verbose' : params['name'],
         'state' : 'new'}
 
+    if self._isValidNewRequest(request_fields, params):
+      # not a valid request
+      context['error_message'] = self.DEF_REQUEST_ERROR_MSG_FMT % (
+          params)
+      return self.requestGet(request, context, params, **kwargs)
+
     # extract the key_name for the new request entity
     key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields)
     key_name = request_logic.logic.getKeyNameForFields(key_fields)
@@ -537,3 +567,34 @@
     template = request_view.view.getParams()['request_processing_template']
 
     return responses.respond(request, template, context=context)
+
+  def _isValidNewRequest(self, request_fields, params):
+    """Checks if this is a valid Request object to make.
+
+    Args:
+    request_fields: dict containing the fields for the new request entity.
+    params: parameters for the current view
+    """
+    fields = request_fields.copy()
+    fields['state'] = ['new', 'group_accepted', 'ignored']
+
+    request_entity = request_logic.logic.getForFields(fields, unique=True)
+    
+    if request_entity:
+      # already outstanding request
+      return False
+
+    # check if the role already exists
+    fields = {'scope' : request_fields['scope'],
+        'link_id': request_fields['link_id'],
+        'state': ['active','inactive'],
+        }
+
+    role_entity = params['logic'].getForFields(fields, unique=True)
+
+    if role_entity:
+      # already has this role
+      return False
+
+    # no oustanding request or a valid role
+    return True