views.py
4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse_lazy, reverse
from django.contrib.auth import authenticate, login as login_user
from django.contrib.auth.mixins import LoginRequiredMixin
from .decorators import log_decorator
from django.contrib import messages
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.views.generic import CreateView, UpdateView, ListView
from django.http import HttpResponse, JsonResponse
from django.core.mail import send_mail,BadHeaderError
from django.conf import settings
from core.mixins import NotificationMixin
from .models import Notification, Log
from rolepermissions.shortcuts import assign_role
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
#API REST IMPORTS
from .serializers import LogSerializer
from rest_framework import status, serializers, permissions, viewsets
from rest_framework.response import Response
from rest_framework.decorators import api_view
from .forms import RegisterUserForm
from .decorators import log_decorator, notification_decorator
from users.models import User
from courses.models import Course, CourseCategory
def index(request):
context = {
'subscribed_courses': 'testando'
}
return render(request, "index.html", context)
class RegisterUser(CreateView, NotificationMixin):
model = User
form_class = RegisterUserForm
template_name = 'register_user.html'
success_url = reverse_lazy('core:home')
def form_valid(self, form):
form.save()
assign_role(form.instance, 'student')
messages.success(self.request, _('User successfully registered!'))
return super(RegisterUser, self).form_valid(form)
def create_account(request):
return render(request, "create_account.html")
def login(request):
context = {}
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login_user(request, user)
return redirect(reverse("app:index"))
else:
messages.add_message(request, messages.ERROR, _('E-mail or password are incorrect.'))
context["username"] = username
elif request.user.is_authenticated:
return redirect(reverse('app:index'))
return render(request,"index.html",context)
def processNotification(self, notificationId):
notification = Notification.objects.get(id= notificationId)
notification.read = True
notification.save()
return redirect(notification.action_resource.resource.url)
def getNotifications(request):
context = {}
if request.user.is_authenticated:
amountGotten = 0 #amountOfNotifications actually received
steps = int(request.GET['steps'])
amount = int(request.GET['amount'])
notifications = Notification.objects.filter(user= request.user, read=False).order_by('-datetime')[steps:steps+amount]
if len(notifications) == 0:
return HttpResponse("nothing")
else:
amountGotten = len(notifications)
context['notifications'] = notifications
else: #go to login page
return HttpResponse('teste')
html = render_to_string("notifications.html", context)
data = {}
data['html'] = html
data['amountGotten'] = amountGotten
return JsonResponse(data)
class GuestView (ListView):
template_name = 'guest.html'
context_object_name = 'courses'
queryset = CourseCategory.objects.all()
paginate_by = 10
def get_context_data (self, **kwargs):
context = super(GuestView, self).get_context_data(**kwargs)
context['title'] = _("Guest")
queryset_list = CourseCategory.objects.all()
paginator = Paginator(queryset_list, 10)
page = self.request.GET.get('page')
try:
queryset_list = paginator.page(page)
except PageNotAnInteger:
queryset_list = paginator.page(1)
except EmptyPage:
queryset_list = paginator.page(paginator.num_pages)
context['categorys_courses'] = queryset_list
return context
#REST API VIEWS
class LogViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]
queryset = Log.objects.all()
serializer_class = LogSerializer