web/quotes: add new and view quotes views

This commit is contained in:
Luke Granger-Brown 2021-01-20 22:48:15 +00:00
parent eb8abcacec
commit 2d634def1c
6 changed files with 67 additions and 10 deletions

View file

@ -11,3 +11,6 @@ class Quote(models.Model):
def __str__(self):
return self.quote
def get_absolute_url(self):
return f"/{self.id}/"

View file

@ -18,4 +18,6 @@ from . import views
urlpatterns = [
path("", views.home, name="home"),
re_path(r"(?P<quote_id>[0-9]+)/", views.show_quote, name="show_quote"),
path("new/", views.add_quote),
]

View file

@ -1,4 +1,5 @@
from django.shortcuts import render
from django.shortcuts import get_object_or_404, render, redirect
from django.forms import ModelForm
from . import models
@ -12,3 +13,29 @@ def home(request):
"quote": random_quote,
},
)
def show_quote(request, quote_id):
quote = get_object_or_404(models.Quote, id=int(quote_id))
return render(request, "quotedb/show_quote.html", {"quote": quote})
class AddQuoteForm(ModelForm):
class Meta:
model = models.Quote
fields = ['quote']
def add_quote(request):
if request.method == 'POST':
form = AddQuoteForm(request.POST)
if form.is_valid():
new_quote = form.save(commit=False)
new_quote.added_by = request.user
new_quote.save()
return redirect(new_quote)
else:
form = AddQuoteForm()
return render(request, "quotedb/new.html", {"form": form})

View file

@ -13,28 +13,29 @@
<body class="bg-bfobGray-dark text-bfobGray font-sans leading-normal tracking-normal">
<div class="container mx-auto">
<div class="container mx-auto max-w-screen-md">
<section class="flex justify-between items-center">
<h1 class="text-5xl font-bnto uppercase">
<a href="/">
BFOB <span class="text-bfobOrange">Quotes</span>
</a>
</h1>
<div>
<div class="flex justify-between">
{% if user.is_authenticated %}
<form action="/accounts/logout/" method="POST">
{% csrf_token %}
<input type="hidden" name="next" value="/accounts/logged_out/">
<button type="submit" class="px-4 py-1 rounded-full text-sm border border-bfobOrange text-bfobOrange hover:border-transparent hover:bg-bfobOrange hover:text-bfobGray-dark">Logout</button>
</form>
<a href="/new/" class="px-4 py-1 rounded-l-full text-sm border border-bfobOrange border-r-0 text-bfobOrange hover:border-transparent hover:bg-bfobOrange hover:text-bfobGray-dark">+</a>
<form action="/accounts/logout/" method="POST">
{% csrf_token %}
<input type="hidden" name="next" value="/accounts/logged_out/">
<button type="submit" class="px-4 py-1 rounded-r-full text-sm border border-bfobOrange text-bfobOrange hover:border-transparent hover:bg-bfobOrange hover:text-bfobGray-dark">Logout</button>
</form>
{% else %}
<a href="/accounts/discord/login/?process=login&next={{ request.path }}" class="px-4 py-1 rounded-full text-sm border border-bfobOrange text-bfobOrange hover:border-transparent hover:bg-bfobOrange hover:text-bfobGray-dark">Login</a>
<a href="/accounts/discord/login/?process=login&next={{ request.path }}" class="px-4 py-1 rounded-full text-sm border border-bfobOrange text-bfobOrange hover:border-transparent hover:bg-bfobOrange hover:text-bfobGray-dark">Login</a>
{% endif %}
</div>
</section>
</div>
<div class="container mx-auto {% block content_extra_classes %}{% endblock %}">
<div class="container mx-auto max-w-screen-md {% block content_extra_classes %}{% endblock %}">
{% block content %}{% endblock %}
</div>

View file

@ -0,0 +1,15 @@
{% extends "base.html" %}
{% block title %}New Quote{% endblock %}
{% block content %}
<h2 class="text-bfobOrange font-bnto text-2xl">gimme quote</h2>
<form method="POST">
{% csrf_token %}
<div class="grid grid-cols-1 gap-4 my-5">
<textarea name="quote"></textarea>
<button class="border border-bfobGray p-2" type="submit">Add Quote</button>
</div>
</form>
{% endblock %}

View file

@ -0,0 +1,9 @@
{% extends "base.html" %}
{% block title %}#{{ quote.id }}{% endblock %}
{% block content %}
<h2 class="text-bfobOrange font-bnto text-2xl">Quote #{{ quote.id }}</h2>
{% include "quotedb/_quote.html" with quote=quote only %}
{% endblock %}