From 4dc3a1374e717e3fe9be7be890befe52bf3c055f Mon Sep 17 00:00:00 2001 From: fbormann Date: Thu, 8 Sep 2016 23:47:53 -0300 Subject: [PATCH] modified header to accept notifications and created notification system, stil has bugs from Python 2.7 implementation on inheritance, had to duplicate code --- amadeus/settings.py | 4 ++-- app/views.py | 47 +++++++++++++++++++++++++++++++++++++++++++---- core/decorators.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- core/mixins.py | 41 ++++++++++++++++++++++++++++++++--------- core/static/css/base/header.css | 11 ++++++++++- core/static/js/base/header.js | 3 +++ core/templates/base.html | 50 +++++++++++++++++++++++++++++++++++++++++++++----- core/views.py | 5 ++++- 8 files changed, 185 insertions(+), 23 deletions(-) create mode 100644 core/static/js/base/header.js diff --git a/amadeus/settings.py b/amadeus/settings.py index c1c756c..27d6a94 100644 --- a/amadeus/settings.py +++ b/amadeus/settings.py @@ -88,8 +88,8 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'amadeus', - 'USER': 'amadeus_admin', - 'PASSWORD': 'amadeus', + 'USER': 'postgres', + 'PASSWORD': 'felipe', 'HOST': '127.0.0.1', 'PORT': '5432', } diff --git a/app/views.py b/app/views.py index 7064edf..549d106 100644 --- a/app/views.py +++ b/app/views.py @@ -2,17 +2,21 @@ from django.shortcuts import render from django.views.generic import TemplateView from django.contrib.auth.mixins import LoginRequiredMixin from django.core.urlresolvers import reverse_lazy -from core.mixins import LogMixin - +from core.mixins import LogMixin, NotificationMixin +from core.models import Notification, Action, Resource, Action_Resource +from users.models import User from courses.models import Course -class AppIndex(LoginRequiredMixin, LogMixin, TemplateView): +class AppIndex(LoginRequiredMixin, LogMixin, TemplateView, NotificationMixin): log_action = "Acessar" log_resource = "Home" login_url = reverse_lazy("core:home") redirect_field_name = 'next' template_name = "home_professor.html" + not_action = "Acessar" + not_resource = "home" + def render_to_response(self, context, **response_kwargs): context = {} @@ -24,5 +28,40 @@ class AppIndex(LoginRequiredMixin, LogMixin, TemplateView): 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) + + 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() - return self.response_class(request = self.request, template = template, context = context, using = self.template_engine, **response_kwargs) \ No newline at end of file diff --git a/core/decorators.py b/core/decorators.py index bf4fc07..8c90c66 100644 --- a/core/decorators.py +++ b/core/decorators.py @@ -1,6 +1,6 @@ from django.conf import settings from functools import wraps -from .models import Action, Resource, Action_Resource, Log +from .models import Action, Resource, Action_Resource, Log, Notification def log_decorator(log_action = '', log_resource = ''): @@ -45,3 +45,48 @@ def log_decorator(log_action = '', log_resource = ''): return wraps(view_function)(_decorator) return _log_decorator + + +def notification_decorator(read = False, message = '', actor = None, users = [], not_action='', not_resource=''): + + def _notification_decorator(view_function): + + def _decorator(request, *args, **kwargs): + #Do something before the call + + response = view_function(request, *args, **kwargs) + action = Action.objects.filter(name = not_action) + resource = Resource.objects.filter(name = not_resource) + + if not action: + action = Action(name = not_action) + action.save() + else: + action = action[0] + + if not resource: + resource = Resource(name = 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] + + if request.user.is_authenticated: #the user was authenticated by the view + notification = Notification(actor = request.user, message= message, + action_resource = action_resource, user = request.user) + + + + #Do something after the call + return response + + return wraps(view_function)(_decorator) + + return _notification_decorator \ No newline at end of file diff --git a/core/mixins.py b/core/mixins.py index 682a377..d28599e 100644 --- a/core/mixins.py +++ b/core/mixins.py @@ -1,5 +1,6 @@ from django.conf import settings from .models import Action, Resource, Action_Resource, Log, Notification +from users.models import User class LogMixin(object): log_action = "" @@ -40,16 +41,38 @@ class LogMixin(object): class NotificationMixin(object): message = "" read = False + not_action = '' + not_resource = '' - def dispatch(self, request, *args, **kwargs): - action = Action.objects.filter(name = self.log_action) - resource = Resource.objects.filter(name = self.log_resource) + def createNotification(message='', actor=None, users = User.objects.all(), not_action = '', not_resource=''): #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: + 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] - action_resource = Action_Resource.objects.filter(action = action, resource = resource)[0] + for user in users: + notification = Notification(user=user, actor= actor, message=message, action_resource= action_resource) - 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 = "" + def dispatch(self, request, *args, **kwargs): + """ + Not quite sure how to do about it""" + return super(NotificationMixin, self).dispatch(request, *args, **kwargs) \ No newline at end of file diff --git a/core/static/css/base/header.css b/core/static/css/base/header.css index 12870ff..0b2c650 100644 --- a/core/static/css/base/header.css +++ b/core/static/css/base/header.css @@ -13,10 +13,19 @@ margin-left: 30%; } -/* ID */ +.link{ + transition-duration: 0.5s; /*Slow down the transformation*/ +} + +.link:hover{ + transform:scale(1.1); +} .notifications{ font-size: 20px; } +/* ID */ + + diff --git a/core/static/js/base/header.js b/core/static/js/base/header.js new file mode 100644 index 0000000..08c7fbb --- /dev/null +++ b/core/static/js/base/header.js @@ -0,0 +1,3 @@ +$(document).ready(function(){ + $('[data-toggle="tooltip"]').tooltip(); //activate tooltip on all elements that has attribute data-toggle +}); \ No newline at end of file diff --git a/core/templates/base.html b/core/templates/base.html index 28e2968..910c180 100644 --- a/core/templates/base.html +++ b/core/templates/base.html @@ -30,6 +30,7 @@ + {% block javascript %} @@ -51,16 +52,55 @@ - + {% endblock %}
diff --git a/core/views.py b/core/views.py index 5a37c1d..fc7ec04 100644 --- a/core/views.py +++ b/core/views.py @@ -13,16 +13,18 @@ from django.conf import settings from rolepermissions.shortcuts import assign_role from .forms import RegisterUserForm -from .decorators import log_decorator +from .decorators import log_decorator, notification_decorator from users.models import User + def index(request): context = { 'subscribed_courses': 'testando' } return render(request, "index.html", context) + class RegisterUser(CreateView): model = User form_class = RegisterUserForm @@ -64,6 +66,7 @@ def remember_password(request): context['danger'] = 'E-mail does not send' return render(request, "remember_password.html",context) +@notification_decorator(message='just connected') @log_decorator('Acessar', 'Sistema') def login(request): context = {} -- libgit2 0.21.2