diff --git a/sdbs_pile/pile/migrations/0004_document_hidden.py b/sdbs_pile/pile/migrations/0004_document_hidden.py new file mode 100644 index 0000000..67c9c16 --- /dev/null +++ b/sdbs_pile/pile/migrations/0004_document_hidden.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.4 on 2020-03-19 19:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pile', '0003_auto_20200318_2201'), + ] + + operations = [ + migrations.AddField( + model_name='document', + name='hidden', + field=models.BooleanField(default=False), + ), + ] diff --git a/sdbs_pile/pile/migrations/0005_auto_20200320_1329.py b/sdbs_pile/pile/migrations/0005_auto_20200320_1329.py new file mode 100644 index 0000000..12f79ff --- /dev/null +++ b/sdbs_pile/pile/migrations/0005_auto_20200320_1329.py @@ -0,0 +1,17 @@ +# Generated by Django 3.0.4 on 2020-03-20 12:29 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('pile', '0004_document_hidden'), + ] + + operations = [ + migrations.AlterModelOptions( + name='document', + options={'ordering': ['-id'], 'permissions': [('see_hidden', 'Can see hidden documents')]}, + ), + ] diff --git a/sdbs_pile/pile/models.py b/sdbs_pile/pile/models.py index 8074efb..9694d06 100644 --- a/sdbs_pile/pile/models.py +++ b/sdbs_pile/pile/models.py @@ -1,6 +1,7 @@ from django.core.exceptions import ValidationError from django.core.files.storage import FileSystemStorage from django.db import models +from django.db.models import Count from model_utils.models import SoftDeletableModel @@ -8,10 +9,29 @@ class Tag(SoftDeletableModel): name = models.CharField(max_length=128, null=False, blank=False) description = models.TextField(null=False, blank=True) + @property + def documents_exclude_hidden(self): + return Document.exclude_hidden.filter(tags__in=[self]) + def __str__(self): return self.name +class DocumentManager(models.Manager): + def __init__(self, include_hidden=True): + super(DocumentManager, self).__init__() + self._include_hidden = include_hidden + + def get_queryset(self): + if self._include_hidden: + return super().get_queryset() + else: + return super().get_queryset().filter(hidden=False) + + def untagged(self): + return self.get_queryset().annotate(tag_count=Count('tags')).filter(tag_count=0) + + class Document(SoftDeletableModel): title = models.CharField(max_length=512, null=False, blank=False) description = models.TextField(null=False, blank=True) @@ -19,9 +39,13 @@ class Document(SoftDeletableModel): published = models.CharField(max_length=128, null=False, blank=True) external_url = models.URLField(null=True, blank=True) file = models.FileField(null=True, blank=True, storage=FileSystemStorage(location='docs')) - tags = models.ManyToManyField(Tag, related_name="documents") + hidden = models.BooleanField(default=False, null=False, blank=False) + tags = models.ManyToManyField(Tag, related_name="documents", blank=True) uploaded = models.DateTimeField(auto_now_add=True, null=True) + objects = DocumentManager() + exclude_hidden = DocumentManager(include_hidden=False) + @property def url(self): if self.file: @@ -30,6 +54,9 @@ class Document(SoftDeletableModel): class Meta: ordering = ['-id'] + permissions = [ + ("see_hidden", "Can see hidden documents") + ] def get_absolute_url(self): from django.urls import reverse diff --git a/sdbs_pile/pile/templates/front_base.html b/sdbs_pile/pile/templates/front_base.html index e07a33e..f7f55ea 100644 --- a/sdbs_pile/pile/templates/front_base.html +++ b/sdbs_pile/pile/templates/front_base.html @@ -28,12 +28,12 @@