Simple rendering of tags in lists

This commit is contained in:
Paul Konstantin Gerke 2022-03-08 14:48:27 +01:00 committed by Christopher Charbonneau Wells
parent ce21f39ec7
commit 3fa652cc25
2 changed files with 25 additions and 0 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import random import random
import re
from datetime import timedelta from datetime import timedelta
from typing import Iterable, Optional from typing import Iterable, Optional
@ -120,6 +121,20 @@ class Tag(TagBase):
blank=False, blank=False,
) )
@property
def complementary_color(self):
DARK, LIGHT = '#101010', '#EFEFEF'
if not self.color:
return DARK
r, g, b = [int(x, 16) for x in re.match("#(..)(..)(..)", self.color).groups()]
yiq = ((r * 299) + (g * 587) + (b * 114)) // 1000
if yiq >= 128:
return DARK
else:
return LIGHT
class Tagged(GenericTaggedItemBase): class Tagged(GenericTaggedItemBase):
tag = models.ForeignKey( tag = models.ForeignKey(

View File

@ -27,6 +27,7 @@
<th>{% trans "Child" %}</th> <th>{% trans "Child" %}</th>
{% endif %} {% endif %}
<th>{% trans "Note" %}</th> <th>{% trans "Note" %}</th>
<th>{% trans "Tags" %}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -54,6 +55,15 @@
<td><a href="{% url 'core:child' note.child.slug %}">{{ note.child }}</a></td> <td><a href="{% url 'core:child' note.child.slug %}">{{ note.child }}</a></td>
{% endif %} {% endif %}
<td>{{ note.note }}</td> <td>{{ note.note }}</td>
<td>
{% for tag in note.tags.all %}
<span class="badge badge-pill" style="background-color:{{ tag.color }};color:{{ tag.complementary_color }};">
{{ tag.name|escape }}
</span>
{% empty %}
-
{% endfor %}
</td>
</tr> </tr>
{% empty %} {% empty %}
<tr> <tr>