Commit 5f579329c07bfe64e5518b65856f5cb0171ad508
1 parent
c4829621
Exists in
master
and in
5 other branches
Adjusting to use database [Issue #24]
Showing
4 changed files
with
21 additions
and
23 deletions
Show diff stats
app/views.py
@@ -7,7 +7,8 @@ from core.mixins import LogMixin | @@ -7,7 +7,8 @@ from core.mixins import LogMixin | ||
7 | from courses.models import Course | 7 | from courses.models import Course |
8 | 8 | ||
9 | class AppIndex(LoginRequiredMixin, LogMixin, TemplateView): | 9 | class AppIndex(LoginRequiredMixin, LogMixin, TemplateView): |
10 | - log_action = "Acessou home" | 10 | + log_action = "Acessar" |
11 | + log_resource = "Home" | ||
11 | login_url = reverse_lazy("core:home") | 12 | login_url = reverse_lazy("core:home") |
12 | redirect_field_name = 'next' | 13 | redirect_field_name = 'next' |
13 | template_name = "home_professor.html" | 14 | template_name = "home_professor.html" |
core/decorators.py
1 | -import os | ||
2 | -import datetime | ||
3 | from django.conf import settings | 1 | from django.conf import settings |
4 | from functools import wraps | 2 | from functools import wraps |
3 | +from .models import Action, Resource, Action_Resource, Log | ||
5 | 4 | ||
6 | -def log_decorator(log_action = ''): | 5 | +def log_decorator(log_action = '', log_resource = ''): |
7 | 6 | ||
8 | def _log_decorator(view_function): | 7 | def _log_decorator(view_function): |
9 | 8 | ||
@@ -12,17 +11,16 @@ def log_decorator(log_action = ''): | @@ -12,17 +11,16 @@ def log_decorator(log_action = ''): | ||
12 | response = view_function(request, *args, **kwargs) | 11 | response = view_function(request, *args, **kwargs) |
13 | 12 | ||
14 | if request.user.is_authenticated and request.POST: | 13 | if request.user.is_authenticated and request.POST: |
15 | - date = datetime.datetime.now() | 14 | + action = Action.objects.filter(name = log_action) |
15 | + resource = Resource.objects.filter(name = log_resource) | ||
16 | 16 | ||
17 | - message = date.strftime("%d/%m/%Y %H:%M:%S") + ' - ' + request.user.username + ' - ' + log_action + '\n' | 17 | + action_resource = Action_Resource.objects.filter(action = action, resource = resource)[0] |
18 | 18 | ||
19 | - file_name = 'log_file_' + date.strftime("%d-%m-%Y") + '.txt' | 19 | + log = Log() |
20 | + log.user = request.user | ||
21 | + log.action_resource = action_resource | ||
20 | 22 | ||
21 | - log_path = os.path.join(settings.LOGS_URL, file_name) | ||
22 | - | ||
23 | - log_file = open(log_path,'a+') | ||
24 | - log_file.write(message) | ||
25 | - log_file.close() | 23 | + log.save() |
26 | 24 | ||
27 | return response | 25 | return response |
28 | 26 |
core/mixins.py
1 | -import os | ||
2 | -import datetime | ||
3 | from django.conf import settings | 1 | from django.conf import settings |
2 | +from .models import Action, Resource, Action_Resource, Log | ||
4 | 3 | ||
5 | class LogMixin(object): | 4 | class LogMixin(object): |
6 | log_action = "" | 5 | log_action = "" |
6 | + log_resource = "" | ||
7 | 7 | ||
8 | def dispatch(self, request, *args, **kwargs): | 8 | def dispatch(self, request, *args, **kwargs): |
9 | - date = datetime.datetime.now() | 9 | + action = Action.objects.filter(name = self.log_action) |
10 | + resource = Resource.objects.filter(name = self.log_resource) | ||
10 | 11 | ||
11 | - message = date.strftime("%d/%m/%Y %H:%M:%S") + ' - ' + request.user.username + ' - ' + self.log_action + '\n' | 12 | + action_resource = Action_Resource.objects.filter(action = action, resource = resource)[0] |
12 | 13 | ||
13 | - file_name = 'log_file_' + date.strftime("%d-%m-%Y") + '.txt' | 14 | + log = Log() |
15 | + log.user = request.user | ||
16 | + log.action_resource = action_resource | ||
14 | 17 | ||
15 | - log_path = os.path.join(settings.LOGS_URL, file_name) | ||
16 | - | ||
17 | - log_file = open(log_path,'a+') | ||
18 | - log_file.write(message) | ||
19 | - log_file.close() | 18 | + log.save() |
20 | 19 | ||
21 | return super(LogMixin, self).dispatch(request, *args, **kwargs) | 20 | return super(LogMixin, self).dispatch(request, *args, **kwargs) |
22 | \ No newline at end of file | 21 | \ No newline at end of file |
core/views.py
@@ -64,7 +64,7 @@ def remember_password(request): | @@ -64,7 +64,7 @@ def remember_password(request): | ||
64 | context['danger'] = 'E-mail does not send' | 64 | context['danger'] = 'E-mail does not send' |
65 | return render(request, "remember_password.html",context) | 65 | return render(request, "remember_password.html",context) |
66 | 66 | ||
67 | -@log_decorator('Entrou no sistema') | 67 | +@log_decorator('Acessar', 'Sistema') |
68 | def login(request): | 68 | def login(request): |
69 | context = {} | 69 | context = {} |
70 | if request.POST: | 70 | if request.POST: |