Commit e921c7b5b8b8ccf62705ecba480d987f7be1572e
1 parent
eee1540e
Exists in
master
and in
3 other branches
added initial view with subjects pagination, still have to add users-cloud and pagination css
Showing
5 changed files
with
78 additions
and
4 deletions
Show diff stats
amadeus/views.py
... | ... | @@ -4,6 +4,6 @@ from django.urls import reverse_lazy |
4 | 4 | |
5 | 5 | def index(request): |
6 | 6 | if request.user.is_authenticated: |
7 | - return redirect(reverse_lazy("subjects:index")) | |
7 | + return redirect(reverse_lazy("subjects:home")) | |
8 | 8 | else: |
9 | 9 | return redirect('users:login') |
10 | 10 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,10 @@ |
1 | +from django.core import serializers | |
2 | +import os | |
3 | + | |
4 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "amadeus.settings") | |
5 | + | |
6 | +from subjects.models import Subject | |
7 | + | |
8 | + | |
9 | +data = serializers.serialize("json", Subject.objects.all()) | |
10 | +print(data) | |
0 | 11 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,31 @@ |
1 | +{% extends "categories/home.html" %} | |
2 | + | |
3 | +{% block content %} | |
4 | + | |
5 | + <div id="cloud-users" class="col-md-12"> | |
6 | + | |
7 | + </div> | |
8 | + <div class="col-md-12 cards-content"> | |
9 | + <div class="panel-group" id="subject-accordion" role="tablist" aria-multiselectable="true"> | |
10 | + {% for subject in subjects %} | |
11 | + {% include "subjects/subject_card.html" %} | |
12 | + {% endfor %} | |
13 | + </div> | |
14 | + </div> | |
15 | + | |
16 | + | |
17 | + <div class="pagination"> | |
18 | + <span class="page-links"> | |
19 | + {% if subjects.has_previous %} | |
20 | + <a href="?page={{ subjects.previous_page_number }}">previous</a> | |
21 | + {% endif %} | |
22 | + <span class="page-current"> | |
23 | + Page {{ subjects.number }} of {{ subjects.paginator.num_pages }}. | |
24 | + </span> | |
25 | + {% if subjects.has_next %} | |
26 | + <a href="?page={{ subjects.next_page_number }}">next</a> | |
27 | + {% endif %} | |
28 | + </span> | |
29 | + </div> | |
30 | + | |
31 | +{% endblock content %} | |
0 | 32 | \ No newline at end of file | ... | ... |
subjects/urls.py
... | ... | @@ -2,6 +2,7 @@ from django.conf.urls import url |
2 | 2 | from . import views |
3 | 3 | |
4 | 4 | urlpatterns = [ |
5 | + url(r'^home/$', views.HomeView.as_view(), name='home'), | |
5 | 6 | url(r'^$', views.IndexView.as_view(), name='index'), |
6 | 7 | url(r'^(?P<option>[\w_-]+)/$', views.IndexView.as_view(), name='index'), |
7 | 8 | url(r'^create/(?P<slug>[\w_-]+)/$', views.SubjectCreateView.as_view(), name='create'), | ... | ... |
subjects/views.py
... | ... | @@ -22,11 +22,43 @@ from log.decorators import log_decorator_ajax |
22 | 22 | from log.models import Log |
23 | 23 | |
24 | 24 | import time |
25 | - | |
25 | +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | |
26 | 26 | from .forms import CreateSubjectForm |
27 | 27 | from users.models import User |
28 | 28 | |
29 | 29 | |
30 | +class HomeView(LoginRequiredMixin, ListView): | |
31 | + login_url = reverse_lazy("users:login") | |
32 | + redirect_field_name = 'next' | |
33 | + queryset = Subject.objects.all() | |
34 | + template_name = 'subjects/initial.html' | |
35 | + | |
36 | + | |
37 | + def get_context_data(self, **kwargs): | |
38 | + context = super(HomeView, self).get_context_data(**kwargs) | |
39 | + if self.request.user.is_staff: | |
40 | + subjects = Subject.objects.all() | |
41 | + else: | |
42 | + subjects = Subject.objects.all() | |
43 | + subjects = [subject for subject in subjects if self.request.user in subject.students.all() or self.request.user in subject.professor.all()] | |
44 | + | |
45 | + paginator = Paginator(subjects, 2) | |
46 | + | |
47 | + page = self.request.GET.get('page') | |
48 | + try: | |
49 | + subjects = paginator.page(page) | |
50 | + except PageNotAnInteger: | |
51 | + # If page is not an integer, deliver first page. | |
52 | + subjects = paginator.page(1) | |
53 | + | |
54 | + except EmptyPage: | |
55 | + # If page is out of range (e.g. 9999), deliver last page of results. | |
56 | + subjects = paginator.page(paginator.num_pages) | |
57 | + | |
58 | + context['subjects'] = subjects | |
59 | + return context | |
60 | + | |
61 | + | |
30 | 62 | class IndexView(LoginRequiredMixin, ListView): |
31 | 63 | |
32 | 64 | login_url = reverse_lazy("users:login") |
... | ... | @@ -65,12 +97,12 @@ class IndexView(LoginRequiredMixin, ListView): |
65 | 97 | categories = self.get_queryset().order_by('name').filter(visible=True) |
66 | 98 | context['all'] = True |
67 | 99 | for category in categories: |
68 | - category.subjects = Subject.objects.all().filter(category= category) | |
100 | + category.subjects = Subject.objects.filter(category= category) | |
69 | 101 | else: |
70 | 102 | context['all'] = False |
71 | 103 | categories = self.get_queryset().filter(visible=True) |
72 | 104 | for category in categories: |
73 | - category.subjects = Subject.objects.all().filter(category= category) | |
105 | + category.subjects = Subject.objects.filter(category= category) | |
74 | 106 | |
75 | 107 | categories = [category for category in categories if category.subjects.count() > 0 or self.request.user in category.coordinators.all()] |
76 | 108 | #So I remove all categories that doesn't have the possibility for the user to be on | ... | ... |