taskapp/utilities/request.py
author nishanth
Thu, 04 Mar 2010 17:29:00 +0530
changeset 199 946d0fe60606
parent 133 34187a80d279
permissions -rw-r--r--
now a notification is sent to all the mentors after there is a new claim.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
     1
from datetime import datetime
122
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
     2
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
     3
from django.contrib.auth.models import User
100
2275886511df now admin can accept a request for assigning credits.
nishanth
parents: 99
diff changeset
     4
from pytask.taskapp.models import Request, Profile
58
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
     5
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
     6
def create_request(sent_by,role,sent_to=None,task=None,receiving_user=None,pynts=0):
58
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
     7
    """
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
     8
    creates an unreplied request, based on the passed arguments
132
ca88bf4ad362 implemented notification functionality for AD MG DV.
nishanth
parents: 122
diff changeset
     9
        sent_to - a list of users to which the request is to be sent
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    10
        sent_by - sender of request
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    11
        role - a two character field which represents the role requested, if role = 'PY' then sent to all admins
78
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 71
diff changeset
    12
        task - a requesting task (useful for sending admins a request to give Pynts to the user)
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    13
        receiving_user - user to whom the Pynts is assigned to(useful for sending admins a request to give Pynts to the user)
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    14
        pynts - the pynts assigned to the receiving user
58
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
    15
    """
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
    16
    req = Request(creation_date=datetime.now())
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    17
    req.sent_by = sent_by
58
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
    18
    req.reply_date = datetime(1970,01,01)
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
    19
    req.role = role
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    20
    req.pynts = pynts
80
8917b190c4c4 added attribute pynts to request model.
anoop
parents: 78
diff changeset
    21
    if task:
71
801cf8fca53a made change to the request model and corresponding create_request utility.
anoop
parents: 58
diff changeset
    22
        req.task = task
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    23
    req.save()
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    24
    if role == 'PY':
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    25
        admin_profiles = Profile.objects.filter(rights='AD')
99
64c34c1f441f assign_credits event now creates a proper request object .
nishanth
parents: 87
diff changeset
    26
        for admin_profile in admin_profiles:
87
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    27
            req.sent_to.add(admin_profile.user)
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    28
        req.receiving_user = receiving_user
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    29
    else:
f0a3d6612462 updated create_request utility.
anoop
parents: 82
diff changeset
    30
        req.sent_to.add(sent_to)
58
e0ec1901dfce changed request model, added create_request utility.
anoop
parents:
diff changeset
    31
    req.save()
78
c5bcafccc135 added utilities reply_to_request, mark_notification_read, delete_notification and made change to create_request utility.
anoop
parents: 71
diff changeset
    32
122
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    33
def get_request(rid, user):
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    34
    """ see if the request is replied or if he can not view the request,
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    35
    raise 404 error. else return request.
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    36
    """
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    37
133
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    38
    active_requests = user.request_sent_to.filter(is_valid=True, is_replied=False).order_by('creation_date')
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    39
    current_requests = active_requests.filter(id=rid)
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    40
    if current_requests:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    41
        current_request = current_requests[0]
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    42
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    43
        try:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    44
            newer_request = current_request.get_next_by_creation_date(sent_to=user, is_replied=False, is_valid=True)
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    45
            newest_request = active_requests.reverse()[0]
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    46
            if newer_request == newest_request:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    47
                newest_request = None
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    48
        except Request.DoesNotExist:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    49
            newer_request, newest_request = None, None
122
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    50
133
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    51
        try:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    52
            older_request = current_request.get_previous_by_creation_date(sent_to=user, is_replied=False, is_valid=True)
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    53
            oldest_request = active_requests[0]
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    54
            if oldest_request == older_request:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    55
                oldest_request = None
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    56
        except Request.DoesNotExist:
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    57
            older_request, oldest_request = None, None
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    58
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    59
        return newest_request, newer_request, current_request, older_request, oldest_request 
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    60
122
daee11bdfbaa now rid is not the position argument. it is request id.
nishanth
parents: 100
diff changeset
    61
    else:
133
34187a80d279 added next and previous capabilities to requests and notifications.
nishanth
parents: 132
diff changeset
    62
        return None, None, None, None, None