From fa30fa76b7cdfbb7fd0ceff4f475f1727c276c7a Mon Sep 17 00:00:00 2001 From: fbormann Date: Mon, 12 Sep 2016 12:30:43 -0300 Subject: [PATCH] Improved and made Mixin 100% functional, still need decorator and improve messages #41 --- app/views.py | 35 +++-------------------------------- core/migrations/0005_resource_link.py | 20 ++++++++++++++++++++ core/migrations/0006_auto_20160912_1138.py | 20 ++++++++++++++++++++ core/migrations/0007_remove_resource_link.py | 19 +++++++++++++++++++ core/migrations/0008_resource_link.py | 20 ++++++++++++++++++++ core/mixins.py | 27 ++++++++++++++------------- core/models.py | 5 ++++- core/templates/base.html | 4 ++-- core/views.py | 3 ++- courses/views.py | 11 ++++++----- 10 files changed, 110 insertions(+), 54 deletions(-) create mode 100644 core/migrations/0005_resource_link.py create mode 100644 core/migrations/0006_auto_20160912_1138.py create mode 100644 core/migrations/0007_remove_resource_link.py create mode 100644 core/migrations/0008_resource_link.py diff --git a/app/views.py b/app/views.py index 549d106..a752d98 100644 --- a/app/views.py +++ b/app/views.py @@ -28,40 +28,11 @@ class AppIndex(LoginRequiredMixin, LogMixin, TemplateView, NotificationMixin): context['courses'] = Course.objects.filter(user = self.request.user) context['title'] = 'Amadeus' - self.createNotification("teste") - notifications = Notification.objects.filter(user= self.request.user, read=False) + #super(AppIndex, self).createNotification("teste", not_resource="home", resource_link="users") + notifications = Notification.objects.filter(user= self.request.user, read=False) context['notifications'] = notifications - print(notifications) - + return self.response_class(request = self.request, template = template, context = context, using = self.template_engine, **response_kwargs) - def createNotification(self, mensagem='', actor=None, users = User.objects.all(), not_action = '', not_resource=''): #the default will be a broadcast - action = Action.objects.filter(name = not_action) - resource = Resource.objects.filter(name = self.not_resource) - print(action) - if not action: - action = Action(name = self.not_action) - action.save() - else: - action = action[0] - - if not resource: - resource = Resource(name = self.not_resource) - resource.save() - else: - resource = resource[0] - - action_resource = Action_Resource.objects.filter(action = action, resource = resource) - - if not action_resource: - action_resource = Action_Resource(action = action, resource = resource) - action_resource.save() - else: - action_resource = action_resource[0] - - for user in users: - print(user) - notification = Notification(user=user, actor= actor, message=mensagem, action_resource= action_resource) - notification.save() diff --git a/core/migrations/0005_resource_link.py b/core/migrations/0005_resource_link.py new file mode 100644 index 0000000..cef38e6 --- /dev/null +++ b/core/migrations/0005_resource_link.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-12 14:36 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0004_auto_20160908_1151'), + ] + + operations = [ + migrations.AddField( + model_name='resource', + name='link', + field=models.CharField(default='', max_length=100, unique=True, verbose_name='URL'), + ), + ] diff --git a/core/migrations/0006_auto_20160912_1138.py b/core/migrations/0006_auto_20160912_1138.py new file mode 100644 index 0000000..dc49559 --- /dev/null +++ b/core/migrations/0006_auto_20160912_1138.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-12 14:38 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0005_resource_link'), + ] + + operations = [ + migrations.AlterField( + model_name='resource', + name='link', + field=models.CharField(default='', max_length=100, verbose_name='URL'), + ), + ] diff --git a/core/migrations/0007_remove_resource_link.py b/core/migrations/0007_remove_resource_link.py new file mode 100644 index 0000000..c49aa70 --- /dev/null +++ b/core/migrations/0007_remove_resource_link.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-12 14:51 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0006_auto_20160912_1138'), + ] + + operations = [ + migrations.RemoveField( + model_name='resource', + name='link', + ), + ] diff --git a/core/migrations/0008_resource_link.py b/core/migrations/0008_resource_link.py new file mode 100644 index 0000000..906bd52 --- /dev/null +++ b/core/migrations/0008_resource_link.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2016-09-12 14:52 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0007_remove_resource_link'), + ] + + operations = [ + migrations.AddField( + model_name='resource', + name='link', + field=models.CharField(default='', max_length=100, verbose_name='URL'), + ), + ] diff --git a/core/mixins.py b/core/mixins.py index 0c3ab37..8f675d0 100644 --- a/core/mixins.py +++ b/core/mixins.py @@ -44,32 +44,33 @@ class NotificationMixin(object): not_action = '' not_resource = '' - def createNotification(message='', actor=None, users = User.objects.all(), not_action = '', not_resource=''): #the default will be a broadcast + def createNotification(self, message='', actor=None, users = User.objects.all(), not_action = '', not_resource='', resource_link=''): #the default will be a broadcast action = Action.objects.filter(name = self.not_action) - resource = Resource.objects.filter(name = self.not_resource) - - if not action: + resource = Resource.objects.filter(name = not_resource) + + if action.exists(): + action = action[0] + else: action = Action(name = self.not_action) action.save() - else: - action = action[0] - if not resource: - resource = Resource(name = self.not_resource) - resource.save() - else: + if resource.exists(): resource = resource[0] + else: + resource = Resource(name = self.not_resource, link= resource_link) + resource.save() action_resource = Action_Resource.objects.filter(action = action, resource = resource) - if not action_resource: + if action_resource.exists(): + action_resource = action_resource[0] + else: action_resource = Action_Resource(action = action, resource = resource) action_resource.save() - else: - action_resource = action_resource[0] for user in users: notification = Notification(user=user, actor= actor, message=message, action_resource= action_resource) + notification.save() def dispatch(self, request, *args, **kwargs): diff --git a/core/models.py b/core/models.py index b6bb996..dae16ac 100644 --- a/core/models.py +++ b/core/models.py @@ -29,11 +29,14 @@ class Resource(models.Model): @name: name of the resource affected, it will be unique because a resource can be affecte by a huge amount of actions @created_date: The date the resource was created + @link: Which URL made that resource able to find """ name = models.CharField(_('Name'), max_length =100, unique=True) created_date = models.DateField(_('Created Date'), auto_now_add=True) - + link = models.CharField(_('URL'), max_length =100, default="") + + class Meta: verbose_name = "Resource" verbose_name_plural = "Resources" diff --git a/core/templates/base.html b/core/templates/base.html index 4bf9d28..7b09181 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -59,7 +59,7 @@ {% for notification in notifications %} {% if notification.actor %}
  • -
  • {% else %}
  • - +
    folder diff --git a/core/views.py b/core/views.py index fc7ec04..b9f753c 100644 --- a/core/views.py +++ b/core/views.py @@ -9,6 +9,7 @@ from django.views.generic import CreateView, UpdateView from django.http import HttpResponse from django.core.mail import send_mail,BadHeaderError from django.conf import settings +from core.mixins import NotificationMixin from rolepermissions.shortcuts import assign_role @@ -25,7 +26,7 @@ def index(request): return render(request, "index.html", context) -class RegisterUser(CreateView): +class RegisterUser(CreateView, NotificationMixin): model = User form_class = RegisterUserForm template_name = 'register_user.html' diff --git a/courses/views.py b/courses/views.py index 1303442..73c36cb 100644 --- a/courses/views.py +++ b/courses/views.py @@ -13,9 +13,10 @@ from django.db.models import Q from .forms import CourseForm, CategoryForm, SubjectForm,TopicForm from .models import Course, Subject, Category,Topic +from core.mixins import NotificationMixin -class IndexView(LoginRequiredMixin, generic.ListView): +class IndexView(LoginRequiredMixin, NotificationMixin, generic.ListView): login_url = reverse_lazy("core:home") redirect_field_name = 'next' @@ -30,7 +31,7 @@ class IndexView(LoginRequiredMixin, generic.ListView): return context -class CreateView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView): +class CreateView(LoginRequiredMixin, HasRoleMixin, NotificationMixin,generic.edit.CreateView): allowed_roles = ['professor', 'system_admin'] login_url = reverse_lazy("core:home") @@ -73,7 +74,7 @@ class UpdateView(LoginRequiredMixin, HasRoleMixin, generic.UpdateView): return self.response_class(request=self.request, template=self.get_template_names(), context=context, using=self.template_engine) -class View(LoginRequiredMixin, generic.DetailView): +class View(LoginRequiredMixin, NotificationMixin, generic.DetailView): login_url = reverse_lazy("core:home") redirect_field_name = 'next' @@ -89,7 +90,7 @@ class View(LoginRequiredMixin, generic.DetailView): return context -class DeleteView(LoginRequiredMixin, HasRoleMixin, generic.DeleteView): +class DeleteView(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.DeleteView): allowed_roles = ['professor', 'system_admin'] login_url = reverse_lazy("core:home") @@ -220,7 +221,7 @@ class SubjectsView(LoginRequiredMixin, generic.ListView): context['topics'] = subject.topics.all() return context -class CreateTopicView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView): +class CreateTopicView(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.edit.CreateView): allowed_roles = ['professor', 'system_admin','student'] login_url = reverse_lazy("core:home") -- libgit2 0.21.2