# -*- coding: utf-8; -*-
# This file is part of Superdesk.
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license
#
# Author  : GyanP
# Creation: 2020-03-09 12:19

from bson import ObjectId
from bson.errors import InvalidId
from superdesk import get_resource_service
from superdesk.vocabularies import is_related_content
from superdesk.commands.data_updates import DataUpdate


class DataUpdate(DataUpdate):

    resource = 'archive'

    def forwards(self, mongodb_collection, mongodb_database):
        for resource in ('archive', 'published'):
            service = get_resource_service(resource)
            for item in mongodb_database[resource].find({'associations': {'$exists': 'true', '$ne': {}}}):
                update = False
                associations = {}
                for key, val in item['associations'].items():
                    associations[key] = val
                    if val and is_related_content(key) and val.get('order', None) is None:
                        update = True
                        order = int(key.split('--')[1])
                        associations[key]['order'] = order
                if update:
                    try:
                        _id = ObjectId(item['_id'])
                    except InvalidId:
                        _id = item['_id']
                    service.system_update(_id, {'associations': associations}, item)

    def backwards(self, mongodb_collection, mongodb_database):
        raise NotImplementedError()

@@ -821,14 +821,23 @@
  '$n
-e
+in
 ': 
+%5B
 %7B%7D
+, None%5D
 %7D%7D):
@@ -902,16 +902,41 @@
 ns = %7B%7D%0A
+                try:%0A    
         
@@ -1001,32 +1001,36 @@
                 
+   
+ 
 associations%5Bkey
@@ -1037,16 +1037,20 @@
 %5D = val%0A
+    
         
@@ -1148,32 +1148,36 @@
                 
+    
 update = True%0A  
@@ -1174,16 +1174,20 @@
  = True%0A
+    
         
@@ -1250,32 +1250,36 @@
                 
+ 
+   
 associations%5Bkey
@@ -1297,16 +1297,80 @@
 = order%0A
+                except AttributeError:%0A                    pass%0A
         

Update 00022_20200309-121947_archive.py (#1883)