def create_person():
  '''
  create a random person in the db and return the person just created
  '''
  auth_api = OmAPI(service_root=settings.API_SERVICE_ROOT,
                   auth=BasicAuthorizer(settings.API_USER, settings.API_PASSWORD))

  try:
    f = open("om/fixtures/op_politician_first_names_sex.csv", "r")
    l = open("om/fixtures/op_politician_last_names.csv", "r")
    loc = open("om/fixtures/op_politician_birth_locations.csv", "r")
    dat = open("om/fixtures/op_politician_birth_dates.csv", "r")

    first_names = unify(f.readlines())
    last_names = unify(l.readlines())
    birth_dates = unify(dat.readlines())
    birth_locations = unify(loc.readlines())
  
    first_name = choice(first_names)
    first_names.remove(first_name)
    first_name = first_name.strip()
    (names, sex) = first_name.split(',')
    names = names.split()
    first_name = choice(names)

    last_name = choice(last_names)
    last_names.remove(last_name)
    last_name = last_name.strip()

    birth_date = choice(birth_dates)
    birth_dates.remove(birth_date)
    birth_date = birth_date.strip()
    birth_date = datetime.strptime(birth_date, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")
  
    birth_location = choice(birth_locations)
    birth_locations.remove(birth_location)
    birth_location = birth_location.strip()

    p_obj = {
      'first_name': first_name, 
      'last_name': last_name, 
      'birth_date': birth_date, 
      'birth_location': birth_location, 
      'sex': sex.lower()
    }
    
    # if the person exists, then return it, else create it and return it
    # avoid duplications
    p = json.loads(auth_api.get_persons_from_data(p_obj))
    if (len(p) == 0):
      auth_api.add_person(json.dumps(p_obj))
      p = json.loads(auth_api.get_persons_from_data(p_obj))

    return p[0]

      
  except IOError:
    print "Error while opening file"
    return 0
except ImportError:
    import simplejson as json
from om.api.clients import OmAPI
from piston_mini_client.auth import BasicAuthorizer
from create_random_persons import create_person
from django.conf import settings

'''
api = OmAPI(service_root='http://localhost:8000/api/1.0')

print "listing all persons"
persons = api.get_persons()
print persons
'''

auth_api = OmAPI(service_root=settings.API_SERVICE_ROOT,
                 auth=BasicAuthorizer(settings.API_USER, settings.API_PASSWORD))
print "----"

p_obj = {
  'first_name': 'Guglielmo',
  'last_name': 'pok pf ok', 
  'birth_date': '1955-04-25', 
  'birth_location': 'Roma', 
  'sex': 'm'
}
p = create_person()
print "added a random person"
print p

'''
print "----"
#!/usr/bin/env python
# -*- coding: utf8 -*-
try:
    import json
except ImportError:
    import simplejson as json
import yaml
from om.api.clients import OmAPI
from piston_mini_client.auth import BasicAuthorizer
from datetime import *
from create_random_persons import create_person
from django.conf import settings


auth_api = OmAPI(service_root=settings.API_SERVICE_ROOT, auth=BasicAuthorizer(settings.API_USER, settings.API_PASSWORD))

"""
# working sample
i_obj = {'charges': [{'charge_type': 'may', 'person': '1', 'start_date': '2010-05-15'}], 'description': 'The Mayor institution not the charge', 'name': 'Mayor', 'institution_type': 'may'}
i = json.dumps(i_obj)
print "adding %s" % i
auth_api.add_institution(i)

"""

institution_file = open("om/fixtures/institutions.yaml", "r")
institutions = yaml.load(institution_file)

# first loop: add random persons
for i in sorted(institutions.iterkeys()):
    institution = institutions[i]
#!/usr/bin/env python
# -*- coding: utf8 -*-
try:
    import json
except ImportError:
    import simplejson as json
import yaml
from om.api.clients import OmAPI
from piston_mini_client.auth import BasicAuthorizer
from datetime import *
from create_random_persons import create_person
from django.conf import settings

auth_api = OmAPI(service_root=settings.API_SERVICE_ROOT,
                 auth=BasicAuthorizer(settings.API_USER,
                                      settings.API_PASSWORD))
'''
# working sample
i_obj = {'charges': [{'charge_type': 'may', 'person': '1', 'start_date': '2010-05-15'}], 'description': 'The Mayor institution not the charge', 'name': 'Mayor', 'institution_type': 'may'}
i = json.dumps(i_obj)
print "adding %s" % i
auth_api.add_institution(i)

'''

institution_file = open("om/fixtures/institutions.yaml", "r")
institutions = yaml.load(institution_file)

# first loop: add random persons
for i in sorted(institutions.iterkeys()):
    institution = institutions[i]
except ImportError:
    import simplejson as json
from om.api.clients import OmAPI
from piston_mini_client.auth import BasicAuthorizer
from create_random_persons import create_person
from django.conf import settings
'''
api = OmAPI(service_root='http://localhost:8000/api/1.0')

print "listing all persons"
persons = api.get_persons()
print persons
'''

auth_api = OmAPI(service_root=settings.API_SERVICE_ROOT,
                 auth=BasicAuthorizer(settings.API_USER,
                                      settings.API_PASSWORD))
print "----"

p_obj = {
    'first_name': 'Guglielmo',
    'last_name': 'pok pf ok',
    'birth_date': '1955-04-25',
    'birth_location': 'Roma',
    'sex': 'm'
}
p = create_person()
print "added a random person"
print p
'''
print "----"