refactoring

This commit is contained in:
Tomáš Mládek 2020-04-14 15:34:13 +02:00
parent 02c157337c
commit e724748a5f
3 changed files with 20 additions and 24 deletions

View file

@ -1,5 +1,4 @@
from django.contrib import admin from django.contrib import admin
from django.db.models import Q
from sdbs_pile.pile.models import Tag, Document from sdbs_pile.pile.models import Tag, Document
@ -25,9 +24,9 @@ class DocumentExternalListFilter(admin.SimpleListFilter):
def queryset(self, request, queryset): def queryset(self, request, queryset):
if self.value() == "True": if self.value() == "True":
return queryset.filter((Q(file__isnull=True) | Q(file='')) & ~Q(external_url__contains="sdbs")) return queryset.external()
elif self.value() == "False": elif self.value() == "False":
return queryset.filter((Q(file__isnull=False) & ~Q(file='')) | Q(external_url__contains="pile.sdbs.cz")) return queryset.local()
else: else:
return queryset return queryset

View file

@ -3,7 +3,7 @@ import markdown2
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
from django.db import models from django.db import models
from django.db.models import Count from django.db.models import Count, Q, QuerySet
from model_utils.models import SoftDeletableModel from model_utils.models import SoftDeletableModel
@ -13,34 +13,32 @@ class Tag(SoftDeletableModel):
@property @property
def documents_exclude_hidden(self): def documents_exclude_hidden(self):
return Document.exclude_hidden.filter(tags__in=[self]) return Document.objects.exclude_hidden().filter(tags__in=[self])
def __str__(self): def __str__(self):
return self.name return self.name
class DocumentManager(models.Manager): class DocumentQuerySet(QuerySet):
def __init__(self, include_hidden=True): def exclude_hidden(self):
super(DocumentManager, self).__init__() return super().filter(public=True)
self._include_hidden = include_hidden
def get_queryset(self):
query_set = super().get_queryset().filter(is_removed=False)
if not self._include_hidden:
return query_set.filter(public=True)
return query_set
def untagged(self): def untagged(self):
return self.get_queryset().annotate(tag_count=Count('tags')).filter(tag_count=0) return super().annotate(tag_count=Count('tags')).filter(tag_count=0)
def local(self):
return super().filter((Q(file__isnull=False) & ~Q(file='')) | Q(external_url__contains="pile.sdbs.cz"))
def external(self):
return super().filter((Q(file__isnull=True) | Q(file='')) & ~Q(external_url__contains="pile.sdbs.cz"))
class Document(SoftDeletableModel):
class DocumentStatus(models.TextChoices): class DocumentStatus(models.TextChoices):
REFERENCE = "REF", "Referential" REFERENCE = "REF", "Referential"
STANDARD = "STD", "Standard" STANDARD = "STD", "Standard"
FRAGMENT = "FRG", "Fragment" FRAGMENT = "FRG", "Fragment"
class Document(SoftDeletableModel):
title = models.CharField(max_length=512, null=False, blank=False) title = models.CharField(max_length=512, null=False, blank=False)
author = models.CharField(max_length=512, null=False, blank=True) author = models.CharField(max_length=512, null=False, blank=True)
published = models.CharField(max_length=128, null=False, blank=True) published = models.CharField(max_length=128, null=False, blank=True)
@ -53,8 +51,7 @@ class Document(SoftDeletableModel):
tags = models.ManyToManyField(Tag, related_name="documents", blank=True) tags = models.ManyToManyField(Tag, related_name="documents", blank=True)
uploaded = models.DateTimeField(auto_now_add=True, null=True) uploaded = models.DateTimeField(auto_now_add=True, null=True)
objects = DocumentManager() objects = DocumentQuerySet.as_manager()
exclude_hidden = DocumentManager(include_hidden=False)
@property @property
def html_description(self): def html_description(self):

View file

@ -26,7 +26,7 @@ class BasePileViewMixin(View):
@property @property
def documents(self): def documents(self):
return Document.objects if self.include_hidden else Document.exclude_hidden return Document.objects if self.include_hidden else Document.objects.exclude_hidden()
class BasePileView(BasePileViewMixin, TemplateView): class BasePileView(BasePileViewMixin, TemplateView):
@ -161,7 +161,7 @@ class RecentlyUploadedFeed(Feed):
description = "A list of most recently uploaded documents." description = "A list of most recently uploaded documents."
def items(self): def items(self):
return Document.exclude_hidden.order_by('-uploaded')[:5] return Document.objects.exclude_hidden().order_by('-uploaded')[:5]
def item_title(self, item: Document): def item_title(self, item: Document):
return item.title return item.title