1 from pytask.taskapp.models import Request |
1 from pytask.taskapp.models import Request, Profile |
2 from datetime import datetime |
2 from datetime import datetime |
|
3 from django.contrib.auth.models import User |
3 |
4 |
4 def create_request(to,by,role,task=None,assigned_user=None,pynts=0): |
5 def create_request(sent_by,role,sent_to=None,task=None,receiving_user=None,pynts=0): |
5 """ |
6 """ |
6 creates an unreplied request, based on the passed arguments |
7 creates an unreplied request, based on the passed arguments |
7 to - a list of users to which the notification is to be sent |
8 sent_to - a list of users to which the notification is to be sent |
8 by - sender of request |
9 sent_by - sender of request |
9 role - a two character field which represents the role requested |
10 role - a two character field which represents the role requested, if role = 'PY' then sent to all admins |
10 task - a requesting task (useful for sending admins a request to give Pynts to the user) |
11 task - a requesting task (useful for sending admins a request to give Pynts to the user) |
11 assigned_user - user to whom the Pynts/Task is assigned to(useful for sending admins a request to give Pynts to the user) |
12 receiving_user - user to whom the Pynts is assigned to(useful for sending admins a request to give Pynts to the user) |
|
13 pynts - the pynts assigned to the receiving user |
12 """ |
14 """ |
13 req = Request(creation_date=datetime.now()) |
15 req = Request(creation_date=datetime.now()) |
14 req.by = by |
16 req.sent_by = sent_by |
15 req.reply_date = datetime(1970,01,01) |
17 req.reply_date = datetime(1970,01,01) |
16 req.save() |
|
17 req.to = to |
|
18 req.role = role |
18 req.role = role |
|
19 req.pynts = pynts |
19 if task: |
20 if task: |
20 req.task = task |
21 req.task = task |
21 if assigned_user: |
22 req.save() |
22 req.assigned_user = assigned_user |
23 if role == 'PY': |
23 req.pynts = pynts |
24 admin_profiles = Profile.objects.filter(rights='AD') |
|
25 for admin in admin_profiles: |
|
26 req.sent_to.add(admin_profile.user) |
|
27 req.receiving_user = receiving_user |
|
28 else: |
|
29 req.sent_to.add(sent_to) |
24 req.save() |
30 req.save() |
25 |
31 |
26 def reply_to_request(request_obj, reply): |
32 def reply_to_request(request_obj, reply, replied_by): |
27 """ |
33 """ |
28 makes a request replied with the given reply. |
34 makes a request replied with the given reply. |
29 arguments: |
35 arguments: |
30 request_obj - Request object for which change is intended |
36 request_obj - Request object for which change is intended |
31 reply - a boolean value to be given as reply (True/False) |
37 reply - a boolean value to be given as reply (True/False) |
|
38 replied_by - the user object who replies to the request |
32 """ |
39 """ |
33 if not request_obj.replied: |
40 if not request_obj.is_replied: |
34 request_obj.reply = reply |
41 request_obj.reply = reply |
35 request_obj.replied = True |
42 request_obj.is_replied = True |
36 request_obj.read = True |
43 request_obj.is_read = True |
37 request_obj.reply_date = datetime.now() |
44 request_obj.reply_date = datetime.now() |
|
45 request_obj.replied_by = replied_by |
38 request_obj.save() |
46 request_obj.save() |
39 return True #Reply has been added successfully |
47 return True #Reply has been added successfully |
40 return False #Already replied |
48 return False #Already replied |