From 252e7ebb59aa0ab8e065f81b106976261f02d28d Mon Sep 17 00:00:00 2001 From: fbormann Date: Mon, 7 Nov 2016 14:32:13 -0300 Subject: [PATCH] added api log list call --- core/serializers.py | 7 +++++++ core/urls.py | 4 ++++ core/views.py | 26 ++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 core/serializers.py diff --git a/core/serializers.py b/core/serializers.py new file mode 100644 index 0000000..e4fe9fc --- /dev/null +++ b/core/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers +from .models import Log + +class LogSerializer(serializers.ModelSerializer): + class Meta: + model = Log + \ No newline at end of file diff --git a/core/urls.py b/core/urls.py index ecd28e2..626d06d 100644 --- a/core/urls.py +++ b/core/urls.py @@ -13,6 +13,9 @@ urlpatterns = [ url(r'^getNotifications/$', views.getNotifications, name='getNotifications'), url(r'^guest/$', views.GuestView.as_view(), name='guest'), + #API REST + url(r'^logs/$', views.get_log), + #Reset Password url(r'^reset/$', password_reset, {'template_name':'registration/passwor_reset_form.html', @@ -25,4 +28,5 @@ urlpatterns = [ 'post_reset_redirect' : '/done/'}, name='password_reset_confirm'), url(r'^done/$', password_reset_complete,{'template_name':'registration/passwor_reset_complete.html'}), + ] diff --git a/core/views.py b/core/views.py index f5e8d0d..1c7d9a9 100644 --- a/core/views.py +++ b/core/views.py @@ -11,8 +11,13 @@ 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 .models import Notification, Log from rolepermissions.shortcuts import assign_role +from django.contrib.auth.decorators import login_required +#API REST IMPORTS +from .serializers import LogSerializer +from rest_framework.renderers import JSONRenderer +from rest_framework.parsers import JSONParser from .forms import RegisterUserForm from .decorators import log_decorator, notification_decorator @@ -128,4 +133,21 @@ class GuestView (ListView): def get_context_data (self, **kwargs): context = super(GuestView, self).get_context_data(**kwargs) context['categorys_courses'] = CourseCategory.objects.all() - return context \ No newline at end of file + return context + +class JSONResponse(HttpResponse): + """ + An HttpResponse that renders its content into JSON. + """ + def __init__(self, data, **kwargs): + content = JSONRenderer().render(data) + kwargs['content_type'] = 'application/json' + super(JSONResponse, self).__init__(content, **kwargs) + +#REST API VIEWS +@login_required +def get_log(request): + if request.method == 'GET': + logs = Log.objects.all() + serializer = LogSerializer(logs, many=True) + return JSONResponse(serializer.data) -- libgit2 0.21.2