Sith/api/views/api.py

47 lines
1.3 KiB
Python
Raw Normal View History

2016-08-05 18:01:23 +00:00
from . import serializers
2016-08-04 22:50:48 +00:00
from rest_framework.response import Response
from rest_framework.decorators import api_view
2016-08-05 18:01:23 +00:00
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from rest_framework.decorators import list_route
2016-08-04 22:50:48 +00:00
2016-08-05 18:01:23 +00:00
from django.shortcuts import get_object_or_404
2016-08-04 22:50:48 +00:00
from core.templatetags.renderer import markdown
2016-08-05 18:01:23 +00:00
from counter.models import Counter
2016-08-04 22:50:48 +00:00
@api_view(['GET'])
def RenderMarkdown(request):
"""
Render Markdown
"""
if request.method == 'GET':
return Response(markdown(request.GET['text']))
2016-08-05 18:01:23 +00:00
class CounterViewSet(viewsets.ModelViewSet):
"""
Manage Counters (api/v1/counter)
"""
serializer_class = serializers.Counter
queryset = Counter.objects.all()
@list_route()
def bar(self, request):
"""
2016-08-06 10:42:08 +00:00
Return all counters (api/v1/counter/bar)
2016-08-05 18:01:23 +00:00
"""
self.queryset = Counter.objects.filter(type="BAR")
serializer = self.get_serializer(self.queryset, many=True)
return Response(serializer.data)
2016-08-06 10:42:08 +00:00
@detail_route()
2016-08-05 18:01:23 +00:00
def id(self, request, pk=None):
"""
Get by id (api/v1/{nk}/id)
"""
self.queryset = get_object_or_404(Counter.objects.filter(id=pk))
serializer = self.get_serializer(self.queryset)
return Response(serializer.data)