taskapp/events/request.py
author nishanth
Sun, 28 Feb 2010 16:09:42 +0530
changeset 146 047473d49b25
parent 145 0c97a02b9bdb
child 153 925af1b4ee65
permissions -rw-r--r--
now accepting an admin request will make all pending AD MG DV reqs as inactive.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     1
from datetime import datetime
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
     2
from pytask.taskapp.models import Profile
105
091b044a3bf4 now adding mentor for a task happens through request. notifications still pending though
nishanth
parents: 100
diff changeset
     3
from pytask.taskapp.events.task import addCredits, addMentor
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
     4
from pytask.taskapp.events.user import changeRole
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
     5
from pytask.taskapp.utilities.notification import create_notification
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     6
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     7
def reply_to_request(request_obj, reply, replied_by):
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     8
    """
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     9
    makes a request replied with the given reply.
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    10
    arguments:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    11
        request_obj - Request object for which change is intended
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    12
        reply - a boolean value to be given as reply (True/False)
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    13
        replied_by - the user object who replies to the request
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    14
    """
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    15
    if not request_obj.is_replied:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    16
        request_obj.reply = reply
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    17
        request_obj.is_replied = True
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    18
        request_obj.reply_date = datetime.now()
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    19
        request_obj.replied_by = replied_by
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    20
        request_obj.save()
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    21
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    22
        if request_obj.role == "PY":
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    23
            ## note that we are not doing any check. we make requests invalid when an event like closing task happens.
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    24
            task = request_obj.task
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    25
            pynts = request_obj.pynts
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    26
            receiving_user = request_obj.receiving_user
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    27
            requested_by = request_obj.sent_by
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    28
            for a_mentor in task.mentors.all():
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    29
                if reply:
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    30
                    addCredits(task, request_obj.sent_by, request_obj.receiving_user, pynts)
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    31
                    create_notification(request_obj.role, a_mentor, replied_by, True, task, request_obj.remarks, requested_by, receiving_user, pynts)
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    32
                else:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    33
                    create_notification(request_obj.role, a_mentor, replied_by, False, task, request_obj.remarks, requested_by, receiving_user, pynts)
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    34
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    35
        elif request_obj.role == "MT":
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    36
            task = request_obj.task
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    37
            requested_by = request_obj.sent_by
105
091b044a3bf4 now adding mentor for a task happens through request. notifications still pending though
nishanth
parents: 100
diff changeset
    38
            if reply:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    39
                ## tell the replied user that he is mentor for this task and give him learn more link
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    40
                create_notification("NT", request_obj.replied_by, task=task) 
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    41
143
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    42
                ## now check if there are such similar requests and mark them as invalid
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    43
                ## they cannot be of type PY and so we can use the replied_by to get requests
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    44
                pending_requests = replied_by.request_sent_to.filter(is_valid=True, is_replied=False, role="MT",task=task)
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    45
                for req in pending_requests:
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    46
                       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)
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    47
                       req.is_valid = False
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    48
                       req.save()
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    49
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    50
                ## alert all the mentors including who made request and all assigned users
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    51
                for a_mentor in task.mentors.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    52
                    create_notification(request_obj.role, a_mentor, replied_by, True, task, request_obj.remarks, requested_by)
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    53
                for a_user in task.assigned_users.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    54
                    create_notification(request_obj.role, a_user, replied_by, True, task, request_obj.remarks, requested_by)
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    55
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    56
                addMentor(task, request_obj.replied_by)
105
091b044a3bf4 now adding mentor for a task happens through request. notifications still pending though
nishanth
parents: 100
diff changeset
    57
            else:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    58
                ## tell the requested user that his request was rejected due to these reasons.
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    59
                create_notification(request_obj.role, requested_by, replied_by, False, task, request_obj.remarks, requested_by)
105
091b044a3bf4 now adding mentor for a task happens through request. notifications still pending though
nishanth
parents: 100
diff changeset
    60
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    61
        elif request_obj.role == "DV":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    62
            if reply:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    63
                ## tell only the user who made him a DV
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    64
                ## drop a welcome message to that fucker
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    65
                changeRole(role=request_obj.role, user=request_obj.replied_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    66
                create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
145
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    67
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    68
                ## here we look for requests that are similar => requesting for DV and make them invalid
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    69
                ## also we drop a notification to user who made request
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    70
                pending_requests = request_obj.replied_by.request_sent_to.filter(is_valid=True,is_replied=False,role="DV")
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    71
                for req in pending_requests:
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    72
                    req.is_valid = False
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    73
                    req.save()
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    74
                    create_notification(role = req.role, sent_to = req.sent_by, sent_from = replied_by, reply = False, \
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    75
                                        remarks = "User has accepted a similar request and has rights same or higher privileged than the request", \
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    76
                                        requested_by = req.sent_by )
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    77
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    78
                create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, remarks=request_obj.remarks, requested_by=request_obj.sent_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    79
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    80
        elif request_obj.role == "MG":
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    81
            if reply:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    82
                ## tell all the MG and AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    83
                ## drop a welcome message to that fucker
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
    84
                changeRole(role=request_obj.role, user=request_obj.replied_by)
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    85
                alerting_users = Profile.objects.filter(user__is_active=True).exclude(rights="CT").exclude(rights="DV")
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    86
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    87
                    create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
145
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    88
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    89
                ## here we look for requests that less or similar => requesting for DV or MG and make them invalid
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    90
                ## also we drop a notification to user who made request
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    91
                active_requests = request_obj.replied_by.request_sent_to.filter(is_valid=True,is_replied=False)
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    92
                pending_requests = active_requests.filter(role="DV") | active_requests.filter(role="MG")
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    93
                for req in pending_requests:
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    94
                    req.is_valid = False
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    95
                    req.save()
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    96
                    create_notification(role = req.role, sent_to = req.sent_by, sent_from = replied_by, reply = False, \
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    97
                                        remarks = "User has accepted a similar request and has rights same or higher privileged than the request", \
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    98
                                        requested_by = req.sent_by )
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    99
            else:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   100
                create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, remarks=request_obj.remarks, requested_by=request_obj.sent_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   101
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   102
        elif request_obj.role == "AD":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   103
            if reply:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   104
                ## tell all the AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   105
                ## drop a welcome message to that fucker
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   106
                changeRole(role=request_obj.role, user=request_obj.replied_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   107
                alerting_users = Profile.objects.filter(user__is_active=True).filter(rights="AD")
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   108
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   109
                    create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
146
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   110
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   111
                ## here we look for requests that less or similar => requesting for DV or MG or AD and make them invalid
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   112
                ## also we drop a notification to user who made request
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   113
                active_requests = request_obj.replied_by.request_sent_to.filter(is_valid=True,is_replied=False)
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   114
                pending_requests = active_requests.filter(role="DV") | active_requests.filter(role="MG") | active_requests.filter(role="AD")
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   115
                for req in pending_requests:
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   116
                    req.is_valid = False
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   117
                    req.save()
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   118
                    create_notification(role = req.role, sent_to = req.sent_by, sent_from = replied_by, reply = False, \
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   119
                                        remarks = "User has accepted a similar request and has rights same or higher privileged than the request", \
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   120
                                        requested_by = req.sent_by )
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   121
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   122
                create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, remarks=request_obj.remarks, requested_by=request_obj.sent_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   123
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
   124
        return True #Reply has been added successfully
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
   125
    return False #Already replied