taskapp/events/request.py
author nishanth
Sun, 28 Feb 2010 14:44:10 +0530
changeset 143 796ff9e279a8
parent 132 ca88bf4ad362
child 145 0c97a02b9bdb
permissions -rw-r--r--
now if a user accepts to be a mentor, all his pending reqs will be made invalid.
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)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    67
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    68
                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
    69
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    70
        elif request_obj.role == "MG":
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    71
            if reply:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    72
                ## tell all the MG and AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    73
                ## drop a welcome message to that fucker
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
    74
                changeRole(role=request_obj.role, user=request_obj.replied_by)
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    75
                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
    76
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    77
                    create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    78
            else:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    79
                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
    80
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    81
        elif request_obj.role == "AD":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    82
            if reply:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    83
                ## tell all the AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    84
                ## drop a welcome message to that fucker
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    85
                changeRole(role=request_obj.role, user=request_obj.replied_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    86
                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
    87
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    88
                    create_notification(request_obj.role, a_profile.user, request_obj.replied_by, reply, requested_by=request_obj.sent_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    89
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    90
                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
    91
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    92
        return True #Reply has been added successfully
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    93
    return False #Already replied