-
Notifications
You must be signed in to change notification settings - Fork 97
Institution feature implementation. #670
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
Merged
Merged
Changes from all commits
Commits
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
71 changes: 71 additions & 0 deletions
71
coldfront/core/project/management/commands/add_institutions.py
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,71 @@ | ||
# SPDX-FileCopyrightText: (C) ColdFront Authors | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from coldfront.core.project.models import Project | ||
from coldfront.core.project.utils import determine_automated_institution_choice | ||
from coldfront.core.utils.common import import_from_settings | ||
|
||
PROJECT_INSTITUTION_EMAIL_MAP = import_from_settings("PROJECT_INSTITUTION_EMAIL_MAP", False) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Update existing projects with institutions based on PIs email address" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--dry-run", | ||
action="store_true", | ||
help="Outputs each project, followed by the assigned institution, without making changes.", | ||
) | ||
|
||
def update_project_institution(self, projects): | ||
if not PROJECT_INSTITUTION_EMAIL_MAP: | ||
self.stdout.write( | ||
"Error, no changes made. Please set PROJECT_INSTITUTION_EMAIL_MAP as a dictionary value inside configuration file." | ||
) | ||
return | ||
|
||
def _update_project_institution(self, projects): | ||
user_input = input( | ||
"Assign all existing projects with institutions? You can use the --dry-run flag to preview changes first. [y/N] " | ||
) | ||
|
||
try: | ||
if user_input == "y" or user_input == "Y": | ||
for project in projects: | ||
project.institution = determine_automated_institution_choice(project, PROJECT_INSTITUTION_EMAIL_MAP) | ||
project.save(update_fields=["institution"]) | ||
self.stdout.write(f"Updated {projects.count()} projects with institutions.") | ||
else: | ||
self.stdout.write("No changes made") | ||
except Exception as e: | ||
self.stdout.write(f"Error: {e}") | ||
|
||
def _institution_dry_run(self, projects): | ||
try: | ||
for project in projects: | ||
new_institution = determine_automated_institution_choice(project, PROJECT_INSTITUTION_EMAIL_MAP) | ||
self.stdout.write( | ||
f"Project {project.pk}, called {project.title}. Institution would be '{new_institution}'" | ||
) | ||
except Exception as e: | ||
self.stdout.write(f"Error: {e}") | ||
|
||
def handle(self, *args, **options): | ||
dry_run = options["dry_run"] | ||
|
||
if not PROJECT_INSTITUTION_EMAIL_MAP: | ||
self.stdout.write( | ||
"Error, no changes made. Please set PROJECT_INSTITUTION_EMAIL_MAP as a dictionary value inside configuration file." | ||
) | ||
return | ||
|
||
projects_without_institution = Project.objects.filter(institution="None") | ||
|
||
if dry_run: | ||
self._institution_dry_run(projects_without_institution) | ||
else: | ||
self._update_project_institution(projects_without_institution) |
26 changes: 26 additions & 0 deletions
26
coldfront/core/project/migrations/0006_historicalproject_institution_project_institution.py
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,26 @@ | ||
# SPDX-FileCopyrightText: (C) ColdFront Authors | ||
# | ||
# SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
# Generated by Django 4.2.11 on 2025-04-27 19:07 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("project", "0005_alter_historicalproject_options_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="historicalproject", | ||
name="institution", | ||
field=models.CharField(blank=True, default="None", max_length=80), | ||
), | ||
migrations.AddField( | ||
model_name="project", | ||
name="institution", | ||
field=models.CharField(blank=True, default="None", max_length=80), | ||
), | ||
] |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
add_automated_institution_choice(project, institution_dict)
(or whatever you rename it to) does not make changes to the database I believe it would be beneficial to add a test case to ensure that that is the case. If someone gets confused down the road and does not realize that and changes the function later, that test should ding the change made to the API foradd_automated_institution_choice
.