Commit e0c4966f65655c25162c0d0b9d176d45b2901759
1 parent
180657c9
Exists in
master
and in
3 other branches
Adding function to generate notifications (not generating yet) and django-cron package
Showing
4 changed files
with
50 additions
and
0 deletions
Show diff stats
amadeus/settings.py
@@ -52,6 +52,7 @@ INSTALLED_APPS = [ | @@ -52,6 +52,7 @@ INSTALLED_APPS = [ | ||
52 | 's3direct', | 52 | 's3direct', |
53 | 'django_summernote', | 53 | 'django_summernote', |
54 | 'session_security', | 54 | 'session_security', |
55 | + 'django_cron', | ||
55 | 56 | ||
56 | 'amadeus', | 57 | 'amadeus', |
57 | 'users', | 58 | 'users', |
@@ -169,6 +170,10 @@ STATICFILES_DIRS = [ | @@ -169,6 +170,10 @@ STATICFILES_DIRS = [ | ||
169 | os.path.join(BASE_DIR, "amadeus/static") | 170 | os.path.join(BASE_DIR, "amadeus/static") |
170 | ] | 171 | ] |
171 | 172 | ||
173 | +CRON_CLASSES = [ | ||
174 | + 'notifications.cron.Test' | ||
175 | +] | ||
176 | + | ||
172 | #SECURITY | 177 | #SECURITY |
173 | SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO','https') | 178 | SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO','https') |
174 | 179 |
@@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
1 | +import datetime | ||
2 | +from django_cron import CronJobBase, Schedule | ||
3 | + | ||
4 | +from .utils import set_notifications | ||
5 | + | ||
6 | +class Test(CronJobBase): | ||
7 | + RUN_EVERY_MINS = 1 # every minute | ||
8 | + | ||
9 | + schedule = Schedule(run_every_mins=RUN_EVERY_MINS) | ||
10 | + code = 'amadeus.notification_cron' # a unique code | ||
11 | + | ||
12 | + def do(self): | ||
13 | + print("Hey") | ||
14 | + set_notifications() | ||
0 | \ No newline at end of file | 15 | \ No newline at end of file |
@@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
1 | +from datetime import datetime | ||
2 | +from django.db.models import Q | ||
3 | + | ||
4 | +from log.models import Log | ||
5 | +from pendencies.models import Pendencies | ||
6 | +from users.models import User | ||
7 | + | ||
8 | +def get_resource_users(resource): | ||
9 | + if resource.all_students: | ||
10 | + return resource.topic.subject.students.all() | ||
11 | + | ||
12 | + return User.objects.filter(Q(resource_students = resource) | Q(group_participants__resource_groups = resource)).distinct() | ||
13 | + | ||
14 | +def set_notifications(): | ||
15 | + pendencies = Pendencies.objects.filter(begin_date__date__lt = datetime.now(), resource__visible = True) | ||
16 | + | ||
17 | + for pendency in pendencies: | ||
18 | + users = get_resource_users(pendency.resource) | ||
19 | + subject_begin_date = pendency.resource.topic.subject.init_date | ||
20 | + pend_action = pendency.action | ||
21 | + resource_type = pendency.resource._my_subclass | ||
22 | + resource_key = resource_type + "_id" | ||
23 | + resource_id = pendency.resource.id | ||
24 | + | ||
25 | + for user in users: | ||
26 | + 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 | + | ||
28 | + print(has_action) | ||
29 | + | ||
30 | + |
requirements.txt
@@ -33,3 +33,4 @@ validators==0.11.0 | @@ -33,3 +33,4 @@ validators==0.11.0 | ||
33 | Werkzeug==0.11.11 | 33 | Werkzeug==0.11.11 |
34 | whitenoise==3.2.2 | 34 | whitenoise==3.2.2 |
35 | django-session-security==2.4.0 | 35 | django-session-security==2.4.0 |
36 | +django-cron==0.5.0 | ||
36 | \ No newline at end of file | 37 | \ No newline at end of file |