Commit 780ddd533c22893e9d903531744c3550718b79d3

Authored by Zambom
1 parent 369dc542

Adding admin home content with infinite scroll. [Issue: #45]

app/templates/home.html
@@ -2,11 +2,59 @@ @@ -2,11 +2,59 @@
2 2
3 {% load static i18n django_bootstrap_breadcrumbs permission_tags %} 3 {% load static i18n django_bootstrap_breadcrumbs permission_tags %}
4 4
5 -{% block breadcrumbs %} 5 +{% block javascript %}
  6 + <script type="text/javascript">
  7 + var pageNum = {{ page_obj.number }}; // The latest page loaded
  8 + var hasNextPage = {{ paginator.num_pages }}; // Indicates whether to expect another page after this one
  9 + var baseUrl = '{% url "app:index" %}';
  10 +
  11 + // loadOnScroll handler
  12 + var loadOnScroll = function() {
  13 + // If the current scroll position is past out cutoff point...
  14 + if ($(window).scrollTop() > $(document).height() - ($(window).height()*3)) {
  15 + // temporarily unhook the scroll event watcher so we don't call a bunch of times in a row
  16 + $(window).unbind();
  17 + // execute the load function below that will visit the JSON feed and stuff data into the HTML
  18 + loadItems();
  19 + }
  20 + };
  21 +
  22 + var loadItems = function() {
  23 + // If the next page doesn't exist, just quit now
  24 + if (pageNum == hasNextPage) {
  25 + return false
  26 + }
  27 + // Update the page number
  28 + pageNum = pageNum + 1;
6 29
  30 + $("#loading").show();
  31 + // Configure the url we're about to hit
  32 + setTimeout(function (){
  33 + $.ajax({
  34 + url: baseUrl,
  35 + data: {'page': pageNum},
  36 + success: function(data) {
  37 + $("#loading").hide();
  38 +
  39 + $("#timeline").append(data);
  40 + },
  41 + complete: function(data, textStatus){
  42 + // Turn the scroll monitor back on
  43 + $(window).bind('scroll', loadOnScroll);
  44 + }
  45 + });
  46 + }, 1000)
  47 + };
  48 +
  49 + $(document).ready(function(){
  50 + $(window).bind('scroll', loadOnScroll);
  51 + });
  52 + </script>
  53 +{% endblock %}
  54 +
  55 +{% block breadcrumbs %}
7 {% clear_breadcrumbs %} 56 {% clear_breadcrumbs %}
8 {% breadcrumb 'Home' 'app:index' %} 57 {% breadcrumb 'Home' 'app:index' %}
9 -  
10 {% endblock %} 58 {% endblock %}
11 59
12 {% block render_breadcrumbs %} 60 {% block render_breadcrumbs %}
@@ -45,7 +93,7 @@ @@ -45,7 +93,7 @@
45 <li> <a href="{% url 'users:manage' %}">{% trans 'Manage Users' %}</a></li> 93 <li> <a href="{% url 'users:manage' %}">{% trans 'Manage Users' %}</a></li>
46 {% endif %} 94 {% endif %}
47 {% if user|has_role:'system_admin, professor' %} 95 {% if user|has_role:'system_admin, professor' %}
48 - <li> <a href="../../course/home_course.html">{% trans 'Manage Courses' %}</a></li> 96 + <li> <a href="{% url 'course:manage' %}">{% trans 'Manage Courses' %}</a></li>
49 {% endif %} 97 {% endif %}
50 </ul> 98 </ul>
51 </div> 99 </div>
@@ -53,27 +101,18 @@ @@ -53,27 +101,18 @@
53 {% endblock %} 101 {% endblock %}
54 102
55 {% block content %} 103 {% block content %}
56 - <h3>{% trans 'Courses' %}</h3>  
57 - {% if courses|length > 0 %}  
58 - {% for course in courses %}  
59 - <a href="{% url 'course:view' course.slug %}">  
60 - <div class="panel panel-default courseHome">  
61 - <div class="panel-body">  
62 - <p>{{ course }}</p>  
63 - </div>  
64 - <div class="panel-footer">  
65 - <ul>  
66 - <li>{% trans 'Students:' %} 5</li>  
67 - <li>{% trans 'Beginning:' %} {{ course.init_date }}</li>  
68 - <li>{% trans 'End:' %} {{ course.end_date }}</li>  
69 - </ul>  
70 - </div>  
71 - </div>  
72 - </a>  
73 - {% endfor %}  
74 - {% else %}  
75 - <p>{% trans "You didn't create any course yet." %}</p> 104 + {% if user|has_role:'system_admin' %}
  105 + <h3>{% trans 'Courses' %}</h3>
76 {% endif %} 106 {% endif %}
  107 +
  108 + <div id="timeline">
  109 + {% include page_template %}
  110 + </div>
  111 + <div id="loading" class="alert alert-primary" role="alert">
  112 + <center>
  113 + <span class="fa fa-spin fa-circle-o-notch"></span>
  114 + </center>
  115 + </div>
77 {% endblock %} 116 {% endblock %}
78 117
79 {% block rightbar %} 118 {% block rightbar %}
app/templates/home_admin_content.html 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +{% load i18n %}
  2 +
  3 +{% for course in courses %}
  4 + <div class="panel panel-default courseHome">
  5 + <div class="panel-body">
  6 + <p>{{ course }}</p>
  7 + </div>
  8 + <div class="panel-footer">
  9 + <ul>
  10 + <li>{% trans 'Students' %}: {{ course.max_students }}</li>
  11 + <li>{% trans 'Beginning' %}: {{ course.init_date }}</li>
  12 + <li>{% trans 'End' %}: {{ course.end_date }}</li>
  13 + </ul>
  14 + </div>
  15 + <button type="button" class="btn btn-outline-info">{% trans 'Edit' %}</button>
  16 + </div>
  17 +{% endfor %}
0 \ No newline at end of file 18 \ No newline at end of file
1 from django.shortcuts import render 1 from django.shortcuts import render
2 -from django.views.generic import TemplateView 2 +from django.views.generic import ListView
3 from django.contrib.auth.mixins import LoginRequiredMixin 3 from django.contrib.auth.mixins import LoginRequiredMixin
4 from django.core.urlresolvers import reverse_lazy 4 from django.core.urlresolvers import reverse_lazy
5 from core.mixins import LogMixin, NotificationMixin 5 from core.mixins import LogMixin, NotificationMixin
@@ -7,32 +7,42 @@ from core.models import Notification, Action, Resource, Action_Resource @@ -7,32 +7,42 @@ from core.models import Notification, Action, Resource, Action_Resource
7 from users.models import User 7 from users.models import User
8 from courses.models import Course 8 from courses.models import Course
9 9
10 -class AppIndex(LoginRequiredMixin, LogMixin, TemplateView, NotificationMixin): 10 +class AppIndex(LoginRequiredMixin, LogMixin, ListView, NotificationMixin):
11 log_action = "Acessar" 11 log_action = "Acessar"
12 log_resource = "Home" 12 log_resource = "Home"
13 - login_url = reverse_lazy("core:home") 13 +
  14 + login_url = reverse_lazy("core:home")
14 redirect_field_name = 'next' 15 redirect_field_name = 'next'
  16 +
15 template_name = "home.html" 17 template_name = "home.html"
  18 + context_object_name = 'courses'
  19 + paginate_by = 3
16 20
17 not_action = "Acessar" 21 not_action = "Acessar"
18 not_resource = "home" 22 not_resource = "home"
19 23
20 - def render_to_response(self, context, **response_kwargs):  
21 - context = {}  
22 -  
23 - if self.request.user.type_profile == 2:  
24 - template = "home_student.html"  
25 - context['courses'] = Course.objects.filter(user = self.request.user) 24 + def get_queryset(self):
  25 + if self.request.user.is_staff:
  26 + objects = Course.objects.all()
26 else: 27 else:
27 - template = self.get_template_names()  
28 - context['courses'] = Course.objects.filter(user = self.request.user) 28 + objects = Notification.objects.filter(user = self.request.user)
  29 +
  30 + return objects
29 31
  32 + def render_to_response(self, context, **response_kwargs):
  33 + if self.request.user.is_staff:
  34 + context['page_template'] = "home_admin_content.html"
  35 +
30 context['title'] = 'Amadeus' 36 context['title'] = 'Amadeus'
  37 +
  38 + if self.request.is_ajax():
  39 + self.template_name = "home_admin_content.html"
  40 +
31 #super(AppIndex, self).createNotification("teste", not_resource="home", resource_link="users") 41 #super(AppIndex, self).createNotification("teste", not_resource="home", resource_link="users")
32 42
33 notifications = Notification.objects.filter(user= self.request.user, read=False) 43 notifications = Notification.objects.filter(user= self.request.user, read=False)
34 context['notifications'] = notifications 44 context['notifications'] = notifications
35 45
36 - return self.response_class(request = self.request, template = template, context = context, using = self.template_engine, **response_kwargs) 46 + return self.response_class(request = self.request, template = self.template_name, context = context, using = self.template_engine, **response_kwargs)
37 47
38 48
core/templates/base.html
@@ -32,9 +32,7 @@ @@ -32,9 +32,7 @@
32 </script> 32 </script>
33 <script src="{% static 'js/base/header.js'%}"></script> 33 <script src="{% static 'js/base/header.js'%}"></script>
34 <!--Javascript block for specific-app ones --> 34 <!--Javascript block for specific-app ones -->
35 - {% block javascript %}  
36 -  
37 - {% endblock %} 35 +
38 </head> 36 </head>
39 <body> 37 <body>
40 <div class="container-fluid"> 38 <div class="container-fluid">
@@ -144,5 +142,9 @@ @@ -144,5 +142,9 @@
144 </div> 142 </div>
145 </div> 143 </div>
146 </div> 144 </div>
  145 +
  146 + {% block javascript %}
  147 +
  148 + {% endblock %}
147 </body> 149 </body>
148 </html> 150 </html>