diff --git a/reader/forms.py b/reader/forms.py index 08d05168327c2f44f0c13f4bf8c6a76abfa58eea..b20e692afe84da92032b55a04ddff1be3d91c10f 100644 --- a/reader/forms.py +++ b/reader/forms.py @@ -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'} diff --git a/reader/migrations/0011_book_file_type.py b/reader/migrations/0011_book_file_type.py new file mode 100644 index 0000000000000000000000000000000000000000..a47df2c097fec29583b2b609ef3a06e8fdc1816e --- /dev/null +++ b/reader/migrations/0011_book_file_type.py @@ -0,0 +1,18 @@ +# 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), + ), + ] diff --git a/reader/migrations/0012_set_existing_book_file_type.py b/reader/migrations/0012_set_existing_book_file_type.py new file mode 100644 index 0000000000000000000000000000000000000000..cedcb4832e313de78b10170eb58536313dfc7738 --- /dev/null +++ b/reader/migrations/0012_set_existing_book_file_type.py @@ -0,0 +1,24 @@ +# 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), + ] diff --git a/reader/models.py b/reader/models.py index b86672f68adcafaa132a629702d81e71ebab4899..263de383c48eb6780824bf97ffd59bf711a71bf3 100644 --- a/reader/models.py +++ b/reader/models.py @@ -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