taskapp/utilities/notification.py
author nishanth
Fri, 26 Feb 2010 23:22:23 +0530
changeset 125 d3cfceb8e120
parent 123 a6b4234388c8
child 129 e747da8bc110
permissions -rw-r--r--
homepage now displays a list of unpublished tasks
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
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
     5
def create_notification(to,subject,message):
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
    """
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    12
    notification = Notification(sent_date = datetime.now())
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    13
    notification.save()
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    14
    notification.sent_to = to
57
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    15
    notification.sub = subject
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    16
    notification.message = message
67e0d0a915e3 added utilities for creating notification.
anoop
parents:
diff changeset
    17
    notification.save()
78
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    18
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    19
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
    20
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    21
    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
    22
    arguments:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    23
        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
    24
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    25
    try:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    26
        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
    27
    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
    28
        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
    29
    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
    30
    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
    31
    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
    32
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    33
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
    34
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    35
    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
    36
    arguments:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    37
        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
    38
    """
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    39
    try:
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 57
diff changeset
    40
        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
    41
    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
    42
        return False
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    43
    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
    44
    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
    45
    return True
123
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    46
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    47
def get_notification(nid, user):
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    48
    """ 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
    49
    else return None.
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    50
    """
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    51
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    52
    try:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    53
        notify_obj = Notification.objects.get(id=nid)
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    54
    except Notification.DoesNotExist:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    55
        return None
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    56
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    57
    try:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    58
        notify_obj.sent_to.get(id=user.id)
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    59
    except User.DoesNotExist:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    60
        return None
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    61
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    62
    if not notify_obj.is_deleted:
a6b4234388c8 now notification depends on id and not pos.
nishanth
parents: 78
diff changeset
    63
        return notify_obj