diff --git a/djaif/book/admin.py b/djaif/book/admin.py index a154276..ae46127 100644 --- a/djaif/book/admin.py +++ b/djaif/book/admin.py @@ -15,4 +15,6 @@ class BookAdmin(admin.ModelAdmin): admin.site.register(models.BookPage, Admin) admin.site.register(models.PageLink, Admin) admin.site.register(models.BookProgress) +admin.site.register(models.ProgressSave) admin.site.register(models.Item) +admin.site.register(models.DroppedItem) diff --git a/djaif/book/migrations/0014_droppeditem.py b/djaif/book/migrations/0014_droppeditem.py new file mode 100644 index 0000000..21c5510 --- /dev/null +++ b/djaif/book/migrations/0014_droppeditem.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.5 on 2021-03-10 16:37 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0013_progresssave_updated_at'), + ] + + operations = [ + migrations.CreateModel( + name='DroppedItem', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('book_page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookPage')), + ('book_progress', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookProgress')), + ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.Item')), + ], + ), + ] diff --git a/djaif/book/migrations/0015_droppeditemsave.py b/djaif/book/migrations/0015_droppeditemsave.py new file mode 100644 index 0000000..2479dee --- /dev/null +++ b/djaif/book/migrations/0015_droppeditemsave.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.5 on 2021-03-10 17:55 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0014_droppeditem'), + ] + + operations = [ + migrations.CreateModel( + name='DroppedItemSave', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('book_page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookPage')), + ('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.Item')), + ('progress_save', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.ProgressSave')), + ], + ), + ] diff --git a/djaif/book/models.py b/djaif/book/models.py index ece07cb..a6eb567 100644 --- a/djaif/book/models.py +++ b/djaif/book/models.py @@ -1,5 +1,5 @@ from django.contrib.auth.models import User -from django.db import models +from django.db import models, transaction # Create your models here. @@ -67,6 +67,7 @@ def start_reading(cls, user, book): progress.save() return progress + @transaction.atomic def save_to(self, save_id): if save_id is None: state = ProgressSave.objects.create( @@ -79,12 +80,27 @@ def save_to(self, save_id): state.book_page = self.book_page state.save() state.items.set(self.items.all()) - + state.droppeditemsave_set.all().delete() + for di in self.droppeditem_set.all(): + DroppedItemSave( + item=di.item, + book_page=di.book_page, + progress_save=state, + ).save() + + @transaction.atomic def load_from(self, save_id): state = ProgressSave.objects.get(id=save_id) self.book_page = state.book_page # noqa: WPS601 self.save() self.items.set(state.items.all()) + self.droppeditem_set.all().delete() + for dis in state.droppeditemsave_set.all(): + DroppedItem( + item=dis.item, + book_page=dis.book_page, + book_progress=self, + ).save() class ProgressSave(models.Model): @@ -102,3 +118,18 @@ class Item(models.Model): def __str__(self): return '{self.name}'.format(self=self) + + +class DroppedItem(models.Model): + item = models.ForeignKey(Item, on_delete=models.CASCADE) + book_page = models.ForeignKey(BookPage, on_delete=models.CASCADE) + book_progress = models.ForeignKey(BookProgress, on_delete=models.CASCADE) + + def __str__(self): + return '{self.item.name} ({self.book_page.title})'.format(self=self) + + +class DroppedItemSave(models.Model): + item = models.ForeignKey(Item, on_delete=models.CASCADE) + book_page = models.ForeignKey(BookPage, on_delete=models.CASCADE) + progress_save = models.ForeignKey(ProgressSave, on_delete=models.CASCADE) diff --git a/djaif/book/templates/page.html b/djaif/book/templates/page.html index c45eb45..51410b6 100644 --- a/djaif/book/templates/page.html +++ b/djaif/book/templates/page.html @@ -21,6 +21,19 @@