Add JSON endpoints
This commit is contained in:
parent
9a66ba0314
commit
aabd1c301a
|
@ -1,4 +1,5 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.forms.models import model_to_dict
|
||||||
|
|
||||||
|
|
||||||
class Suggester(models.Model):
|
class Suggester(models.Model):
|
||||||
|
@ -27,6 +28,14 @@ class Quote(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.text
|
return self.text
|
||||||
|
|
||||||
|
def as_dict(self):
|
||||||
|
result = model_to_dict(self, fields=["text"])
|
||||||
|
result["author"] = model_to_dict(self.author, exclude=["id"])
|
||||||
|
result["suggester"] = model_to_dict(
|
||||||
|
self.suggester, exclude=["id"]) if self.suggester else None
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class Suggestion(models.Model):
|
class Suggestion(models.Model):
|
||||||
text = models.TextField()
|
text = models.TextField()
|
||||||
|
|
|
@ -6,6 +6,9 @@ app_name = "lazaro_quotes"
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.show_quote, name="show_quote"),
|
path("", views.show_quote, name="show_quote"),
|
||||||
|
path("json", views.get_quote_json, name="get_quote_json"),
|
||||||
|
path("all/json", views.get_all_quotes_json, name="get_all_quotes_json"),
|
||||||
path("<str:author_slug>", views.show_quote, name="show_quote"),
|
path("<str:author_slug>", views.show_quote, name="show_quote"),
|
||||||
|
path("<str:author_slug>/json", views.get_quote_json, name="get_quote_json"),
|
||||||
path("suggest", views.suggest_quote_form, name="suggest_quote_form")
|
path("suggest", views.suggest_quote_form, name="suggest_quote_form")
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
from django.forms.models import model_to_dict
|
||||||
|
from django.http import JsonResponse
|
||||||
|
|
||||||
from .models import Quote, Suggester
|
from .models import Quote, Suggester
|
||||||
from .forms import SuggestionForm
|
from .forms import SuggestionForm
|
||||||
|
@ -15,6 +17,26 @@ def show_quote(request, author_slug=None):
|
||||||
return render(request, 'lazaro_quotes/show_quote.html', {"quote": quote})
|
return render(request, 'lazaro_quotes/show_quote.html', {"quote": quote})
|
||||||
|
|
||||||
|
|
||||||
|
def get_quote_json(request, author_slug=None):
|
||||||
|
if author_slug is None:
|
||||||
|
quote = Quote.objects.order_by('?').first()
|
||||||
|
else:
|
||||||
|
quote = Quote.objects.filter(
|
||||||
|
author__slug=author_slug).order_by('?').first()
|
||||||
|
|
||||||
|
return JsonResponse(quote.as_dict())
|
||||||
|
|
||||||
|
|
||||||
|
def get_all_quotes_json(request):
|
||||||
|
quotes = Quote.objects.all()
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
for i, quote in enumerate(quotes):
|
||||||
|
result[i] = quote.as_dict()
|
||||||
|
|
||||||
|
return JsonResponse(result)
|
||||||
|
|
||||||
|
|
||||||
def suggest_quote_form(request):
|
def suggest_quote_form(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = SuggestionForm(request.POST)
|
form = SuggestionForm(request.POST)
|
||||||
|
|
Loading…
Reference in a new issue