Commit fa30fa76b7cdfbb7fd0ceff4f475f1727c276c7a

Authored by fbormann
1 parent d6f77fae

Improved and made Mixin 100% functional, still need decorator and improve messages #41

app/views.py
... ... @@ -28,40 +28,11 @@ class AppIndex(LoginRequiredMixin, LogMixin, TemplateView, NotificationMixin):
28 28 context['courses'] = Course.objects.filter(user = self.request.user)
29 29  
30 30 context['title'] = 'Amadeus'
31   - self.createNotification("teste")
32   - notifications = Notification.objects.filter(user= self.request.user, read=False)
  31 + #super(AppIndex, self).createNotification("teste", not_resource="home", resource_link="users")
33 32  
  33 + notifications = Notification.objects.filter(user= self.request.user, read=False)
34 34 context['notifications'] = notifications
35   - print(notifications)
36   -
  35 +
37 36 return self.response_class(request = self.request, template = template, context = context, using = self.template_engine, **response_kwargs)
38 37  
39   - def createNotification(self, mensagem='', actor=None, users = User.objects.all(), not_action = '', not_resource=''): #the default will be a broadcast
40   - action = Action.objects.filter(name = not_action)
41   - resource = Resource.objects.filter(name = self.not_resource)
42   - print(action)
43   - if not action:
44   - action = Action(name = self.not_action)
45   - action.save()
46   - else:
47   - action = action[0]
48   -
49   - if not resource:
50   - resource = Resource(name = self.not_resource)
51   - resource.save()
52   - else:
53   - resource = resource[0]
54   -
55   - action_resource = Action_Resource.objects.filter(action = action, resource = resource)
56   -
57   - if not action_resource:
58   - action_resource = Action_Resource(action = action, resource = resource)
59   - action_resource.save()
60   - else:
61   - action_resource = action_resource[0]
62   -
63   - for user in users:
64   - print(user)
65   - notification = Notification(user=user, actor= actor, message=mensagem, action_resource= action_resource)
66   - notification.save()
67 38  
... ...
core/migrations/0005_resource_link.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10 on 2016-09-12 14:36
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('core', '0004_auto_20160908_1151'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AddField(
  16 + model_name='resource',
  17 + name='link',
  18 + field=models.CharField(default='', max_length=100, unique=True, verbose_name='URL'),
  19 + ),
  20 + ]
... ...
core/migrations/0006_auto_20160912_1138.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10 on 2016-09-12 14:38
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('core', '0005_resource_link'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AlterField(
  16 + model_name='resource',
  17 + name='link',
  18 + field=models.CharField(default='', max_length=100, verbose_name='URL'),
  19 + ),
  20 + ]
... ...
core/migrations/0007_remove_resource_link.py 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10 on 2016-09-12 14:51
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('core', '0006_auto_20160912_1138'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.RemoveField(
  16 + model_name='resource',
  17 + name='link',
  18 + ),
  19 + ]
... ...
core/migrations/0008_resource_link.py 0 → 100644
... ... @@ -0,0 +1,20 @@
  1 +# -*- coding: utf-8 -*-
  2 +# Generated by Django 1.10 on 2016-09-12 14:52
  3 +from __future__ import unicode_literals
  4 +
  5 +from django.db import migrations, models
  6 +
  7 +
  8 +class Migration(migrations.Migration):
  9 +
  10 + dependencies = [
  11 + ('core', '0007_remove_resource_link'),
  12 + ]
  13 +
  14 + operations = [
  15 + migrations.AddField(
  16 + model_name='resource',
  17 + name='link',
  18 + field=models.CharField(default='', max_length=100, verbose_name='URL'),
  19 + ),
  20 + ]
... ...
core/mixins.py
... ... @@ -44,32 +44,33 @@ class NotificationMixin(object):
44 44 not_action = ''
45 45 not_resource = ''
46 46  
47   - def createNotification(message='', actor=None, users = User.objects.all(), not_action = '', not_resource=''): #the default will be a broadcast
  47 + def createNotification(self, message='', actor=None, users = User.objects.all(), not_action = '', not_resource='', resource_link=''): #the default will be a broadcast
48 48 action = Action.objects.filter(name = self.not_action)
49   - resource = Resource.objects.filter(name = self.not_resource)
50   -
51   - if not action:
  49 + resource = Resource.objects.filter(name = not_resource)
  50 +
  51 + if action.exists():
  52 + action = action[0]
  53 + else:
52 54 action = Action(name = self.not_action)
53 55 action.save()
54   - else:
55   - action = action[0]
56 56  
57   - if not resource:
58   - resource = Resource(name = self.not_resource)
59   - resource.save()
60   - else:
  57 + if resource.exists():
61 58 resource = resource[0]
  59 + else:
  60 + resource = Resource(name = self.not_resource, link= resource_link)
  61 + resource.save()
62 62  
63 63 action_resource = Action_Resource.objects.filter(action = action, resource = resource)
64 64  
65   - if not action_resource:
  65 + if action_resource.exists():
  66 + action_resource = action_resource[0]
  67 + else:
66 68 action_resource = Action_Resource(action = action, resource = resource)
67 69 action_resource.save()
68   - else:
69   - action_resource = action_resource[0]
70 70  
71 71 for user in users:
72 72 notification = Notification(user=user, actor= actor, message=message, action_resource= action_resource)
  73 + notification.save()
73 74  
74 75  
75 76 def dispatch(self, request, *args, **kwargs):
... ...
core/models.py
... ... @@ -29,11 +29,14 @@ class Resource(models.Model):
29 29 @name: name of the resource affected, it will be unique because a resource can be affecte
30 30 by a huge amount of actions
31 31 @created_date: The date the resource was created
  32 + @link: Which URL made that resource able to find
32 33 """
33 34  
34 35 name = models.CharField(_('Name'), max_length =100, unique=True)
35 36 created_date = models.DateField(_('Created Date'), auto_now_add=True)
36   -
  37 + link = models.CharField(_('URL'), max_length =100, default="")
  38 +
  39 +
37 40 class Meta:
38 41 verbose_name = "Resource"
39 42 verbose_name_plural = "Resources"
... ...
core/templates/base.html
... ... @@ -59,7 +59,7 @@
59 59 {% for notification in notifications %}
60 60 {% if notification.actor %} <!-- if the notification has a user-->
61 61 <li>
62   - <a><div class="list-group-item">
  62 + <a href="{{ notification.action_resource.resource.link }}"><div class="list-group-item">
63 63 <div class="row-picture">
64 64 <img class="circle" src="http://lorempixel.com/56/56/people/1" alt="icon">
65 65 <div class="least-content pull-right">{{ notification.datetime }}</div>
... ... @@ -72,7 +72,7 @@
72 72 </li>
73 73 {% else %}
74 74 <li>
75   - <a>
  75 + <a href="{{ notification.action_resource.resource.link }}">
76 76 <div class="list-group-item">
77 77 <div class="row-action-primary">
78 78 <i class="material-icons">folder</i>
... ...
core/views.py
... ... @@ -9,6 +9,7 @@ from django.views.generic import CreateView, UpdateView
9 9 from django.http import HttpResponse
10 10 from django.core.mail import send_mail,BadHeaderError
11 11 from django.conf import settings
  12 +from core.mixins import NotificationMixin
12 13  
13 14 from rolepermissions.shortcuts import assign_role
14 15  
... ... @@ -25,7 +26,7 @@ def index(request):
25 26 return render(request, "index.html", context)
26 27  
27 28  
28   -class RegisterUser(CreateView):
  29 +class RegisterUser(CreateView, NotificationMixin):
29 30 model = User
30 31 form_class = RegisterUserForm
31 32 template_name = 'register_user.html'
... ...
courses/views.py
... ... @@ -13,9 +13,10 @@ from django.db.models import Q
13 13  
14 14 from .forms import CourseForm, CategoryForm, SubjectForm,TopicForm
15 15 from .models import Course, Subject, Category,Topic
  16 +from core.mixins import NotificationMixin
16 17  
17 18  
18   -class IndexView(LoginRequiredMixin, generic.ListView):
  19 +class IndexView(LoginRequiredMixin, NotificationMixin, generic.ListView):
19 20  
20 21 login_url = reverse_lazy("core:home")
21 22 redirect_field_name = 'next'
... ... @@ -30,7 +31,7 @@ class IndexView(LoginRequiredMixin, generic.ListView):
30 31  
31 32 return context
32 33  
33   -class CreateView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView):
  34 +class CreateView(LoginRequiredMixin, HasRoleMixin, NotificationMixin,generic.edit.CreateView):
34 35  
35 36 allowed_roles = ['professor', 'system_admin']
36 37 login_url = reverse_lazy("core:home")
... ... @@ -73,7 +74,7 @@ class UpdateView(LoginRequiredMixin, HasRoleMixin, generic.UpdateView):
73 74  
74 75 return self.response_class(request=self.request, template=self.get_template_names(), context=context, using=self.template_engine)
75 76  
76   -class View(LoginRequiredMixin, generic.DetailView):
  77 +class View(LoginRequiredMixin, NotificationMixin, generic.DetailView):
77 78  
78 79 login_url = reverse_lazy("core:home")
79 80 redirect_field_name = 'next'
... ... @@ -89,7 +90,7 @@ class View(LoginRequiredMixin, generic.DetailView):
89 90  
90 91 return context
91 92  
92   -class DeleteView(LoginRequiredMixin, HasRoleMixin, generic.DeleteView):
  93 +class DeleteView(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.DeleteView):
93 94  
94 95 allowed_roles = ['professor', 'system_admin']
95 96 login_url = reverse_lazy("core:home")
... ... @@ -220,7 +221,7 @@ class SubjectsView(LoginRequiredMixin, generic.ListView):
220 221 context['topics'] = subject.topics.all()
221 222 return context
222 223  
223   -class CreateTopicView(LoginRequiredMixin, HasRoleMixin, generic.edit.CreateView):
  224 +class CreateTopicView(LoginRequiredMixin, HasRoleMixin, NotificationMixin, generic.edit.CreateView):
224 225  
225 226 allowed_roles = ['professor', 'system_admin','student']
226 227 login_url = reverse_lazy("core:home")
... ...