From e54d8884bf51b54676cf274b52bb5396eca2020b Mon Sep 17 00:00:00 2001 From: Hojagulyyev Date: Tue, 26 Dec 2023 22:12:49 +0500 Subject: [PATCH] feat(diary): make create comment interactor --- apps/diaries/interactors.py | 41 ++++++++++++++++++- ...s_last_read_by_alter_diarycomment_diary.py | 25 +++++++++++ apps/diaries/models.py | 3 +- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 apps/diaries/migrations/0005_diary_comments_last_read_by_alter_diarycomment_diary.py diff --git a/apps/diaries/interactors.py b/apps/diaries/interactors.py index 5d44a26..6464889 100644 --- a/apps/diaries/interactors.py +++ b/apps/diaries/interactors.py @@ -7,7 +7,7 @@ from rp2.business_logic import COMMIT_MIN_LENGTH -from .models import Diary, DiaryCommit +from .models import Diary, DiaryCommit, DiaryComment from .signals import diary_commit_created @@ -59,3 +59,42 @@ def create_commit(request, diary_id: int): diary_commit_created.send(sender=DiaryCommit, instance=diary_commit) return redirect("diaries:detail_view", diary.id) + + +@login_required +def create_comment(request, diary_id: int): + + # ===== DTO + + body = request.POST.get("body", "").strip() + diary = Diary.objects.get(id=diary_id) + + # ===== VALIDATION + + if ( + DiaryComment.objects + .filter( + diary=diary, + body=body, + created_datetime__date=datetime.date.today() + ) + .exists() + ): + messages.error(request, message="this message already exists for today") + return redirect( + f"{reverse(viewname='diaries:detail_view', kwargs={'id': diary.id})}" + f"?body={body}" + ) + + # ===== PROCESS + + diary_comment = DiaryComment() + diary_comment.diary = diary + diary_comment.author = request.user.account + diary_comment.body = body + diary_comment.save() + + diary.comments_last_read_by = request.user.account + diary.save(update_fields=["comments_last_read_by"]) + + return redirect("diaries:detail_view", diary.id) diff --git a/apps/diaries/migrations/0005_diary_comments_last_read_by_alter_diarycomment_diary.py b/apps/diaries/migrations/0005_diary_comments_last_read_by_alter_diarycomment_diary.py new file mode 100644 index 0000000..d367ebd --- /dev/null +++ b/apps/diaries/migrations/0005_diary_comments_last_read_by_alter_diarycomment_diary.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.6 on 2023-12-26 16:59 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0005_delete_xp'), + ('diaries', '0004_alter_diary_account'), + ] + + operations = [ + migrations.AddField( + model_name='diary', + name='comments_last_read_by', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='accounts.account'), + ), + migrations.AlterField( + model_name='diarycomment', + name='diary', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='diaries.diary'), + ), + ] diff --git a/apps/diaries/models.py b/apps/diaries/models.py index cc1b1b7..8037c2a 100644 --- a/apps/diaries/models.py +++ b/apps/diaries/models.py @@ -9,6 +9,7 @@ class Diary(models.Model): account = models.ForeignKey(Account, on_delete=models.CASCADE, related_name="diaries") + comments_last_read_by = models.ForeignKey(Account, on_delete=models.CASCADE, blank=True, null=True) created_date = models.DateField(default=timezone.now) class Meta: @@ -37,7 +38,7 @@ def __str__(self): class DiaryComment(models.Model): - diary = models.ForeignKey(Diary, on_delete=models.CASCADE) + diary = models.ForeignKey(Diary, on_delete=models.CASCADE, related_name="comments") author = models.ForeignKey(Account, on_delete=models.CASCADE) body = models.TextField() created_datetime = models.DateTimeField(default=timezone.now)