views.py
3.77 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
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
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
from rolepermissions.shortcuts import assign_role
from .forms import RegisterUserForm
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, 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 remember_password(request):
context = {}
if request.POST:
email = request.POST['email']
registration = request.POST['registration']
if email and registration:
subject = _('Recover your password')
message = _('Hello %s, \nRecover your password to use your account.\nNumber of registration: %s\nLink for recuver password.\n\nRespectfully,\nTeam Amadeus.' % (request.user,registration))
try:
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [email],fail_silently=False)
context['success'] = 'Email successfully sent'
except BadHeaderError:
context['email'] = email
context['registration'] = registration
context['danger'] = 'E-mail does not send'
else:
context['email'] = email
context['registration'] = registration
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 = {}
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:
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]
context['notifications'] = notifications
else: #go to login page
return HttpResponse('teste')
html = render_to_string("notifications.html", context)
print(html)
return HttpResponse(html)
# class LoginClass(LoginView):
# template_name='index.html'
#
# def get_context_data(self, **kwargs):
# context = super(LoginClass,self).get_context_data(**kwargs)
# print ("deu certo")
# return context