-
-
Notifications
You must be signed in to change notification settings - Fork 301
Make series a separate model #3747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hughrun
wants to merge
28
commits into
bookwyrm-social:main
Choose a base branch
from
hughrun:series-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
e3699ff
Create Series and SeriesBook models
hughrun 423e187
add connector logic and fix templates and views
hughrun 6a046ec
finalise connectors
hughrun aa5470e
refactor series models and templates
hughrun 89406e0
bunch of changes and tests
hughrun 1739f50
formatting
hughrun 5d350ad
final commit for series
hughrun 486a8ae
add migration
hughrun 77d67e2
use SearchVector in connectors
hughrun dd18940
add management command and improve connector series matching
hughrun bfc8304
fix upgrade_series admin command
hughrun 57b70fd
fix linters hopefully
hughrun 4c19da1
Merge branch 'main' into series-model
mouse-reeve ec56489
fix rank match value for series matches
hughrun 393190c
Resolve conflicts and merge 'main' into series-model
hughrun 6e11d75
fix series title search in ConfirmEditBook
hughrun 76da2cb
Merge branch 'main' into series-model
mouse-reeve d78bbd2
Hughrun series model (#10)
mouse-reeve d1e9ea1
clean up
hughrun 4a941fb
Merge branch 'main' into series-model
hughrun 02b6a5f
Merge branch 'main' into series-model
hughrun b652f69
basically undo previous changes to models
hughrun 2177432
fix series model
hughrun c29d61c
Merge branch 'main' into series-model
hughrun dc90a04
Merge branch 'main' into series-model
hughrun ddbbe97
Merge branch 'main' into series-model
hughrun 3356a3e
fix inventaire series and clean up
hughrun 124e08e
Merge branch 'main' into series-model
hughrun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| """fix legacy series""" | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
| from django.contrib.postgres.search import SearchRank, SearchVector | ||
| from django.db.models import Subquery, Q | ||
| from django.db.models.functions import Length | ||
| from bookwyrm import activitypub | ||
| from bookwyrm.models import Book, Edition, Series, SeriesBook, User | ||
|
|
||
|
|
||
| def upgrade_series_data(): | ||
| """turn strings into things""" | ||
|
|
||
| series_count = Series.objects.count() | ||
| seriesbook_count = SeriesBook.objects.count() | ||
|
|
||
| for book in Edition.objects.exclude(series=None): | ||
| user = activitypub.get_representative() | ||
| vector = SearchVector("name", weight="A") + SearchVector( | ||
| "alternative_names", weight="B" | ||
| ) | ||
| possible_series = ( | ||
| Series.objects.annotate(search=vector) | ||
| .annotate(rank=SearchRank(vector, book.series, normalization=32)) | ||
| .filter(rank__gt=0.19) | ||
| .order_by("-rank") | ||
| ) | ||
|
|
||
| if possible_series.exists(): | ||
| books = Edition.objects.filter( | ||
| authors__in=Subquery(book.authors.values("pk")) | ||
| ).values( | ||
| "parent_work__pk" | ||
| ) # the parent work is the book attached to the series | ||
|
|
||
| same_author_sb = SeriesBook.objects.filter(book__in=books).filter( | ||
| series__in=Subquery(possible_series.values("pk")) | ||
| ) # there is a possible series with a seriesbook by a matching author | ||
|
|
||
| match = same_author_sb.filter( | ||
| Q(series__name__iexact=book.series) | ||
| | Q(series__alternative_names__icontains=book.series) | ||
| ) # it's the same series | ||
|
|
||
| if match: | ||
| series = match.first().series | ||
|
|
||
| else: | ||
| # there might be a matching series but we don't know | ||
| # leave it for a user to work out manually | ||
| continue | ||
| else: | ||
| series = Series.objects.create(user=user, name=book.series) | ||
|
|
||
| # Use get_or_create so we don't duplicate. | ||
| SeriesBook.objects.get_or_create( | ||
| book=book.parent_work, | ||
| series=series, | ||
| defaults={"user": user, "series_number": book.series_number}, | ||
| ) | ||
|
|
||
| book.series = None | ||
| book.series_number = None | ||
| book.save(broadcast=False) | ||
|
|
||
| # print how many things we created | ||
| new_series_count = Series.objects.count() | ||
| new_seriesbook_count = SeriesBook.objects.count() | ||
| net_series = new_series_count - series_count | ||
| net_books = new_seriesbook_count - seriesbook_count | ||
|
|
||
| print("-------") | ||
| print(f"Created {net_series} new Series and {net_books} new SeriesBooks") | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| """Turn legacy series data into Series and SeriesBook objects""" | ||
|
|
||
| help = "Turn legacy series data into Series and SeriesBook objects" | ||
|
|
||
| def handle(self, *args, **options): | ||
| """run data migration""" | ||
| upgrade_series_data() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.