Commit 0f3dd7a8cb98770a791e0edd28f02a5b9c726a0d

Authored by Zambom
1 parent b737ff62

Adding category pendencies page

amadeus/permissions.py
1 1 # File used to store functions to handle permissions
2 2  
  3 +from subjects.models import Subject
3 4 from topics.models import Resource
4 5  
5 6 """
... ... @@ -20,6 +21,10 @@ def has_subject_permissions(user, subject):
20 21  
21 22 return False
22 23  
  24 +"""
  25 + Function to know if user has permission to:
  26 + - See subject
  27 +"""
23 28 def has_subject_view_permissions(user, subject):
24 29 if has_subject_permissions(user, subject):
25 30 return True
... ... @@ -30,6 +35,14 @@ def has_subject_view_permissions(user, subject):
30 35 return False
31 36  
32 37 """
  38 + Function to know if user is student of some subject in category
  39 +"""
  40 +def has_category_permission(user, cat_slug):
  41 + exist = Subject.objects.filter(students__id = user.id, category__slug = cat_slug).exists()
  42 +
  43 + return exist
  44 +
  45 +"""
33 46 Function to know if user has permission to:
34 47 - Access Resource
35 48 """
... ...
notifications/templates/notifications/index.html
... ... @@ -6,13 +6,18 @@
6 6 {% block breadcrumbs %}
7 7 {{ block.super }}
8 8  
  9 + {% if category %}
  10 + {% breadcrumb category 'subjects:cat_view' category.slug %}
  11 + {% endif %}
  12 +
9 13 {% breadcrumb 'Pendencies' 'notifications:manage' %}
  14 +
10 15 {% endblock %}
11 16  
12 17 {% block content %}
13 18 <input type="hidden" id="pend_url" value="{% url 'notifications:manage' %}" />
14   -
15 19 {% if notifications.count > 0 %}
  20 + <p>{% trans 'You got pendencies in the following subjects' %}: </p>
16 21 <div class="panel-group" id="subject-accordion" role="tablist" aria-multiselectable="true">
17 22 {% for notification in notifications %}
18 23 <div class="panel panel-info subject-panel">
... ...
notifications/urls.py
... ... @@ -3,6 +3,7 @@ from . import views
3 3  
4 4 urlpatterns = [
5 5 url(r'^$', views.IndexView.as_view(), name='manage'),
  6 + url(r'^category/(?P<slug>[\w_-]+)/$', views.IndexView.as_view(), name='manage_cat'),
6 7 url(r'^set_goal/$', views.set_goal, name='set_goal'),
7 8 url(r'^ajax/(?P<id>[\w_-]+)/$', views.AjaxNotifications.as_view(), name='ajax_view'),
8 9 url(r'^ajax_history/(?P<id>[\w_-]+)/$', views.AjaxHistory.as_view(), name='ajax_history'),
... ...
notifications/views.py
... ... @@ -12,9 +12,10 @@ from dateutil import parser
12 12 from datetime import datetime
13 13 from django.utils import formats, timezone
14 14  
15   -from amadeus.permissions import has_subject_view_permissions
  15 +from amadeus.permissions import has_subject_view_permissions, has_category_permission
16 16  
17 17 from subjects.models import Subject
  18 +from categories.models import Category
18 19  
19 20 from .models import Notification
20 21 from .utils import get_order_by, is_date
... ... @@ -43,6 +44,8 @@ class SubjectNotifications(LoginRequiredMixin, generic.ListView):
43 44  
44 45 notifications = Notification.objects.filter(user = self.request.user, task__resource__topic__subject = subject, creation_date = datetime.now()).order_by("task__limit_date", "task__end_date")
45 46  
  47 + notifications.update(viewed = True)
  48 +
46 49 self.total = notifications.count()
47 50  
48 51 return notifications
... ... @@ -134,8 +137,22 @@ class IndexView(LoginRequiredMixin, generic.ListView):
134 137 template_name = 'notifications/index.html'
135 138 paginate_by = 10
136 139  
  140 + def dispatch(self, request, *args, **kwargs):
  141 + cat = self.kwargs.get('slug', None)
  142 +
  143 + if cat:
  144 + if not has_category_permission(request.user, cat):
  145 + return redirect(reverse_lazy('subjects:home'))
  146 +
  147 + return super(IndexView, self).dispatch(request, *args, **kwargs)
  148 +
137 149 def get_queryset(self):
138   - notifications = Notification.objects.filter(user = self.request.user, viewed = False, creation_date = datetime.now()).values('task__resource__topic__subject', 'task__resource__topic__subject__name').annotate(total = Count('task__resource__topic__subject'))
  150 + cat = self.kwargs.get('slug', None)
  151 +
  152 + if cat:
  153 + notifications = Notification.objects.filter(user = self.request.user, creation_date = datetime.now(), task__resource__topic__subject__category__slug = cat).values('task__resource__topic__subject', 'task__resource__topic__subject__name').annotate(total = Count('task__resource__topic__subject'))
  154 + else:
  155 + notifications = Notification.objects.filter(user = self.request.user, creation_date = datetime.now()).values('task__resource__topic__subject', 'task__resource__topic__subject__name').annotate(total = Count('task__resource__topic__subject'))
139 156  
140 157 return notifications
141 158  
... ... @@ -143,7 +160,14 @@ class IndexView(LoginRequiredMixin, generic.ListView):
143 160 context = super(IndexView, self).get_context_data(**kwargs)
144 161  
145 162 context['title'] = _('Pendencies')
146   - context['pendencies_menu_active'] = "subjects_menu_active"
  163 +
  164 + cat = self.kwargs.get('slug', None)
  165 +
  166 + if cat:
  167 + context['category'] = get_object_or_404(Category, slug = cat)
  168 + else:
  169 + context['pendencies_menu_active'] = "subjects_menu_active"
  170 +
147 171  
148 172 return context
149 173  
... ... @@ -159,6 +183,8 @@ class AjaxNotifications(LoginRequiredMixin, generic.ListView):
159 183  
160 184 notifications = Notification.objects.filter(user = self.request.user, task__resource__topic__subject__id = subject_id, creation_date = datetime.now()).order_by("task__limit_date", "task__end_date")
161 185  
  186 + notifications.update(viewed = True)
  187 +
162 188 return notifications
163 189  
164 190 class AjaxHistory(LoginRequiredMixin, generic.ListView):
... ...
subjects/templates/subjects/list.html
... ... @@ -81,7 +81,7 @@
81 81 {% endif %}
82 82  
83 83 <a href="" class="pull-right action_icon"><i class="fa fa-bar-chart" aria-hidden="true"></i></a>
84   - <a href="" class="pull-right action_icon">
  84 + <a href="{% url 'notifications:manage_cat' category.slug %}" class="pull-right action_icon">
85 85 <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
86 86 {% notifies_cat_number category request.user %}
87 87 </a>
... ...