<commit_before># -*- coding: utf-8 -*-

"""
Fix duplicate author "order" values

Related to https://github.com/hydroshare/hydroshare/issues/4695
"""

from django.core.management.base import BaseCommand, CommandError
from hs_core.models import BaseResource
from hs_core.hydroshare.utils import set_dirty_bag_flag


class Command(BaseCommand):
    help = "Fix duplicate author 'order' values"

    def add_arguments(self, parser):
        # ID of a resource for which users should be re-ordered
        parser.add_argument('--resource_id', type=str, help=('Required. The id (short_id) of'
                                                             ' the resource'))

    def handle(self, *args, **options):
        if not options['resource_id']:
            raise CommandError('resource_id argument is required')
        res_id = options['resource_id']
        res = BaseResource.objects.filter(short_id=res_id).first()
        if res.raccess.published:
            raise CommandError(f"Resource id: {res_id} is already published--can't update author order.")
        if res.metadata is not None:
            creators = res.metadata.creators.all()
            is_dirty = False
            for index, creator in enumerate(creators, start=1):
                if creator.order != index:
                    print("*" * 100)
                    print(f"Author out of order.\nR:{res.short_id}"
                            f"\nExpected: {index}, got: {creator.order}")
                    creator.order = index
                    creator.save()
                    is_dirty = True
            if is_dirty:
                set_dirty_bag_flag(res)
<commit_msg>[#4818] raise exception when res not found<commit_after># -*- coding: utf-8 -*-

"""
Fix duplicate author "order" values

Related to https://github.com/hydroshare/hydroshare/issues/4695
"""

from django.core.management.base import BaseCommand, CommandError
from hs_core.models import BaseResource
from hs_core.hydroshare.utils import set_dirty_bag_flag


class Command(BaseCommand):
    help = "Fix duplicate author 'order' values"

    def add_arguments(self, parser):
        # ID of a resource for which users should be re-ordered
        parser.add_argument('--resource_id', type=str, help=('Required. The id (short_id) of'
                                                             ' the resource'))

    def handle(self, *args, **options):
        if not options['resource_id']:
            raise CommandError('resource_id argument is required')
        res_id = options['resource_id']
        res = BaseResource.objects.filter(short_id=res_id).first()
        if not res:
            raise CommandError('No resource found for the provided resource_id')
        if res.raccess.published:
            raise CommandError(f"Resource id: {res_id} is already published--can't update author order.")
        if res.metadata is not None:
            creators = res.metadata.creators.all()
            is_dirty = False
            for index, creator in enumerate(creators, start=1):
                if creator.order != index:
                    print("*" * 100)
                    print(f"Author out of order.\nR:{res.short_id}"
                            f"\nExpected: {index}, got: {creator.order}")
                    creator.order = index
                    creator.save()
                    is_dirty = True
            if is_dirty:
                set_dirty_bag_flag(res)

{'fraction_non_alphanumeric': 0.10069848661233993, 'fraction_numerical': 0.006402793946449359, 'mean_word_length': 2.052397868561279, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6638301', 'n_tokens_mistral': 931, 'n_tokens_neox': 887, 'n_words': 253}