Add computed field (book status)

This commit is contained in:
Francesco Esposito 2019-11-29 00:15:59 +01:00
parent 6c27cbe7c1
commit 04e2046b20
8 changed files with 95 additions and 2 deletions

View File

@ -40,6 +40,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework', 'rest_framework',
'djoser', 'djoser',
'computedfields',
'corsheaders', 'corsheaders',
'segnalibre_app' 'segnalibre_app'
] ]

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -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),
),
]

View File

@ -1,7 +1,8 @@
from django.db import models from django.db import models
from computedfields.models import ComputedFieldsModel, computed
# Create your models here. # Create your models here.
class Book(models.Model): class Book(ComputedFieldsModel):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
author = models.CharField(max_length=100) author = models.CharField(max_length=100)
cover_url = models.URLField(null=True) cover_url = models.URLField(null=True)
@ -16,3 +17,9 @@ class Book(models.Model):
def _str_(self): def _str_(self):
return self.title return self.title
@computed(models.CharField(max_length=32, default='uncompleted'))
def status(self):
if self.position == self.pages:
return 'completed'
else:
return 'uncompleted'

View File

@ -14,6 +14,7 @@ class BookSerializer(serializers.ModelSerializer):
'cover_url', 'cover_url',
'publisher', 'publisher',
'pub_date', 'pub_date',
'status',
'pages', 'pages',
'position', 'position',
'owner', 'owner',

View File

@ -27,7 +27,17 @@ class BookList(generics.ListCreateAPIView):
ordering = ['title'] ordering = ['title']
def get_queryset(self): 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): def perform_create(self, serializer):
serializer.save(owner=self.request.user) serializer.save(owner=self.request.user)