コード例 #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
コード例 #2
0
    #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)
コード例 #3
0
  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)
        else:
コード例 #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))