-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from DDMAL/merge-indexer-user
Merge indexer user
- Loading branch information
Showing
31 changed files
with
357 additions
and
481 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
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
55 changes: 55 additions & 0 deletions
55
django/cantusdb_project/main_app/management/commands/merge_indexer_user.py
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,55 @@ | ||
from operator import index | ||
from django.core.management.base import BaseCommand | ||
from main_app.models import Indexer | ||
from users.models import User | ||
from faker import Faker | ||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
pass | ||
|
||
def handle(self, *args, **options): | ||
faker = Faker() | ||
users = User.objects.all() | ||
indexers = Indexer.objects.all() | ||
|
||
for user in users: | ||
# set all users to be non-indexer first | ||
# those listed as indexers on old Cantus will have this adjusted to True | ||
user.is_indexer = False | ||
if user.first_name or user.last_name: | ||
user.full_name = f"{user.first_name.strip()} {user.last_name.strip()}" | ||
else: | ||
user.full_name = "Anonymous User" | ||
user.save() | ||
|
||
for indexer in indexers: | ||
indexer_full_name = f"{indexer.given_name} {indexer.family_name}" | ||
print(indexer_full_name) | ||
homonymous_users = User.objects.filter(full_name__iexact=indexer_full_name) | ||
# if the indexer also exists as a user | ||
if homonymous_users: | ||
assert homonymous_users.count() == 1 | ||
homonymous_user = homonymous_users.get() | ||
print(f"homonymous: {homonymous_user.full_name}") | ||
# keep the user as it is (merge the indexer into existing user) | ||
# and store the ID of its indexer object | ||
homonymous_user.old_indexer_id = indexer.id | ||
homonymous_user.is_indexer = True | ||
homonymous_user.save() | ||
# if the indexer doesn't exist as a user | ||
else: | ||
# create a new user with the indexer information | ||
User.objects.create( | ||
institution=indexer.institution, | ||
city=indexer.city, | ||
country=indexer.country, | ||
full_name=indexer_full_name, | ||
# assign random email to dummy users | ||
email=f"{faker.lexify('????????')}@fakeemail.com", | ||
# leave the password empty for dummy users | ||
# the password can't be empty in login form, so they can't log in | ||
password="", | ||
old_indexer_id = indexer.id, | ||
is_indexer=True, | ||
) |
40 changes: 40 additions & 0 deletions
40
django/cantusdb_project/main_app/management/commands/replace_indexer_fields.py
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,40 @@ | ||
from main_app.models import Source | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
|
||
# This command is intended as a temporary approach to | ||
# populate the user-related fields using the indexer-related fields. | ||
# The goal is to eventually remove the Indexer model and point all fields to User. | ||
# After running this command, the indexer-related fields can be removed from the Source model. | ||
# The user-related fields can then be renamed as needed. | ||
# Run with `python manage.py replace_indexer_fields`. | ||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
sources = Source.objects.all() | ||
for source in sources: | ||
print(source.id) | ||
|
||
inventoried_by = source.inventoried_by.all() | ||
for indexer in inventoried_by: | ||
user = get_user_model().objects.get(old_indexer_id=indexer.id) | ||
source.inventoried_by_u.add(user) | ||
|
||
full_text_entered_by = source.full_text_entered_by.all() | ||
for indexer in full_text_entered_by: | ||
user = get_user_model().objects.get(old_indexer_id=indexer.id) | ||
source.full_text_entered_by_u.add(user) | ||
|
||
melodies_entered_by = source.melodies_entered_by.all() | ||
for indexer in melodies_entered_by: | ||
user = get_user_model().objects.get(old_indexer_id=indexer.id) | ||
source.melodies_entered_by_u.add(user) | ||
|
||
proofreaders = source.proofreaders.all() | ||
for indexer in proofreaders: | ||
user = get_user_model().objects.get(old_indexer_id=indexer.id) | ||
source.proofreaders_u.add(user) | ||
|
||
other_editors = source.other_editors.all() | ||
for indexer in other_editors: | ||
user = get_user_model().objects.get(old_indexer_id=indexer.id) | ||
source.other_editors_u.add(user) |
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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.