]> gitweb.fperrin.net Git - djsite.git/commitdiff
Initial draft of the quotes app
authorFrédéric Perrin <fred@fperrin.net>
Wed, 28 Sep 2016 20:35:00 +0000 (21:35 +0100)
committerFrédéric Perrin <fred@fperrin.net>
Wed, 28 Sep 2016 20:35:00 +0000 (21:35 +0100)
quotes/admin.py
quotes/models.py
quotes/static/quotations/style.css [new file with mode: 0644]
quotes/templates/quotations/author.html [new file with mode: 0644]
quotes/templates/quotations/base.html [new file with mode: 0644]
quotes/templates/quotations/display.html [new file with mode: 0644]
quotes/templates/quotations/many.html [new file with mode: 0644]
quotes/templates/quotations/onequote.html [new file with mode: 0644]
quotes/templates/quotations/tag.html [new file with mode: 0644]
quotes/urls.py [new file with mode: 0644]
quotes/views.py

index 8c38f3f3dad51e4585f3984282c2a4bec5349c1e..61caaab1fb8087db04286529e14af258dad8a6d8 100644 (file)
@@ -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)
index 71a836239075aa6e6e4ecb700e9c42c95c022d91..9791aa0b093b00eb924a76d8b0b52e1ac98574b2 100644 (file)
@@ -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 (file)
index 0000000..007cfe1
--- /dev/null
@@ -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 (file)
index 0000000..38b36c7
--- /dev/null
@@ -0,0 +1,15 @@
+{% extends 'quotations/base.html' %}
+
+{% block body %}
+
+
+<h1>{{ author.name }}</h1>
+
+<p>All the quotes for {{ author.name }}:<p>
+
+{% 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 (file)
index 0000000..b11766b
--- /dev/null
@@ -0,0 +1,10 @@
+<html>
+  <head>
+    {% load staticfiles %}
+    <link rel="stylesheet" type="text/css" href="{% static 'quotations/style.css' %}" />
+  </head>
+  <body>
+    {% block body %}
+    {% endblock %}
+  </body>
+</html>
diff --git a/quotes/templates/quotations/display.html b/quotes/templates/quotations/display.html
new file mode 100644 (file)
index 0000000..62d36d9
--- /dev/null
@@ -0,0 +1,20 @@
+<div class="quote">
+  <p class="text">
+    {{ quote.text }}
+  </p>
+
+  <p class="author">
+    — <span class="name"><a href="{% url 'author' quote.author.id %}">
+       {{ quote.author.name }}
+    </a></span>
+  </p>
+
+  {% if quote.tags.all %}
+    <p class="tags">
+      Tags: 
+      {% for tag in quote.tags.all %}
+        <a href="{% url 'tags' tag.id %}">{{ tag.tag }}</a>
+      {% endfor %}
+    </p>
+  {% endif %}
+</div>
diff --git a/quotes/templates/quotations/many.html b/quotes/templates/quotations/many.html
new file mode 100644 (file)
index 0000000..63d4f03
--- /dev/null
@@ -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 (file)
index 0000000..0102dcc
--- /dev/null
@@ -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 (file)
index 0000000..bf06aff
--- /dev/null
@@ -0,0 +1,15 @@
+{% extends 'quotations/base.html' %}
+
+{% block body %}
+
+
+<h1>{{ tag.tag }}</h1>
+
+<p>All the quotes tagged with {{ tag.tag }}:<p>
+
+{% 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 (file)
index 0000000..014b690
--- /dev/null
@@ -0,0 +1,11 @@
+from django.conf.urls import url
+
+from . import views
+
+urlpatterns = [
+    url(r'^$', views.random, name='random'),
+    url(r'^show/(?P<quote_id>[0-9]+)/$', views.onequote, name="onequote"),
+    url(r'^tag/(?P<tag_id>[0-9]+)/$', views.tags, name="tags"),
+    url(r'^author/(?P<author_id>[0-9]+)/$', views.author, name="author"),
+    url(r'^all/$', views.all, name="all"),
+]
index 91ea44a218fbd2f408430959283f0419c921093e..615719209327f58096622d8b3d5e8b51d3928c98 100644 (file)
@@ -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)