-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate ArchivedProjects and associated objects to ActiveProjects. Ref …
…#2166. Note that we do not migrate files. Files for archived projects will remain in the pn-media/archived-projects subfolder. We should migrate them to pn-media/active-projects later.
- Loading branch information
1 parent
204a99c
commit c6e3870
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
physionet-django/project/migrations/0074_migrate_archived_to_active.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,73 @@ | ||
from django.db import migrations | ||
from django.contrib.contenttypes.models import ContentType | ||
from project.models import SubmissionStatus | ||
|
||
|
||
def migrate_archived_to_active(apps, schema_editor): | ||
""" | ||
Copy all ArchivedProject data to the ActiveProject model. | ||
""" | ||
AnonymousAccess = apps.get_model('project', 'AnonymousAccess') | ||
ActiveProject = apps.get_model('project', 'ActiveProject') | ||
ArchivedProject = apps.get_model('project', 'ArchivedProject') | ||
Author = apps.get_model('project', 'Author') | ||
CopyeditLog = apps.get_model('project', 'CopyeditLog') | ||
EditLog = apps.get_model('project', 'EditLog') | ||
Log = apps.get_model('project', 'Log') | ||
Publication = apps.get_model('project', 'Publication') | ||
Reference = apps.get_model('project', 'Reference') | ||
Topic = apps.get_model('project', 'Topic') | ||
UploadedDocument = apps.get_model('project', 'UploadedDocument') | ||
|
||
# Get content types for both models | ||
archived_project_type = ContentType.objects.get_for_model(ArchivedProject) | ||
active_project_type = ContentType.objects.get_for_model(ActiveProject) | ||
|
||
for archived_project in ArchivedProject.objects.all(): | ||
# Create a new ActiveProject instance | ||
active_project = ActiveProject( | ||
# Map fields from ArchivedProject to ActiveProject | ||
abstract=archived_project.abstract, | ||
archive_datetime=archived_project.archive_datetime, | ||
background=archived_project.background, | ||
conflicts_of_interest=archived_project.conflicts_of_interest, | ||
content_description=archived_project.content_description, | ||
core_project_id=archived_project.core_project_id, | ||
license=archived_project.license, | ||
methods=archived_project.methods, | ||
resource_type=archived_project.resource_type, | ||
short_description=archived_project.short_description, | ||
slug=archived_project.slug, | ||
submission_status=SubmissionStatus.ARCHIVED.value, | ||
title=archived_project.title, | ||
usage_notes=archived_project.usage_notes, | ||
version=archived_project.version | ||
) | ||
# You can also copy other fields if necessary | ||
active_project.save() | ||
|
||
# Migrate references, authors, logs, etc from ArchivedProject to ActiveProject | ||
for model in [Reference, Author, Log, AnonymousAccess, Topic, Publication, UploadedDocument, EditLog, CopyeditLog]: | ||
items = model.objects.filter( | ||
content_type_id=archived_project_type.id, | ||
object_id=archived_project.id | ||
) | ||
for item in items: | ||
item.content_type_id = active_project_type.id | ||
item.object_id = active_project.id | ||
item.save() | ||
|
||
|
||
def migrate_backward(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('project', '0073_activeproject_archive_datetime'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(migrate_archived_to_active, migrate_backward), | ||
] |