# -*- coding: utf-8 -*-
from ppms.migration.packet import *

BEFORE_HASH = '452b0beacb43da3f0b4e02a39119bea3'
AFTER_HASH = 'a21f0b88d9a18730c49bef9dbb7150a2'

NEW_VR = """def computeOutput(di):
    # Since this VR is evaluated before DT342 and DT343 are available
    # We can't import this submodule earlier (WI 10477)
    from ppms.module_subclasses.twiki import get_cached_twiki_obj

    twiki_obj = get_cached_twiki_obj()

    dtp_rec = di.get_dtp_record()
    dt_id = dtp_rec.dt_id.get_value()

    twiki_topics = twiki_obj.dt_topics_dict
    if dt_id in twiki_topics:
        return twiki_topics[dt_id].dt_name
    else:
        return ''

computeOutput.deps = ('DI000265', )"""


class ChangeVRImportOrder(MigratePythonPacket):
    '''Fix a SQL Error by shuffling the import in a VR around'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10477

    def fix(self, runner):
        if not self.migrate_python(BEFORE_HASH, AFTER_HASH,
                                   self.CU_TYPE_DATAITEM, '062138', lambda code: NEW_VR):
            return self.fail()

        return self.success()
