From 52745c40122a1012cc03cc4f461fb371d3573e7c Mon Sep 17 00:00:00 2001 From: Jean-Marie Favreau Date: Sat, 21 Oct 2023 22:10:31 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20gestion=20des=20cat=C3=A9gori?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agenda_culturel/admin.py | 3 +- .../migrations/0003_category.py | 23 ++++++++++ .../migrations/0004_alter_category_color.py | 19 ++++++++ .../migrations/0005_alter_category_options.py | 17 +++++++ src/agenda_culturel/models.py | 41 +++++++++++++++++ src/agenda_culturel/settings/base.py | 2 + .../templates/agenda_culturel/event.html | 4 +- .../templates/agenda_culturel/home.html | 4 +- .../agenda_culturel/page-events.html | 24 ++++++++++ .../templates/agenda_culturel/page.html | 16 ++++++- src/agenda_culturel/urls.py | 7 ++- src/agenda_culturel/views.py | 44 ++++++++++++------- src/requirements.txt | 3 ++ src/scripts/__init__.py | 0 src/scripts/create_categories.py | 25 +++++++++++ 15 files changed, 207 insertions(+), 25 deletions(-) create mode 100644 src/agenda_culturel/migrations/0003_category.py create mode 100644 src/agenda_culturel/migrations/0004_alter_category_color.py create mode 100644 src/agenda_culturel/migrations/0005_alter_category_options.py create mode 100644 src/agenda_culturel/templates/agenda_culturel/page-events.html create mode 100644 src/scripts/__init__.py create mode 100644 src/scripts/create_categories.py diff --git a/src/agenda_culturel/admin.py b/src/agenda_culturel/admin.py index 1fd3116..76d8b04 100644 --- a/src/agenda_culturel/admin.py +++ b/src/agenda_culturel/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin -from .models import Event, EventSubmissionForm +from .models import Event, EventSubmissionForm, Category admin.site.register(Event) admin.site.register(EventSubmissionForm) +admin.site.register(Category) diff --git a/src/agenda_culturel/migrations/0003_category.py b/src/agenda_culturel/migrations/0003_category.py new file mode 100644 index 0000000..1764d3f --- /dev/null +++ b/src/agenda_culturel/migrations/0003_category.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.1 on 2023-10-21 14:59 + +import colorfield.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0002_eventsubmissionform_alter_event_reference_urls'), + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(help_text='Category name', max_length=512, verbose_name='Name')), + ('codename', models.CharField(help_text='Short name of the category', max_length=3, verbose_name='Short name')), + ('color', colorfield.fields.ColorField(default='#CCCCCCC', help_text='Color used as background for the category', image_field=None, max_length=25, samples=None, verbose_name='Color')), + ], + ), + ] diff --git a/src/agenda_culturel/migrations/0004_alter_category_color.py b/src/agenda_culturel/migrations/0004_alter_category_color.py new file mode 100644 index 0000000..ce8afc6 --- /dev/null +++ b/src/agenda_culturel/migrations/0004_alter_category_color.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.1 on 2023-10-21 19:18 + +import colorfield.fields +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0003_category'), + ] + + operations = [ + migrations.AlterField( + model_name='category', + name='color', + field=colorfield.fields.ColorField(default='#FFFFFF', help_text='Color used as background for the category', image_field=None, max_length=25, samples=None, verbose_name='Color'), + ), + ] diff --git a/src/agenda_culturel/migrations/0005_alter_category_options.py b/src/agenda_culturel/migrations/0005_alter_category_options.py new file mode 100644 index 0000000..bc0c5b1 --- /dev/null +++ b/src/agenda_culturel/migrations/0005_alter_category_options.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.1 on 2023-10-21 19:30 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('agenda_culturel', '0004_alter_category_color'), + ] + + operations = [ + migrations.AlterModelOptions( + name='category', + options={'verbose_name': 'Category', 'verbose_name_plural': 'Categories'}, + ), + ] diff --git a/src/agenda_culturel/models.py b/src/agenda_culturel/models.py index 3951155..049c9c9 100644 --- a/src/agenda_culturel/models.py +++ b/src/agenda_culturel/models.py @@ -3,11 +3,52 @@ from django.contrib.postgres.fields import ArrayField from django.utils.translation import gettext_lazy as _ from django.template.defaultfilters import slugify # new from django.urls import reverse +from colorfield.fields import ColorField from django.template.defaultfilters import date as _date from datetime import datetime +class Category(models.Model): + + COLOR_PALETTE = [ + ("#ea5545", "color 1"), + ("#f46a9b", "color 2"), + ("#ef9b20", "color 3"), + ("#edbf33", "color 4"), + ("#ede15b", "color 5"), + ("#bdcf32", "color 6"), + ("#87bc45", "color 7"), + ("#27aeef", "color 8"), + ("#b33dc6", "color 9")] + + name = models.CharField(verbose_name=_('Name'), help_text=_('Category name'), max_length=512) + codename = models.CharField(verbose_name=_('Short name'), help_text=_('Short name of the category'), max_length=3) + color = ColorField(verbose_name=_('Color'), help_text=_('Color used as background for the category'), blank=True, null=True) + + def save(self, *args, **kwargs): + if self.color is None: + existing_colors = [c.color for c in Category.objects.all()] + if len(existing_colors) > len(Category.COLOR_PALETTE): + self.color = "#CCCCCC" + else: + for c, n in Category.COLOR_PALETTE: + if c not in existing_colors: + self.color = c + break + if self.color is None: + self.color = "#CCCCCC" + + super(Category, self).save(*args, **kwargs) + + + def __str__(self): + return self.name + " (" + self.codename + ")" + + class Meta: + verbose_name = _('Category') + verbose_name_plural = _('Categories') + class Event(models.Model): class STATUS(models.TextChoices): diff --git a/src/agenda_culturel/settings/base.py b/src/agenda_culturel/settings/base.py index 7435eb1..f538d4c 100644 --- a/src/agenda_culturel/settings/base.py +++ b/src/agenda_culturel/settings/base.py @@ -36,6 +36,8 @@ INSTALLED_APPS = [ "django.contrib.staticfiles", "corsheaders", "agenda_culturel", + "colorfield", + 'django_extensions', ] MIDDLEWARE = [ diff --git a/src/agenda_culturel/templates/agenda_culturel/event.html b/src/agenda_culturel/templates/agenda_culturel/event.html index 2ecd9c1..9f955db 100644 --- a/src/agenda_culturel/templates/agenda_culturel/event.html +++ b/src/agenda_culturel/templates/agenda_culturel/event.html @@ -1,8 +1,8 @@ -{% extends "agenda_culturel/page.html" %} +{% extends "agenda_culturel/page_events.html" %} {% block title %}{{ object.title }}{% endblock %} -{% block content %} +{% block events %}

{{ object.title }}

Date : {{ object.start_day }}

diff --git a/src/agenda_culturel/templates/agenda_culturel/home.html b/src/agenda_culturel/templates/agenda_culturel/home.html index 2934e51..213d7ed 100644 --- a/src/agenda_culturel/templates/agenda_culturel/home.html +++ b/src/agenda_culturel/templates/agenda_culturel/home.html @@ -1,8 +1,8 @@ -{% extends "agenda_culturel/page.html" %} +{% extends "agenda_culturel/page_event.html" %} {% block title %}Accueil{% endblock %} -{% block content %} +{% block events %}

Événements