Commit 6f82ec2dddc474272b29c8806c8309d7629f9644
1 parent
d93501ee
Exists in
master
and in
3 other branches
Adding notification creation
Showing
1 changed file
with
36 additions
and
3 deletions
Show diff stats
notifications/utils.py
| 1 | -from datetime import datetime | |
| 1 | +from datetime import date | |
| 2 | +from django.utils import timezone | |
| 2 | 3 | from django.db.models import Q |
| 3 | 4 | |
| 4 | 5 | from log.models import Log |
| 5 | 6 | from pendencies.models import Pendencies |
| 6 | 7 | from users.models import User |
| 7 | 8 | |
| 9 | +from .models import Notification | |
| 10 | + | |
| 8 | 11 | def get_resource_users(resource): |
| 9 | 12 | if resource.all_students: |
| 10 | 13 | return resource.topic.subject.students.all() |
| ... | ... | @@ -12,7 +15,7 @@ def get_resource_users(resource): |
| 12 | 15 | return User.objects.filter(Q(resource_students = resource) | Q(group_participants__resource_groups = resource)).distinct() |
| 13 | 16 | |
| 14 | 17 | def set_notifications(): |
| 15 | - pendencies = Pendencies.objects.filter(begin_date__date__lt = datetime.now(), resource__visible = True) | |
| 18 | + pendencies = Pendencies.objects.filter(begin_date__date__lt = timezone.now(), resource__visible = True) | |
| 16 | 19 | |
| 17 | 20 | for pendency in pendencies: |
| 18 | 21 | users = get_resource_users(pendency.resource) |
| ... | ... | @@ -23,8 +26,38 @@ def set_notifications(): |
| 23 | 26 | resource_id = pendency.resource.id |
| 24 | 27 | |
| 25 | 28 | for user in users: |
| 29 | + prev_notify = Notification.objects.filter(user = user, task = pendency).order_by("-creation_date") | |
| 30 | + notify_type = 1 | |
| 31 | + | |
| 32 | + if prev_notify.count() > 0: | |
| 33 | + last_notify = prev_notify[0] | |
| 34 | + | |
| 35 | + if last_notify.creation_date == date.today(): | |
| 36 | + continue | |
| 37 | + | |
| 38 | + if last_notify.meta: | |
| 39 | + if last_notify.creation_date < date.today() < last_notify.meta: | |
| 40 | + continue | |
| 41 | + | |
| 42 | + notify_type = 2 | |
| 43 | + | |
| 26 | 44 | has_action = Log.objects.filter(user_id = user.id, action = pend_action, resource = resource_type, context__contains = {resource_key: resource_id}, datetime__date__gte = subject_begin_date).exists() |
| 27 | 45 | |
| 28 | - print(has_action) | |
| 46 | + if not has_action: | |
| 47 | + if pendency.end_date: | |
| 48 | + if timezone.now() > pendency.end_date: | |
| 49 | + notify_type = 3 | |
| 50 | + | |
| 51 | + if pendency.limit_date: | |
| 52 | + if timezone.now() > pendency.limit_date: | |
| 53 | + notify_type = 4 | |
| 54 | + | |
| 55 | + | |
| 56 | + notification = Notification() | |
| 57 | + notification.user = user | |
| 58 | + notification.level = notify_type | |
| 59 | + notification.task = pendency | |
| 60 | + | |
| 61 | + notification.save() | |
| 29 | 62 | |
| 30 | 63 | ... | ... |