Skip to content
Snippets Groups Projects
Verified Commit aa3d0975 authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

Add REST API in separate app

parent 91b6011e
Branches 10-basic-ui
No related tags found
1 merge request!2Resolve "Add REST API"
Showing
with 103 additions and 16 deletions
django
djangorestframework
from django.apps import AppConfig
class UheppApiConfig(AppConfig):
name = 'uhepp_api'
from rest_framework import serializers
from uhepp_vault.models import Plot
class PlotSerializer(serializers.ModelSerializer):
class Meta:
model = Plot
fields = ['data']
def get_initial(self):
return {}
def to_internal_value(self, data):
return {'data': data}
def to_representation(self, instance):
return instance.data
from django.urls import path, include
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register('plots', views.PlotSet)
app_name = 'uhepp_api'
urlpatterns = [
path('', include(router.urls)),
]
from django.urls import reverse
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework import status
from rest_framework.response import Response
from uhepp_vault.models import Plot
from .serializers import PlotSerializer
class CreateModelMixin:
"""Create a model instance with Location header"""
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
obj = self.perform_create(serializer)
headers = self.get_success_headers(obj)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def perform_create(self, serializer):
obj = serializer.save()
return obj
def get_success_headers(self, obj):
rel_url = reverse('uhepp_api:plot-detail', args=[obj.uuid])
url = self.request.build_absolute_uri(rel_url)
return {'Location': url}
class PlotSet(CreateModelMixin,
viewsets.mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Plot.objects.all()
serializer_class = PlotSerializer
permission_classes = []
......@@ -32,12 +32,14 @@ ALLOWED_HOSTS = []
INSTALLED_APPS = [
'uhepp_vault',
'uhepp_api',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
MIDDLEWARE = [
......
......@@ -18,5 +18,6 @@ from django.urls import path, include
urlpatterns = [
path('', include('uhepp_vault.urls')),
path('api/', include('uhepp_api.urls')),
path('admin/', admin.site.urls),
]
from django.contrib import admin
from .models import UHeppPlot
from .models import Plot
admin.site.register(UHeppPlot)
admin.site.register(Plot)
# Generated by Django 3.1.3 on 2020-11-14 12:22
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('uhepp_vault', '0001_initial'),
]
operations = [
migrations.RenameModel(
old_name='UHeppPlot',
new_name='Plot',
),
]
from django.db import models
import uuid
class UHeppPlot(models.Model):
class Plot(models.Model):
"""Database record of a uhepp compliant plot"""
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4)
......
{{ uhepplot.data }}
{{ plot.data }}
{% if uhepplots %}
{% if plots %}
<ul>
{% for plot in uhepplots %}
{% for plot in plots %}
<li><a href="{% url 'uhepp_vault:plot-detail-json' plot.uuid %}">{{ plot }}: {{ plot.uuid }}<a></li>
{% endfor %}
</ul>
......
from django.urls import path
from django.urls import path, include
from . import views
......
......@@ -3,27 +3,26 @@ from django.views import generic
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from .models import UHeppPlot
from .models import Plot
class PlotIndex(generic.ListView):
template_name = 'uhepp_vault/plots.html'
context_object_name = 'uhepplots'
context_object_name = 'plots'
def get_queryset(self):
"""Return all uhepplots in the db"""
return UHeppPlot.objects.all()
"""Return all plots in the db"""
return Plot.objects.all()
class PlotDetail(generic.DetailView):
model = UHeppPlot
model = Plot
template_name = 'uhepp_vault/plot.html'
context_object_name = 'uhepplot'
context_object_name = 'plot'
def plot_detail_json(request, uuid):
plot = get_object_or_404(UHeppPlot, pk=uuid)
plot = get_object_or_404(Plot, pk=uuid)
return JsonResponse(plot.data)
def home(request):
plot_count = UHeppPlot.objects.count()
plot_count = Plot.objects.count()
context = dict(plot_count=plot_count)
return render(request, "uhepp_vault/home.html", context=context)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment