Added primitive doc type checking

This commit is contained in:
Tris Forster 2023-03-01 13:56:52 +11:00
parent 6e1b26440f
commit b11cfcd6ea
4 changed files with 21 additions and 7 deletions

View File

@ -75,9 +75,8 @@ class Ensemble(models.Model):
class Meta:
ordering = ('slug', )
#def active_projects(self):
# #return self.projects.filter(active=True, event_date__gte=timezone.now())
# return self.projects.active().current()
def active_projects(self):
return self.projects.active().current()
def has_admin(self, user):
if not user.is_authenticated:

View File

@ -309,15 +309,21 @@ class Document(models.Model):
DOCTYPE_PDF = 1
DOCTYPE_AUDIO = 2
DOCTYPE_VIDEO = 3
DOCTYPE_SOURCE = 4
DOCTYPE_MISC= 4
DOCTYPES = (
(DOCTYPE_PDF, 'PDF'),
(DOCTYPE_AUDIO, 'Audio'),
(DOCTYPE_VIDEO, 'Video'),
(DOCTYPE_SOURCE, 'Source'),
(DOCTYPE_MISC, 'Misc'),
)
DOCTYPE_MAP = {
'.pdf': DOCTYPE_PDF,
'.mp3': DOCTYPE_AUDIO,
'.mp4': DOCTYPE_VIDEO,
}
work = models.ForeignKey('Work', on_delete=models.CASCADE, related_name="docs")
doctype = models.PositiveSmallIntegerField(choices=DOCTYPES, default=DOCTYPE_PDF)
upload = models.FileField(upload_to=doc_upload_filename, storage=library_storage)

View File

@ -9,8 +9,10 @@
</td>
<td class="has-text-right" style="white-space: nowrap;">
{% if request.is_admin %}
{% if doc.doctype == 1 %}
<a href="{% url 'document_annotate' collection.pk doc.pk %}"><i class="fas fa-tags"
title="Manage Tags"></i></a>
{% endif %}
<a href="{% url 'document_delete' collection.pk doc.pk %}"><i class="fas fa-trash-alt" title="Delete Document"></i></a>
{% endif %}
</td>

View File

@ -23,6 +23,8 @@ from library.music_tags import MUSIC_TAGS, MUSIC_TAG_BY_NAME
from library import forms, models
from library.pdf_utils import extract_pages, extract_and_concat
import logging
logger = logging.getLogger(__name__)
class ProjectItemListView(ProjectMixin, ListView):
template_name = "library/item_list.html"
@ -321,14 +323,18 @@ class WorkAddDocumentView(CollectionMixin, CreateView):
return super().form_invalid(form)
def form_valid(self, form):
orig_name, ext = os.path.splitext(form.cleaned_data['upload'].name)
logger.info("Uploaded: %s", orig_name)
doc = form.save(commit=False)
doc.doctype = models.Document.DOCTYPE_MAP.get(ext.lower(), models.Document.DOCTYPE_MISC)
doc.work_id = self.kwargs['pk']
doc.save()
# auto tag the document
name, _ = os.path.splitext(os.path.basename(doc.upload.name))
parts = re.split(r'[^A-Za-z]+', name)
#name, ext = os.path.splitext(os.path.basename(doc.upload.name))
parts = re.split(r'[^A-Za-z]+', orig_name)
parts.reverse()
for word in parts:
try:
@ -339,6 +345,7 @@ class WorkAddDocumentView(CollectionMixin, CreateView):
pass
if self.request.headers['Accept'] == 'application/json':
filename = os.path.basename(doc.upload.name)
return JsonResponse({