Skip to content

Commit

Permalink
add autologin, book progress tracking, inventory, conditional links
Browse files Browse the repository at this point in the history
  • Loading branch information
astynax committed May 1, 2020
1 parent e6c85f2 commit f0424f7
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 30 deletions.
2 changes: 2 additions & 0 deletions djaif/book/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
33 changes: 33 additions & 0 deletions djaif/book/migrations/0007_auto_20200430_1629.py
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')},
},
),
]
25 changes: 25 additions & 0 deletions djaif/book/migrations/0008_auto_20200430_1703.py
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'),
),
]
18 changes: 18 additions & 0 deletions djaif/book/migrations/0009_bookpage_items.py
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'),
),
]
18 changes: 18 additions & 0 deletions djaif/book/migrations/0010_pagelink_items.py
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'),
),
]
35 changes: 34 additions & 1 deletion djaif/book/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth.models import User
from django.db import models


Expand All @@ -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)

Expand All @@ -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} '
Expand All @@ -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)
38 changes: 35 additions & 3 deletions djaif/book/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,45 @@
<title>{{ page.book.title }}: {{ page.title }}</title>
</head>
<body>
<h1>
{{ page.title }}
</h1>
{{ page.body }}
{% if page_items %}
<h3>Вы видите</h3>
<ul>
{% for link in page.pagelink_set.all %}
<li><a href="{% url 'page' page.book.id link.to_page.id %}">
{% for item in page_items %}
<li>
<a href="{% url 'take' page.book.id page.id item.id %}">
{{ item.name }}
</a>
</li>
{% endfor %}
</ul>
{% endif %}
<h3>Куда податься?</h3>
<ul>
{% for link, passable in links %}
<li>
{% if passable %}
<a href="{% url 'page' page.book.id link.to_page.id %}">
{{ link.name }}
</a>
{% else %}
{{ link.name }}
</a></li>
{% endif %}
</li>
{% endfor %}
</ul>
{% if progress.items.all %}
<h3>Инвентарь</h3>
<ul>
{% for item in progress.items.all %}
<li>
{{ item.name }}
</li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
11 changes: 8 additions & 3 deletions djaif/book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from djaif.book import views

urlpatterns = [
path('', views.index),
path('book/<int:book_id>', views.book),
path('book/<int:book_id>/page/<int:page_id>', views.page, name='page'),
path('', views.view_books),
path('book/<int:book_id>', views.view_book, name='book'),
path('book/<int:book_id>/page/<int:page_id>', views.view_page, name='page'),
path(
'book/<int:book_id>/page/<int:page_id>/take/<int:item_id>',
views.take_item,
name='take',
),
]
94 changes: 75 additions & 19 deletions djaif/book/views.py
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}),
)
15 changes: 15 additions & 0 deletions djaif/middleware.py
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
1 change: 1 addition & 0 deletions djaif/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'djaif.middleware.auto_login',
]

ROOT_URLCONF = 'djaif.urls'
Expand Down
Empty file removed djaif/views.py
Empty file.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ black = "^19.10b0"

[tool.black]
skip-string-normalization=true
line-length=80

[build-system]
requires = ["poetry>=0.12"]
Expand Down
9 changes: 5 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[isort]
include_trailing_comma = true
multi_line_output = 3
force_grid_wrap = 0
include_trailing_comma = True
line_length = 80
multi_line_output = 3
use_parentheses = True

[flake8]
format = wemake
Expand All @@ -12,7 +14,6 @@ doctests = True
max-complexity = 6
max-line-length = 80

# Self settings:
max-imports = 16

# Excluding some directories:
Expand All @@ -24,7 +25,7 @@ exclude =
*.egg
**/migrations/**

ignore = D100, D101, D105, D106, WPS326, WPS306, WPS317
ignore = D100, D101, D103, D105, D106, WPS326, WPS306, WPS317

per-file-ignores =
settings.py: WPS407, E501, C812, WPS221, WPS226

0 comments on commit f0424f7

Please sign in to comment.