Ejemplo n.º 1
0
    if primary_sources.has_key(int(rdict["primary_source_1"])):
      rdict["primary_source"] = "imports: %s\n" % primary_sources[int(rdict["primary_source_1"])]
    if primary_sources.has_key(int(rdict["primary_source_2"])):
      rdict["primary_source"] += "exports: %s" % primary_sources[int(rdict["primary_source_2"])]

    if not rdict["primary_source"]:
      del rdict["primary_source"]

    del rdict["primary_source_1"]
    del rdict["primary_source_2"]

    rdict["active"] = True
    rdict["submitted_by"] = migtools.mig_user
    
    try:
      entry = BilateralTradeDataEntry(**rdict)

      if entry.imports == "-9":
        if entry.exports == "-9":
          sys.stdout.write("[%i] %s - %s (dropped; missing data)\n" % (i, entry.location, entry.trade_partner))
          continue
        else:
          entry.imports = None
      elif entry.exports == "-9":
        entry.exports == None 
      else: # TEMPORARY: add only entries with missing data
        sys.stdout.write("[%i] %s - %s (ignored for now)\n" % (i, entry.location, entry.trade_partner))
        continue 

      entry.save()
      num_mig += 1
Ejemplo n.º 2
0
          f = float(string.replace(t, ',', '')) if t else None
          return unicode(f) if f and (f != 0) else None 
        except ValueError: 
          return None

      trade = map(clean_trade, row[1:])

      i = 0
      
      while i < len(trade):
        if trade[i]:
          if (exp_imp[i] == "imports") and ((i + 1) < len(trade)) and (partners[i] == partners[i + 1]) and trade[i + 1]:
            new_data = BilateralTradeDataEntry(location = loc, 
                                               trade_partner = partners[i], 
                                               imports = trade[i],
                                               exports = trade[i + 1],
                                               begin_date = begin_year,
                                               end_date = end_year,
                                               source = src,
                                               submitted_by = mig_user)
            new_data.save()
            i += 1
          else:
            kwargs = { 'location' : loc, 
                       'trade_partner' : partners[i], 
                       exp_imp[i] : trade[i], 
                       'begin_date' : begin_year,
                       'end_date' : end_year,
                       'source' : src,
                       'submitted_by' : mig_user } 
            new_data = BilateralTradeDataEntry(**kwargs)
            new_data.save()
Ejemplo n.º 3
0
        else:
          rdict[loc_name] = Location(name = codebook_dict[rdict[loc_name]], submitted_by = migtools.mig_user, active = True) 
          rdict[loc_name].save()
          new_locs_added.add(rdict[loc_name].name)
      else:
        locs_excluded += 1
        sys.stdout.write("[%i] Location '%s' excluded\n" % (i, rdict[loc_name]))
        continue

    rdict["begin_date"] = datetime.date(int(float(rdict["year"])), 1, 1)
    rdict["end_date"] = datetime.date(int(float(rdict["year"])), 12, 31)
    del rdict["year"]

    rdict["active"] = True
    rdict["submitted_by"] = migtools.mig_user

    rdict["source"] = src

    try:
      entry = BilateralTradeDataEntry(**rdict)
      entry.save()
      num_mig += 1
      sys.stdout.write("[%i] %s\n" % (i, entry))
    except (ValueError, DatabaseError, ValidationError) as e:
      sys.stderr.write('[%i] Failed to save data row: %s\n' % (i, e))
      num_err_rows += 1

  print 'Migration complete. %i rows migrated, %i rows excluded, %i locations excluded, and %i row errors encountered and ignored' % (num_mig, num_excluded, locs_excluded, num_err_rows)
  if len(new_locs_added) > 0:
    print("Locations added: %s" % (", ".join(new_locs_added)))