-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add autologin, book progress tracking, inventory, conditional links
- Loading branch information
Showing
14 changed files
with
270 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')}, | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,85 @@ | ||
from django.shortcuts import render, redirect, get_object_or_404 | ||
from functools import wraps | ||
|
||
from django.shortcuts import get_object_or_404, redirect, render | ||
from django.urls import reverse | ||
|
||
from djaif.book import models | ||
|
||
|
||
def index(request): | ||
return render(request, "book_index.html", context={ | ||
'books': models.Book.objects.all(), | ||
}) | ||
def on_progress(view): | ||
@wraps(view) # noqa: WPS430 | ||
def inner(request, book_id, **kwargs): | ||
try: | ||
progress = models.BookProgress.objects.get( | ||
book=book_id, user=request.user, | ||
) | ||
except models.BookProgress.DoesNotExist: | ||
return redirect(reverse('book', kwargs={'book_id': book_id})) | ||
return view( | ||
request=request, progress=progress, book_id=book_id, **kwargs, | ||
) | ||
|
||
return inner | ||
|
||
|
||
def book(request, book_id): | ||
b = get_object_or_404(models.Book, id=book_id) | ||
if not b.first_page: | ||
return render(request, "book.html", context={ | ||
'book': b, | ||
}) | ||
return redirect(reverse('page', kwargs={ | ||
'book_id': b.id, 'page_id': b.first_page.id | ||
})) | ||
def view_books(request): | ||
return render( | ||
request, | ||
'book_index.html', | ||
context={'books': models.Book.objects.all()}, | ||
) | ||
|
||
|
||
def page(request, book_id, page_id): | ||
return render(request, "page.html", context={ | ||
'page': get_object_or_404( | ||
models.BookPage, book__id=book_id, id=page_id, | ||
def view_book(request, book_id): | ||
book = get_object_or_404(models.Book, id=book_id) | ||
if not book.first_page: | ||
return render(request, 'book.html', context={'book': book}) | ||
try: | ||
progress = models.BookProgress.objects.get( | ||
book=book, user=request.user, | ||
) | ||
except models.BookProgress.DoesNotExist: | ||
progress = models.BookProgress.start_reading( | ||
user=request.user, book=book, | ||
) | ||
return redirect( | ||
reverse( | ||
'page', | ||
kwargs={'book_id': book.id, 'page_id': progress.book_page.id}, | ||
), | ||
}) | ||
) | ||
|
||
|
||
@on_progress | ||
def view_page(request, progress, book_id, page_id): | ||
page = get_object_or_404(models.BookPage, book__id=book_id, id=page_id) | ||
|
||
progress.book_page = page | ||
progress.save() | ||
|
||
links = [ | ||
(link, link.has_all_needed(list(progress.items.all()))) | ||
for link in page.pagelink_set.all() | ||
] | ||
|
||
return render( | ||
request, | ||
'page.html', | ||
context={ | ||
'page': page, | ||
'progress': progress, | ||
'links': links, | ||
'page_items': page.items.exclude(id__in=progress.items.only('id')), | ||
}, | ||
) | ||
|
||
|
||
@on_progress | ||
def take_item(request, progress, book_id, page_id, item_id): | ||
item = get_object_or_404(models.Item, id=item_id) # noqa: WPS110 | ||
|
||
progress.items.add(item) | ||
|
||
return redirect( | ||
reverse('page', kwargs={'book_id': book_id, 'page_id': page_id}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.contrib.auth import authenticate, login | ||
|
||
|
||
def auto_login(get_response): | ||
def middleware(request): # noqa: WPS430 | ||
|
||
if not request.user.is_authenticated: | ||
user = authenticate( # noqa: S106 | ||
username='admin', password='admin', | ||
) | ||
login(request, user) | ||
|
||
return get_response(request) | ||
|
||
return middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters