Amélioration de la gestion des couleurs des étiquettes
This commit is contained in:
parent
52745c4012
commit
de11fa9e08
19
src/agenda_culturel/migrations/0006_alter_category_color.py
Normal file
19
src/agenda_culturel/migrations/0006_alter_category_color.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Generated by Django 4.2.1 on 2023-10-22 16:54
|
||||||
|
|
||||||
|
import colorfield.fields
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('agenda_culturel', '0005_alter_category_options'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='category',
|
||||||
|
name='color',
|
||||||
|
field=colorfield.fields.ColorField(blank=True, default=None, help_text='Color used as background for the category', image_field=None, max_length=25, null=True, samples=None, verbose_name='Color'),
|
||||||
|
),
|
||||||
|
]
|
@ -41,6 +41,9 @@ class Category(models.Model):
|
|||||||
|
|
||||||
super(Category, self).save(*args, **kwargs)
|
super(Category, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
def css_class(self):
|
||||||
|
return "cat-" + str(self.id)
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name + " (" + self.codename + ")"
|
return self.name + " (" + self.codename + ")"
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
.cat {
|
||||||
|
line-height: 1.5em;
|
||||||
|
padding: 0.3em 0.8em;
|
||||||
|
border-radius: 0.5em;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
@ -1,5 +1,11 @@
|
|||||||
{% extends "agenda_culturel/page.html" %}
|
{% extends "agenda_culturel/page.html" %}
|
||||||
|
|
||||||
|
{% load cat_extra %}
|
||||||
|
|
||||||
|
{% block entete_header %}
|
||||||
|
{% css_categories %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<header>
|
<header>
|
||||||
<div id="display-mode">
|
<div id="display-mode">
|
||||||
@ -8,9 +14,9 @@
|
|||||||
<div id="categories">
|
<div id="categories">
|
||||||
{% for cat in categories %}
|
{% for cat in categories %}
|
||||||
{% if category == cat %}
|
{% if category == cat %}
|
||||||
<span style="background: {{ cat.color }}">{{ cat }}</span>
|
<span class="cat {{ cat.css_class }}">{{ cat }}</span>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="{% url 'view_mode_cat' selected_mode cat.pk %}" style="background: {{ cat.color }}">{{ cat }}</a>
|
<a href="{% url 'view_mode_cat' selected_mode cat.pk %}" class="cat {{ cat.css_class }}">{{ cat }}</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
@ -20,9 +20,6 @@
|
|||||||
<header>
|
<header>
|
||||||
|
|
||||||
</header>
|
</header>
|
||||||
<aside>
|
|
||||||
<!-- -->
|
|
||||||
</aside>
|
|
||||||
<section>
|
<section>
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
</section>
|
</section>
|
||||||
|
37
src/agenda_culturel/templatetags/cat_extra.py
Normal file
37
src/agenda_culturel/templatetags/cat_extra.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from django import template
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
|
from agenda_culturel.models import Category
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
|
|
||||||
|
def color_variant(hex_color, brightness_offset=1):
|
||||||
|
""" takes a color like #87c95f and produces a lighter or darker variant """
|
||||||
|
if len(hex_color) != 7:
|
||||||
|
raise Exception("Passed %s into color_variant(), needs to be in #87c95f format." % hex_color)
|
||||||
|
rgb_hex = [hex_color[x:x + 2] for x in [1, 3, 5]]
|
||||||
|
new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex]
|
||||||
|
new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] # make sure new values are between 0 and 255
|
||||||
|
# hex() produces "0x88", we want just "88"
|
||||||
|
return "#" + "".join([hex(i)[2:] for i in new_rgb_int])
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag
|
||||||
|
def css_categories():
|
||||||
|
result = '<style type="text/css">'
|
||||||
|
|
||||||
|
cats = Category.objects.all()
|
||||||
|
|
||||||
|
for c in cats:
|
||||||
|
result += "." + c.css_class() + " {"
|
||||||
|
result += " background-color: " + c.color + ";"
|
||||||
|
result += " border: 2px solid " + color_variant(c.color, -20) + ";"
|
||||||
|
result += "}"
|
||||||
|
|
||||||
|
result += "." + c.css_class() + ":hover {"
|
||||||
|
result += " background-color: " + color_variant(c.color, 20) + ";"
|
||||||
|
result += " border: 2px solid " + c.color + ";"
|
||||||
|
result += "}"
|
||||||
|
|
||||||
|
result += '</style>'
|
||||||
|
return mark_safe(result)
|
@ -23,7 +23,7 @@ def home(request):
|
|||||||
# TODO: si on est au début de la semaine, on affiche la semaine en entier
|
# TODO: si on est au début de la semaine, on affiche la semaine en entier
|
||||||
# sinon, on affiche le week-end
|
# sinon, on affiche le week-end
|
||||||
# sauf si on est dimanche après 23h, on affiche la semaine prochaine
|
# sauf si on est dimanche après 23h, on affiche la semaine prochaine
|
||||||
return view_mode(request, DisplayMode.this_week)
|
return view_mode(request, DisplayMode.this_week.name)
|
||||||
|
|
||||||
|
|
||||||
def view_mode(request, mode):
|
def view_mode(request, mode):
|
||||||
|
Loading…
Reference in New Issue
Block a user