taskapp/events/request.py
author nishanth
Sat, 27 Feb 2010 17:20:22 +0530
changeset 131 85276c5aee5c
parent 130 9417ae986d2a
child 132 ca88bf4ad362
permissions -rw-r--r--
added notifications for approval and rejection of a mentor.
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
105
091b044a3bf4 now adding mentor for a task happens through request. notifications still pending though
nishanth
parents: 100
diff changeset
     2
from pytask.taskapp.events.task import addCredits, addMentor
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
     3
from pytask.taskapp.events.user import changeRole
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
     4
from pytask.taskapp.utilities.notification import create_notification
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     5
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     6
def reply_to_request(request_obj, reply, replied_by):
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     7
    """
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     8
    makes a request replied with the given reply.
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
     9
    arguments:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    10
        request_obj - Request object for which change is intended
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    11
        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
    12
        replied_by - the user object who replies to the request
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    13
    """
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    14
    if not request_obj.is_replied:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    15
        request_obj.reply = reply
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    16
        request_obj.is_replied = True
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    17
        request_obj.reply_date = datetime.now()
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    18
        request_obj.replied_by = replied_by
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    19
        request_obj.save()
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    20
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    21
        if request_obj.role == "PY":
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    22
            ## 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
    23
            task = request_obj.task
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    24
            pynts = request_obj.pynts
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    25
            receiving_user = request_obj.receiving_user
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    26
            requested_by = request_obj.sent_by
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    27
            for a_mentor in task.mentors.all():
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    28
                if reply:
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 107
diff changeset
    29
                    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
    30
                    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
    31
                else:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    32
                    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
    33
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    34
        elif request_obj.role == "MT":
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    35
            task = request_obj.task
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    36
            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
    37
            if reply:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    38
                ## 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
    39
                create_notification("NT", request_obj.replied_by, task=task) 
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    40
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    41
                ## 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
    42
                for a_mentor in task.mentors.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    43
                    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
    44
                for a_user in task.assigned_users.all():
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    45
                    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
    46
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    47
                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
    48
            else:
131
85276c5aee5c added notifications for approval and rejection of a mentor.
nishanth
parents: 130
diff changeset
    49
                ## 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
    50
                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
    51
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    52
        elif request_obj.role in ["AD", "MG", "DV"]:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    53
            if reply:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    54
                ## make him the role
107
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
    55
                ## here we check for rights just in case to be fine with demoted users. we change only the user who made request has that rights.
4903b4973fc8 completed the process_request part.
nishanth
parents: 105
diff changeset
    56
                changeRole(role=request_obj.role, user=request_obj.replied_by)
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    57
            else:
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    58
                ## notify request_obj.sent_by that it has been rejected
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    59
                pass
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    60
        return True #Reply has been added successfully
2275886511df now admin can accept a request for assigning credits.
nishanth
parents:
diff changeset
    61
    return False #Already replied