diff --git a/amadeus/settings.py b/amadeus/settings.py index eea3c33..a3b18bf 100644 --- a/amadeus/settings.py +++ b/amadeus/settings.py @@ -52,6 +52,7 @@ INSTALLED_APPS = [ 's3direct', 'django_summernote', 'session_security', + 'django_cron', 'amadeus', 'users', @@ -169,6 +170,10 @@ STATICFILES_DIRS = [ os.path.join(BASE_DIR, "amadeus/static") ] +CRON_CLASSES = [ + 'notifications.cron.Test' +] + #SECURITY SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO','https') diff --git a/notifications/cron.py b/notifications/cron.py new file mode 100644 index 0000000..82389fb --- /dev/null +++ b/notifications/cron.py @@ -0,0 +1,14 @@ +import datetime +from django_cron import CronJobBase, Schedule + +from .utils import set_notifications + +class Test(CronJobBase): + RUN_EVERY_MINS = 1 # every minute + + schedule = Schedule(run_every_mins=RUN_EVERY_MINS) + code = 'amadeus.notification_cron' # a unique code + + def do(self): + print("Hey") + set_notifications() \ No newline at end of file diff --git a/notifications/utils.py b/notifications/utils.py new file mode 100644 index 0000000..ba255d8 --- /dev/null +++ b/notifications/utils.py @@ -0,0 +1,30 @@ +from datetime import datetime +from django.db.models import Q + +from log.models import Log +from pendencies.models import Pendencies +from users.models import User + +def get_resource_users(resource): + if resource.all_students: + return resource.topic.subject.students.all() + + return User.objects.filter(Q(resource_students = resource) | Q(group_participants__resource_groups = resource)).distinct() + +def set_notifications(): + pendencies = Pendencies.objects.filter(begin_date__date__lt = datetime.now(), resource__visible = True) + + for pendency in pendencies: + users = get_resource_users(pendency.resource) + subject_begin_date = pendency.resource.topic.subject.init_date + pend_action = pendency.action + resource_type = pendency.resource._my_subclass + resource_key = resource_type + "_id" + resource_id = pendency.resource.id + + for user in users: + 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() + + print(has_action) + + diff --git a/requirements.txt b/requirements.txt index 926bef7..b351794 100644 --- a/requirements.txt +++ b/requirements.txt @@ -33,3 +33,4 @@ validators==0.11.0 Werkzeug==0.11.11 whitenoise==3.2.2 django-session-security==2.4.0 +django-cron==0.5.0 \ No newline at end of file -- libgit2 0.21.2