@@ -626,7 +626,12 @@ def rescan(self):
626626 if scannable_uri :
627627 scannable_uri .rescan ()
628628
629- def update_field (self , field , value , save = False ):
629+ def _update_field (self , field , value ):
630+ """
631+ Update `field` of this Package with a value of `value` and return a
632+ 3-tuple containing this Package, a list of fields that were updated, and
633+ a list of history entries.
634+ """
630635 # Ensure the incoming value is of the correct type
631636 if field == 'qualifiers' and isinstance (value , dict ):
632637 value = normalize_qualifiers (value , encode = True )
@@ -640,25 +645,32 @@ def update_field(self, field, value, save=False):
640645 if field in date_fields and isinstance (value , str ):
641646 value = dateutil_parse (value )
642647
643- # Update field value if it differs from the existing value
644- updated_field = None
645648 history_entries = []
649+ # Update Package value
646650 package_value = getattr (self , field )
647- if package_value != value :
648- setattr (self , field , value )
649- # Update history
650- if field in date_fields :
651- package_value = str (package_value )
652- value = str (value )
653- entry = dict (
654- field = field ,
655- old_value = package_value ,
656- new_value = value ,
657- )
658- updated_field = field
659- history_entries .append (entry )
651+ setattr (self , field , value )
652+
653+ # Update history
654+ if field in date_fields :
655+ package_value = str (package_value )
656+ value = str (value )
657+ entry = dict (
658+ field = field ,
659+ old_value = package_value ,
660+ new_value = value ,
661+ )
662+ updated_fields = [field , 'history' ]
663+ history_entries .append (entry )
664+ return self , updated_fields , history_entries
665+
666+ def update_field (self , field , value , save = False ):
667+ """
668+ Update `field` of this Package with a value of `value` and return a
669+ 2-tuple containing this Package and a list of fields that were updated.
670+ """
671+ package , updated_fields , history_entries = self ._update_field (field = field , value = value )
660672
661- if updated_field :
673+ if updated_fields :
662674 data = {
663675 'updated_fields' : history_entries ,
664676 }
@@ -668,41 +680,32 @@ def update_field(self, field, value, save=False):
668680 )
669681
670682 if save :
671- self .save ()
683+ package .save ()
672684
673- return self , updated_field
685+ return package , updated_fields
674686
675687 def update_fields (self , save = False , ** values_by_fields ):
688+ """
689+ Given keyword arguments from `values_by_fields`, where the argument
690+ names passed into the method are the fields of this Package to be
691+ updated and the values are the new values to be set (e.g.
692+ path="/new/path", sha1="newsha1"), update all the fields and return a
693+ 2-tuple containing this Package and a list containing the fields that
694+ were updated.
695+ """
676696 updated_fields = []
677697 history_entries = []
678698 for field , value in values_by_fields .items ():
679- # Ensure the incoming value is of the correct type
680- if field == 'qualifiers' and isinstance (value , dict ):
681- value = normalize_qualifiers (value , encode = True )
682-
683- date_fields = [
684- 'created_date' ,
685- 'last_indexed_date' ,
686- 'last_modified_date' ,
687- 'release_date' ,
688- ]
689- if field in date_fields and isinstance (value , str ):
690- value = dateutil_parse (value )
691-
692- package_value = getattr (self , field )
693- if package_value != value :
694- setattr (self , field , value )
695- # Update history
696- if field in date_fields :
697- package_value = str (package_value )
698- value = str (value )
699- entry = dict (
700- field = field ,
701- old_value = package_value ,
702- new_value = value ,
703- )
704- updated_fields .append (field )
705- history_entries .append (entry )
699+ (
700+ package ,
701+ uf ,
702+ he
703+ ) = self ._update_field (field = field , value = value )
704+ updated_fields .extend (uf )
705+ history_entries .extend (he )
706+
707+ # Deduplicate field names
708+ updated_fields = list (set (updated_fields ))
706709
707710 if updated_fields :
708711 data = {
@@ -714,9 +717,9 @@ def update_fields(self, save=False, **values_by_fields):
714717 )
715718
716719 if save :
717- self .save ()
720+ package .save ()
718721
719- return self , updated_fields
722+ return package , updated_fields
720723
721724
722725party_person = 'person'
0 commit comments