taskapp/utilities/notification.py
author nishanth
Sat, 27 Feb 2010 14:22:26 +0530
changeset 129 e747da8bc110
parent 123 a6b4234388c8
child 131 85276c5aee5c
permissions -rw-r--r--
notifications work for approving and rejecting credits.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
     1
from datetime import datetime
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
     2
from django.contrib.auth.models import User
57
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     3
from pytask.taskapp.models import Notification
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     4
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
     5
def create_notification(role, sent_to, sent_from=None, reply=None, task=None, receiving_user=None, pynts=None, requested_by=None, remarks=None):
57
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     6
    """
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     7
    creates a notification based on the passed arguments.
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     8
        to - a list of users to which the notification is to be sent
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     9
        subject - subject of the notification message to be sent
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    10
        message - message body of the notification
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    11
    """
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    12
57
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    13
    notification = Notification(sent_date = datetime.now())
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    14
    notification.role = role
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    15
    notification.sent_to = sent_to
57
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    16
    notification.save()
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    17
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    18
    if role == "PY":
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    19
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    20
        notification.sent_from = sent_from
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    21
        notification.task = task
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    22
        notification.pynts = pynts
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    23
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    24
        task_url= '<a href="/task/view/tid=%s">%s</a>'%(task.id, task.title)
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    25
        credits_url = '<a href="/task/assigncredits/tid=%s">%s</a>'%(task.id, "click here")
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    26
        mentor_url = '<a href="/user/view/uid=%s">%s</a>'%(requested_by.id, requested_by.username)
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    27
        admin_url = '<a href="/user/view/uid=%s">%s</a>'%(sent_from.id, sent_from.username)
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    28
        user_url = '<a href="/user/view/uid=%s">%s</a>'%(receiving_user.id, receiving_user.username)
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    29
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    30
        if reply:
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    31
            notification.sub = "Approved request for assign of credits"
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    32
            notification.message  = """ Request made by %s to assign %s pynts to %s for the task %s has been approved by %s<br />
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    33
                                    %s if you want the view/assign pynts page of the task.<br />"""%(mentor_url, pynts, user_url, task_url, admin_url, credits_url)
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    34
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    35
        else:
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    36
            notification.sub = "Rejected request for assign of credits"
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    37
            notification.message = """ Request made by %s to assign %s pynts to %s for the task %s has been rejected by %s.<br /> """%(mentor_url, pynts, user_url, task_url, admin_url)
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    38
            if remarks:
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    39
                notification.remarks = remarks
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    40
                notification.message += "Reason: %s<br />"%remarks
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    41
            notification.message += "<br />"
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    42
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    43
        notification.save()
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    44
78
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    45
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    46
def mark_notification_read(notification_id):
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    47
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    48
    makes a notification identified by the notification_id read.
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    49
    arguments:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    50
        notification_id - a number denoting the id of the Notification object
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    51
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    52
    try:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    53
        notification = Notification.objects.get(id = notification_id)
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    54
    except Notification.DoesNotExist:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    55
        return False
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    56
    notification.is_read = True
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    57
    notification.save()
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    58
    return True
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    59
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    60
def delete_notification(notification_id):
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    61
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    62
    deletes a notification identified by the notification_id.
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    63
    arguments:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    64
        notification_id - a number denoting the id of the Notification object
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    65
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    66
    try:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    67
        notification = Notification.objects.get(id = notification_id)
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    68
    except Notification.DoesNotExist:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    69
        return False
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    70
    notification.is_deleted = True
78
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    71
    notification.save()
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    72
    return True
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    73
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    74
def get_notification(nid, user):
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    75
    """ if notification exists, and belongs to the current user, return it.
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    76
    else return None.
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    77
    """
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    78
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    79
    try:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    80
        notify_obj = Notification.objects.get(id=nid)
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    81
    except Notification.DoesNotExist:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    82
        return None
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    83
129
e747da8bc110 notifications work for approving and rejecting credits.
nishanth
parents: 123
diff changeset
    84
    if notify_obj.sent_to == user and ( not notify_obj.is_deleted ):
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    85
        return notify_obj