From 079b2073a1155bb6b31e156346af41377c7617f4 Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 20 Mar 2024 11:02:24 +0100 Subject: [PATCH] feat: Add rules --- .../management/commands/populate_db.py | 5 +++++ src/fellchensammlung/migrations/0002_rule.py | 21 +++++++++++++++++++ src/fellchensammlung/models.py | 13 ++++++++++++ .../templates/fellchensammlung/about.html | 4 ++-- .../fellchensammlung/list-rules.html | 5 +++++ .../fellchensammlung/partial-rule.html | 3 +++ .../templatetags/custom_tags.py | 10 ++++++++- src/fellchensammlung/views.py | 8 +++---- 8 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/fellchensammlung/migrations/0002_rule.py create mode 100644 src/fellchensammlung/templates/fellchensammlung/list-rules.html create mode 100644 src/fellchensammlung/templates/fellchensammlung/partial-rule.html diff --git a/src/fellchensammlung/management/commands/populate_db.py b/src/fellchensammlung/management/commands/populate_db.py index 0b8aa36..746c5af 100644 --- a/src/fellchensammlung/management/commands/populate_db.py +++ b/src/fellchensammlung/management/commands/populate_db.py @@ -29,6 +29,11 @@ class Command(BaseCommand): rat2 = baker.make(Animal, name="Rat2", adoption_notice=adoption1, species=rat) cat1 = baker.make(Animal, name="Cat1", adoption_notice=adoption2, species=cat) + rule1 = baker.make(Rule, title="Be excellent ot each other", rule_text="This is **markdown**") + rule2 = baker.make(Rule, + title="Keep al least the minimum number of animals for species", + rule_text="This is not markdown") + User.objects.create_user('test', password='foobar') User.objects.create_superuser(username="admin", password="admin") diff --git a/src/fellchensammlung/migrations/0002_rule.py b/src/fellchensammlung/migrations/0002_rule.py new file mode 100644 index 0000000..fd3c945 --- /dev/null +++ b/src/fellchensammlung/migrations/0002_rule.py @@ -0,0 +1,21 @@ +# Generated by Django 5.0.3 on 2024-03-20 09:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('fellchensammlung', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Rule', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('rule_text', models.TextField()), + ], + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 51a33e4..1bc746d 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -135,3 +135,16 @@ class MarkdownContent(models.Model): def __str__(self): return self.title + +class Rule(models.Model): + """ + Class to store rules + """ + title = models.CharField(max_length=200) + + # Markdown is allowed in rule text + rule_text = models.TextField() + + def __str__(self): + return self.title + diff --git a/src/fellchensammlung/templates/fellchensammlung/about.html b/src/fellchensammlung/templates/fellchensammlung/about.html index 35b2e63..db444ab 100644 --- a/src/fellchensammlung/templates/fellchensammlung/about.html +++ b/src/fellchensammlung/templates/fellchensammlung/about.html @@ -2,6 +2,6 @@ {% load i18n %} {% block content %} -

{{ markdown_content.title }}

-

{{ markdown_content.content|safe }}

+

Rules

+ {% include "fellchensammlung/list-rules.html" %} {% endblock %} \ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/list-rules.html b/src/fellchensammlung/templates/fellchensammlung/list-rules.html new file mode 100644 index 0000000..f170da5 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/list-rules.html @@ -0,0 +1,5 @@ +
+{% for rule in rules %} + {% include "fellchensammlung/partial-rule.html" %} +{% endfor %} +
\ No newline at end of file diff --git a/src/fellchensammlung/templates/fellchensammlung/partial-rule.html b/src/fellchensammlung/templates/fellchensammlung/partial-rule.html new file mode 100644 index 0000000..6a10865 --- /dev/null +++ b/src/fellchensammlung/templates/fellchensammlung/partial-rule.html @@ -0,0 +1,3 @@ +{% load custom_tags %} +

{{ rule.title }}

+

{{ rule.rule_text | render_markdown }}

\ No newline at end of file diff --git a/src/fellchensammlung/templatetags/custom_tags.py b/src/fellchensammlung/templatetags/custom_tags.py index 1e7b3cd..eb31f49 100644 --- a/src/fellchensammlung/templatetags/custom_tags.py +++ b/src/fellchensammlung/templatetags/custom_tags.py @@ -1,4 +1,8 @@ +import markdown + from django import template +from django.template.defaultfilters import stringfilter +from django.utils.safestring import mark_safe register = template.Library() @@ -26,4 +30,8 @@ def join_link(value, arg): def get_type(value): return type(value) - +@register.filter +@stringfilter +def render_markdown(value): + md = markdown.Markdown(extensions=["fenced_code"]) + return mark_safe(md.convert(value)) diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 3005e1d..51202cb 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -3,7 +3,7 @@ from django.http import HttpResponse from django.urls import reverse import markdown -from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal +from fellchensammlung.models import AdoptionNotice, MarkdownContent, Animal, Rule from .forms import AdoptionNoticeForm, AnimalForm @@ -64,10 +64,8 @@ def add_animal_to_adoption(request, adoption_notice_id): def about(request): - md = markdown.Markdown(extensions=["fenced_code"]) - markdown_content = MarkdownContent.objects.first() - markdown_content.content = md.convert(markdown_content.content) - context = {"markdown_content": markdown_content} + rules = Rule.objects.all() + context = {"rules": rules} return render( request, "fellchensammlung/about.html",