mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-27 02:20:31 +00:00
Add shelved_date field and populate it on import
This commit is contained in:
parent
6ffd8a7822
commit
a16d759766
3 changed files with 43 additions and 2 deletions
|
@ -2,6 +2,8 @@
|
|||
import csv
|
||||
import logging
|
||||
|
||||
from django.utils import timezone
|
||||
|
||||
from bookwyrm import models
|
||||
from bookwyrm.models import ImportJob, ImportItem
|
||||
from bookwyrm.tasks import app
|
||||
|
@ -100,7 +102,10 @@ def handle_imported_book(source, user, item, include_reviews, privacy):
|
|||
# shelve the book if it hasn't been shelved already
|
||||
if item.shelf and not existing_shelf:
|
||||
desired_shelf = models.Shelf.objects.get(identifier=item.shelf, user=user)
|
||||
models.ShelfBook.objects.create(book=item.book, shelf=desired_shelf, user=user)
|
||||
shelved_date = item.date_added or timezone.now()
|
||||
models.ShelfBook.objects.create(
|
||||
book=item.book, shelf=desired_shelf, user=user, shelved_date=shelved_date
|
||||
)
|
||||
|
||||
for read in item.reads:
|
||||
# check for an existing readthrough with the same dates
|
||||
|
|
34
bookwyrm/migrations/0078_add_shelved_date.py
Normal file
34
bookwyrm/migrations/0078_add_shelved_date.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# Generated by Django 3.2.4 on 2021-07-03 08:25
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
def copy_created_date(app_registry, schema_editor):
|
||||
db_alias = schema_editor.connection.alias
|
||||
ShelfBook = app_registry.get_model("bookwyrm", "ShelfBook")
|
||||
ShelfBook.objects.all().update(shelved_date=models.F("created_date"))
|
||||
|
||||
|
||||
def do_nothing(app_registry, schema_editor):
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("bookwyrm", "0077_auto_20210623_2155"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name="shelfbook",
|
||||
options={"ordering": ("-shelved_date",)},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="shelfbook",
|
||||
name="shelved_date",
|
||||
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||
),
|
||||
migrations.RunPython(copy_created_date, reverse_code=do_nothing),
|
||||
]
|
|
@ -1,6 +1,7 @@
|
|||
""" puttin' books on shelves """
|
||||
import re
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
from bookwyrm import activitypub
|
||||
from .activitypub_mixin import CollectionItemMixin, OrderedCollectionMixin
|
||||
|
@ -69,6 +70,7 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel):
|
|||
"Edition", on_delete=models.PROTECT, activitypub_field="book"
|
||||
)
|
||||
shelf = models.ForeignKey("Shelf", on_delete=models.PROTECT)
|
||||
shelved_date = models.DateTimeField(default=timezone.now)
|
||||
user = fields.ForeignKey(
|
||||
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
||||
)
|
||||
|
@ -86,4 +88,4 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel):
|
|||
you can't put a book on shelf twice"""
|
||||
|
||||
unique_together = ("book", "shelf")
|
||||
ordering = ("-created_date",)
|
||||
ordering = ("-shelved_date", "-created_date", "-updated_date")
|
||||
|
|
Loading…
Reference in a new issue