From 04e2046b205fb6bacf48e6506e4c977d43d52302 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frsposito@users.noreply.github.com> Date: Fri, 29 Nov 2019 00:15:59 +0100 Subject: [PATCH] Add computed field (book status) --- segnalibre/settings.py | 1 + segnalibre_app/migrations/0011_book_status.py | 19 +++++++++++++++++++ .../migrations/0012_auto_20191128_2322.py | 19 +++++++++++++++++++ .../migrations/0013_auto_20191128_2344.py | 18 ++++++++++++++++++ .../migrations/0014_auto_20191128_2347.py | 18 ++++++++++++++++++ segnalibre_app/models.py | 9 ++++++++- segnalibre_app/serializers.py | 1 + segnalibre_app/views.py | 12 +++++++++++- 8 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 segnalibre_app/migrations/0011_book_status.py create mode 100644 segnalibre_app/migrations/0012_auto_20191128_2322.py create mode 100644 segnalibre_app/migrations/0013_auto_20191128_2344.py create mode 100644 segnalibre_app/migrations/0014_auto_20191128_2347.py diff --git a/segnalibre/settings.py b/segnalibre/settings.py index 6fa0d1a..fe40b0b 100644 --- a/segnalibre/settings.py +++ b/segnalibre/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'rest_framework', 'djoser', + 'computedfields', 'corsheaders', 'segnalibre_app' ] diff --git a/segnalibre_app/migrations/0011_book_status.py b/segnalibre_app/migrations/0011_book_status.py new file mode 100644 index 0000000..e2fa4b6 --- /dev/null +++ b/segnalibre_app/migrations/0011_book_status.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.7 on 2019-11-28 22:15 + +import computed_property.fields +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('segnalibre_app', '0010_auto_20191123_1356'), + ] + + operations = [ + migrations.AddField( + model_name='book', + name='status', + field=computed_property.fields.ComputedCharField(compute_from='calculate_status', default='uncompleted', editable=False, max_length=30), + ), + ] diff --git a/segnalibre_app/migrations/0012_auto_20191128_2322.py b/segnalibre_app/migrations/0012_auto_20191128_2322.py new file mode 100644 index 0000000..2dd5ed8 --- /dev/null +++ b/segnalibre_app/migrations/0012_auto_20191128_2322.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.7 on 2019-11-28 22:22 + +import computed_property.fields +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('segnalibre_app', '0011_book_status'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='status', + field=computed_property.fields.ComputedCharField(compute_from='calculate_status', editable=False, max_length=30, null=True), + ), + ] diff --git a/segnalibre_app/migrations/0013_auto_20191128_2344.py b/segnalibre_app/migrations/0013_auto_20191128_2344.py new file mode 100644 index 0000000..690129a --- /dev/null +++ b/segnalibre_app/migrations/0013_auto_20191128_2344.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-11-28 22:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('segnalibre_app', '0012_auto_20191128_2322'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='status', + field=models.CharField(max_length=32, null=True), + ), + ] diff --git a/segnalibre_app/migrations/0014_auto_20191128_2347.py b/segnalibre_app/migrations/0014_auto_20191128_2347.py new file mode 100644 index 0000000..42ceac1 --- /dev/null +++ b/segnalibre_app/migrations/0014_auto_20191128_2347.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-11-28 22:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('segnalibre_app', '0013_auto_20191128_2344'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='status', + field=models.CharField(default='uncompleted', max_length=32), + ), + ] diff --git a/segnalibre_app/models.py b/segnalibre_app/models.py index 001a460..daa8f30 100644 --- a/segnalibre_app/models.py +++ b/segnalibre_app/models.py @@ -1,7 +1,8 @@ from django.db import models +from computedfields.models import ComputedFieldsModel, computed # Create your models here. -class Book(models.Model): +class Book(ComputedFieldsModel): title = models.CharField(max_length=100) author = models.CharField(max_length=100) cover_url = models.URLField(null=True) @@ -16,3 +17,9 @@ class Book(models.Model): def _str_(self): return self.title + @computed(models.CharField(max_length=32, default='uncompleted')) + def status(self): + if self.position == self.pages: + return 'completed' + else: + return 'uncompleted' diff --git a/segnalibre_app/serializers.py b/segnalibre_app/serializers.py index b6ee069..64ca8c9 100644 --- a/segnalibre_app/serializers.py +++ b/segnalibre_app/serializers.py @@ -14,6 +14,7 @@ class BookSerializer(serializers.ModelSerializer): 'cover_url', 'publisher', 'pub_date', + 'status', 'pages', 'position', 'owner', diff --git a/segnalibre_app/views.py b/segnalibre_app/views.py index 2072e95..259ac48 100644 --- a/segnalibre_app/views.py +++ b/segnalibre_app/views.py @@ -27,7 +27,17 @@ class BookList(generics.ListCreateAPIView): ordering = ['title'] def get_queryset(self): - return Book.objects.filter(owner=self.request.user) + queryset = Book.objects.filter(owner=self.request.user) + show = self.request.query_params.get('show', None) + + if show == 'all': + queryset = queryset.filter(status='completed') | queryset.filter(status='uncompleted') + elif show == 'completed': + queryset = queryset.filter(status='completed') + else: + queryset = queryset.filter(status='uncompleted') + + return queryset def perform_create(self, serializer): serializer.save(owner=self.request.user)