diff --git a/djaif/book/migrations/0012_progresssave.py b/djaif/book/migrations/0012_progresssave.py new file mode 100644 index 0000000..24c4932 --- /dev/null +++ b/djaif/book/migrations/0012_progresssave.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.5 on 2021-02-10 17:01 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0011_auto_20200526_1529'), + ] + + operations = [ + migrations.CreateModel( + name='ProgressSave', + 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')), + ('items', models.ManyToManyField(blank=True, to='book.Item')), + ('progress', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookProgress')), + ], + ), + ] diff --git a/djaif/book/migrations/0013_progresssave_updated_at.py b/djaif/book/migrations/0013_progresssave_updated_at.py new file mode 100644 index 0000000..53301ed --- /dev/null +++ b/djaif/book/migrations/0013_progresssave_updated_at.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.5 on 2021-02-10 17:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0012_progresssave'), + ] + + operations = [ + migrations.AddField( + model_name='progresssave', + name='updated_at', + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/djaif/book/models.py b/djaif/book/models.py index eaaf317..a1d6fa8 100644 --- a/djaif/book/models.py +++ b/djaif/book/models.py @@ -67,6 +67,35 @@ def start_reading(cls, user, book): progress.save() return progress + def save_to(self, save_id): + if save_id is None: + state = ProgressSave.objects.create( + progress=self, + book_page=self.book_page, + ) + state.items.set(self.items.all()) + else: + state = ProgressSave.objects.get(id=save_id) + state.book_page=self.book_page + state.save() + state.items.set(self.items.all()) + + def load_from(self, save_id): + state = ProgressSave.objects.get(id=save_id) + self.book_page=state.book_page + self.save() + self.items.set(state.items.all()) + + +class ProgressSave(models.Model): + progress = models.ForeignKey(BookProgress, on_delete=models.CASCADE) + + updated_at = models.DateTimeField(auto_now=True) + + book_page = models.ForeignKey(BookPage, on_delete=models.CASCADE) + + items = models.ManyToManyField('book.Item', blank=True) # noqa: WPS110 + class Item(models.Model): name = models.TextField() diff --git a/djaif/book/templates/page.html b/djaif/book/templates/page.html index 5560db8..c45eb45 100644 --- a/djaif/book/templates/page.html +++ b/djaif/book/templates/page.html @@ -14,7 +14,7 @@