-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ade7ca
commit 3963fa9
Showing
6 changed files
with
207 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# Generated by Django 3.2.23 on 2024-02-04 20:27 | ||
|
||
import bookwyrm.models.fields | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
def make_series(apps, schema_editor): | ||
Edition = apps.get_model("bookwyrm", "Edition") | ||
Series = apps.get_model("bookwyrm", "Series") | ||
SeriesBook = apps.get_model("bookwyrm", "SeriesBook") | ||
|
||
db_alias = schema_editor.connection.alias | ||
|
||
with_series = ( | ||
Edition.objects.using(db_alias) | ||
.exclude(series_name__isnull=True) | ||
.exclude(series_name__exact="") | ||
.order_by("series_name", "series_number") | ||
) | ||
for edition in with_series: | ||
# TODO: Try to parse number from series_name if series_number is empty? | ||
series, _ = Series.objects.using(db_alias).get_or_create( | ||
name=edition.series_name, | ||
authors=edition.authors.all(), | ||
) | ||
SeriesBook.objects.using(db_alias).create( | ||
book=edition, series=series, number=edition.series_number | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("bookwyrm", "0195_alter_user_preferred_language"), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="book", | ||
old_name="series", | ||
new_name="series_name", | ||
), | ||
migrations.CreateModel( | ||
name="Series", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_date", models.DateTimeField(auto_now_add=True)), | ||
("updated_date", models.DateTimeField(auto_now=True)), | ||
( | ||
"remote_id", | ||
bookwyrm.models.fields.RemoteIdField( | ||
max_length=255, | ||
null=True, | ||
validators=[bookwyrm.models.fields.validate_remote_id], | ||
), | ||
), | ||
("name", bookwyrm.models.fields.CharField(max_length=100)), | ||
( | ||
"authors", | ||
bookwyrm.models.fields.ManyToManyField(to="bookwyrm.Author"), | ||
), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name="SeriesBook", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_date", models.DateTimeField(auto_now_add=True)), | ||
("updated_date", models.DateTimeField(auto_now=True)), | ||
( | ||
"remote_id", | ||
bookwyrm.models.fields.RemoteIdField( | ||
max_length=255, | ||
null=True, | ||
validators=[bookwyrm.models.fields.validate_remote_id], | ||
), | ||
), | ||
( | ||
"number", | ||
bookwyrm.models.fields.CharField( | ||
blank=True, max_length=255, null=True | ||
), | ||
), | ||
( | ||
"book", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, to="bookwyrm.book" | ||
), | ||
), | ||
( | ||
"series", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to="bookwyrm.series", | ||
), | ||
), | ||
], | ||
options={ | ||
"ordering": ["-number"], | ||
"unique_together": {("book", "series")}, | ||
}, | ||
), | ||
migrations.AddField( | ||
model_name="series", | ||
name="books", | ||
field=bookwyrm.models.fields.ManyToManyField( | ||
related_name="series_books", | ||
through="bookwyrm.SeriesBook", | ||
to="bookwyrm.Book", | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="book", | ||
name="series", | ||
field=models.ManyToManyField( | ||
through="bookwyrm.SeriesBook", to="bookwyrm.Series" | ||
), | ||
), | ||
migrations.RunPython(make_series), # TODO: reverse_code | ||
migrations.RemoveField( | ||
model_name="book", | ||
name="series_number", | ||
), | ||
migrations.RemoveField( | ||
model_name="book", | ||
name="series_name", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""series of books""" | ||
|
||
from django.db import models | ||
|
||
from .base_model import BookWyrmModel | ||
from . import fields | ||
|
||
|
||
class Series(BookWyrmModel): | ||
"""a named series of books""" | ||
|
||
name = fields.CharField(max_length=100) | ||
authors = fields.ManyToManyField("Author") # TODO: add on Author model | ||
books = fields.ManyToManyField( | ||
"Book", | ||
through="SeriesBook", | ||
through_fields=("series", "book"), | ||
related_name="series_books", | ||
) | ||
|
||
|
||
class SeriesBook(BookWyrmModel): | ||
"""membership of a series""" | ||
|
||
book = models.ForeignKey("Book", on_delete=models.PROTECT) | ||
series = models.ForeignKey("Series", on_delete=models.PROTECT) | ||
number = fields.CharField(max_length=255, blank=True, null=True) | ||
|
||
collection_field = "series" | ||
|
||
class Meta: | ||
"""a series can't contain the same book twice""" | ||
|
||
unique_together = ("book", "series") | ||
ordering = ["-number"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters