]> gitweb.fperrin.net Git - djsite.git/commitdiff
First iteration towards doing a tag cloud
authorFrédéric Perrin <frederic.perrin@resel.fr>
Fri, 4 Nov 2016 21:20:42 +0000 (21:20 +0000)
committerFrédéric Perrin <frederic.perrin@resel.fr>
Fri, 4 Nov 2016 21:20:42 +0000 (21:20 +0000)
pytest.ini
quotes/tagcloud.py [new file with mode: 0644]
quotes/templates/quotes/cloud.html [new file with mode: 0644]
quotes/test_cloud.py [new file with mode: 0644]
quotes/urls.py
quotes/views.py

index 8cab58e6b38a34093da1e9cc1df356378d08056b..435962eb94f554cc97fa4f63fe073986f24fad3d 100644 (file)
@@ -1,2 +1,3 @@
 [pytest]
 DJANGO_SETTINGS_MODULE=djsite.settings
+addopts = --fail-on-template-vars
\ No newline at end of file
diff --git a/quotes/tagcloud.py b/quotes/tagcloud.py
new file mode 100644 (file)
index 0000000..0e80984
--- /dev/null
@@ -0,0 +1,17 @@
+from quotes.models import QuoteTag
+from django.db.models import Count, Max
+
+def build_cloud():
+    maxtag = QuoteTag.objects \
+                     .all() \
+                     .aggregate(max=Max('quote'))['max']
+    print maxtag
+    tags = QuoteTag.objects \
+                   .annotate(num_quotes=Count('quote'),
+                             size=Count(1)) \
+                   .order_by('-num_quotes')[:20]
+
+    for tag in tags:
+        tag.size = int(10 * float(tag.num_quotes) / maxtag)
+    
+    return tags
diff --git a/quotes/templates/quotes/cloud.html b/quotes/templates/quotes/cloud.html
new file mode 100644 (file)
index 0000000..71fdcbb
--- /dev/null
@@ -0,0 +1,16 @@
+{% extends 'quotes/base.html' %}
+
+{% block title %}All the quotes in the database{% endblock %}
+
+{% block body %}
+
+<ul>
+  {% for tag in cloud %}
+  <li class="tag-size-{{ tag.size }}">
+    <a href="{{ tag.get_absolute_url }}"
+       title="{{ tag.tag }}, {{ tag.num_quotes }} quotes">{{ tag.tag }}</a>: {{ tag.num_quotes }}
+  </li>
+  {% endfor %}
+</ul>
+
+{% endblock %}
diff --git a/quotes/test_cloud.py b/quotes/test_cloud.py
new file mode 100644 (file)
index 0000000..906aa5b
--- /dev/null
@@ -0,0 +1,45 @@
+import pytest
+import re
+
+from quotes.models import QuoteTag, Quote, Work, Author
+
+class Test_Cloud(object):
+    @pytest.fixture
+    def size_range_small(self):
+        return [1, 5, 10, 7, 3]
+
+    def fill_db(self, size_range):
+        maxsize = max(size_range)
+
+        a = Author.objects.create(name="blah")
+        w = Work.objects.create(name="foo", author=a)
+
+        for i in range(maxsize):
+            Quote.objects.create(text="some text for quote %d" % i,
+                                 work = w)
+
+        for size in size_range:
+            tag = QuoteTag.objects.create(tag="tag-%d-" % size)
+            quotelist = Quote.objects.all()[:size]
+
+            for i in range(size):
+                quotelist[i].tags.add(tag)
+
+    def check_cloud(self, c, size_range):
+        top20 = sorted(size_range, reverse=True)[0:20]
+
+        cloud = c.getPage("cloud/")
+        for size in top20:
+            assert re.search("tag-%d-, %d quotes" % (size, size), cloud)
+
+    def test_cloud(self, size_range_small, db, c):
+        self.fill_db(size_range_small)
+        self.check_cloud(c, size_range_small)
+
+    @pytest.fixture
+    def size_range_large(self):
+        return range(1, 50)
+
+    def test_cloud_large(self, size_range_large, db, c):
+        self.fill_db(size_range_large)
+        self.check_cloud(c, size_range_large)
index f05a3fcb3786a7135ffd862e7bb18b01bb1b8046..0c43cc47259dffeb47f4e0e19dab07063c57bb2c 100644 (file)
@@ -13,6 +13,7 @@ urlpatterns = [
     url(r'^author/(?P<author_id>[0-9]+)/$', views.author, name="author"),
     url(r'^work/(?P<work_id>[0-9]+)/$', views.work, name="work"),
 
+    url(r'^cloud/$', views.cloud, name="cloud"),
     url(r'^search/$', views.searchpage, name="search"),
 
     url(r'^all/$', views.all, name="all"),
index 2171062cf781702aa881dcd7c49feca9826a8f79..01cb4e33f5ced7d73efac3edfd0ae303d26b604f 100644 (file)
@@ -4,8 +4,9 @@ from random import randint
 
 from quotes.models import Author, Work, Quote, QuoteTag
 import quotes.search as search
+import quotes.tagcloud as tagcloud
 
-# Create your views here.
+# create your views here.
 def index(request):
     return render(request, 'quotes/index.html')
 
@@ -46,3 +47,7 @@ def searchpage(request):
     if 'q' in request.POST:
         results = search.search(request.POST['q'])
     return render(request, 'quotes/search.html', results)
+
+def cloud(request):
+    clouddata = tagcloud.build_cloud()
+    return render(request, 'quotes/cloud.html', { 'cloud': clouddata })