feat: Add comments to adoption notices

This commit is contained in:
moanos [he/him] 2024-05-30 13:58:24 +02:00
parent a16098f648
commit e77a936c30
8 changed files with 132 additions and 10 deletions

View File

@ -3,7 +3,7 @@ from django.contrib import admin
from .models import User, Language, Text
from .models import Animal, Species, RescueOrganization, AdoptionNotice, Location, Rule, Image, ModerationAction, \
Report, Member
Report, Member, Comment
# Define an inline admin descriptor for Employee model
@ -33,3 +33,4 @@ admin.site.register(Report)
admin.site.register(ModerationAction)
admin.site.register(Language)
admin.site.register(Text)
admin.site.register(Comment)

View File

@ -6,7 +6,8 @@ from django.core.files import File
from fellchensammlung import baker_recipes
from model_bakery import baker
from fellchensammlung.models import AdoptionNotice, Species, Animal, Image, ModerationAction, User, Member, Rule, Report
from fellchensammlung.models import AdoptionNotice, Species, Animal, Image, ModerationAction, User, Member, Rule, \
Report, Comment
class Command(BaseCommand):
@ -24,7 +25,6 @@ class Command(BaseCommand):
except AdoptionNotice.DoesNotExist:
pass
rescue1 = baker.make_recipe(
'fellchensammlung.rescue_org'
)
@ -88,5 +88,12 @@ class Command(BaseCommand):
mod1.trust_level = Member.MODERATOR
mod1.save()
comment1 = baker.make(Comment, user=admin1, text="This is a comment", adoption_notice=adoption1)
comment2 = baker.make(Comment,
user=mod1,
text="This is a reply",
adoption_notice=adoption1,
reply_to=comment1)
def handle(self, *args, **options):
self.populate_db()

View File

@ -0,0 +1,56 @@
# Generated by Django 5.0.6 on 2024-06-03 19:22
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("fellchensammlung", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="Comment",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("text", models.TextField(verbose_name="Inhalt")),
(
"adoption_notice",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="fellchensammlung.adoptionnotice",
verbose_name="AdoptionNotice",
),
),
(
"reply_to",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="fellchensammlung.comment",
verbose_name="Antwort auf",
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="fellchensammlung.member",
verbose_name="Nutzer*in",
),
),
],
),
]

View File

@ -44,7 +44,7 @@ class Image(models.Model):
@property
def as_html(self):
return f'<img src="{ MEDIA_URL }/{ self.image }" alt="{ self.alt_text }">'
return f'<img src="{MEDIA_URL}/{self.image}" alt="{self.alt_text}">'
class Species(models.Model):
@ -116,6 +116,10 @@ class AdoptionNotice(models.Model):
def animals(self):
return Animal.objects.filter(adoption_notice=self)
@property
def comments(self):
return Comment.objects.filter(adoption_notice=self)
def get_absolute_url(self):
"""Returns the url to access a detailed page for the animal."""
return reverse('adoption-notice-detail', args=[str(self.id)])
@ -350,3 +354,17 @@ class Text(models.Model):
def __str__(self):
return f"{self.title} ({self.language})"
class Comment(models.Model):
"""
Class to store comments in markdown content
"""
user = models.ForeignKey(Member, on_delete=models.CASCADE, verbose_name=_('Nutzer*in'))
created_at = models.DateTimeField(auto_now_add=True)
adoption_notice = models.ForeignKey(AdoptionNotice, on_delete=models.CASCADE, verbose_name=_('AdoptionNotice'))
text = models.TextField(verbose_name="Inhalt")
reply_to = models.ForeignKey("self", verbose_name="Antwort auf", blank=True, null=True, on_delete=models.CASCADE)
def __str__(self):
return f"{self.user} at {self.created_at.strftime('%H:%M %d.%m.%y')}: {self.text:.10}"

View File

@ -60,7 +60,7 @@ th {
background-color: var(--secondary-light-two);
}
h1 {
h1, h2 {
word-wrap: break-word;
color: var(--text-one);
text-shadow: 2px 2px var(--shadow-one);
@ -410,4 +410,21 @@ form {
display: inline-block;
float: none;
}
}
.container-comments {
display: flex;
flex-wrap: wrap;
background: var(--background-two);
border-radius: 8px;
padding: 5px;
}
.comment {
flex: 1 100%;
margin: 10px;
border-radius: 8px;
padding: 5px;
background: var(--background-three);
color: var(--text-two);
}

View File

@ -37,9 +37,6 @@
{% endif %}
</p>
<div class="container-cards">
{% for animal in adoption_notice.animals %}
{% include "fellchensammlung/partials/partial-animal-card.html" %}
{% endfor %}
</div>
{% include "fellchensammlung/partials/partial-comment-section.html" %}
{% endblock %}

View File

@ -0,0 +1,13 @@
{% load i18n %}
<div class="container-comments">
<h2>{% translate 'Comments' %}</h2>
{% if adoption_notice.comments %}
{% for comment in adoption_notice.comments %}
{% include "fellchensammlung/partials/partial-comment.html" %}
{% endfor %}
{% else %}
{% translate 'Noch keine Kommentare' %}
{% endif %}
</div>

View File

@ -0,0 +1,13 @@
{% load i18n %}
{% load custom_tags %}
<div class="comment">
<div class="comment-header">
<b>{{ comment.user }}</b>
{{ comment.created_at }}
</div>
<p>
{{ comment.text | render_markdown }}
</p>
</div>