feat: Add rules

This commit is contained in:
moanos [he/him] 2024-03-20 11:02:24 +01:00
parent 1bcdbdbbf5
commit 079b2073a1
8 changed files with 61 additions and 8 deletions

View File

@ -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")

View File

@ -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()),
],
),
]

View File

@ -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

View File

@ -2,6 +2,6 @@
{% load i18n %}
{% block content %}
<h1>{{ markdown_content.title }}</h1>
<p>{{ markdown_content.content|safe }}</p>
<h1>Rules</h1>
{% include "fellchensammlung/list-rules.html" %}
{% endblock %}

View File

@ -0,0 +1,5 @@
<div class="list-rules">
{% for rule in rules %}
{% include "fellchensammlung/partial-rule.html" %}
{% endfor %}
</div>

View File

@ -0,0 +1,3 @@
{% load custom_tags %}
<h2>{{ rule.title }}</h2>
<p>{{ rule.rule_text | render_markdown }}</p>

View File

@ -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))

View File

@ -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",