Compare commits
3 commits
94750870f3
...
aabd1c301a
Author | SHA1 | Date | |
---|---|---|---|
|
aabd1c301a | ||
|
9a66ba0314 | ||
|
beec22f40a |
|
@ -1,4 +1,4 @@
|
||||||
# Generated by Django 5.1.3 on 2024-12-02 22:49
|
# Generated by Django 5.1.3 on 2024-12-03 12:13
|
||||||
|
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
@ -17,7 +17,7 @@ class Migration(migrations.Migration):
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
('name', models.CharField(max_length=254)),
|
('name', models.CharField(max_length=254)),
|
||||||
('slug', models.CharField(max_length=24)),
|
('slug', models.CharField(max_length=24, unique=True)),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
|
|
|
@ -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):
|
||||||
|
@ -11,7 +12,7 @@ class Suggester(models.Model):
|
||||||
|
|
||||||
class Author(models.Model):
|
class Author(models.Model):
|
||||||
name = models.CharField(max_length=254)
|
name = models.CharField(max_length=254)
|
||||||
slug = models.CharField(max_length=24)
|
slug = models.CharField(max_length=24, unique=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
@ -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,5 +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>/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,16 +1,42 @@
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
def show_quote(request):
|
def show_quote(request, author_slug=None):
|
||||||
quote = Quote.objects.order_by('?').first() or 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 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