예제 #1
0
파일: display.py 프로젝트: jochengcd/gcd
def field_value(revision, field):
    value = getattr(revision, field)
    if field in ['is_surrogate', 'no_volume', 'display_volume_with_number',
                 'no_brand', 'page_count_uncertain', 'title_inferred',
                 'no_barcode', 'no_indicia_frequency', 'no_isbn',
                 'year_began_uncertain', 'year_ended_uncertain',
                 'on_sale_date_uncertain', 'is_comics_publication']:
        return yesno(value, 'Yes,No')
    elif field in ['is_current']:
        res_holder_display = ''
        if revision.previous():
            reservation = revision.source.get_ongoing_reservation()
            if revision.previous().is_current and not value and reservation:
                res_holder = reservation.indexer
                res_holder_display = ' (ongoing reservation held by %s %s)' % \
                  (res_holder.first_name, res_holder.last_name)
        return yesno(value, 'Yes,No') + res_holder_display
    elif field in ['publisher', 'indicia_publisher', 'brand', 'series']:
        return absolute_url(value)
    elif field in ['notes', 'tracking_notes', 'publication_notes',
                   'characters', 'synopsis']:
        return linebreaksbr(value)
    elif field == 'reprint_notes':
        reprint = ''
        if value.strip() != '':
            for string in split_reprint_string(value):
                string = string.strip()
                reprint += '<li> ' + esc(string) + ' </li>'
            if reprint != '':
                reprint = '<ul>' + reprint + '</ul>'
        return mark_safe(reprint)
    elif field in ['url']:
        return urlize(value)
    elif field in ['indicia_pub_not_printed']:
        return yesno(value, 'Not Printed,Printed')
    elif field in ['no_editing', 'no_script', 'no_pencils', 'no_inks',
                   'no_colors', 'no_letters']:
        return yesno(value, 'X, ')
    elif field in ['page_count']:
        if revision.source_name == 'issue' and \
           revision.changeset.storyrevisions.count():
            # only calculate total sum for issue not sequences
            total_pages = sum_page_counts(revision.active_stories())
            if revision.variant_of:
                if revision.changeset.issuerevisions.count() > 1:
                    stories = revision.changeset.storyrevisions\
                                      .exclude(issue=revision.issue)
                else:
                    stories = revision.variant_of.active_stories()
                if revision.active_stories().count():
                    # variant has cover sequence, add page counts without cover
                    stories = stories.exclude(sequence_number=0)
                    total_pages += sum_page_counts(stories)
                else:
                    # variant has no extra cover sequence
                    total_pages = sum_page_counts(stories)
            sum_story_pages = format_page_count(total_pages)

            return u'%s (note: total sum of story page counts is %s)' % \
                   (format_page_count(value), sum_story_pages)
        return format_page_count(value)
    elif field == 'isbn':
        if value:
            if validated_isbn(value):
                return u'%s (note: valid ISBN)' % value
            elif len(value.split(';')) > 1:
                return_val = value + ' (note: '
                for isbn in value.split(';'):
                    return_val = return_val + u'%s; ' % ("valid ISBN" \
                                   if validated_isbn(isbn) else "invalid ISBN")
                return return_val + 'ISBNs are inequal)'
            elif value:
                return u'%s (note: invalid ISBN)' % value
    elif field == 'barcode':
        if value:
            barcodes = value.split(';')
            return_val = value + ' (note: '
            for barcode in barcodes:
                return_val = return_val + u'%s; ' % ("valid UPC/EAN" \
                             if valid_barcode(barcode) \
                             else "invalid UPC/EAN or non-standard")
            return return_val[:-2] + ')'
    elif field == 'leading_article':
        if value == True:
            return u'Yes (sorted as: %s)' % remove_leading_article(revision.name)
        else:
            return u'No'
    elif field in ['has_barcode', 'has_isbn', 'has_issue_title',
                   'has_indicia_frequency', 'has_volume']:
        if hasattr(revision, 'changed'):
            if revision.changed[field] and value == False:
                kwargs = {field[4:]: ''}
                if field[4:] == 'issue_title':
                    kwargs = {'title': ''}
                if revision.series:
                    value_count = revision.series.active_issues()\
                                                 .exclude(**kwargs).count()
                    if value_count:
                        return 'No (note: %d issues have a non-empty %s value)' % \
                                (value_count, field[4:])
        return yesno(value, 'Yes,No')
    elif field == 'after' and not hasattr(revision, 'changed'):
        # for previous revision (no attr changed) display empty string
        return ''
    return value
예제 #2
0
def field_value(revision, field):
    value = getattr(revision, field)
    if field in ['is_surrogate', 'no_volume', 'display_volume_with_number',
                 'no_brand', 'page_count_uncertain', 'title_inferred',
                 'no_barcode', 'no_indicia_frequency', 'no_isbn',
                 'year_began_uncertain', 'year_ended_uncertain',
                 'on_sale_date_uncertain', 'is_comics_publication']:
        return yesno(value, 'Yes,No')
    elif field in ['is_current']:
        res_holder_display = ''
        if revision.previous():
            reservation = revision.source.get_ongoing_reservation()
            if revision.previous().is_current and not value and reservation:
                res_holder = reservation.indexer
                res_holder_display = ' (ongoing reservation held by %s %s)' % \
                  (res_holder.first_name, res_holder.last_name)
        return yesno(value, 'Yes,No') + res_holder_display
    elif field in ['publisher', 'indicia_publisher', 'series',
                   'origin_issue', 'target_issue']:
        return absolute_url(value)
    elif field in ['origin', 'target']:
        return value.full_name_with_link()
    elif field == 'brand':
        if value and value.emblem:
            return mark_safe('<img src="' + value.emblem.icon.url + '"> ' \
                             + absolute_url(value))
        return absolute_url(value)
    elif field in ['notes', 'tracking_notes', 'publication_notes',
                   'characters', 'synopsis']:
        return linebreaksbr(value)
    elif field == 'reprint_notes':
        reprint = ''
        if value.strip() != '':
            for string in split_reprint_string(value):
                string = string.strip()
                reprint += '<li> ' + esc(string) + ' </li>'
            if reprint != '':
                reprint = '<ul>' + reprint + '</ul>'
        return mark_safe(reprint)
    elif field in ['url']:
        return urlize(value)
    elif field in ['indicia_pub_not_printed']:
        return yesno(value, 'Not Printed,Printed')
    elif field == 'group':
        brand_groups = ''
        for brand in value.all():
            brand_groups += absolute_url(brand) + '; '
        if brand_groups:
            brand_groups = brand_groups[:-2]
        return mark_safe(brand_groups)
    elif field in ['no_editing', 'no_script', 'no_pencils', 'no_inks',
                   'no_colors', 'no_letters']:
        return yesno(value, 'X, ')
    elif field in ['page_count']:
        if revision.source_name == 'issue' and \
           revision.changeset.storyrevisions.count():
            # only calculate total sum for issue not sequences
            total_pages = sum_page_counts(revision.active_stories())
            if revision.variant_of:
                if revision.changeset.issuerevisions.count() > 1:
                    stories = revision.changeset.storyrevisions\
                                      .exclude(issue=revision.issue)
                else:
                    stories = revision.variant_of.active_stories()
                if revision.active_stories().count():
                    # variant has cover sequence, add page counts without cover
                    stories = stories.exclude(sequence_number=0)
                    total_pages += sum_page_counts(stories)
                else:
                    # variant has no extra cover sequence
                    total_pages = sum_page_counts(stories)
            sum_story_pages = format_page_count(total_pages)

            return u'%s (note: total sum of story page counts is %s)' % \
                   (format_page_count(value), sum_story_pages)
        return format_page_count(value)
    elif field == 'isbn':
        if value:
            if validated_isbn(value):
                return u'%s (note: valid ISBN)' % value
            elif len(value.split(';')) > 1:
                return_val = value + ' (note: '
                for isbn in value.split(';'):
                    return_val = return_val + u'%s; ' % ("valid ISBN" \
                                   if validated_isbn(isbn) else "invalid ISBN")
                return return_val + 'ISBNs are inequal)'
            elif value:
                return u'%s (note: invalid ISBN)' % value
    elif field == 'barcode':
        if value:
            barcodes = value.split(';')
            return_val = value + ' (note: '
            for barcode in barcodes:
                return_val = return_val + u'%s; ' % ("valid UPC/EAN" \
                             if valid_barcode(barcode) \
                             else "invalid UPC/EAN or non-standard")
            return return_val[:-2] + ')'
    elif field == 'leading_article':
        if value == True:
            return u'Yes (sorted as: %s)' % remove_leading_article(revision.name)
        else:
            return u'No'
    elif field in ['has_barcode', 'has_isbn', 'has_issue_title',
                   'has_indicia_frequency', 'has_volume', 'has_rating']:
        if hasattr(revision, 'changed'):
            if revision.changed[field] and value == False:
                kwargs = {field[4:]: ''}
                if field[4:] == 'issue_title':
                    kwargs = {'title': ''}
                if revision.series:
                    value_count = revision.series.active_issues()\
                                                 .exclude(**kwargs).count()
                    if value_count:
                        return 'No (note: %d issues have a non-empty %s value)' % \
                                (value_count, field[4:])
        return yesno(value, 'Yes,No')
    elif field == 'is_singleton':
        if hasattr(revision, 'changed'):
            if revision.changed[field] and value == True:
                 if revision.series:
                     value_count = revision.series.active_base_issues().count()
                     if value_count:
                        return 'Yes (note: the series has %d issue%s)' % \
                                (value_count, pluralize(value_count))
        return yesno(value, 'Yes,No')
    elif field == 'after' and not hasattr(revision, 'changed'):
        # for previous revision (no attr changed) display empty string
        return ''
    return value
예제 #3
0
def field_value(revision, field):
    value = getattr(revision, field)
    if field in ['script', 'pencils', 'inks', 'colors', 'letters', 'editing']:
        if type(revision).__name__ == 'IssueRevision':
            credits = revision.issue_credit_revisions.filter(
                credit_type__id=CREDIT_TYPES[field], deleted=False)
        else:
            credits = revision.story_credit_revisions.filter(
                credit_type__id=CREDIT_TYPES[field], deleted=False)
        if value and credits:
            value += '; '
        for credit in credits:
            value += credit.creator.display_credit(credit, url=True) + '; '
        if credits:
            value = value[:-2]
        return mark_safe(value)
    if field in [
            'is_surrogate', 'no_volume', 'display_volume_with_number',
            'no_brand', 'page_count_uncertain', 'title_inferred', 'no_barcode',
            'no_indicia_frequency', 'no_isbn', 'year_began_uncertain',
            'year_overall_began_uncertain', 'year_ended_uncertain',
            'year_overall_ended_uncertain', 'on_sale_date_uncertain',
            'is_comics_publication', 'no_indicia_printer'
    ]:
        return yesno(value, 'Yes,No')
    elif field in ['is_current']:
        res_holder_display = ''
        if revision.previous():
            reservation = revision.source.get_ongoing_reservation()
            if revision.previous().is_current and not value and reservation:
                res_holder = reservation.indexer
                res_holder_display = ' (ongoing reservation held by %s %s)' % \
                                     (res_holder.first_name,
                                      res_holder.last_name)
        return yesno(value, 'Yes,No') + res_holder_display
    elif field in [
            'publisher', 'indicia_publisher', 'series', 'origin_issue',
            'target_issue', 'award', 'from_feature', 'to_feature'
    ]:
        return absolute_url(value)
    elif field in ['origin', 'target']:
        return value.full_name_with_link()
    elif field == 'brand':
        if value and value.emblem:
            if settings.FAKE_IMAGES:
                return absolute_url(value)
            else:
                return mark_safe('<img src="' + value.emblem.icon.url + '"> ' +
                                 absolute_url(value))
        return absolute_url(value)
    elif field in ['feature_object', 'feature_logo']:
        first = True
        features = ''
        for feature in value.all():
            if first:
                first = False
            else:
                features += '; '
            features += '<a href="%s">%s</a>' % (feature.get_absolute_url(),
                                                 esc(feature.name))
        return mark_safe(features)
    elif field in [
            'notes', 'tracking_notes', 'publication_notes', 'characters',
            'synopsis'
    ]:
        return linebreaksbr(value)
    elif field == 'reprint_notes':
        reprint = ''
        if value.strip() != '':
            for string in split_reprint_string(value):
                string = string.strip()
                reprint += '<li> ' + esc(string) + ' </li>'
            if reprint != '':
                reprint = '<ul>' + reprint + '</ul>'
        return mark_safe(reprint)
    elif field in ['url']:
        return urlize(value)
    elif field in ['indicia_pub_not_printed']:
        return yesno(value, 'Not Printed,Printed')
    elif field == 'group':
        brand_groups = ''
        for brand in value.all():
            brand_groups += absolute_url(brand) + '; '
        if brand_groups:
            brand_groups = brand_groups[:-2]
        return mark_safe(brand_groups)
    elif field in [
            'no_editing', 'no_script', 'no_pencils', 'no_inks', 'no_colors',
            'no_letters'
    ]:
        return yesno(value, 'X, ')
    elif field in ['page_count']:
        if revision.source_name == 'issue' and \
           revision.changeset.storyrevisions.count():
            # only calculate total sum for issue not sequences
            total_pages = sum_page_counts(revision.active_stories())
            if revision.variant_of:
                if revision.changeset.issuerevisions.count() > 1:
                    stories = revision.changeset.storyrevisions\
                                      .exclude(issue=revision.issue)
                else:
                    stories = revision.variant_of.active_stories()
                if revision.active_stories().count():
                    # variant has cover sequence, add page counts without cover
                    stories = stories.exclude(sequence_number=0)
                    total_pages += sum_page_counts(stories)
                else:
                    # variant has no extra cover sequence
                    total_pages = sum_page_counts(stories)
            sum_story_pages = format_page_count(total_pages)

            return '%s (note: total sum of story page counts is %s)' % \
                   (format_page_count(value), sum_story_pages)
        return format_page_count(value)
    elif field == 'isbn':
        if value:
            if validated_isbn(value):
                return '%s (note: valid ISBN)' % show_isbn(value)
            elif len(value.split(';')) > 1:
                return_val = show_isbn(value) + ' (note: '
                for isbn in value.split(';'):
                    return_val = return_val + '%s; ' % ("valid ISBN"
                                                        if validated_isbn(isbn)
                                                        else "invalid ISBN")
                return return_val + 'ISBNs are inequal)'
            elif value:
                return '%s (note: invalid ISBN)' % value
    elif field == 'barcode':
        if value:
            barcodes = value.split(';')
            return_val = show_barcode(value) + ' (note: '
            for barcode in barcodes:
                return_val = return_val + '%s; ' % (
                    "valid UPC/EAN part" if valid_barcode(barcode) else
                    "invalid UPC/EAN part or non-standard")
            return return_val[:-2] + ')'
    elif field == 'leading_article':
        if value is True:
            return 'Yes (sorted as: %s)' % remove_leading_article(
                revision.name)
        else:
            return 'No'
    elif field in [
            'has_barcode', 'has_isbn', 'has_issue_title',
            'has_indicia_frequency', 'has_volume', 'has_rating'
    ]:
        if hasattr(revision, 'changed'):
            if revision.changed[field] and value is False:
                kwargs = {field[4:]: ''}
                if field[4:] == 'issue_title':
                    kwargs = {'title': ''}
                if revision.series:
                    value_count = revision.series.active_issues()\
                                                 .exclude(**kwargs).count()
                    if value_count:
                        return 'No (note: %d issues have a non-empty %s value)' % \
                                (value_count, field[4:])
        return yesno(value, 'Yes,No')
    elif field == 'is_singleton':
        if hasattr(revision, 'changed'):
            if revision.changed[field] and value is True:
                if revision.series:
                    value_count = revision.series.active_base_issues().count()
                    if value_count != 1:
                        return 'Yes (note: the series has %d issue%s)' % \
                               (value_count, pluralize(value_count))
                    elif revision.series.active_issues()\
                                 .exclude(indicia_frequency='').count():
                        return 'Yes (note: the issue has an indicia frequency)'
        return yesno(value, 'Yes,No')
    elif field == 'after' and not hasattr(revision, 'changed'):
        # for previous revision (no attr changed) display empty string
        return ''
    elif field == 'cr_creator_names':
        creator_names = ", ".join(value.all().values_list('name', flat=True))
        return creator_names
    elif field == 'creator_name':
        creator_names = "; ".join(value.all().values_list('name', flat=True))
        return creator_names
    elif field == 'feature_object':
        features = "; ".join(value.all().values_list('name', flat=True))
        return features
    elif field == 'feature_logo':
        features = "; ".join(value.all().values_list('name', flat=True))
        return features
    elif field == 'indicia_printer':
        features = "; ".join(value.all().values_list('name', flat=True))
        return features
    elif field == 'feature' and \
      revision._meta.model_name == 'featurelogorevision':
        features = ''
        for feature in value.all():
            features += absolute_url(feature) + '; '
        if features:
            features = features[:-2]
        return mark_safe(features)
    return value
예제 #4
0
def migrate_reprint_notes(i, standard=True, do_save=True):
    text_reprint = ""
    for string in split_reprint_string(i.reprint_notes):
        issue, notes, sequence_number, story,is_origin = \
          find_migration_candidates(i, string, standard=standard)
        if notes == None:
            notes = ''
        if issue and sequence_number < 0:
            if i.sequence_number == 0 and is_origin == False:
                sequence_number = 0
                story = Story.objects.exclude(deleted=True).filter(issue=issue)
                story = story.filter(sequence_number = \
                                                sequence_number)
                if len(story) == 1:
                    story = story[0]
                else:
                    sequence_number = -1
                    story = None
        if story:
            if is_origin:
                if Reprint.objects.filter(origin = i.story, target = story).count() > 0 \
                  or ReprintRevision.objects.filter(origin_story=i.story, target_story=story, changeset__state=1).count() > 0:
                    if notes:
                        existing_revision = ReprintRevision.objects.filter(
                            origin_story=i.story,
                            target_story=story,
                            changeset__state=1)
                        if existing_revision:
                            if existing_revision.count() > 1:
                                raise ValueError
                            existing_revision = existing_revision[0]
                            if existing_revision.notes != notes:
                                if existing_revision.notes:
                                    existing_revision.notes += '; ' + notes
                                else:
                                    existing_revision.notes = notes
                                existing_revision.save()
                        else:
                            existing = Reprint.objects.get(origin=i.story,
                                                           target=story)
                            if existing.notes != notes:
                                reprint_revision = ReprintRevision.objects.clone_revision(\
                                    reprint=existing, changeset=i.changeset)
                                if reprint_revision.notes:
                                    reprint_revision.notes += '; ' + notes
                                else:
                                    reprint_revision.notes = notes
                                reprint_revision.save()
                else:
                    if do_save:
                        i.changeset.reprintrevisions.create(
                            origin_story=i.story,
                            target_story=story,
                            notes=notes)
                        #test = Reprint(origin=i, target=story,
                        #notes=notes)
                        #test.save()
                    else:
                        print "S2S:", string, "to:"
                        print i, i.issue, story, story.issue
            else:
                if Reprint.objects.filter(origin = story, target = i.story).count() > 0 \
                  or ReprintRevision.objects.filter(origin_story=story, target_story=i.story, changeset__state=1).count() > 0:
                    if notes:
                        existing_revision = ReprintRevision.objects.filter(
                            origin_story=story,
                            target_story=i.story,
                            changeset__state=1)
                        if existing_revision:
                            if existing_revision.count() > 1:
                                raise ValueError
                            existing_revision = existing_revision[0]
                            if existing_revision.notes != notes:
                                if existing_revision.notes:
                                    existing_revision.notes += '; ' + notes
                                else:
                                    existing_revision.notes = notes
                                existing_revision.save()
                        else:
                            existing = Reprint.objects.get(origin=story,
                                                           target=i.story)
                            if existing.notes != notes:
                                reprint_revision = ReprintRevision.objects.clone_revision(\
                                    reprint=existing, changeset=i.changeset)
                                if reprint_revision.notes:
                                    reprint_revision.notes += '; ' + notes
                                else:
                                    reprint_revision.notes = notes
                                reprint_revision.save()
                else:
                    if do_save:
                        i.changeset.reprintrevisions.create(
                            origin_story=story,
                            target_story=i.story,
                            notes=notes)
                        #test = Reprint(origin=story, target=i,
                        #notes=notes)
                        #test.save()
                    else:
                        print "S2S", string, "to:"
                        print story, story.issue, i, i.issue
        elif issue:
            if do_save:
                if is_origin:
                    i.changeset.reprintrevisions.create(origin_story=i.story,
                                                        target_issue=issue,
                                                        notes=notes)
                    #test = ReprintToIssue(origin=i, target_issue=issue,
                    #notes=notes)
                else:
                    i.changeset.reprintrevisions.create(origin_issue=issue,
                                                        target_story=i.story,
                                                        notes=notes)
                    #test = ReprintFromIssue(origin_issue=issue, target=i,
                    #notes=notes)
                #test.save()
            else:
                print "S2I", string, "to:"
                print i, i.issue, issue
        elif string.strip() != '':
            text_reprint += string + "; "
    if len(text_reprint) > 0:
        text_reprint = text_reprint[:-2]
    if do_save:
        if i.reprint_notes != text_reprint:
            i.reprint_notes = text_reprint
            i.save()
            if not standard:
                i.story.migration_status.reprint_needs_inspection = True
                i.story.migration_status.save()
            return True
        else:
            return False
    else:
        if i.reprint_notes != text_reprint:
            print "changed:", i.reprint_notes
            print "to:", text_reprint
예제 #5
0
def migrate_reprints_lars():
    # 18732  Superman
    # 26245  Superboy
    # 31648  Batman Sonderheft
    # 36955  Grüne Leuchte
    # 36980  Wundergirl
    # 36973  Superman Sonderausgabe
    # 36949  Batman Sonderausgabe
    # 36975  Superman Superband
    # 36964  Roter Blitz
    # 36953  Gerechtigkeitsliga
    # 36967  Superfreunde
    # 39648  Atom
    issues = Issue.objects.exclude(deleted=True).filter(series__id__in=[
        18732, 26245, 31648, 36955, 36980, 36973, 36949, 36975, 36964, 36953,
        36967, 39648
    ],
                                                        reserved=False)
    #issues = Issue.objects.filter(series__id__in = [36953], reserved=False)
    migrated = []
    for migrate_issue in issues:
        changeset = migrate_reserve(migrate_issue, 'issue',
                                    'to migrate reprint notes into links')
        if not changeset:  # this should not happen since we filter out reserved issues
            raise ValueError, "%s is reserved" % migrate_issue
        #else:
        #print migrate_issue, migrate_issue.id
        things = changeset.storyrevisions.all()
        for i in things:
            text_reprint = ""
            if i.reprint_notes:
                for string in split_reprint_string(i.reprint_notes):
                    #print len(string)
                    if string == " USA":
                        break
                    #print string
                    issue, notes, sequence_number, story = parse_reprint_lars(
                        string)
                    if issue and issue.series.language.code.lower(
                    ) == 'en' and string.startswith('in '):
                        # Lars sometimes copied further US printings, don't create links
                        #print "double", string
                        issue = None
                    #print issue, notes, sequence_number, story
                    if sequence_number < 0 and issue:
                        if i.sequence_number == 0:
                            story = Story.objects.exclude(deleted=True).filter(
                                issue=issue)
                            story = story.filter(sequence_number=0)
                            if story.count() == 1:
                                story = story[0]
                                sequence_number = 0
                            else:
                                story = False
                        #else:
                        #print "no sequence number found", string, issue
                    #print issue
                    if issue:
                        if sequence_number >= 0 and story:
                            changeset.reprintrevisions.create(
                                origin_story=story,
                                target_story=i.story,
                                notes=notes)
                        else:
                            if issue.series.language.code.lower() == 'de':
                                nr = find_reprint_sequence_in_issue(
                                    i.story, issue.id)
                                if string.lower().startswith('from'):
                                    if nr > 0:
                                        story = issue.active_stories().get(
                                            sequence_number=nr)
                                        changeset.reprintrevisions.create(
                                            origin_story=story,
                                            target_story=i.story,
                                            notes=notes)
                                    else:
                                        changeset.reprintrevisions.create(
                                            origin_issue=issue,
                                            target_story=i.story,
                                            notes=notes)
                                else:
                                    if nr > 0:
                                        story = issue.active_stories().get(
                                            sequence_number=nr)
                                        changeset.reprintrevisions.create(
                                            origin_story=i.story,
                                            target_story=story,
                                            notes=notes)
                                    else:
                                        changeset.reprintrevisions.create(
                                            target_issue=issue,
                                            origin_story=i.story,
                                            notes=notes)
                            else:
                                changeset.reprintrevisions.create(
                                    origin_issue=issue,
                                    target_story=i.story,
                                    notes=notes)
                    else:
                        text_reprint += string + "; "
                if len(text_reprint) > 0:
                    text_reprint = text_reprint[:-2]
                    #print "Reprint Note Left: ", i.issue, i.issue.id, text_reprint
                    i.migration_status.reprint_needs_inspection = True
                    i.migration_status.save()
                i.reprint_notes = text_reprint
                i.save()
        if changeset.reprintrevisions.count():
            migrated.append((changeset, True))
        else:  # nothing migrated
            #print 'nothing migrated'
            changeset.discard(changeset.indexer)  # free reservation
            changeset.delete()  # no need to keep changeset
        if len(migrated) > 100:
            do_auto_approve(migrated, 'reprint note migration')
            migrated = []
    if len(migrated):
        do_auto_approve(migrated, 'reprint note migration')
def migrate_reprint_notes(i, standard = True, do_save = True):
    text_reprint = ""
    for string in split_reprint_string(i.reprint_notes):
        issue, notes, sequence_number, story,is_origin = \
          find_migration_candidates(i, string, standard=standard)
        if notes == None:
            notes = ''
        if issue and sequence_number < 0:
            if i.sequence_number == 0 and is_origin == False:
                sequence_number = 0
                story = Story.objects.exclude(deleted=True).filter(issue = issue)
                story = story.filter(sequence_number = \
                                                sequence_number)
                if len(story) == 1:
                    story = story[0]
                else:
                    sequence_number = -1
                    story = None
        if story:
            if is_origin:
                if Reprint.objects.filter(origin = i.story, target = story).count() > 0 \
                  or ReprintRevision.objects.filter(origin_story=i.story, target_story=story, changeset__state=1).count() > 0:
                    if notes:
                        existing_revision = ReprintRevision.objects.filter(origin_story = i.story,
                                                          target_story = story, changeset__state=1)
                        if existing_revision:
                            if existing_revision.count() > 1:
                                raise ValueError
                            existing_revision = existing_revision[0]
                            if existing_revision.notes != notes:
                                if existing_revision.notes:
                                    existing_revision.notes += '; ' + notes
                                else:
                                    existing_revision.notes = notes
                                existing_revision.save()
                        else:
                            existing = Reprint.objects.get(origin = i.story,
                                                            target = story)
                            if existing.notes != notes:
                                reprint_revision = ReprintRevision.objects.clone_revision(\
                                    reprint=existing, changeset=i.changeset)
                                if reprint_revision.notes:
                                    reprint_revision.notes += '; ' + notes
                                else:
                                    reprint_revision.notes = notes
                                reprint_revision.save()
                else:
                    if do_save:
                        i.changeset.reprintrevisions.create(origin_story=i.story, target_story=story, notes=notes)
                        #test = Reprint(origin=i, target=story,
                                                    #notes=notes)
                        #test.save()
                    else:
                        print "S2S:",string, "to:"
                        print i, i.issue, story, story.issue
            else:
                if Reprint.objects.filter(origin = story, target = i.story).count() > 0 \
                  or ReprintRevision.objects.filter(origin_story=story, target_story=i.story, changeset__state=1).count() > 0:
                    if notes:
                        existing_revision = ReprintRevision.objects.filter(origin_story = story,
                                                          target_story = i.story, changeset__state=1)
                        if existing_revision:
                            if existing_revision.count() > 1:
                                raise ValueError
                            existing_revision = existing_revision[0]
                            if existing_revision.notes != notes:
                                if existing_revision.notes:
                                    existing_revision.notes += '; ' + notes
                                else:
                                    existing_revision.notes = notes
                                existing_revision.save()
                        else:
                            existing = Reprint.objects.get(origin = story,
                                                            target = i.story)
                            if existing.notes != notes:
                                reprint_revision = ReprintRevision.objects.clone_revision(\
                                    reprint=existing, changeset=i.changeset)
                                if reprint_revision.notes:
                                    reprint_revision.notes += '; ' + notes
                                else:
                                    reprint_revision.notes = notes
                                reprint_revision.save()
                else:
                    if do_save:
                        i.changeset.reprintrevisions.create(origin_story=story, target_story=i.story, notes=notes)
                        #test = Reprint(origin=story, target=i,
                                                    #notes=notes)
                        #test.save()
                    else:
                        print "S2S",string, "to:"
                        print story, story.issue, i, i.issue
        elif issue:
            if do_save:
                if is_origin:
                    i.changeset.reprintrevisions.create(origin_story=i.story, target_issue=issue, notes=notes)
                    #test = ReprintToIssue(origin=i, target_issue=issue,
                                                        #notes=notes)
                else:
                    i.changeset.reprintrevisions.create(origin_issue=issue, target_story=i.story, notes=notes)
                    #test = ReprintFromIssue(origin_issue=issue, target=i,
                                                        #notes=notes)
                #test.save()
            else:
                print "S2I",string, "to:"
                print i, i.issue, issue
        elif string.strip() != '':
            text_reprint += string + "; "
    if len(text_reprint) > 0:
        text_reprint = text_reprint[:-2]
    if do_save:
        if i.reprint_notes != text_reprint:
            i.reprint_notes = text_reprint
            i.save()
            if not standard:
                i.story.migration_status.reprint_needs_inspection = True
                i.story.migration_status.save()
            return True
        else:
            return False
    else:
        if i.reprint_notes != text_reprint:
            print "changed:", i.reprint_notes
            print "to:", text_reprint
def migrate_reprints_lars():
    # 18732  Superman
    # 26245  Superboy
    # 31648  Batman Sonderheft
    # 36955  Grüne Leuchte
    # 36980  Wundergirl
    # 36973  Superman Sonderausgabe
    # 36949  Batman Sonderausgabe
    # 36975  Superman Superband
    # 36964  Roter Blitz
    # 36953  Gerechtigkeitsliga
    # 36967  Superfreunde
    # 39648  Atom
    issues = Issue.objects.exclude(deleted=True).filter(series__id__in = [18732,26245,31648,36955,36980,36973,36949,36975,36964, 36953, 36967, 39648], reserved=False)
    #issues = Issue.objects.filter(series__id__in = [36953], reserved=False)
    migrated = []
    for migrate_issue in issues:
        changeset = migrate_reserve(migrate_issue, 'issue', 'to migrate reprint notes into links')
        if not changeset: # this should not happen since we filter out reserved issues
            raise ValueError, "%s is reserved" % migrate_issue
        #else:
            #print migrate_issue, migrate_issue.id
        things = changeset.storyrevisions.all()
        for i in things:
            text_reprint = ""
            if i.reprint_notes:
                for string in split_reprint_string(i.reprint_notes):
                    #print len(string)
                    if string == " USA":
                        break
                    #print string
                    issue, notes, sequence_number, story = parse_reprint_lars(string)
                    if issue and issue.series.language.code.lower() == 'en' and string.startswith('in '):
                        # Lars sometimes copied further US printings, don't create links
                        #print "double", string
                        issue = None
                    #print issue, notes, sequence_number, story
                    if sequence_number < 0 and issue:
                        if i.sequence_number == 0:
                            story = Story.objects.exclude(deleted=True).filter(issue = issue)
                            story = story.filter(sequence_number = 0)
                            if story.count() == 1:
                                story = story[0]
                                sequence_number = 0
                            else:
                                story = False
                        #else:
                            #print "no sequence number found", string, issue
                    #print issue
                    if issue:
                        if sequence_number >= 0 and story:
                            changeset.reprintrevisions.create(origin_story=story, target_story=i.story, notes=notes)
                        else:
                            if issue.series.language.code.lower() == 'de':
                                nr = find_reprint_sequence_in_issue(i.story, issue.id)
                                if string.lower().startswith('from'):
                                    if nr > 0:
                                        story = issue.active_stories().get(sequence_number = nr)
                                        changeset.reprintrevisions.create(origin_story=story, target_story=i.story, notes=notes)
                                    else:
                                        changeset.reprintrevisions.create(origin_issue=issue, target_story=i.story, notes=notes)
                                else:
                                    if nr > 0:
                                        story = issue.active_stories().get(sequence_number = nr)
                                        changeset.reprintrevisions.create(origin_story=i.story, target_story=story, notes=notes)
                                    else:
                                        changeset.reprintrevisions.create(target_issue=issue,
                                                            origin_story=i.story, notes=notes)
                            else:
                                changeset.reprintrevisions.create(origin_issue=issue, target_story=i.story, notes=notes)
                    else:
                        text_reprint += string + "; "
                if len(text_reprint) > 0:
                    text_reprint = text_reprint[:-2]
                    #print "Reprint Note Left: ", i.issue, i.issue.id, text_reprint
                    i.migration_status.reprint_needs_inspection = True
                    i.migration_status.save()
                i.reprint_notes = text_reprint
                i.save()
        if changeset.reprintrevisions.count():
            migrated.append((changeset, True))
        else: # nothing migrated
            #print 'nothing migrated'
            changeset.discard(changeset.indexer) # free reservation
            changeset.delete() # no need to keep changeset
        if len(migrated) > 100:
            do_auto_approve(migrated, 'reprint note migration')
            migrated = []
    if len(migrated):
        do_auto_approve(migrated, 'reprint note migration')