app/soc/modules/ghop/views/models/task_subscription.py
changeset 2889 5c0728efdefb
child 2953 3c024d61290c
equal deleted inserted replaced
2888:e85c47c17abc 2889:5c0728efdefb
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Views for GHOP Task Subscription.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21     '"Madhusudan.C.S" <madhusudancs@gmail.com>'
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.ext import db
       
    26 
       
    27 from django import http
       
    28 
       
    29 from soc.logic import dicts
       
    30 from soc.logic.models import user as user_logic
       
    31 from soc.views.helper import decorators
       
    32 from soc.views.models import base
       
    33 
       
    34 from soc.modules.ghop.logic.models import task as ghop_task_logic
       
    35 from soc.modules.ghop.logic.models import task_subscription as \
       
    36     ghop_task_subscription_logic
       
    37 from soc.modules.ghop.views.helper import access as ghop_access
       
    38 
       
    39 import soc.modules.ghop.logic.models.task_subscription
       
    40 
       
    41 
       
    42 class View(base.View):
       
    43   """View methods for the Task Subscriptions.
       
    44   """
       
    45 
       
    46   def __init__(self, params=None):
       
    47     """Defines the fields and methods required for the task_subscription.
       
    48 
       
    49     Params:
       
    50       params: a dict with params for this View
       
    51     """
       
    52 
       
    53     rights = ghop_access.GHOPChecker(params)
       
    54     rights['subscribe'] = ['checkIsUser']
       
    55 
       
    56     new_params = {}
       
    57     new_params['logic'] = soc.modules.ghop.logic.models.task_subscription.logic
       
    58     new_params['rights'] = rights
       
    59 
       
    60     new_params['name'] = "Task Subscription"
       
    61     new_params['module_name'] = "task_subscription"
       
    62 
       
    63     new_params['module_package'] = 'soc.modules.ghop.views.models'
       
    64     new_params['url_name'] = 'ghop/task_subscription'
       
    65 
       
    66     patterns = []
       
    67     patterns += [
       
    68         (r'^%(url_name)s/(?P<access_type>subscribe)$',
       
    69         '%(module_package)s.%(module_name)s.subscribe',
       
    70         'Subscribe to the %(name)s'),
       
    71         ]
       
    72 
       
    73     new_params['extra_django_patterns'] = patterns
       
    74 
       
    75     params = dicts.merge(params, new_params, sub_merge=True)
       
    76 
       
    77     super(View, self).__init__(params=params)
       
    78 
       
    79   @decorators.merge_params
       
    80   @decorators.check_access
       
    81   def subscribe(self, request, access_type, page_name=None,
       
    82                 params=None, **kwargs):
       
    83     """View that subscribes/unsubscribes an user.
       
    84 
       
    85     This view is accessed by an AJAX call from task public page.
       
    86 
       
    87     Args:
       
    88       request: the standard Django HTTP request object
       
    89     """
       
    90 
       
    91     get_params = request.GET
       
    92 
       
    93     task_entity = ghop_task_logic.logic.getFromKeyNameOr404(
       
    94         get_params['key_name'])
       
    95 
       
    96     user_account = user_logic.logic.getForCurrentAccount()
       
    97 
       
    98     fields = {
       
    99         'task': task_entity,
       
   100         }
       
   101 
       
   102     entity = ghop_task_subscription_logic.logic.getForFields(
       
   103         fields, unique=True)
       
   104 
       
   105     subscribers = db.get(entity.subscribers)
       
   106 
       
   107     # TODO: this should not loop over all subscribers but use GET argument
       
   108     remove = False
       
   109 
       
   110     if entity:
       
   111       for subscriber in subscribers:
       
   112         if subscriber.key() == user_account.key():
       
   113           remove = True
       
   114           break 
       
   115 
       
   116     if remove:
       
   117       subscribers.remove(subscriber)
       
   118       data = 'remove'
       
   119     else:
       
   120       subscribers.append(user_account)
       
   121       data = 'add'
       
   122 
       
   123     # TODO: missing description for this argument, is it even necessary?
       
   124     if not get_params.get('no_toggle'):
       
   125       sub_keys = []
       
   126       for subscriber in subscribers:
       
   127         sub_keys.append(subscriber.key())
       
   128 
       
   129       properties = {
       
   130           'subscribers': sub_keys,
       
   131           }
       
   132 
       
   133       ghop_task_subscription_logic.logic.updateEntityProperties(
       
   134           entity, properties)
       
   135 
       
   136     return http.HttpResponse(data)
       
   137 
       
   138 
       
   139 view = View()
       
   140 
       
   141 subscribe = decorators.view(view.subscribe)