<commit_before># -*- coding: utf-8 -*-
from south.v2 import DataMigration
from django.template.defaultfilters import slugify
import string
import random


def id_generator(size=6, chars=string.ascii_lowercase):
    return ''.join(random.choice(chars) for x in range(size))


class Migration(DataMigration):

    def get_or_create(self, model, name, parent, max_slug=50):
        slug = slugify(name)[:max_slug]
        try:
            obj = model.objects.get(slug=slug, parent=parent)
        except model.DoesNotExist:
            obj = model(name=name, slug=slug, parent=parent)
            obj.save()
        except Exception as e:
            print e

        return obj

    def forwards(self, orm):
        # Fix cultural blocs
        c = orm.CulturalBloc.objects.get(name="None")
        orm.MuseumObject.objects.filter(cultural_bloc=None).update(cultural_bloc=c)

        # Migrate to new geo-locations structure
        "Write your forwards methods here."
        for mo in orm.MuseumObject.objects.all():
            cultural_bloc = mo.cultural_bloc
            country = mo.place.country
            region = mo.place.region
            australian_state = mo.place.australian_state
            place_name = mo.place.name

            global_region, c = orm['location.GlobalRegion'].objects.get_or_create(
                name=cultural_bloc.name, description=cultural_bloc.definition,
                slug=slugify(cultural_bloc.name))
            country = self.get_or_create(orm['location.Country'], country, global_region)
            state_province = self.get_or_create(orm['location.StateProvince'], australian_state, country)
            region_district = self.get_or_create(orm['location.RegionDistrict'], region, state_province)
            locality = self.get_or_create(orm['location.Locality'], place_name, region_district)

            mo.global_region = global_region
            mo.country = country
            mo.state_province = state_province
            mo.region_district = region_district
            mo.locality = locality

            mo.save()

        orm.MuseumObject.objects.all().update(is_public_comment=True)
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."

    def backwards(self, orm):
        "Write your backwards methods here."
        orm.MuseumObject.objects.all().update(global_region=None, country=None,
            state_province=None, region_district=None, locality=None)
#        for mo in orm.MuseumObject.objects.all():
#            mo.global_region = None
#            mo.country = None
#            mo.state_province = None
#            mo.region_district = None
#            mo.locality = None
#            mo.save()
        orm['location.GlobalRegion'].objects.all().delete()
        orm['location.Country'].objects.all().delete()
        orm['location.StateProvince'].objects.all().delete()
        orm['location.RegionDistrict'].objects.all().delete()
        orm['location.Locality'].objects.all().delete()

    models = {
        'auth.group': {
            'Meta': {'object_name': 'Group'},
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
        },
        'auth.permission': {
            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),<commit_msg>copy extra info in geolocs migration<commit_after># -*- coding: utf-8 -*-
from south.v2 import DataMigration
from django.template.defaultfilters import slugify
import string
import random


def id_generator(size=6, chars=string.ascii_lowercase):
    return ''.join(random.choice(chars) for x in range(size))


class Migration(DataMigration):

    def get_or_create(self, model, name, parent, max_slug=50, description=None, place=None):
        slug = slugify(name)[:max_slug]
        try:
            obj = model.objects.get(slug=slug, parent=parent)
        except model.DoesNotExist:
            obj = model(name=name, slug=slug, parent=parent)
            obj.description = description
            if place is not None:
                obj.gn_id = place.gn_id
                obj.gn_name = place.gn_name
            obj.save()

        return obj

    def forwards(self, orm):
        # Fix cultural blocs
        c = orm.CulturalBloc.objects.get(name="None")
        orm.MuseumObject.objects.filter(cultural_bloc=None).update(cultural_bloc=c)

        # Migrate to new geo-locations structure
        for mo in orm.MuseumObject.objects.all():
            cultural_bloc = mo.cultural_bloc
            country = mo.place.country
            region = mo.place.region
            australian_state = mo.place.australian_state
            place_name = mo.place.name

            global_region, c = orm['location.GlobalRegion'].objects.get_or_create(
                name=cultural_bloc.name, description=cultural_bloc.definition,
                slug=slugify(cultural_bloc.name))
            country = self.get_or_create(orm['location.Country'], country, global_region)
            state_province = self.get_or_create(orm['location.StateProvince'], australian_state, country)
            region_district = self.get_or_create(
                orm['location.RegionDistrict'], region, state_province, description=region.description)
            locality = self.get_or_create(orm['location.Locality'], place_name,
                region_district, place=mo.place)

            mo.global_region = global_region
            mo.country = country
            mo.state_province = state_province
            mo.region_district = region_district
            mo.locality = locality

            mo.save()

        orm.MuseumObject.objects.all().update(is_public_comment=True)
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."

    def backwards(self, orm):
        "Write your backwards methods here."
        orm.MuseumObject.objects.all().update(global_region=None, country=None,
            state_province=None, region_district=None, locality=None)

        orm['location.GlobalRegion'].objects.all().delete()
        orm['location.Country'].objects.all().delete()
        orm['location.StateProvince'].objects.all().delete()
        orm['location.RegionDistrict'].objects.all().delete()
        orm['location.Locality'].objects.all().delete()

    models = {
        'auth.group': {
            'Meta': {'object_name': 'Group'},
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
        },
        'auth.permission': {
            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
{'fraction_non_alphanumeric': 0.1357629154985983, 'fraction_numerical': 0.002669870511280203, 'mean_word_length': 2.7610441767068274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2883256', 'n_tokens_mistral': 2234, 'n_tokens_neox': 2140, 'n_words': 394}