From 9323b9da77d531bb681d940458306c44ef1479cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20I=C3=9Fbr=C3=BCcker?= Date: Sun, 30 Jun 2019 19:31:32 +0200 Subject: [PATCH] Implement tag cloud on bookmark page --- bookmarks/styles/bookmarks.scss | 17 +++++++++ bookmarks/styles/index.scss | 3 +- bookmarks/templates/bookmarks/index.html | 2 ++ bookmarks/templates/bookmarks/tag_cloud.html | 14 ++++++++ bookmarks/templatetags/bookmarks.py | 36 +++++++++++++++++++- bookmarks/templatetags/shared.py | 4 +-- bookmarks/views/bookmarks.py | 4 ++- 7 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 bookmarks/templates/bookmarks/tag_cloud.html diff --git a/bookmarks/styles/bookmarks.scss b/bookmarks/styles/bookmarks.scss index d3aa5ff..3ea3055 100644 --- a/bookmarks/styles/bookmarks.scss +++ b/bookmarks/styles/bookmarks.scss @@ -25,3 +25,20 @@ ul.bookmark-list { } } } + +.tag-cloud { + + a { + color: $alternative-color; + } + + .group { + margin-bottom: 0.4rem; + } + + .group-label { + font-weight: bold; + text-transform: uppercase; + color: $alternative-color-dark; + } +} diff --git a/bookmarks/styles/index.scss b/bookmarks/styles/index.scss index e1b1f6e..c834b3a 100644 --- a/bookmarks/styles/index.scss +++ b/bookmarks/styles/index.scss @@ -5,7 +5,8 @@ $alternative-color: #f45d00; $alternative-color: #FF84E8; $alternative-color: #98C1D9; $alternative-color: #7B287D; -$alternative-color: #06c5c2; +$alternative-color: #05a6a3; +$alternative-color-dark: darken($alternative-color, 5%); // Import Spectre CSS lib @import "../../node_modules/spectre.css/src/spectre"; diff --git a/bookmarks/templates/bookmarks/index.html b/bookmarks/templates/bookmarks/index.html index 6f632af..86eabe8 100644 --- a/bookmarks/templates/bookmarks/index.html +++ b/bookmarks/templates/bookmarks/index.html @@ -1,5 +1,6 @@ {% extends "bookmarks/layout.html" %} {% load shared %} +{% load bookmarks %} {% block content %}
@@ -65,6 +66,7 @@

Tags

+ {% tag_cloud tags %}
diff --git a/bookmarks/templates/bookmarks/tag_cloud.html b/bookmarks/templates/bookmarks/tag_cloud.html new file mode 100644 index 0000000..2b94af1 --- /dev/null +++ b/bookmarks/templates/bookmarks/tag_cloud.html @@ -0,0 +1,14 @@ +{% load shared %} + +
+ {% for group in groups %} +

+ {{ group.char }} + {% for tag in group.tags %} + + {{ tag.name }} + + {% endfor %} +

+ {% endfor %} +
diff --git a/bookmarks/templatetags/bookmarks.py b/bookmarks/templatetags/bookmarks.py index 2b2b96d..5ba6020 100644 --- a/bookmarks/templatetags/bookmarks.py +++ b/bookmarks/templatetags/bookmarks.py @@ -1,6 +1,8 @@ +from typing import List + from django import template -from bookmarks.models import BookmarkForm +from bookmarks.models import BookmarkForm, Tag register = template.Library() @@ -10,3 +12,35 @@ def bookmark_form(form: BookmarkForm): return { 'form': form, } + + +class TagGroup: + def __init__(self, char): + self.tags = [] + self.char = char + + +def create_tag_groups(tags: List[Tag]): + sorted_tags = sorted(tags, key=lambda x: str.lower(x.name)) + group = None + groups = [] + + # Group tags that start with a different character than the previous one + for tag in sorted_tags: + tag_char = tag.name[0].lower() + + if not group or group.char != tag_char: + group = TagGroup(tag_char) + groups.append(group) + + group.tags.append(tag) + + return groups + + +@register.inclusion_tag('bookmarks/tag_cloud.html', name='tag_cloud', takes_context=True) +def tag_cloud(context, tags: List[Tag]): + groups = create_tag_groups(tags) + return { + 'groups': groups, + } diff --git a/bookmarks/templatetags/shared.py b/bookmarks/templatetags/shared.py index 54d0275..b8e2b96 100644 --- a/bookmarks/templatetags/shared.py +++ b/bookmarks/templatetags/shared.py @@ -5,7 +5,7 @@ register = template.Library() @register.simple_tag(takes_context=True) def update_query_string(context, **kwargs): - query = context['request'].GET.copy() + query = context.request.GET.copy() # Replace query params with the ones from tag parameters for key in kwargs: @@ -16,7 +16,7 @@ def update_query_string(context, **kwargs): @register.simple_tag(takes_context=True) def append_query_param(context, **kwargs): - query = context['request'].GET.copy() + query = context.request.GET.copy() # Append to or create query param for key in kwargs: diff --git a/bookmarks/views/bookmarks.py b/bookmarks/views/bookmarks.py index 047e1ea..a06d22c 100644 --- a/bookmarks/views/bookmarks.py +++ b/bookmarks/views/bookmarks.py @@ -4,7 +4,7 @@ from django.shortcuts import render from django.urls import reverse from bookmarks import queries -from bookmarks.models import Bookmark, BookmarkForm +from bookmarks.models import Bookmark, BookmarkForm, Tag from bookmarks.services.bookmarks import create_bookmark, update_bookmark _default_page_size = 30 @@ -16,6 +16,7 @@ def index(request): query_set = queries.query_bookmarks(request.user, query_string) paginator = Paginator(query_set, _default_page_size) bookmarks = paginator.get_page(page) + tags = Tag.objects.all() if request.GET.get('tag'): mod = request.GET.copy() @@ -24,6 +25,7 @@ def index(request): context = { 'bookmarks': bookmarks, + 'tags': tags, 'query': query_string if query_string else '', } return render(request, 'bookmarks/index.html', context)