-
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 #1463 from DDMAL/develop
Merge develop into staging, 16 May 2024
- Loading branch information
Showing
13 changed files
with
200 additions
and
93 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
66 changes: 66 additions & 0 deletions
66
.../cantusdb_project/main_app/management/commands/populate_exists_on_cantus_ultimus_field.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,66 @@ | ||
from main_app.models import Source | ||
from django.core.management.base import BaseCommand | ||
import requests | ||
from requests import Response | ||
|
||
# This management command updates the exists_on_cantus_ultimus field on a single | ||
# Source or all sources. If a source argument is provided, it will check if the | ||
# Source exists on Cantus Ultimus and update exists_on_cantus_ultimus to True if it | ||
# does. | ||
|
||
# If no Source argument is provided, it will update all Cantus Segment Sources in | ||
# the database. | ||
|
||
# run with `python manage.py add_cantus_ultimus_links <source_id>` | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"source_id", | ||
nargs="?", | ||
type=int, | ||
default=None, | ||
help="This command updates the exists_on_cantus_ultimus field for the Source with the specified ID", | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
source_id: int = kwargs["source_id"] | ||
if source_id: # update exists_on_cantus_ultimus for a specified source | ||
try: | ||
source: Source = Source.objects.get(id=source_id) | ||
self.update_exists_on_cantus_ultimus(source) | ||
except Source.DoesNotExist: | ||
self.stdout.write( | ||
self.style.ERROR(f"Source {source_id} does not exist") | ||
) | ||
return | ||
else: # No source argument provided, update all sources | ||
sources = Source.objects.filter(segment=4063) | ||
for source in sources: | ||
self.update_exists_on_cantus_ultimus(source) | ||
|
||
def update_exists_on_cantus_ultimus(self, source: Source) -> None: | ||
try: | ||
response: Response = requests.get( | ||
f"https://cantus.simssa.ca/manuscript/{source.id}", timeout=10 | ||
) | ||
except (TimeoutError, ConnectionError) as e: | ||
self.stdout.write(self.style.ERROR(f"{e} for source: {source.id}")) | ||
# We expect a 404 response if the source doesn't exist on Cantus Ultimus | ||
if response.status_code != 200: | ||
self.stdout.write( | ||
self.style.NOTICE( | ||
f"Source {source.id} may not exist on Cantus Ultimus (Status Code: {response.status_code})\n" | ||
) | ||
) | ||
return | ||
# update the link for the entire source | ||
self.stdout.write(f"\nUpdating exists_on_cantus_ultimus for source: {source}") | ||
source.exists_on_cantus_ultimus = True | ||
source.save() | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
"Source exists_on_cantus_ultimus updated successfully!\n" | ||
) | ||
) |
18 changes: 18 additions & 0 deletions
18
django/cantusdb_project/main_app/migrations/0010_source_exists_on_cantus_ultimus.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,18 @@ | ||
# Generated by Django 4.2.11 on 2024-05-14 15:02 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("main_app", "0009_chant_segment_sequence_segment"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="source", | ||
name="exists_on_cantus_ultimus", | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
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 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
Oops, something went wrong.