diff --git a/web/quotes/quotedb/models.py b/web/quotes/quotedb/models.py index 2df20279ce..3774f2eef4 100644 --- a/web/quotes/quotedb/models.py +++ b/web/quotes/quotedb/models.py @@ -11,3 +11,6 @@ class Quote(models.Model): def __str__(self): return self.quote + + def get_absolute_url(self): + return f"/{self.id}/" diff --git a/web/quotes/quotedb/urls.py b/web/quotes/quotedb/urls.py index 2588686fd2..4f1072259c 100644 --- a/web/quotes/quotedb/urls.py +++ b/web/quotes/quotedb/urls.py @@ -18,4 +18,6 @@ from . import views urlpatterns = [ path("", views.home, name="home"), + re_path(r"(?P[0-9]+)/", views.show_quote, name="show_quote"), + path("new/", views.add_quote), ] diff --git a/web/quotes/quotedb/views.py b/web/quotes/quotedb/views.py index 665db3b73c..c01d05a4e2 100644 --- a/web/quotes/quotedb/views.py +++ b/web/quotes/quotedb/views.py @@ -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}) + diff --git a/web/quotes/templates/base.html b/web/quotes/templates/base.html index 53208eec92..3fbcb06502 100644 --- a/web/quotes/templates/base.html +++ b/web/quotes/templates/base.html @@ -13,28 +13,29 @@ -
+

BFOB Quotes

-
+
{% if user.is_authenticated %} -
- {% csrf_token %} - - -
+ + +
+ {% csrf_token %} + + +
{% else %} - Login + Login {% endif %}
-
+
{% block content %}{% endblock %}
diff --git a/web/quotes/templates/quotedb/new.html b/web/quotes/templates/quotedb/new.html new file mode 100644 index 0000000000..ca105a0a6d --- /dev/null +++ b/web/quotes/templates/quotedb/new.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block title %}New Quote{% endblock %} + +{% block content %} +

gimme quote

+ +
+ {% csrf_token %} +
+ + +
+
+{% endblock %} diff --git a/web/quotes/templates/quotedb/show_quote.html b/web/quotes/templates/quotedb/show_quote.html new file mode 100644 index 0000000000..3b6a983023 --- /dev/null +++ b/web/quotes/templates/quotedb/show_quote.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block title %}#{{ quote.id }}{% endblock %} + +{% block content %} +

Quote #{{ quote.id }}

+ +{% include "quotedb/_quote.html" with quote=quote only %} +{% endblock %}