diff --git a/clipping_manager/admin.py b/clipping_manager/admin.py index 876c18c..6b8194e 100644 --- a/clipping_manager/admin.py +++ b/clipping_manager/admin.py @@ -3,7 +3,7 @@ from django.contrib.auth.models import User from django.db import models -from clipping_manager.models import Clipping, Book, EmailDelivery +from clipping_manager.models import Clipping, Book, EmailDelivery, MyClippingsFile @admin.register(Clipping) @@ -23,6 +23,11 @@ class EmailDeliveryAdmin(admin.ModelAdmin): list_filter = ('user', ) list_display = ('user', 'interval', 'last_delivery', ) +@admin.register(MyClippingsFile) +class MyClippingsFileAdmin(admin.ModelAdmin): + search_fields = ('language_header',) + list_display = ('language_header', 'uploaded_at',) + # Custom UserAdmin class UserAdmin(admin.ModelAdmin): diff --git a/clipping_manager/managers.py b/clipping_manager/managers.py index 2468990..34283c8 100644 --- a/clipping_manager/managers.py +++ b/clipping_manager/managers.py @@ -27,4 +27,15 @@ def random(self, limit=None): class BookQuerySetManager(models.QuerySet): def for_user(self, user): - return self.filter(user=user) \ No newline at end of file + return self.filter(user=user) + +class MyClippingsFileManager(models.Manager): + def create_file(self, content, language_header): + # Make sure file creation proceeds + # even if language_header > field's max_length + if language_header: + language_header = language_header if len(language_header) <= 255 else language_header[:255] + + my_clippings_file = self.create(content=content, language_header=language_header) + + return my_clippings_file \ No newline at end of file diff --git a/clipping_manager/migrations/0013_myclippingsfile.py b/clipping_manager/migrations/0013_myclippingsfile.py new file mode 100644 index 0000000..cc5b266 --- /dev/null +++ b/clipping_manager/migrations/0013_myclippingsfile.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.23 on 2021-07-27 07:43 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('clipping_manager', '0012_data_generate_clipping_hashs'), + ] + + operations = [ + migrations.CreateModel( + name='MyClippingsFile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('content', models.TextField(verbose_name='Content')), + ('uploaded_at', models.DateTimeField(auto_now_add=True, verbose_name='Upload datetime')), + ('language_header', models.CharField(blank=True, max_length=255, null=True, verbose_name='Accept-Language header')), + ], + options={ + 'verbose_name': 'MyClippings file', + 'verbose_name_plural': 'MyClippings files', + }, + ), + ] diff --git a/clipping_manager/models/__init__.py b/clipping_manager/models/__init__.py index 0f34664..8bd9a5b 100644 --- a/clipping_manager/models/__init__.py +++ b/clipping_manager/models/__init__.py @@ -1,3 +1,4 @@ from .book import * from .clipping import * -from .email_delivery import * \ No newline at end of file +from .email_delivery import * +from .my_clippings_file import * \ No newline at end of file diff --git a/clipping_manager/models/my_clippings_file.py b/clipping_manager/models/my_clippings_file.py new file mode 100644 index 0000000..bd00ff7 --- /dev/null +++ b/clipping_manager/models/my_clippings_file.py @@ -0,0 +1,29 @@ +from django.db import models +from django.utils.translation import ugettext_lazy as _ +from clipping_manager.managers import MyClippingsFileManager + + +class MyClippingsFile(models.Model): + objects = MyClippingsFileManager() + + content = models.TextField( + verbose_name=_('Content'), + blank=False + ) + + uploaded_at = models.DateTimeField( + verbose_name=_('Upload datetime'), + auto_now_add=True, + blank=False + ) + + language_header = models.CharField( + verbose_name=_('Accept-Language header'), + max_length=255, + blank=True, + null=True + ) + + class Meta: + verbose_name = _('MyClippings file') + verbose_name_plural = _('MyClippings files') \ No newline at end of file diff --git a/clipping_manager/views.py b/clipping_manager/views.py index 5f03350..64e426f 100644 --- a/clipping_manager/views.py +++ b/clipping_manager/views.py @@ -21,7 +21,7 @@ from clipping_manager.clipping_parser import kindle_clipping_parser, plaintext_parser from clipping_manager.filters import ClippingFilter from clipping_manager.forms import UploadKindleClippingsForm, UploadTextClippings -from clipping_manager.models import Clipping, Book +from clipping_manager.models import Clipping, Book, MyClippingsFile from clipping_manager.models.email_delivery import EmailDelivery logger = logging.getLogger(__name__) @@ -79,6 +79,13 @@ def form_valid(self, form): clippings_file = EncodedFile(self.request.FILES['clippings_file'], data_encoding='utf-8', errors='ignore') clippings_file_content = clippings_file.read() clips = kindle_clipping_parser.get_clips_from_text(clippings_file_content) + + # Save the file in db + language_header = self.request.META.get('HTTP_ACCEPT_LANGUAGE') + MyClippingsFile.objects.create_file( + content=clippings_file_content, + language_header=language_header + ) except Exception as e: logger.error(f'Error parsing a clippings file.', exc_info=True) messages.add_message(