add subdirectories field with django-jsonfield, separate actual files and subdirectories, note some TODOs

This commit is contained in:
Xevion
2020-10-31 20:45:40 -05:00
parent 02c58830f7
commit fbd623c0f2

View File

@@ -1,7 +1,9 @@
import json
import mimetypes import mimetypes
import os import os
import uuid import uuid
import jsonfield
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from easy_thumbnails.alias import aliases from easy_thumbnails.alias import aliases
@@ -26,16 +28,30 @@ class ServedDirectory(models.Model):
regex_pattern = models.CharField('RegEx Matching Pattern', max_length=100, default='') regex_pattern = models.CharField('RegEx Matching Pattern', max_length=100, default='')
regex = models.BooleanField('Directory RegEx Option', default=False) regex = models.BooleanField('Directory RegEx Option', default=False)
match_filename = models.BooleanField('RegEx Matches Against Filename', default=True) match_filename = models.BooleanField('RegEx Matches Against Filename', default=True)
known_subdirectories = jsonfield.JSONField('Tracked Subdirectories JSON', default=[])
def refresh(self): def refresh(self):
"""Refresh the directory listing to see if any new files have appeared and add them to the list.""" """Refresh the directory listing to see if any new files have appeared and add them to the list."""
# TODO: Implement separate recursive file matching implementation
# TODO: Implement RegEx filtering step
directories = []
for file in os.listdir(self.path): for file in os.listdir(self.path):
# Check if the file has been entered before file_path = os.path.join(self.path, file)
entry = self.files.filter(filename__exact=file).first()
if entry is None: if os.path.isfile(file_path):
# create the file entry # Check if the file has been entered before
entry = File.create(full_path=os.path.join(self.path, file)) entry = self.files.filter(filename__exact=file).first()
entry.save() if entry is None:
# create the file entry
entry = File.create(full_path=file_path)
entry.save()
else:
# directory found, remember it
directories.append(file_path)
# Dump subdirectories found
self.known_subdirectories = directories
def __str__(self): def __str__(self):
return self.path return self.path