taskapp/events/request.py
author nishanth
Tue, 02 Mar 2010 14:46:05 +0530
changeset 174 d034cd77cfa6
parent 161 74ef330c9185
child 218 59107ce0a618
permissions -rw-r--r--
added footer to base.html.
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
153
925af1b4ee65 ditchaxed credit model.
nishanth
parents: 146
diff changeset
     3
from pytask.taskapp.events.task import  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
153
925af1b4ee65 ditchaxed credit model.
nishanth
parents: 146
diff changeset
    28
            create_notification(request_obj.role, receiving_user, replied_by, reply, task, request_obj.remarks, requested_by, receiving_user, pynts)
925af1b4ee65 ditchaxed credit model.
nishanth
parents: 146
diff changeset
    29
            if receiving_user != requested_by:
925af1b4ee65 ditchaxed credit model.
nishanth
parents: 146
diff changeset
    30
                create_notification(request_obj.role, requested_by, replied_by, reply, task, request_obj.remarks, requested_by, receiving_user, pynts)
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    31
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    32
        elif request_obj.role == "MT":
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    33
            task = request_obj.task
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    34
            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
    35
            if reply:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    36
                ## 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
    37
                create_notification("NT", request_obj.replied_by, task=task) 
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    38
143
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    39
                ## 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
    40
                ## 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
    41
                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
    42
                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
    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)
796ff9e279a8 now if a user accepts to be a mentor, all his pending reqs will be made invalid.
nishanth
parents: 132
diff changeset
    44
                       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
    45
                       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
    46
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    47
                ## 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
    48
                for a_mentor in task.mentors.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    49
                    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
    50
                for a_user in task.assigned_users.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    51
                    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
    52
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    53
                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
    54
            else:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    55
                ## 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
    56
                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
    57
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    58
        elif request_obj.role == "DV":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    59
            if reply:
145
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    60
                ## 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
    61
                ## 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
    62
                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
    63
                for req in pending_requests:
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    64
                    req.is_valid = False
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    65
                    req.save()
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    66
                    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
    67
                                        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
    68
                                        requested_by = req.sent_by )
161
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    69
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    70
                ## tell only the user who made him a DV
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    71
                ## drop a welcome message to that fucker
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    72
                create_notification(request_obj.role, request_obj.sent_by, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    73
                create_notification("ND", request_obj.replied_by, requested_by=request_obj.sent_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    74
                changeRole(role=request_obj.role, user=request_obj.replied_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    75
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    76
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    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)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    78
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    79
        elif request_obj.role == "MG":
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    80
            if reply:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    81
                ## tell all the MG and AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    82
                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
    83
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    84
                    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
    85
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    86
                ## 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
    87
                ## 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
    88
                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
    89
                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
    90
                for req in pending_requests:
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    91
                    req.is_valid = False
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    92
                    req.save()
0c97a02b9bdb now accepting to be a MG deleted pending DV and MG reqs.
nishanth
parents: 143
diff changeset
    93
                    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
    94
                                        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
    95
                                        requested_by = req.sent_by )
161
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    96
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    97
                ## drop a welcome message to that fucker
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    98
                create_notification("NG", request_obj.replied_by, requested_by=request_obj.sent_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
    99
                changeRole(role=request_obj.role, user=request_obj.replied_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   100
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
   101
            else:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   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)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   103
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   104
        elif request_obj.role == "AD":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   105
            if reply:
146
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   106
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   107
                ## 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
   108
                ## 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
   109
                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
   110
                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
   111
                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
   112
                    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
   113
                    req.save()
047473d49b25 now accepting an admin request will make all pending AD MG DV reqs as inactive.
nishanth
parents: 145
diff changeset
   114
                    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
   115
                                        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
   116
                                        requested_by = req.sent_by )
161
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   117
                ## tell all the AD
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   118
                alerting_users = Profile.objects.filter(user__is_active=True).filter(rights="AD")
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   119
                for a_profile in alerting_users:
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   120
                    create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   121
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   122
                ## drop a welcome message to that fucker
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   123
                create_notification("NA", request_obj.replied_by, requested_by=request_obj.sent_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   124
                changeRole(role=request_obj.role, user=request_obj.replied_by)
74ef330c9185 implemented nu nt nd ng na notifications.
nishanth
parents: 153
diff changeset
   125
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   126
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   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)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
   128
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
   129
        return True #Reply has been added successfully
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
   130
    return False #Already replied