Browse Source

Add computed field (book status)

pull/1/head
Francesco Esposito 2 years ago
parent
commit
04e2046b20
  1. 1
      segnalibre/settings.py
  2. 19
      segnalibre_app/migrations/0011_book_status.py
  3. 19
      segnalibre_app/migrations/0012_auto_20191128_2322.py
  4. 18
      segnalibre_app/migrations/0013_auto_20191128_2344.py
  5. 18
      segnalibre_app/migrations/0014_auto_20191128_2347.py
  6. 9
      segnalibre_app/models.py
  7. 1
      segnalibre_app/serializers.py
  8. 12
      segnalibre_app/views.py

1
segnalibre/settings.py

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

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

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

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

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

9
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'

1
segnalibre_app/serializers.py

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

12
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)

Loading…
Cancel
Save