Example #1
0
def get_or_add_location(place_list, mig_user=mig_user):
    place_list = map(lambda x: x.title(), filter(lambda x: x, place_list))
    prev_loc = None
    prev_tree = Location.objects.all()
    location_created = False

    for in_loc_name in reversed(place_list):
        found_loc = prev_tree.filter(Q(name__exact=in_loc_name), Q(politically_in=None))
        found_loc_count = found_loc.count()

        if found_loc_count > 1:
            loc = None

            if prev_loc:
                immediate_subloc_tree = prev_loc.get_geographic_sub_locations(include_self=False, max_distance=1)

                immed_found_loc = immediate_subloc_tree.filter(Q(name__exact=in_loc_name), Q(politically_in=None))
                if immed_found_loc.count() > 0:  # Just taking the first match no matter what for now
                    loc = immed_found_loc[0]

            if not loc:
                loc = found_loc[0]  # Taking the first match for now
        elif found_loc_count == 0:
            loc = Location(
                name=in_loc_name, geographically_in=prev_loc, active=True, submitted_by=mig_user
            )  # , log = default_log)
            loc.save()
            location_created = True
        else:
            loc = found_loc[0]
            if location_created and not prev_loc.is_root():
                raise LocationTooComplicated("Found existing location after having created a non-existent one")
            else:
                loc.in_location = prev_loc

        prev_loc = loc
        prev_tree = prev_loc.get_geographic_sub_locations(include_self=False)

    return prev_loc
Example #2
0
try:
  src = Source.objects.get(pk = 3381)
except Source.DoesNotExist:
  src = Source.objects.get(name = "temp")

if __name__ == "__main__":
  infile = sys.argv[1]
  loc_name = sys.argv[2]

  reader = csv.reader(migtools.UTF8Recoder(open(infile, "r"), migtools.STRING_ENCODING), delimiter='\t', quotechar = '"')

  locs = Location.objects.filter(name__iexact = loc_name).filter(politically_in = None)[:1]
  loc = None

  if len(locs) == 0:
    loc = Location(name = loc_name, submitted_by = mig_user)
    loc.save()
  else:
    loc = locs[0]

  exp_imp = None
  partners = list()

  for i, row in enumerate(reader):
    if i == 0:
      exp_imp = row[1:]
    elif i == 1:
      prev_partner = None
      for partner_name in row[1:]:
        if prev_partner and (prev_partner.name.lower() == partner_name.lower()):
          partners.append(prev_partner)
Example #3
0
import sys

from django.db import transaction
from django.db.models import Q
from django.contrib.auth.models import User

from clio.common.models import PoliticalUnit, Location
from clio.common.admin import merge

mig_user = User.objects.get(username = '******')

if __name__ == "__main__":
  if len(sys.argv) > 1:
    loc_name = sys.argv[1]

    for l in Location.get_toplevel().filter(name__iexact = loc_name):
      print("%s (politically in %s)" % (l.name, l.politically_in))
  else:
    loc_dict = dict()
    for l in Location.get_toplevel():
      l_name = l.name.lower()
      if loc_dict.has_key(l_name):
        loc_dict[l_name].append(l)
      else:
        loc_dict[l_name] = [l,]

    for l_name, l_list in loc_dict.iteritems():
      if len(l_list) > 1:
        print("%s : %i" % (l_name, len(l_list)))

Example #4
0
    
if __name__ == "__main__":
  src = Source.objects.get(pk = 3400)
  
  infile = sys.argv[1]
  reader = csv.reader(migtools.UTF8Recoder(open(infile, "r"), migtools.STRING_ENCODING), delimiter='\t', quotechar = '"')

  new_locs_added = set()

  africa_regions = dict()
  for region_name in africa_regions_names:
    region_matches = Location.objects.filter(Q(name__iexact = region_name), Q(geographically_in = africa), Q(politically_in = None))
    if len(region_matches) > 0:
      africa_regions[region_name] = region_matches[0]
    else:
      new_region = Location(name = region_name, geographically_in = africa, submitted_by = migtools.mig_user, active = True)
      new_region.save()
      new_locs_added.add(unicode(new_region))
      africa_regions[region_name] = new_region

  num_err_rows = 0
  num_mig = 0

  last_region = None
  last_country = None

  for i, row in enumerate(reader):
    if i < 1:
      continue

    rdict = dict(zip(('location', 'pop_50', 'pop_60'), row))
Example #5
0
#!/usr/bin/python

import sys

from django.db import transaction
from django.db.models import Q
from django.contrib.auth.models import User

from clio.common.models import PoliticalUnit, Location
from clio.common.admin import merge

if __name__ == "__main__":
  if len(sys.argv) > 1:
    loc_name = sys.argv[1]

    merge_into = Location.get_toplevel().filter(name__iexact = loc_name)[0]

    if Location.get_toplevel().filter(name__iexact = loc_name).count() > 1:
      for l in Location.get_toplevel().filter(name__iexact = loc_name)[1:]:
        l.merge_into(merge_into)
        l.save()
      for l in Location.get_toplevel().filter(name__iexact = loc_name)[1:]:
        l.delete()
      merge_into.save()

    if len(sys.argv) > 3:
      if sys.argv[2] == "pk":
        put_in_loc = Location.objects.get(pk = sys.argv[3])
      else:
        put_in_loc = Location.get_toplevel().get(name__iexact = sys.argv[3])
      merge_into.geographically_in = put_in_loc
Example #6
0
    top_obj = None
    #cont_obj = None
    base_loc_obj = None
    name = rdict["name"]
    continent = rdict["continent"]
    country1 = rdict["country"]
    country2 = rdict["country_1"]

    if name == country1:
      country1 = None
      country2 = None

    cont_matches = Location.objects.filter(name__iexact = continent).filter(geographically_in = None)

    if cont_matches.count() == 0:
      top_obj = Location(name = continent, submitted_by = mig_user, active = True, locked = True)
      top_obj.save()
    else:
      top_obj = cont_matches[0]

    for country in (country2, country1):
      if country:
        country_matches = top_obj.get_geographic_sub_locations(include_self = False).filter(name__iexact = country)

        if country_matches.count() == 0:
          top_obj = Location(name = country, geographically_in = top_obj, submitted_by = mig_user, active = True, locked = True)
          top_obj.save()
        else:
          top_obj = country_matches[0]

    loc_matches = top_obj.get_geographic_sub_locations(include_self = False).filter(name__iexact = name)