Skip to content
Snippets Groups Projects
Verified Commit db387f89 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Add file type field

parent 4f30e3f3
Branches
No related tags found
No related merge requests found
......@@ -42,7 +42,7 @@ class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'
exclude = ['partial_md5', "created_by"]
exclude = ['partial_md5', "created_by", "file_type"]
original_file = FileField(widget=FileUploadInput(
attrs={'accept': 'application/pdf,application/epub+zip'}
......
# Generated by Django 5.0.7 on 2024-08-25 14:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('reader', '0010_book_created_by'),
]
operations = [
migrations.AddField(
model_name='book',
name='file_type',
field=models.CharField(blank=True, max_length=5, null=True),
),
]
# Generated by Django 5.0.7 on 2024-08-25 14:33
from django.db import migrations
def add_book_file_type(apps, schema_editor):
# noinspection PyPep8Naming
Book = apps.get_model("reader", "Book")
for book in Book.objects.all():
if book.original_file.name.endswith('.pdf'):
book.file_type = 'pdf'
elif book.original_file.name.endswith('.epub'):
book.file_type = 'epub'
book.save()
class Migration(migrations.Migration):
dependencies = [
('reader', '0011_book_file_type'),
]
operations = [
migrations.RunPython(add_book_file_type),
]
......@@ -77,6 +77,8 @@ class Book(models.Model):
help_text="The source file to upload. Only accepts .pdf and .epub files."
)
file_type = models.CharField(max_length=5, null=True, blank=True)
image = models.ImageField(
null=True, blank=True,
upload_to="book-images/",
......@@ -95,6 +97,12 @@ class Book(models.Model):
# TODO maybe only do this when the original file has changed?
self.partial_md5 = partial_md5(self.original_file)
if not self.file_type:
if self.original_file.name.endswith('.pdf'):
self.file_type = 'pdf'
elif self.original_file.name.endswith('.epub'):
self.file_type = 'epub'
# If image isn't set try to create it from the original file
if not self.image:
# Try to get image
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment