77
88from email .parser import HeaderParser
99
10- from patchwork .models import Label , Patch
10+ from patchwork .models import Label , Patch , Cover , Series
1111from patchwork .parser import clean_subject
1212
1313
@@ -17,7 +17,7 @@ def get_labels_per_project():
1717 global_labels = list (Label .objects .filter (project_id = None ).all ())
1818
1919 labels_per_project = {}
20- for label in Label .objects .filter (project_id__isnull = True ).all ():
20+ for label in Label .objects .filter (project_id__isnull = False ).all ():
2121 labels_list = labels_per_project .get (label .project_id , [])
2222 labels_list .append (label )
2323 labels_per_project [label .project_id ] = labels_list
@@ -116,9 +116,76 @@ def update_patches(self, projects):
116116
117117 self .flush_patches ()
118118
119+ def flush_covers (self ):
120+ if len (self .covers_to_update ) > 0 :
121+ Cover .objects .bulk_update (self .covers_to_update , ['name' ])
122+ Series .objects .bulk_update (self .series_to_update , ['name' ])
123+ self .covers_to_update = []
124+ self .series_to_update = []
125+ self .covers_to_update_ids = set ()
126+
127+ if len (self .labels_to_add ) > 0 :
128+ Cover .labels .through .objects .bulk_create (self .labels_to_add )
129+ self .labels_to_add = []
130+
131+ def update_covers (self , projects ):
132+ self .covers_to_update = []
133+ self .series_to_update = []
134+ self .covers_to_update_ids = set ()
135+ self .labels_to_add = []
136+
137+ query = Cover .objects .all ().only ('id' , 'headers' , 'project_id' )
138+
139+ if projects :
140+ query = query .filter (project__listid__in = projects )
141+
142+ count = query .count ()
143+
144+ for i , cover in enumerate (query .iterator (chunk_size = 1000 )):
145+ project_labels = self .labels_per_project .get (
146+ cover .project_id , self .global_labels
147+ )
148+
149+ name , cleaned_prefixes = get_name_and_labels (
150+ cover .headers , [label .name for label in project_labels ]
151+ )
152+ if name is None or cleaned_prefixes is None :
153+ continue
154+
155+ for label in project_labels :
156+ if (
157+ label .name in cleaned_prefixes
158+ and label not in cover .labels .all ()
159+ ):
160+ self .labels_to_add .append (
161+ Cover .labels .through (
162+ cover_id = cover .id , label_id = label .id
163+ )
164+ )
165+ if cover .id not in self .covers_to_update_ids :
166+ series = Series .objects .get (cover_letter = cover )
167+ cover .name = name [:255 ]
168+ series .name = name [:255 ]
169+ self .covers_to_update .append (cover )
170+ self .series_to_update .append (series )
171+ self .covers_to_update_ids .add (cover .id )
172+
173+ if (i % 100 ) == 0 :
174+ self .stdout .write ('%06d/%06d\r ' % (i , count ), ending = '' )
175+ self .stdout .flush ()
176+
177+ if len (self .covers_to_update ) >= 1000 :
178+ self .flush_covers ()
179+
180+ self .flush_covers ()
181+
119182 def handle (self , * args , ** options ):
120183 self .labels_per_project , self .global_labels = get_labels_per_project ()
121184
185+ self .stdout .write ('\n updating covers' )
186+
187+ self .update_covers (options ['projects' ])
188+
122189 self .stdout .write ('\n updating patches' )
123190
124191 self .update_patches (options ['projects' ])
0 commit comments