diff --git a/app/views.py b/app/views.py index a6cde56..7064edf 100644 --- a/app/views.py +++ b/app/views.py @@ -7,7 +7,8 @@ from core.mixins import LogMixin from courses.models import Course class AppIndex(LoginRequiredMixin, LogMixin, TemplateView): - log_action = "Acessou home" + log_action = "Acessar" + log_resource = "Home" login_url = reverse_lazy("core:home") redirect_field_name = 'next' template_name = "home_professor.html" diff --git a/core/decorators.py b/core/decorators.py index 45b4eff..15f4c20 100644 --- a/core/decorators.py +++ b/core/decorators.py @@ -1,9 +1,8 @@ -import os -import datetime from django.conf import settings from functools import wraps +from .models import Action, Resource, Action_Resource, Log -def log_decorator(log_action = ''): +def log_decorator(log_action = '', log_resource = ''): def _log_decorator(view_function): @@ -12,17 +11,16 @@ def log_decorator(log_action = ''): response = view_function(request, *args, **kwargs) if request.user.is_authenticated and request.POST: - date = datetime.datetime.now() + action = Action.objects.filter(name = log_action) + resource = Resource.objects.filter(name = log_resource) - message = date.strftime("%d/%m/%Y %H:%M:%S") + ' - ' + request.user.username + ' - ' + log_action + '\n' + action_resource = Action_Resource.objects.filter(action = action, resource = resource)[0] - file_name = 'log_file_' + date.strftime("%d-%m-%Y") + '.txt' + log = Log() + log.user = request.user + log.action_resource = action_resource - log_path = os.path.join(settings.LOGS_URL, file_name) - - log_file = open(log_path,'a+') - log_file.write(message) - log_file.close() + log.save() return response diff --git a/core/mixins.py b/core/mixins.py index d5301bd..00681fa 100644 --- a/core/mixins.py +++ b/core/mixins.py @@ -1,21 +1,20 @@ -import os -import datetime from django.conf import settings +from .models import Action, Resource, Action_Resource, Log class LogMixin(object): log_action = "" + log_resource = "" def dispatch(self, request, *args, **kwargs): - date = datetime.datetime.now() + action = Action.objects.filter(name = self.log_action) + resource = Resource.objects.filter(name = self.log_resource) - message = date.strftime("%d/%m/%Y %H:%M:%S") + ' - ' + request.user.username + ' - ' + self.log_action + '\n' + action_resource = Action_Resource.objects.filter(action = action, resource = resource)[0] - file_name = 'log_file_' + date.strftime("%d-%m-%Y") + '.txt' + log = Log() + log.user = request.user + log.action_resource = action_resource - log_path = os.path.join(settings.LOGS_URL, file_name) - - log_file = open(log_path,'a+') - log_file.write(message) - log_file.close() + log.save() return super(LogMixin, self).dispatch(request, *args, **kwargs) \ No newline at end of file diff --git a/core/views.py b/core/views.py index cc2ba16..5a37c1d 100644 --- a/core/views.py +++ b/core/views.py @@ -64,7 +64,7 @@ def remember_password(request): context['danger'] = 'E-mail does not send' return render(request, "remember_password.html",context) -@log_decorator('Entrou no sistema') +@log_decorator('Acessar', 'Sistema') def login(request): context = {} if request.POST: -- libgit2 0.21.2