From 3ea9a7719f06dc6107cd52456745980bd5c8b6f9 Mon Sep 17 00:00:00 2001 From: fbormann Date: Thu, 8 Sep 2016 11:10:58 -0300 Subject: [PATCH] Modifiying Models and Adding a NotificationMixin Isuee#41 --- core/migrations/0003_auto_20160908_1108.py | 28 ++++++++++++++++++++++++++++ core/mixins.py | 26 ++++++++++++++++++++++++-- core/models.py | 5 +++-- users/migrations/0011_auto_20160908_1108.py | 22 ++++++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 core/migrations/0003_auto_20160908_1108.py create mode 100644 users/migrations/0011_auto_20160908_1108.py diff --git a/core/migrations/0003_auto_20160908_1108.py b/core/migrations/0003_auto_20160908_1108.py new file mode 100644 index 0000000..8164680 --- /dev/null +++ b/core/migrations/0003_auto_20160908_1108.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-08 14:08 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('core', '0002_auto_20160907_0038'), + ] + + operations = [ + migrations.AddField( + model_name='notification', + name='actor', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notification_Performer', to=settings.AUTH_USER_MODEL, verbose_name='Perfomer'), + ), + migrations.AlterField( + model_name='notification', + name='user', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notification_Actor', to=settings.AUTH_USER_MODEL, verbose_name='User'), + ), + ] diff --git a/core/mixins.py b/core/mixins.py index 00681fa..7e744ad 100644 --- a/core/mixins.py +++ b/core/mixins.py @@ -1,5 +1,5 @@ from django.conf import settings -from .models import Action, Resource, Action_Resource, Log +from .models import Action, Resource, Action_Resource, Log, Notification class LogMixin(object): log_action = "" @@ -17,4 +17,26 @@ class LogMixin(object): log.save() - return super(LogMixin, self).dispatch(request, *args, **kwargs) \ No newline at end of file + return super(LogMixin, self).dispatch(request, *args, **kwargs) + + + +class NotificationMixin(object): + message = "" + read = False + + def dispatch(self, request, *args, **kwargs): + action = Action.objects.filter(name = self.log_action) + resource = Resource.objects.filter(name = self.log_resource) + + action_resource = Action_Resource.objects.filter(action = action, resource = resource)[0] + + notification = Notification() + notification.action_resource = action_resource + notification.user = request.user #We still have to handle the notification to be sent to an amount of Users + + notification.read = read + notification.message = "" + + + diff --git a/core/models.py b/core/models.py index cda594f..466f295 100644 --- a/core/models.py +++ b/core/models.py @@ -52,11 +52,12 @@ class Action_Resource(models.Model): class Notification(models.Model): message = models.TextField(_('Message')) - user = models.ForeignKey(User, verbose_name = _('Actor')) + user = models.ForeignKey(User, related_name = _('%(class)s_Actor'), verbose_name= _('User')) read = models.BooleanField(_('Read'), default = False) datetime = models.DateTimeField(_("Date and Time of action"), auto_now_add = True) action_resource = models.ForeignKey(Action_Resource, verbose_name = _('Action_Resource')) - + actor = models.ForeignKey(User, related_name = _('%(class)s_Performer'), verbose_name= _('Perfomer'), null = True) + class Meta: verbose_name = _("Notification") verbose_name_plural = _("Notifications") diff --git a/users/migrations/0011_auto_20160908_1108.py b/users/migrations/0011_auto_20160908_1108.py new file mode 100644 index 0000000..c53c79f --- /dev/null +++ b/users/migrations/0011_auto_20160908_1108.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-08 14:08 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import re + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0010_auto_20160906_2332'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='username', + field=models.CharField(help_text='A short name that will be used to identify you in the platform and to access it', max_length=35, unique=True, validators=[django.core.validators.RegexValidator(re.compile(b'^[\\w.@+-]+$'), 'Type a valid username. This fields should only contain letters, numbers and the characteres: @/./+/-/_ .', b'invalid')], verbose_name='Login'), + ), + ] -- libgit2 0.21.2