-
Notifications
You must be signed in to change notification settings - Fork 11
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 #627 from mapswipe/dev
Dev
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
django/apps/existing_database/management/commands/update_organization_name.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,36 @@ | ||
from apps.existing_database.models import Project | ||
from django.core.management.base import BaseCommand | ||
from django.db import models | ||
|
||
EMTPY_TEXT = "" | ||
|
||
|
||
@staticmethod | ||
def parse_organization_name(name): | ||
if name and "\n" in name: | ||
name_parts = [x.strip() for x in name.split("\n")] | ||
if name_parts[1] != "": | ||
return name_parts[1] | ||
return EMTPY_TEXT # Return empty to track this as proccessed | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, **_): | ||
updated_projects = [] | ||
project_qs = Project.objects.filter( | ||
models.Q(organization_name__isnull=True) | ||
& ~models.Q(organization_name=EMTPY_TEXT) | ||
) | ||
self.stdout.write(f"Projects to update: {project_qs.count()}") | ||
for project_id, name in project_qs.values_list("project_id", "name"): | ||
organization_name = parse_organization_name(name) | ||
updated_projects.append( | ||
Project( | ||
project_id=project_id, | ||
organization_name=organization_name, | ||
) | ||
) | ||
Project.objects.bulk_update(updated_projects, ["organization_name"]) | ||
self.stdout.write( | ||
self.style.SUCCESS(f"Successfully updated: {len(updated_projects)}") | ||
) |