# -*- coding: utf-8 -*-

from urllib.parse import urljoin

from ppms.migration import *


class ChangeTwikiUrl(ChangeDTPPacket):
    """Changes the URL in global settings from the old one to the current"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10911

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4'),
                MaxVersionDependency(version='39.4'))

    def fix(self, runner):
        planta_url = 'http://wiki.planta.de/twiki/bin/view/'
        new_version = 'Current/'
        old_version = 'Release394/'
        primary_key = '3518dad8-7df1-45f5-99c3-19ea8273b04a'

        record = self.get_record(333, [primary_key], ['alpha120'])
        if not record:
            return self.fail()

        if self.change_value(record=record, attribute='alpha120',
                             old_value=urljoin(planta_url, old_version),
                             new_value=urljoin(planta_url, new_version)) or \
            self.change_value(record=record, attribute='alpha120',
                              old_value='http://planta16.planta.de/twiki/bin/view/Release394/',
                              new_value=urljoin(planta_url, new_version)):
            return self.success()

        return self.fail()