Skip to content

Commit

Permalink
optimize an extraction of notes, update templates, add favicon
Browse files Browse the repository at this point in the history
  • Loading branch information
astynax committed Jun 5, 2021
1 parent 0b0312a commit cf4b06b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 50 deletions.
26 changes: 26 additions & 0 deletions djaif/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ def load_from(self, save_id):
book_progress=self,
).save()

def notes(self):
"""Return a prepared set of notes.
These notes will be annotated with relation to the
current page and will appear in order:
1. pinned notes (key=0, pinned=True)
2. current page notes (key=1)
3. other notes (key=0, pinned=False)
Within each group the notes will be ordered
by decreasing of 'updated_at'.
"""
return Note.objects.filter(
progress=self,
).annotate(
key=models.Count(
'page',
filter=models.Q(page=self.book_page),
),
).order_by(
'-pinned',
'-key',
'-updated_at',
)


class ProgressSave(models.Model):
progress = models.ForeignKey(BookProgress, on_delete=models.CASCADE)
Expand Down
27 changes: 10 additions & 17 deletions djaif/book/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<head>
<meta charset="utf-8">
<title>{{ page.book.title }}: {{ page.title }}</title>
<link rel="stylesheet" href="/static/page.css" type="text/css" media="screen" />
<link rel="icon" sizes="64x64" href="/static/favicon.png" type="image/png" />
</head>
<body>
<div class="main" style="display: flex;">
Expand Down Expand Up @@ -78,28 +80,19 @@ <h3>Заметки</h3>
<button type="submit">Добавить</button>
</form>
{% for name, noteset in notesets %}
<ul>
{% for note in noteset %}
<ul class="noteset noteset-{{ name }}">
{% for note_id, note_text, note_page_title 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 }})
<a href="{% url 'delete_note' page.book.id note_id %}">🚮</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_title and name == 'other' %}
({{ note_page_title }})
{% endif %}
</li>
{% endfor %}
</ul>
{% if noteset %}
<hr>
{% endif %}
{% endfor %}
</div>
</div>
Expand Down
56 changes: 28 additions & 28 deletions djaif/book/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import wraps
from itertools import groupby

from django.db import transaction
from django.db.models import F, Func # noqa: WPS347
Expand Down Expand Up @@ -39,6 +40,7 @@ def view_books(request):

def view_book(request, book_id):
book = get_object_or_404(models.Book, id=book_id)

if not book.first_page:
raise ValueError("Book {0.id} hasn't a first page!")
try:
Expand All @@ -50,52 +52,50 @@ def view_book(request, book_id):
book=book, user=request.user,
)

page = progress.book_page

links = [
(link, link.has_all_needed(list(progress.items.all())))
for link in page.pagelink_set.all()
for link in progress.book_page.pagelink_set.all()
]

notesets = [
(name, [
(
note.id,
note.text,
note.page.title if note.page else '',
)
for note in group
])
for name, group in groupby(
progress.notes().select_related(
'page',
).all(),
key=lambda note: (
'page' if note.key else
'pinned' if note.pinned else
'other'
),
)
]

return render(
request,
'page.html',
context={
'page': page,
'page': progress.book_page,
'progress': progress,
'links': links,
'page_items': page.items.exclude(
'notesets': notesets,
'page_items': progress.book_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,
book_page=progress.book_page,
).all(),
'notesets': [
(
'pinned',
progress.note_set.filter(
pinned=True,
).order_by('-updated_at').all(),
),
(
'page',
progress.note_set.filter(
page=page, pinned=False,
).order_by('-updated_at').all(),
),
(
'other',
progress.note_set.exclude(
page=page,
).filter(
pinned=False,
).order_by('-updated_at').all(),
),
],
},
)

Expand Down
9 changes: 4 additions & 5 deletions djaif/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,12 @@

USE_TZ = True

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

SHELL_PLUS_PRINT_SQL = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

Expand All @@ -130,7 +133,3 @@

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

SHELL_PLUS_PRINT_SQL = True
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ exclude =
ignore =
# documentation isn't so important for now
D100, D101, D102, D103, D104, D105, D106,
DAR201,
# yes, we have bad names here and there
WPS110, WPS111,
# annoying stuff
WPS326, WPS306, WPS317, WPS202, WPS226, WPS411, WPS323,
WPS509,
# I like multiline conditions!
WPS337, W503, W504,

per-file-ignores =
settings.py: WPS407, E501, C812, WPS221, WPS226, S105
models.py: WPS226
views.py: WPS210
Binary file added static/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions static/page.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.noteset {}

.noteset-page {
background-color: greenyellow;
}

.noteset-pinned {
background-color: yellow;
}

0 comments on commit cf4b06b

Please sign in to comment.