Skip to content

Commit

Permalink
Feat: store each uploaded MyClippings.txt (#38)
Browse files Browse the repository at this point in the history
* add: gitginore db folder and .vscode settings

* feat: add MyClippings file model

* feat: add saving MyClippings to db

* feat: add language header field to MyClippings model

* feat: add manager for MyClippings model

* refactor: rename MyClippingsFiles model, file & DateTime field

* feat: trim long language headers in MyClippingsFileManager

* fix: imports old names

* refactor: squash MyClippingsFile migration file

* fix: wrong language_header len limit
  • Loading branch information
JSerwatka authored Jul 30, 2021
1 parent 3710e66 commit aeedfd1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
7 changes: 6 additions & 1 deletion clipping_manager/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
13 changes: 12 additions & 1 deletion clipping_manager/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ def random(self, limit=None):
class BookQuerySetManager(models.QuerySet):

def for_user(self, user):
return self.filter(user=user)
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
28 changes: 28 additions & 0 deletions clipping_manager/migrations/0013_myclippingsfile.py
Original file line number Diff line number Diff line change
@@ -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',
},
),
]
3 changes: 2 additions & 1 deletion clipping_manager/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .book import *
from .clipping import *
from .email_delivery import *
from .email_delivery import *
from .my_clippings_file import *
29 changes: 29 additions & 0 deletions clipping_manager/models/my_clippings_file.py
Original file line number Diff line number Diff line change
@@ -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')
9 changes: 8 additions & 1 deletion clipping_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit aeedfd1

Please sign in to comment.