예제 #1
0
def save_user(migration_rules, model_hash, instance):
    model = hash_to_model(migration_rules, 'user', model_hash, instance)

    try:
        # TODO: this is REALLY ambiguous. If the try fails,
        # does the previously assigned value of 'model' persist?
        model = User.objects.get(email__iexact=model.email)
    except User.DoesNotExist:
        model.username = _uniquify_username(model.username)
        model.save()

    try:
        InstanceUser.objects.get(instance=instance,
                                 user=model)
    except InstanceUser.DoesNotExist:
        InstanceUser.objects.create(instance=instance,
                                    user=model, role=instance.default_role)

    (OTM1UserRelic
     .objects
     .get_or_create(instance=instance,
                    otm1_username=model_hash['fields']['username'],
                    otm2_user=model,
                    otm1_id=model_hash['pk'],
                    email=model_hash['fields']['email']))
예제 #2
0
def save_comment(migration_rules, dependency_ids, model_hash, instance):
    model = hash_to_model(migration_rules,
                          'comment', model_hash,
                          instance)

    model.site_id = 1

    if model.content_type_id == -1:
        print("Can't import comment %s because "
              "it is assigned to a ContentType (model) "
              "that does not exist in OTM2 .. SKIPPING"
              % model.comment)
        return None

    old_object_id = int(model_hash['fields']['object_pk'])
    new_object_id = dependency_ids[model.content_type.model][old_object_id]

    # object_id is called object_pk in later versions
    model.object_pk = new_object_id

    model.save()

    OTM1CommentRelic.objects.create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_id=model.pk)

    return model
예제 #3
0
def save_audit(migration_rules, dependency_ids, model_hash, instance):
    model = hash_to_model(migration_rules, 'audit', model_hash, instance)
    fields = model_hash['fields']

    # update the object_id
    audit_object_relic = OTM1ModelRelic.objects.get(
        instance=instance,
        otm2_model_name__iexact=model_hash['fields']['model'],
        otm1_model_id=model_hash['fields']['model_id'])
    model.model_id = audit_object_relic.otm2_model_id

    if ((fields['field'] == 'id' and
         fields['current_value'] == fields['model_id'])):
        model.current_value = audit_object_relic.otm2_model_id

    model.save()

    # should we save relics audits, or is this a lie?
    # TODO: make audit export ids have meaning, instead of just
    # serializing them.
    OTM1ModelRelic.objects.create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_name='audit',
        otm2_model_id=model.pk)
예제 #4
0
def save_boundary(migration_rules, model_hash, instance):
    model = hash_to_model(migration_rules, 'boundary', model_hash, instance)
    model.save()
    OTM1ModelRelic.objects.get_or_create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_name='boundary',
        otm2_model_id=model.pk)

    instance.boundaries.add(model)
    return model
예제 #5
0
def save_species(migration_rules, model_hash, instance):
    # Does this species already exist?
    genus, species, cultivar, other = [
        model_hash['fields'].get(thing, '')
        for thing in ['genus', 'species', 'cultivar', 'other_part_of_name']]

    existingspecies = Species.objects.filter(genus=genus,
                                             species=species,
                                             cultivar=cultivar,
                                             other=other)

    relicinfo = {
        'instance': instance,
        'otm1_model_id': model_hash['pk'],
        'otm2_model_name': 'species'
    }

    relics = OTM1ModelRelic.objects.filter(**relicinfo)

    if len(relics) == 0:
        relic = OTM1ModelRelic(**relicinfo)
    else:
        relic = relics[0]

    if len(existingspecies) == 0:

        model = hash_to_model(migration_rules,
                              'species', model_hash,
                              instance)

        for sp in SPECIES:
            if ((sp['genus'] == genus and
                 sp['species'] == species and
                 sp['cultivar'] == cultivar and
                 sp['other'] == other)):
                model.otm_code = sp['otm_code']
                break

        # this field can be null in otm1, but not otm2
        if not model.common_name:
            model.common_name = ''

        model = save_model_with_user(migration_rules, model, instance)

        relic.otm2_model_id = model.pk
    else:
        model = None
        relic.otm2_model_id = existingspecies[0].pk

    relic.save()

    return model
예제 #6
0
def save_other_with_user(migration_rules, model_name, model_hash, instance):

    model = hash_to_model(migration_rules,
                          model_name, model_hash,
                          instance)

    model = save_model_with_user(migration_rules, model, instance)

    OTM1ModelRelic.objects.get_or_create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_name=model_name,
        otm2_model_id=model.pk)
    return model
def save_species(migration_rules, model_hash, instance):
    # Does this species already exist?
    genus, species, cultivar, other = [
        model_hash['fields'].get(thing, '')
        for thing in ['genus', 'species', 'cultivar', 'other_part_of_name']
    ]

    existingspecies = Species.objects.filter(genus=genus,
                                             species=species,
                                             cultivar=cultivar,
                                             other=other)

    relicinfo = {
        'instance': instance,
        'otm1_model_id': model_hash['pk'],
        'otm2_model_name': 'species'
    }

    relics = OTM1ModelRelic.objects.filter(**relicinfo)

    if len(relics) == 0:
        relic = OTM1ModelRelic(**relicinfo)
    else:
        relic = relics[0]

    if len(existingspecies) == 0:

        model = hash_to_model(migration_rules, 'species', model_hash, instance)

        for sp in SPECIES:
            if ((sp['genus'] == genus and sp['species'] == species
                 and sp['cultivar'] == cultivar and sp['other'] == other)):
                model.otm_code = sp['otm_code']
                break

        # this field can be null in otm1, but not otm2
        if not model.common_name:
            model.common_name = ''

        model = save_model_with_user(migration_rules, model, instance)

        relic.otm2_model_id = model.pk
    else:
        model = None
        relic.otm2_model_id = existingspecies[0].pk

    relic.save()

    return model
예제 #8
0
def save_threadedcomment(
        migration_rules, dependency_ids, model_hash, instance):

    model = hash_to_model(migration_rules,
                          'threadedcomment', model_hash,
                          instance)

    model.site_id = 1

    if model.content_type_id == -1:
        print("Can't import threadedcomment %s because "
              "it is assigned to a ContentType (model) "
              "that does not exist in OTM2 .. SKIPPING"
              % model.comment)
        return None
    model.save()

    old_object_id = model_hash['fields']['object_id']
    new_object_id = dependency_ids[model.content_type.model][old_object_id]
    # object_id is called object_pk in later versions
    model.object_pk = new_object_id

    # find relic/dependency id for the parent and set that.
    if model_hash['fields']['parent']:
        parent_relic = (OTM1CommentRelic.objects
                        .get(otm1_model_id=model_hash['fields']['parent']))
        model.parent_id = parent_relic.otm2_model_id

    else:
        parent_relic = None

    model.save()

    if ((parent_relic and
         parent_relic.otm1_last_child_id is not None and
         parent_relic.otm1_last_child_id == model_hash['pk'])):
        parent = parent_relic.summon()
        parent.last_child = model.pk
        parent.save()

    OTM1CommentRelic.objects.create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_id=model.pk,
        otm1_last_child_id=model_hash['fields'].get('last_child', None))

    return model
예제 #9
0
def save_treephoto(migration_rules, treephoto_path, model_hash, instance):
    model = hash_to_model(migration_rules, 'treephoto', model_hash, instance)

    image = open(os.path.join(treephoto_path, model_hash['fields']['photo']))
    model.set_image(image)
    model.map_feature_id = Tree.objects.values_list('plot__id', flat=True)\
                                       .get(pk=model.tree_id)

    del model_hash['fields']['photo']

    model = save_model_with_user(migration_rules, model, instance)

    OTM1ModelRelic.objects.get_or_create(instance=instance,
                                         otm1_model_id=model_hash['pk'],
                                         otm2_model_name='treephoto',
                                         otm2_model_id=model.pk)
    return model
예제 #10
0
def save_threadedcomment(migration_rules, dependency_ids, model_hash,
                         instance):

    model = hash_to_model(migration_rules, 'threadedcomment', model_hash,
                          instance)

    model.site_id = 1

    if model.content_type_id == -1:
        print("Can't import threadedcomment %s because "
              "it is assigned to a ContentType (model) "
              "that does not exist in OTM2 .. SKIPPING" % model.comment)
        return None
    model.save()

    old_object_id = model_hash['fields']['object_id']
    new_object_id = dependency_ids[model.content_type.model][old_object_id]
    # object_id is called object_pk in later versions
    model.object_pk = new_object_id

    # find relic/dependency id for the parent and set that.
    if model_hash['fields']['parent']:
        parent_relic = (OTM1CommentRelic.objects.get(
            otm1_model_id=model_hash['fields']['parent']))
        model.parent_id = parent_relic.otm2_model_id

    else:
        parent_relic = None

    model.save()

    if ((parent_relic and parent_relic.otm1_last_child_id is not None
         and parent_relic.otm1_last_child_id == model_hash['pk'])):
        parent = parent_relic.summon()
        parent.last_child = model.pk
        parent.save()

    OTM1CommentRelic.objects.create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_id=model.pk,
        otm1_last_child_id=model_hash['fields'].get('last_child', None))

    return model
예제 #11
0
def save_treephoto(treephoto_path, model_hash, instance):
    model = hash_to_model(MIGRATION_RULES,
                          'treephoto', model_hash,
                          instance)

    image = open(os.path.join(treephoto_path,
                              model_hash['fields']['photo']))
    model.set_image(image)

    del model_hash['fields']['photo']

    model = save_model_with_user(model, instance)

    OTM1ModelRelic.objects.get_or_create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_name='treephoto',
        otm2_model_id=model.pk)
    return model
예제 #12
0
def save_audit(migration_rules, dependency_ids, model_hash, instance):
    model = hash_to_model(migration_rules, 'audit', model_hash, instance)
    fields = model_hash['fields']

    # update the object_id
    audit_object_relic = OTM1ModelRelic.objects.get(
        instance=instance,
        otm2_model_name__iexact=model_hash['fields']['model'],
        otm1_model_id=model_hash['fields']['model_id'])
    model.model_id = audit_object_relic.otm2_model_id

    if ((fields['field'] == 'id'
         and fields['current_value'] == fields['model_id'])):
        model.current_value = audit_object_relic.otm2_model_id

    model.save()
    OTM1ModelRelic.objects.create(instance=instance,
                                  otm1_model_id=model_hash['pk'],
                                  otm2_model_name='audit',
                                  otm2_model_id=model.pk)
예제 #13
0
def save_audit(migration_rules, dependency_ids, model_hash, instance):
    model = hash_to_model(migration_rules, 'audit', model_hash, instance)
    fields = model_hash['fields']

    # update the object_id
    audit_object_relic = OTM1ModelRelic.objects.get(
        instance=instance,
        otm2_model_name__iexact=model_hash['fields']['model'],
        otm1_model_id=model_hash['fields']['model_id'])
    model.model_id = audit_object_relic.otm2_model_id

    if ((fields['field'] == 'id' and
         fields['current_value'] == fields['model_id'])):
        model.current_value = audit_object_relic.otm2_model_id

    model.save()
    OTM1ModelRelic.objects.create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_name='audit',
        otm2_model_id=model.pk)
예제 #14
0
def save_treephoto(migration_rules, treephoto_path, model_hash, instance):
    model = hash_to_model(migration_rules,
                          'treephoto', model_hash,
                          instance)

    image = open(os.path.join(treephoto_path,
                              model_hash['fields']['photo']))
    model.set_image(image)
    model.map_feature_id = Tree.objects.values_list('plot__id', flat=True)\
                                       .get(pk=model.tree_id)

    del model_hash['fields']['photo']

    model = save_model_with_user(migration_rules, model, instance)

    OTM1ModelRelic.objects.get_or_create(
        instance=instance,
        otm1_model_id=model_hash['pk'],
        otm2_model_name='treephoto',
        otm2_model_id=model.pk)
    return model
예제 #15
0
def save_comment(migration_rules, dependency_ids, model_hash, instance):
    model = hash_to_model(migration_rules, 'comment', model_hash, instance)

    model.site_id = 1

    if model.content_type_id == -1:
        print("Can't import comment %s because "
              "it is assigned to a ContentType (model) "
              "that does not exist in OTM2 .. SKIPPING" % model.comment)
        return None

    old_object_id = int(model_hash['fields']['object_pk'])
    new_object_id = dependency_ids[model.content_type.model][old_object_id]

    # object_id is called object_pk in later versions
    model.object_pk = new_object_id

    model.save()

    OTM1CommentRelic.objects.create(instance=instance,
                                    otm1_model_id=model_hash['pk'],
                                    otm2_model_id=model.pk)

    return model
예제 #16
0
def save_user(migration_rules, model_hash, instance):
    model = hash_to_model(migration_rules, 'user', model_hash, instance)

    try:
        # TODO: this is REALLY ambiguous. If the try fails,
        # does the previously assigned value of 'model' persist?
        model = User.objects.get(email__iexact=model.email)
    except User.DoesNotExist:
        model.username = _uniquify_username(model.username)
        model.save()

    try:
        InstanceUser.objects.get(instance=instance, user=model)
    except InstanceUser.DoesNotExist:
        InstanceUser.objects.create(instance=instance,
                                    user=model,
                                    role=instance.default_role)

    (OTM1UserRelic.objects.get_or_create(
        instance=instance,
        otm1_username=model_hash['fields']['username'],
        otm2_user=model,
        otm1_id=model_hash['pk'],
        email=model_hash['fields']['email']))
def save_audit(migration_rules, dependency_ids, model_hash, instance):
    model = hash_to_model(migration_rules, 'audit', model_hash, instance)
    fields = model_hash['fields']

    # update the object_id
    audit_object_relic = OTM1ModelRelic.objects.get(
        instance=instance,
        otm2_model_name__iexact=model_hash['fields']['model'],
        otm1_model_id=model_hash['fields']['model_id'])
    model.model_id = audit_object_relic.otm2_model_id

    if ((fields['field'] == 'id'
         and fields['current_value'] == fields['model_id'])):
        model.current_value = audit_object_relic.otm2_model_id

    model.save()

    # should we save relics audits, or is this a lie?
    # TODO: make audit export ids have meaning, instead of just
    # serializing them.
    OTM1ModelRelic.objects.create(instance=instance,
                                  otm1_model_id=model_hash['pk'],
                                  otm2_model_name='audit',
                                  otm2_model_id=model.pk)