From 45be55604156dc0064aa32ea508df0d90ac5a17a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Wed, 28 Sep 2016 21:35:00 +0100 Subject: [PATCH] Initial draft of the quotes app --- quotes/admin.py | 4 ++++ quotes/models.py | 24 +++++++++++++++++++ quotes/static/quotations/style.css | 18 ++++++++++++++ quotes/templates/quotations/author.html | 15 ++++++++++++ quotes/templates/quotations/base.html | 10 ++++++++ quotes/templates/quotations/display.html | 20 ++++++++++++++++ quotes/templates/quotations/many.html | 9 +++++++ quotes/templates/quotations/onequote.html | 7 ++++++ quotes/templates/quotations/tag.html | 15 ++++++++++++ quotes/urls.py | 11 +++++++++ quotes/views.py | 29 +++++++++++++++++++++++ 11 files changed, 162 insertions(+) create mode 100644 quotes/static/quotations/style.css create mode 100644 quotes/templates/quotations/author.html create mode 100644 quotes/templates/quotations/base.html create mode 100644 quotes/templates/quotations/display.html create mode 100644 quotes/templates/quotations/many.html create mode 100644 quotes/templates/quotations/onequote.html create mode 100644 quotes/templates/quotations/tag.html create mode 100644 quotes/urls.py diff --git a/quotes/admin.py b/quotes/admin.py index 8c38f3f..61caaab 100644 --- a/quotes/admin.py +++ b/quotes/admin.py @@ -1,3 +1,7 @@ from django.contrib import admin # Register your models here. +from .models import Author, Quote, Tag +admin.site.register(Author) +admin.site.register(Tag) +admin.site.register(Quote) diff --git a/quotes/models.py b/quotes/models.py index 71a8362..9791aa0 100644 --- a/quotes/models.py +++ b/quotes/models.py @@ -1,3 +1,27 @@ from django.db import models # Create your models here. + +class Author(models.Model): + name = models.CharField(max_length=100) + + def __unicode__(self): + return self.name + +class Tag(models.Model): + tag = models.CharField(max_length=100) + + def __unicode__(self): + return self.tag + + class Meta: + ordering = ("tag", ) + +class Quote(models.Model): + text = models.TextField() + author = models.ForeignKey(Author) + tags = models.ManyToManyField(Tag, blank=True) + + def __unicode__(self): + return self.author.name + ": " + self.text + diff --git a/quotes/static/quotations/style.css b/quotes/static/quotations/style.css new file mode 100644 index 0000000..007cfe1 --- /dev/null +++ b/quotes/static/quotations/style.css @@ -0,0 +1,18 @@ +a { + color: black; +} + +.quote { + background-color: #ffbd33; + border: 3px solid #ff5733; + padding: 1.5em 2em; + margin: 2em 30em; +} + +.quote .author { + padding-left: 5em; +} + +.quote .author .name { + font-style: italic; +} diff --git a/quotes/templates/quotations/author.html b/quotes/templates/quotations/author.html new file mode 100644 index 0000000..38b36c7 --- /dev/null +++ b/quotes/templates/quotations/author.html @@ -0,0 +1,15 @@ +{% extends 'quotations/base.html' %} + +{% block body %} + + +

{{ author.name }}

+ +

All the quotes for {{ author.name }}:

+ +{% for quote in author.quote_set.all %} + {% include "quotations/display.html" with quote=quote %} +{% endfor %} + + +{% endblock %} diff --git a/quotes/templates/quotations/base.html b/quotes/templates/quotations/base.html new file mode 100644 index 0000000..b11766b --- /dev/null +++ b/quotes/templates/quotations/base.html @@ -0,0 +1,10 @@ + + + {% load staticfiles %} + + + + {% block body %} + {% endblock %} + + diff --git a/quotes/templates/quotations/display.html b/quotes/templates/quotations/display.html new file mode 100644 index 0000000..62d36d9 --- /dev/null +++ b/quotes/templates/quotations/display.html @@ -0,0 +1,20 @@ +

+

+ {{ quote.text }} +

+ +

+ — + {{ quote.author.name }} + +

+ + {% if quote.tags.all %} +

+ Tags: + {% for tag in quote.tags.all %} + {{ tag.tag }} + {% endfor %} +

+ {% endif %} +
diff --git a/quotes/templates/quotations/many.html b/quotes/templates/quotations/many.html new file mode 100644 index 0000000..63d4f03 --- /dev/null +++ b/quotes/templates/quotations/many.html @@ -0,0 +1,9 @@ +{% extends 'quotations/base.html' %} + +{% block body %} + +{% for quote in quotes %} + {% include "quotations/display.html" with quote=quote %} +{% endfor %} + +{% endblock %} diff --git a/quotes/templates/quotations/onequote.html b/quotes/templates/quotations/onequote.html new file mode 100644 index 0000000..0102dcc --- /dev/null +++ b/quotes/templates/quotations/onequote.html @@ -0,0 +1,7 @@ +{% extends 'quotations/base.html' %} + +{% block body %} + +{% include "quotations/display.html" with quote=quote %} + +{% endblock %} diff --git a/quotes/templates/quotations/tag.html b/quotes/templates/quotations/tag.html new file mode 100644 index 0000000..bf06aff --- /dev/null +++ b/quotes/templates/quotations/tag.html @@ -0,0 +1,15 @@ +{% extends 'quotations/base.html' %} + +{% block body %} + + +

{{ tag.tag }}

+ +

All the quotes tagged with {{ tag.tag }}:

+ +{% for quote in tag.quote_set.all %} + {% include "quotations/display.html" with quote=quote %} +{% endfor %} + + +{% endblock %} diff --git a/quotes/urls.py b/quotes/urls.py new file mode 100644 index 0000000..014b690 --- /dev/null +++ b/quotes/urls.py @@ -0,0 +1,11 @@ +from django.conf.urls import url + +from . import views + +urlpatterns = [ + url(r'^$', views.random, name='random'), + url(r'^show/(?P[0-9]+)/$', views.onequote, name="onequote"), + url(r'^tag/(?P[0-9]+)/$', views.tags, name="tags"), + url(r'^author/(?P[0-9]+)/$', views.author, name="author"), + url(r'^all/$', views.all, name="all"), +] diff --git a/quotes/views.py b/quotes/views.py index 91ea44a..6157192 100644 --- a/quotes/views.py +++ b/quotes/views.py @@ -1,3 +1,32 @@ from django.shortcuts import render +from django.http import HttpResponse +from django.template import loader + +from random import randint + +from .models import Author, Quote, Tag # Create your views here. +def onequote(request, quote_id): + q = Quote.objects.get(id=quote_id) + context = { 'quote' : q } + return render(request, 'quotations/onequote.html', context) + +def random(request): + count = Quote.objects.count() + return onequote(request, randint(1, count)) + +def tags(request, tag_id): + tag = Tag.objects.get(id=tag_id) + context = { 'tag' : tag } + return render(request, 'quotations/tag.html', context) + +def author(request, author_id): + author = Author.objects.get(id=author_id) + context = { 'author' : author } + return render(request, 'quotations/author.html', context) + +def all(request): + quotes = Quote.objects.all() + context = { 'quotes' : quotes } + return render(request, 'quotations/all.html', context) -- 2.43.0