taskapp/events/request.py
author nishanth
Sat, 27 Feb 2010 19:21:15 +0530
changeset 132 ca88bf4ad362
parent 131 85276c5aee5c
child 143 796ff9e279a8
permissions -rw-r--r--
implemented notification functionality for AD MG DV.
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
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    42
                ## 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
    43
                for a_mentor in task.mentors.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    44
                    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
    45
                for a_user in task.assigned_users.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    46
                    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
    47
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    48
                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
    49
            else:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    50
                ## 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
    51
                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
    52
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    53
        elif request_obj.role == "DV":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    54
            if reply:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    55
                ## tell only the user who made him a DV
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    56
                ## drop a welcome message to that fucker
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    57
                changeRole(role=request_obj.role, user=request_obj.replied_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    58
                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
    59
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    60
                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
    61
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    62
        elif request_obj.role == "MG":
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    63
            if reply:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    64
                ## tell all the MG and AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    65
                ## drop a welcome message to that fucker
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
    66
                changeRole(role=request_obj.role, user=request_obj.replied_by)
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    67
                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
    68
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    69
                    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
    70
            else:
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    71
                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
    72
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    73
        elif request_obj.role == "AD":
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    74
            if reply:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    75
                ## tell all the AD
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    76
                ## drop a welcome message to that fucker
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    77
                changeRole(role=request_obj.role, user=request_obj.replied_by)
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    78
                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
    79
                for a_profile in alerting_users:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    80
                    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
    81
            else:
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 131
diff changeset
    82
                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
    83
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    84
        return True #Reply has been added successfully
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    85
    return False #Already replied