taskapp/events/request.py
branchbuildout
changeset 227 3c8f3b0e5b00
parent 214 679c7e237052
child 228 81994e525e69
equal deleted inserted replaced
214:679c7e237052 227:3c8f3b0e5b00
     1 from datetime import datetime
       
     2 from pytask.taskapp.models import Profile
       
     3 from pytask.taskapp.events.task import  addMentor
       
     4 from pytask.taskapp.events.user import changeRole
       
     5 from pytask.taskapp.utilities.notification import create_notification
       
     6 
       
     7 def reply_to_request(request_obj, reply, replied_by):
       
     8     """
       
     9     makes a request replied with the given reply.
       
    10     arguments:
       
    11         request_obj - Request object for which change is intended
       
    12         reply - a boolean value to be given as reply (True/False)
       
    13         replied_by - the user object who replies to the request
       
    14     """
       
    15     if not request_obj.is_replied:
       
    16         request_obj.reply = reply
       
    17         request_obj.is_replied = True
       
    18         request_obj.reply_date = datetime.now()
       
    19         request_obj.replied_by = replied_by
       
    20         request_obj.save()
       
    21 
       
    22         if request_obj.role == "PY":
       
    23             ## note that we are not doing any check. we make requests invalid when an event like closing task happens.
       
    24             task = request_obj.task
       
    25             pynts = request_obj.pynts
       
    26             receiving_user = request_obj.receiving_user
       
    27             requested_by = request_obj.sent_by
       
    28             create_notification(request_obj.role, receiving_user, replied_by, reply, task, request_obj.remarks, requested_by, receiving_user, pynts)
       
    29             if receiving_user != requested_by:
       
    30                 create_notification(request_obj.role, requested_by, replied_by, reply, task, request_obj.remarks, requested_by, receiving_user, pynts)
       
    31 
       
    32         elif request_obj.role == "MT":
       
    33             task = request_obj.task
       
    34             requested_by = request_obj.sent_by
       
    35             if reply:
       
    36                 ## tell the replied user that he is mentor for this task and give him learn more link
       
    37                 create_notification("NT", request_obj.replied_by, task=task) 
       
    38 
       
    39                 ## now check if there are such similar requests and mark them as invalid
       
    40                 ## they cannot be of type PY and so we can use the replied_by to get requests
       
    41                 pending_requests = replied_by.request_sent_to.filter(is_valid=True, is_replied=False, role="MT",task=task)
       
    42                 for req in pending_requests:
       
    43                        create_notification("MT", req.sent_by, replied_by, False, task=req.task, remarks = "User has already accepted one such request and is a mentor.", requested_by = req.sent_by)
       
    44                        req.is_valid = False
       
    45                        req.save()
       
    46 
       
    47                 ## alert all the mentors including who made request and all assigned users
       
    48                 for a_mentor in task.mentors.all():
       
    49                     create_notification(request_obj.role, a_mentor, replied_by, True, task, request_obj.remarks, requested_by)
       
    50                 for a_user in task.assigned_users.all():
       
    51                     create_notification(request_obj.role, a_user, replied_by, True, task, request_obj.remarks, requested_by)
       
    52 
       
    53                 addMentor(task, request_obj.replied_by)
       
    54             else:
       
    55                 ## tell the requested user that his request was rejected due to these reasons.
       
    56                 create_notification(request_obj.role, requested_by, replied_by, False, task, request_obj.remarks, requested_by)
       
    57 
       
    58         elif request_obj.role == "DV":
       
    59             if reply:
       
    60                 ## here we look for requests that are similar => requesting for DV and make them invalid
       
    61                 ## also we drop a notification to user who made request
       
    62                 pending_requests = request_obj.replied_by.request_sent_to.filter(is_valid=True,is_replied=False,role="DV")
       
    63                 for req in pending_requests:
       
    64                     req.is_valid = False
       
    65                     req.save()
       
    66                     create_notification(role = req.role, sent_to = req.sent_by, sent_from = replied_by, reply = False, \
       
    67                                         remarks = "User has accepted a similar request and has rights same or higher privileged than the request", \
       
    68                                         requested_by = req.sent_by )
       
    69 
       
    70                 ## tell only the user who made him a DV
       
    71                 ## drop a welcome message to that fucker
       
    72                 create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
       
    73                 create_notification("ND", request_obj.replied_by, requested_by=request_obj.sent_by)
       
    74                 changeRole(role=request_obj.role, user=request_obj.replied_by)
       
    75 
       
    76             else:
       
    77                 create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, remarks=request_obj.remarks, requested_by=request_obj.sent_by)
       
    78 
       
    79         elif request_obj.role == "MG":
       
    80             if reply:
       
    81                 ## tell all the MG and AD
       
    82                 alerting_users = Profile.objects.filter(user__is_active=True).exclude(rights="CT").exclude(rights="DV")
       
    83                 for a_profile in alerting_users:
       
    84                     create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
       
    85 
       
    86                 ## here we look for requests that less or similar => requesting for DV or MG and make them invalid
       
    87                 ## also we drop a notification to user who made request
       
    88                 active_requests = request_obj.replied_by.request_sent_to.filter(is_valid=True,is_replied=False)
       
    89                 pending_requests = active_requests.filter(role="DV") | active_requests.filter(role="MG")
       
    90                 for req in pending_requests:
       
    91                     req.is_valid = False
       
    92                     req.save()
       
    93                     create_notification(role = req.role, sent_to = req.sent_by, sent_from = replied_by, reply = False, \
       
    94                                         remarks = "User has accepted a similar request and has rights same or higher privileged than the request", \
       
    95                                         requested_by = req.sent_by )
       
    96 
       
    97                 ## drop a welcome message to that fucker
       
    98                 create_notification("NG", request_obj.replied_by, requested_by=request_obj.sent_by)
       
    99                 changeRole(role=request_obj.role, user=request_obj.replied_by)
       
   100 
       
   101             else:
       
   102                 create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, remarks=request_obj.remarks, requested_by=request_obj.sent_by)
       
   103 
       
   104         elif request_obj.role == "AD":
       
   105             if reply:
       
   106 
       
   107                 ## here we look for requests that less or similar => requesting for DV or MG or AD and make them invalid
       
   108                 ## also we drop a notification to user who made request
       
   109                 active_requests = request_obj.replied_by.request_sent_to.filter(is_valid=True,is_replied=False)
       
   110                 pending_requests = active_requests.filter(role="DV") | active_requests.filter(role="MG") | active_requests.filter(role="AD")
       
   111                 for req in pending_requests:
       
   112                     req.is_valid = False
       
   113                     req.save()
       
   114                     create_notification(role = req.role, sent_to = req.sent_by, sent_from = replied_by, reply = False, \
       
   115                                         remarks = "User has accepted a similar request and has rights same or higher privileged than the request", \
       
   116                                         requested_by = req.sent_by )
       
   117                 ## tell all the AD
       
   118                 alerting_users = Profile.objects.filter(user__is_active=True).filter(rights="AD")
       
   119                 for a_profile in alerting_users:
       
   120                     create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
       
   121 
       
   122                 ## drop a welcome message to that fucker
       
   123                 create_notification("NA", request_obj.replied_by, requested_by=request_obj.sent_by)
       
   124                 changeRole(role=request_obj.role, user=request_obj.replied_by)
       
   125 
       
   126             else:
       
   127                 create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, remarks=request_obj.remarks, requested_by=request_obj.sent_by)
       
   128 
       
   129         return True #Reply has been added successfully
       
   130     return False #Already replied