From 4190488376235333ba2c0ef689ba15c52ddd1f85 Mon Sep 17 00:00:00 2001 From: Dipen Patel Date: Tue, 12 Oct 2021 16:51:43 +0530 Subject: [PATCH] Moderated objects unavailable even with visible_until_rejected = True #188 --- moderation/constants.py | 1 + moderation/managers.py | 8 ++++++-- .../migrations/0006_auto_20211012_1311.py | 18 ++++++++++++++++++ moderation/models.py | 14 ++++++++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 moderation/migrations/0006_auto_20211012_1311.py diff --git a/moderation/constants.py b/moderation/constants.py index 338e18d..2d52de7 100644 --- a/moderation/constants.py +++ b/moderation/constants.py @@ -4,3 +4,4 @@ MODERATION_STATUS_REJECTED = 0 MODERATION_STATUS_APPROVED = 1 MODERATION_STATUS_PENDING = 2 +MODERATION_STATUS_VISIBLE_UNTIL_REJECTED = 3 diff --git a/moderation/managers.py b/moderation/managers.py index 086e916..e81bc4a 100644 --- a/moderation/managers.py +++ b/moderation/managers.py @@ -3,7 +3,7 @@ from django.db.models.manager import Manager from . import moderation -from .constants import MODERATION_READY_STATE +from .constants import MODERATION_READY_STATE, MODERATION_STATUS_VISIBLE_UNTIL_REJECTED from .queryset import ModeratedObjectQuerySet @@ -45,7 +45,11 @@ def filter_moderated_objects(self, queryset): only_ready = { '_relation_object__state': MODERATION_READY_STATE, } - return queryset.filter(Q(**only_no_relation_objects) | Q(**only_ready)) + visible_until_rejected = { + '_relation_object__status': MODERATION_STATUS_VISIBLE_UNTIL_REJECTED, + } + return queryset.filter(Q(**only_no_relation_objects) | Q(**only_ready) + | Q(**visible_until_rejected)) def exclude_objs_by_visibility_col(self, query_set): return query_set.exclude(**{self.moderator.visibility_column: False}) diff --git a/moderation/migrations/0006_auto_20211012_1311.py b/moderation/migrations/0006_auto_20211012_1311.py new file mode 100644 index 0000000..1637a57 --- /dev/null +++ b/moderation/migrations/0006_auto_20211012_1311.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.14 on 2021-10-12 10:11 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('moderation', '0005_auto_20190412_0442'), + ] + + operations = [ + migrations.AlterField( + model_name='moderatedobject', + name='status', + field=models.SmallIntegerField(choices=[(0, 'Rejected'), (1, 'Approved'), (2, 'Pending'), (3, 'Visible until rejected')], default=2, editable=False), + ), + ] diff --git a/moderation/models.py b/moderation/models.py index d1c990e..76a54f6 100644 --- a/moderation/models.py +++ b/moderation/models.py @@ -9,7 +9,8 @@ from . import moderation from .constants import (MODERATION_DRAFT_STATE, MODERATION_READY_STATE, MODERATION_STATUS_APPROVED, - MODERATION_STATUS_PENDING, MODERATION_STATUS_REJECTED) + MODERATION_STATUS_PENDING, MODERATION_STATUS_REJECTED, + MODERATION_STATUS_VISIBLE_UNTIL_REJECTED) from .diff import get_changes_between_models from .fields import SerializedObjectField from .managers import ModeratedObjectManager @@ -24,6 +25,7 @@ (MODERATION_STATUS_REJECTED, 'rejected', _('Rejected')), (MODERATION_STATUS_APPROVED, 'approved', _('Approved')), (MODERATION_STATUS_PENDING, 'pending', _('Pending')), + (MODERATION_STATUS_VISIBLE_UNTIL_REJECTED, 'visible_until_rejected', _('Visible until rejected')), ) @@ -102,6 +104,8 @@ def automoderate(self, user=None): self.reject(by=self.by, reason=reason) elif status == MODERATION_STATUS_APPROVED: self.approve(by=self.by, reason=reason) + elif self.moderator.visible_until_rejected: + self.visible_until_rejected(by=self.by, reason=reason) else: # MODERATION_STATUS_PENDING self.save() @@ -119,6 +123,9 @@ def _get_moderation_status_and_reason(self, obj, user): if reason: return MODERATION_STATUS_APPROVED, reason + if self.moderator.visible_until_rejected: + return MODERATION_STATUS_VISIBLE_UNTIL_REJECTED, None + return MODERATION_STATUS_PENDING, None def get_object_for_this_type(self): @@ -207,7 +214,7 @@ def _moderate(self, new_status, by, reason): # inherited visibility_column. base_object._save_parents(base_object.__class__, None, None) - if self.changed_by: + if self.changed_by and self.status != MODERATION_STATUS_VISIBLE_UNTIL_REJECTED: self.moderator.inform_user(self.content_object, self.changed_by) def has_object_been_changed(self, original_obj, only_excluded=False): @@ -234,3 +241,6 @@ def approve(self, by=None, reason=None): def reject(self, by=None, reason=None): self._send_signals_and_moderate(MODERATION_STATUS_REJECTED, by, reason) + + def visible_until_rejected(self, by=None, reason=None): + self._send_signals_and_moderate(MODERATION_STATUS_VISIBLE_UNTIL_REJECTED, by, reason)