Skip to content

Commit

Permalink
add the "book notes" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
astynax committed Jun 4, 2021
1 parent 9ebb68d commit a247ff6
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 59 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MANAGE := poetry run python manage.py

run:
$(MANAGE) runserver
24 changes: 24 additions & 0 deletions djaif/book/migrations/0016_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 3.0.5 on 2021-06-04 16:31

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('book', '0015_droppeditemsave'),
]

operations = [
migrations.CreateModel(
name='Note',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('page', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='book.BookPage')),
],
),
]
20 changes: 20 additions & 0 deletions djaif/book/migrations/0017_note_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.0.5 on 2021-06-04 16:39

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('book', '0016_note'),
]

operations = [
migrations.AddField(
model_name='note',
name='progress',
field=models.ForeignKey(default=42, on_delete=django.db.models.deletion.CASCADE, to='book.BookProgress'),
preserve_default=False,
),
]
18 changes: 18 additions & 0 deletions djaif/book/migrations/0018_note_pinned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.0.5 on 2021-06-04 17:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('book', '0017_note_progress'),
]

operations = [
migrations.AddField(
model_name='note',
name='pinned',
field=models.BooleanField(default=False),
),
]
19 changes: 17 additions & 2 deletions djaif/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.db import models, transaction


# Create your models here.
class Book(models.Model):
title = models.CharField(name='title', max_length=100, unique=True)
first_page = models.ForeignKey(
Expand Down Expand Up @@ -63,7 +62,9 @@ class Meta:

@classmethod
def start_reading(cls, user, book):
progress = BookProgress(user=user, book=book, book_page=book.first_page)
progress = BookProgress(
user=user, book=book, book_page=book.first_page,
)
progress.save()
return progress

Expand Down Expand Up @@ -133,3 +134,17 @@ 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)


class Note(models.Model):
text = models.TextField()
progress = models.ForeignKey(BookProgress, on_delete=models.CASCADE)
page = models.ForeignKey(
BookPage,
on_delete=models.CASCADE,
null=True,
blank=True,
)
pinned = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
45 changes: 45 additions & 0 deletions djaif/book/templates/note.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ page.book.title }}: {{ page.title }}</title>
</head>
<body>
<h3>
{{ page.title }}: Заметка
</h3>
<form method="POST" style="display: grid;"
action="{% url 'update_note' page.book.id note.id %}">
{% csrf_token %}
<label for="memo-text">Текст</label>
<textarea id="memo-text" name="text">{{ note.text }}</textarea>

<label for="memo-page">Привязка</label>
<select id="memo-page" name="page">
{% if note.page %}
<option value="keep"
{% if note.page.id != page.id %}
selected
{% endif %}>
Оставить привязанной к [{{ note.page.title }}]
</option>
{% endif %}
{% if not note.page or note.page.id != page.id %}
<option value="change">
Привязать к [{{ page.title }}]
</option>
{% endif %}
<option value="remove"
{% if not note.page %}
selected
{% endif %}>
{% if note.page %}Отвязать{% endif %}
</option>
</select>
<label for="memo-pinned">Закреплена</label>
<input id="memo-pinned" name="pinned" type="checkbox"
{% if note.pinned %}checked{% endif %}>
<button type="submit">Обновить</button>
</form>
</body>
</html>
154 changes: 97 additions & 57 deletions djaif/book/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,103 @@
<title>{{ page.book.title }}: {{ page.title }}</title>
</head>
<body>
<h1>
{{ page.title }}
</h1>
{{ page.body }}
{% if page_items %}
<h3>Вы видите</h3>
<ul>
{% for item in page_items %}
<li>
<a href="{% url 'take' page.book.id item.id %}">
{{ item.name }}
</a>
</li>
{% 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 %}
<li>
{% if passable %}
<a href="{% url 'go_to' page.book.id link.id %}">
{{ link.name }}
</a>
{% else %}
{{ link.name }}
<div class="main" style="display: flex;">
<div class="content">
<h1>
{{ page.title }}
</h1>
{{ page.body }}
{% if page_items %}
<h3>Вы видите</h3>
<ul>
{% for item in page_items %}
<li>
<a href="{% url 'take' page.book.id item.id %}">
{{ item.name }}
</a>
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% if progress.items.all %}
<h3>Инвентарь</h3>
<ul>
{% for item in progress.items.all %}
<li>
{{ item.name }}
<a href="{% url 'drop' page.book.id item.id %}">
[Выбросить]
</a>
</li>
{% endfor %}
</ul>
{% endif %}
<hr />
<a href="{% url 'saves' page.book.id %}">Сохранения</a>
{% 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 %}
<li>
{% if passable %}
<a href="{% url 'go_to' page.book.id link.id %}">
{{ link.name }}
</a>
{% else %}
{{ link.name }}
{% endif %}
</li>
{% endfor %}
</ul>
{% if progress.items.all %}
<h3>Инвентарь</h3>
<ul>
{% for item in progress.items.all %}
<li>
{{ item.name }}
<a href="{% url 'drop' page.book.id item.id %}">
[Выбросить]
</a>
</li>
{% endfor %}
</ul>
{% endif %}
<hr />
<a href="{% url 'saves' page.book.id %}">Сохранения</a>
</div>
<div class="sidebar">
<h3>Заметки</h3>
<form method="POST" style="display: grid;"
action="{% url 'add_note' page.book.id %}">
{% csrf_token %}
<label for="memo-text">Новая</label>
<textarea id="memo-text" name="text"></textarea>
<label for="memo-pin">Для этого места</label>
<input id="memo-pin" name="pin" type="checkbox">
<button type="submit">Добавить</button>
</form>
{% for name, noteset in notesets %}
<ul>
{% for note in noteset %}
<li>
<a href="{% url 'delete_note' page.book.id note.id %}">
[X]
</a>
<a href="{% url 'toggle_note' page.book.id note.id %}">
[📎]
</a>
<a href="{% url 'update_note' page.book.id note.id %}">
[🖉]
</a>
<pre>{{ note.text }}</pre>
{% if note.page and name == 'other' %}
({{ note.page.title }})
{% endif %}
</li>
{% endfor %}
</ul>
{% if noteset %}
<hr>
{% endif %}
{% endfor %}
</div>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions djaif/book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,25 @@
views.delete_save,
name='delete_save',
),
path(
'book/<int:book_id>/notes/add',
views.add_note,
name='add_note',
),
path(
'book/<int:book_id>/notes/delete/<int:note_id>',
views.delete_note,
name='delete_note',
),
path(
'book/<int:book_id>/notes/toggle/<int:note_id>',
views.toggle_note,
name='toggle_note',
),
path(
'book/<int:book_id>/notes/update/<int:note_id>',
views.update_note,
name='update_note',
),
path('book/<int:book_id>/map.svg', views.view_book_map),
]
Loading

0 comments on commit a247ff6

Please sign in to comment.