Skip to content

Commit

Permalink
add item dropping
Browse files Browse the repository at this point in the history
  • Loading branch information
astynax committed Mar 10, 2021
1 parent 4efc782 commit 981a684
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 6 deletions.
2 changes: 2 additions & 0 deletions djaif/book/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
23 changes: 23 additions & 0 deletions djaif/book/migrations/0014_droppeditem.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
23 changes: 23 additions & 0 deletions djaif/book/migrations/0015_droppeditemsave.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
35 changes: 33 additions & 2 deletions djaif/book/models.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand All @@ -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)
16 changes: 16 additions & 0 deletions djaif/book/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ <h3>Вы видите</h3>
{% endfor %}
</ul>
{% endif %}
{% if dropped_items %}
<h3>Брошено вами ранее</h3>
<ul>
{% for dropped_item in dropped_items %}
<li>
{{ dropped_item.item.name }}
<a href="{% url 'take_back' page.book.id dropped_item.id %}">
[Подобрать]
</a>
</li>
{% endfor %}
</ul>
{% endif %}
<h3>Куда податься?</h3>
<ul>
{% for link, passable in links %}
Expand All @@ -41,6 +54,9 @@ <h3>Инвентарь</h3>
{% for item in progress.items.all %}
<li>
{{ item.name }}
<a href="{% url 'drop' page.book.id item.id %}">
[Выбросить]
</a>
</li>
{% endfor %}
</ul>
Expand Down
8 changes: 7 additions & 1 deletion djaif/book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
path('', views.view_books),
path('book/<int:book_id>', views.view_book, name='book'),
path('book/<int:book_id>/go/<int:pagelink_id>', views.go_to, name='go_to'),
path('book/<int:book_id>/take/<int:item_id>', views.take, name='take'),
path('book/<int:book_id>/saves', views.view_saves, name='saves'),
path('book/<int:book_id>/take/<int:item_id>', views.take, name='take'),
path('book/<int:book_id>/drop/<int:item_id>', views.drop, name='drop'),
path(
'book/<int:book_id>/take_back/<int:dropped_item_id>',
views.take_back,
name='take_back',
),
path(
'book/<int:book_id>/saves/new',
views.save_to,
Expand Down
50 changes: 47 additions & 3 deletions djaif/book/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import wraps

from django.db import transaction
from django.http import FileResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
Expand Down Expand Up @@ -62,7 +63,16 @@ def view_book(request, book_id):
'page': page,
'progress': progress,
'links': links,
'page_items': page.items.exclude(id__in=progress.items.only('id')),
'page_items': page.items.exclude(
id__in=progress.items.only('id'),
).exclude(
id__in=progress.droppeditem_set.values_list(
'item__id', flat=True,
),
).all(),
'dropped_items': progress.droppeditem_set.filter(
book_page=page,
).all(),
},
)

Expand All @@ -81,13 +91,47 @@ def go_to(request, progress, book_id, pagelink_id):


@on_progress
def take(request, progress, book_id, item_id):
def take(request, progress, item_id, **kwargs):
item = get_object_or_404(models.Item, id=item_id) # noqa: WPS110

if item in progress.book_page.items.all():
progress.items.add(item)

return _return_to(book_id)
return _return_to(progress.book.id)


@on_progress
def drop(request, progress, item_id, **kwargs):
item = get_object_or_404(models.Item, id=item_id) # noqa: WPS110

if item in progress.items.all():
with transaction.atomic():
models.DroppedItem(
item=item,
book_page=progress.book_page,
book_progress=progress,
).save()
progress.items.remove(item)

return _return_to(progress.book.id)


@on_progress
def take_back(request, progress, dropped_item_id, **kwargs):
dropped_item = get_object_or_404(
models.DroppedItem, id=dropped_item_id,
) # noqa: WPS110

if (
dropped_item.book_page.id == progress.book_page.id
and
dropped_item.book_progress.id == progress.id
):
with transaction.atomic():
progress.items.add(dropped_item.item)
dropped_item.delete()

return _return_to(progress.book.id)


@on_progress
Expand Down

0 comments on commit 981a684

Please sign in to comment.