Skip to content

Commit

Permalink
✨ add FoiMessageDraft proxy model to remove drafts in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
krmax44 committed Nov 18, 2024
1 parent daa767d commit d6e3dc4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 8 additions & 4 deletions froide/foirequest/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from froide.account.models import UserTag
from froide.comments.models import FroideComment
from froide.foirequest.models.message import FoiMessageDraft
from froide.guide.models import Action
from froide.guide.utils import assign_guidance_action
from froide.helper.admin_utils import (
Expand Down Expand Up @@ -522,7 +523,6 @@ class MessageTagsFilter(MultiFilterMixin, TaggitListFilter):
class FoiMessageAdmin(admin.ModelAdmin):
save_on_top = True
list_display = (
"is_draft",
"subject",
"timestamp",
"message_page",
Expand Down Expand Up @@ -600,9 +600,7 @@ def get_urls(self):
return my_urls + urls

def get_queryset(self, request):
qs = super(FoiMessageAdmin, self).get_queryset(request)
qs = qs.prefetch_related("deliverystatus")
return qs
return self.model.objects.with_drafts(False).prefetch_related("deliverystatus")

def save_model(self, request, obj, form, change):
if (
Expand Down Expand Up @@ -752,6 +750,12 @@ def add_comment(
)


@admin.register(FoiMessageDraft)
class FoiMessageDraftAdmin(FoiMessageAdmin):
def get_queryset(self, request):
return self.model.objects.all().prefetch_related("deliverystatus")


@admin.register(MessageTag)
class MessageTagAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("name",)}
Expand Down
16 changes: 15 additions & 1 deletion froide/foirequest/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ def get_throttle_filter(self, queryset, user, extra_filters=None):
qs = qs.filter(**extra_filters)
return qs, "timestamp"

def get_drafts(self, drafts=True):
def with_drafts(self, drafts=True):
return super().get_queryset().filter(is_draft=drafts)


class FoiMessageDraftManager(FoiMessageManager):
def get_queryset(self):
return self.with_drafts()


class MessageTag(TagBase):
class Meta:
verbose_name = _("message tag")
Expand Down Expand Up @@ -746,6 +751,15 @@ def set_cached_rendered_content(self, authenticated_read, content):
FoiMessage.objects.filter(id=self.id).update(**update)


class FoiMessageDraft(FoiMessage):
objects = FoiMessageDraftManager()

class Meta:
proxy = True
verbose_name = _("Freedom of Information Message Draft")
verbose_name_plural = _("Freedom of Information Message Drafts")


class Delivery(models.TextChoices):
STATUS_UNKNOWN = ("unknown", _("unknown"))
STATUS_SENDING = ("sending", _("sending"))
Expand Down

0 comments on commit d6e3dc4

Please sign in to comment.