File tree 1 file changed +36
-0
lines changed
django/apps/existing_database/management/commands
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ from apps .existing_database .models import Project
2
+ from django .core .management .base import BaseCommand
3
+ from django .db import models
4
+
5
+ EMTPY_TEXT = ""
6
+
7
+
8
+ @staticmethod
9
+ def parse_organization_name (name ):
10
+ if name and "\n " in name :
11
+ name_parts = [x .strip () for x in name .split ("\n " )]
12
+ if name_parts [1 ] != "" :
13
+ return name_parts [1 ]
14
+ return EMTPY_TEXT # Return empty to track this as proccessed
15
+
16
+
17
+ class Command (BaseCommand ):
18
+ def handle (self , ** _ ):
19
+ updated_projects = []
20
+ project_qs = Project .objects .filter (
21
+ models .Q (organization_name__isnull = True )
22
+ & ~ models .Q (organization_name = EMTPY_TEXT )
23
+ )
24
+ self .stdout .write (f"Projects to update: { project_qs .count ()} " )
25
+ for project_id , name in project_qs .values_list ("project_id" , "name" ):
26
+ organization_name = parse_organization_name (name )
27
+ updated_projects .append (
28
+ Project (
29
+ project_id = project_id ,
30
+ organization_name = organization_name ,
31
+ )
32
+ )
33
+ Project .objects .bulk_update (updated_projects , ["organization_name" ])
34
+ self .stdout .write (
35
+ self .style .SUCCESS (f"Successfully updated: { len (updated_projects )} " )
36
+ )
You can’t perform that action at this time.
0 commit comments