add "related documents"

This commit is contained in:
Tomáš Mládek 2020-06-09 12:27:58 +02:00
parent 16a57f0a97
commit 1ea30f38af
6 changed files with 58 additions and 5 deletions

View file

@ -1,3 +1,4 @@
from django import forms
from django.contrib import admin
from sdbs_pile.pile.models import Tag, Document
@ -31,12 +32,25 @@ class DocumentExternalListFilter(admin.SimpleListFilter):
return queryset
class DocumentAdminForm(forms.ModelForm):
related = forms.ModelMultipleChoiceField(queryset=Document.objects.none())
class Meta:
model = Document
fields = '__all__'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['related'].queryset = Document.objects.exclude(pk=self.instance.pk)
class DocumentAdmin(admin.ModelAdmin):
exclude = ('is_removed',)
list_display = ('title', 'author', 'published', 'media_type', 'status', 'has_file', 'public', 'filed_under')
list_filter = ('tags', 'media_type', 'status', DocumentExternalListFilter, 'public')
search_fields = ('title', 'author', 'published')
actions = ('make_published', 'make_hidden')
form = DocumentAdminForm
def has_file(self, document: Document):
return document.file is not None and str(document.file).strip() != ''

View file

@ -0,0 +1,18 @@
# Generated by Django 3.0.4 on 2020-06-09 08:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('pile', '0010_document_media_type'),
]
operations = [
migrations.AddField(
model_name='document',
name='related',
field=models.ManyToManyField(related_name='_document_related_+', to='pile.Document'),
),
]

View file

@ -57,6 +57,7 @@ class Document(SoftDeletableModel):
max_length=3, choices=DocumentStatus.choices, default=DocumentStatus.STANDARD)
tags = models.ManyToManyField(Tag, related_name="documents", blank=True)
uploaded = models.DateTimeField(auto_now_add=True, null=True)
related = models.ManyToManyField('self', related_name='related')
objects = DocumentManager()

View file

@ -220,7 +220,7 @@ ul > li:before {
margin-bottom: .5em;
}
.index-listing-desc {
.short-doc-desc {
font-size: 12pt;
color: #bfbfbf;
padding-left: calc(20px + .5rem);
@ -231,7 +231,7 @@ ul > li:before {
text-overflow: ellipsis;
}
.index-listing-desc p {
.short-doc-desc p {
margin-top: 0;
}

View file

@ -64,6 +64,26 @@
<a href="{% url "pile:label" document.id %}">{% url "pile:label" document.id %}</a>
</div>
{% endif %}
{% if document.related.exists %}
<hr>
<h2>Related documents</h2>
<ul>
{% for related in document.related.all %}
<li>
<a href="{% url 'pile:document' related.id %}">
{% if related.uploaded %}
<em>({{ related.uploaded|date:"Y/m/d G:i:s" }})</em>
{% endif %}
{{ related.title }}
<div class="short-doc-desc">
{{ related.plain_description }}
</div>
</a>
</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endblock %}

View file

@ -17,12 +17,12 @@
<a href="{% url 'pile:document' random_document.id %}">
<span>#{{ random_document.id }}: {{ random_document.title }}</span>
<div class="index-listing-desc">
<div class="short-doc-desc">
{% if random_document.tags.count > 0 %}
<div class="random-tags">Filed under: {{ random_document.tags.all | join:" / " }}</div>
{% endif %}
</div>
<div class="index-listing-desc">
<div class="short-doc-desc">
{{ random_document.plain_description }}
</div>
</a>
@ -41,7 +41,7 @@
{% endif %}
{{ document.title }}
<div class="index-listing-desc">
<div class="short-doc-desc">
{{ document.plain_description }}
</div>
</a>