diff --git a/djaif/book/admin.py b/djaif/book/admin.py index fac91c8..bc7f887 100644 --- a/djaif/book/admin.py +++ b/djaif/book/admin.py @@ -5,3 +5,5 @@ admin.site.register(models.Book) admin.site.register(models.BookPage) admin.site.register(models.PageLink) +admin.site.register(models.BookProgress) +admin.site.register(models.Item) diff --git a/djaif/book/migrations/0007_auto_20200430_1629.py b/djaif/book/migrations/0007_auto_20200430_1629.py new file mode 100644 index 0000000..d7034d9 --- /dev/null +++ b/djaif/book/migrations/0007_auto_20200430_1629.py @@ -0,0 +1,33 @@ +# Generated by Django 3.0.5 on 2020-04-30 16:29 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('book', '0006_book_cover_art'), + ] + + operations = [ + migrations.AlterField( + model_name='book', + name='cover_art', + field=models.ImageField(null=True, upload_to=''), + ), + migrations.CreateModel( + name='BookProgress', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.Book')), + ('book_page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookPage')), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + options={ + 'unique_together': {('user', 'book')}, + }, + ), + ] diff --git a/djaif/book/migrations/0008_auto_20200430_1703.py b/djaif/book/migrations/0008_auto_20200430_1703.py new file mode 100644 index 0000000..329b03a --- /dev/null +++ b/djaif/book/migrations/0008_auto_20200430_1703.py @@ -0,0 +1,25 @@ +# Generated by Django 3.0.5 on 2020-04-30 17:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0007_auto_20200430_1629'), + ] + + operations = [ + migrations.CreateModel( + name='Item', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.TextField()), + ], + ), + migrations.AddField( + model_name='bookprogress', + name='items', + field=models.ManyToManyField(to='book.Item'), + ), + ] diff --git a/djaif/book/migrations/0009_bookpage_items.py b/djaif/book/migrations/0009_bookpage_items.py new file mode 100644 index 0000000..7c9845b --- /dev/null +++ b/djaif/book/migrations/0009_bookpage_items.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.5 on 2020-04-30 17:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0008_auto_20200430_1703'), + ] + + operations = [ + migrations.AddField( + model_name='bookpage', + name='items', + field=models.ManyToManyField(to='book.Item'), + ), + ] diff --git a/djaif/book/migrations/0010_pagelink_items.py b/djaif/book/migrations/0010_pagelink_items.py new file mode 100644 index 0000000..1a00111 --- /dev/null +++ b/djaif/book/migrations/0010_pagelink_items.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.5 on 2020-04-30 17:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('book', '0009_bookpage_items'), + ] + + operations = [ + migrations.AddField( + model_name='pagelink', + name='items', + field=models.ManyToManyField(to='book.Item'), + ), + ] diff --git a/djaif/book/models.py b/djaif/book/models.py index b1e9ee1..984aa65 100644 --- a/djaif/book/models.py +++ b/djaif/book/models.py @@ -1,3 +1,4 @@ +from django.contrib.auth.models import User from django.db import models @@ -19,6 +20,8 @@ class BookPage(models.Model): title = models.TextField(name='title') body = models.TextField(name='body') + items = models.ManyToManyField('book.Item', blank=True) # noqa: WPS110 + def __str__(self): return '{self.title} ({self.id})'.format(self=self) @@ -30,6 +33,11 @@ class PageLink(models.Model): ) name = models.TextField() + items = models.ManyToManyField('book.Item', blank=True) # noqa: WPS110 + + class Meta: + unique_together = ['from_page', 'to_page'] + def __str__(self): return ( '{self.from_page.title} ➝ {self.to_page.title} ' @@ -38,5 +46,30 @@ def __str__(self): ) ) + def has_all_needed(self, items): + return all(i in items for i in self.items.all()) + + +class BookProgress(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + + book = models.ForeignKey(Book, on_delete=models.CASCADE) + book_page = models.ForeignKey(BookPage, on_delete=models.CASCADE) + + items = models.ManyToManyField('book.Item', blank=True) # noqa: WPS110 + class Meta: - unique_together = ['from_page', 'to_page'] + unique_together = ['user', 'book'] + + @classmethod + def start_reading(cls, user, book): + progress = BookProgress(user=user, book=book, book_page=book.first_page) + progress.save() + return progress + + +class Item(models.Model): + name = models.TextField() + + def __str__(self): + return '{self.name}'.format(self=self) diff --git a/djaif/book/templates/page.html b/djaif/book/templates/page.html index 6d0867f..5560db8 100644 --- a/djaif/book/templates/page.html +++ b/djaif/book/templates/page.html @@ -5,13 +5,45 @@