2017-05-06 22:38:32 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
|
|
# Create your views here.
|
2017-11-21 16:21:59 -05:00
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
2017-05-06 22:38:32 -04:00
|
|
|
from django.contrib.auth.models import User, Group
|
2018-01-18 17:14:31 -05:00
|
|
|
|
2017-10-17 16:13:42 -04:00
|
|
|
from django.views.decorators.clickjacking import xframe_options_exempt
|
2017-05-06 22:38:32 -04:00
|
|
|
from rest_framework import viewsets, status
|
|
|
|
|
from rest_framework.decorators import api_view
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from compile_server.app.serializers import (UserSerializer,
|
|
|
|
|
GroupSerializer,
|
2017-09-06 09:14:18 -04:00
|
|
|
ResourceSerializer,
|
2018-04-20 11:01:14 -04:00
|
|
|
ExampleSerializer)
|
2017-05-06 22:38:32 -04:00
|
|
|
|
2018-04-20 11:01:14 -04:00
|
|
|
from compile_server.app.models import Resource, Example
|
2017-05-06 22:38:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class UserViewSet(viewsets.ModelViewSet):
|
|
|
|
|
"""
|
|
|
|
|
API endpoint that allows users to be viewed or edited.
|
|
|
|
|
"""
|
|
|
|
|
queryset = User.objects.all().order_by('-date_joined')
|
|
|
|
|
serializer_class = UserSerializer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GroupViewSet(viewsets.ModelViewSet):
|
|
|
|
|
"""
|
|
|
|
|
API endpoint that allows groups to be viewed or edited.
|
|
|
|
|
"""
|
|
|
|
|
queryset = Group.objects.all()
|
|
|
|
|
serializer_class = GroupSerializer
|
|
|
|
|
|
|
|
|
|
|
2017-07-19 14:42:35 -04:00
|
|
|
class ResourceSet(viewsets.ModelViewSet):
|
2017-05-06 22:38:32 -04:00
|
|
|
"""View/Edit"""
|
2017-07-19 14:42:35 -04:00
|
|
|
queryset = Resource.objects.all()
|
|
|
|
|
serializer_class = ResourceSerializer
|
2017-05-06 22:38:32 -04:00
|
|
|
|
|
|
|
|
|
2017-10-10 15:53:04 -04:00
|
|
|
def CrossDomainResponse(data=None):
|
|
|
|
|
"""Return a response which accepts cross-domain queries"""
|
|
|
|
|
r = Response(data)
|
|
|
|
|
r["Access-Control-Allow-Origin"] = "*"
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
2017-05-06 22:38:32 -04:00
|
|
|
@api_view(['GET'])
|
|
|
|
|
def examples(request):
|
2017-07-19 14:42:35 -04:00
|
|
|
"""Return a list of example names and their description"""
|
2017-07-19 16:42:02 -04:00
|
|
|
examples = Example.objects.all()
|
|
|
|
|
results = []
|
|
|
|
|
for e in examples:
|
|
|
|
|
results.append({'name': e.name, 'description': e.description})
|
|
|
|
|
|
2017-10-10 15:53:04 -04:00
|
|
|
return CrossDomainResponse(results)
|
2017-05-06 22:38:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
|
|
|
def example(request, name):
|
2017-07-21 15:48:38 -04:00
|
|
|
# TODO: create an example serializer
|
2017-07-21 15:59:27 -04:00
|
|
|
matches = Example.objects.filter(name=name)
|
|
|
|
|
if not matches:
|
2017-10-10 15:53:04 -04:00
|
|
|
return CrossDomainResponse()
|
2017-07-21 15:59:27 -04:00
|
|
|
|
|
|
|
|
e = matches[0]
|
2017-07-21 15:48:38 -04:00
|
|
|
resources = []
|
|
|
|
|
for r in e.resources.all():
|
|
|
|
|
serializer = ResourceSerializer(r)
|
|
|
|
|
resources.append(serializer.data)
|
2017-07-21 15:59:27 -04:00
|
|
|
|
|
|
|
|
result = {'name': e.name,
|
|
|
|
|
'description': e.description,
|
2017-10-10 11:52:49 -04:00
|
|
|
'main': e.main,
|
2017-07-21 15:59:27 -04:00
|
|
|
'resources': resources}
|
2017-10-10 15:53:04 -04:00
|
|
|
return CrossDomainResponse(result)
|
2017-09-02 14:24:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def code_page(request, example_name):
|
2017-09-06 09:14:18 -04:00
|
|
|
matches = Example.objects.filter(name=example_name)
|
|
|
|
|
if not matches:
|
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
|
|
e = matches[0]
|
|
|
|
|
serializer = ExampleSerializer(e)
|
|
|
|
|
context = {'example': serializer.data}
|
2017-09-02 14:24:26 -04:00
|
|
|
return render(request, 'code_page.html', context)
|
2017-09-04 14:01:38 -04:00
|
|
|
|
2017-09-06 09:14:18 -04:00
|
|
|
|
2017-10-17 16:13:42 -04:00
|
|
|
@xframe_options_exempt
|
2017-10-17 15:36:09 -04:00
|
|
|
def code_embed(request, example_name):
|
|
|
|
|
matches = Example.objects.filter(name=example_name)
|
|
|
|
|
if not matches:
|
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
|
|
e = matches[0]
|
|
|
|
|
serializer = ExampleSerializer(e)
|
|
|
|
|
context = {'example': serializer.data}
|
|
|
|
|
return render(request, 'code_embed.html', context)
|
|
|
|
|
|
|
|
|
|
|
2017-09-04 14:01:38 -04:00
|
|
|
def examples_list(request):
|
|
|
|
|
context = {'examples': Example.objects.all}
|
|
|
|
|
return render(request, 'examples_list.html', context)
|