def main(): api = PopIt(instance = 'professors', hostname = '127-0-0-1.org.uk', port = 3000, user = '******', password = '******') # Create print("CREATE") new = api.person.post({'name': 'Albert Keinstein'}) pprint(new) id = new['result']['_id'] # Update print("UPDATE") result = api.person(id).put({"name": "Albert Einstein"}) pprint(result) # Read print("READ") result = api.person(id).get() pprint(result) # read all results = api.person().get() pprint(results) # Delete print("DELETE") result = api.person(id).delete() pprint(result)
def main(): config = yaml.load(open(os.path.join(BASE_DIR, 'config.yml'), 'r')) api = PopIt(instance=config.get('POPIT_INSTANCE'), hostname=config.get('POPIT_HOSTNAME', 'popit.mysociety.org'), port=config.get('POPIT_PORT', 80), api_version='v0.1', user=config.get('POPIT_EMAIL'), password=config.get('POPIT_PASSWORD')) init(api) # cleanup parliament = create_parliament(api) create_parties(api) create_committees(api) with open('data/mps.yml', 'r') as fl: # read mps yaml file created by scraper for data in yaml.load(fl): person = create_person(api, data) constituency = data['constituency'] _, _, party_id = party_keys(data['party']) # ?? why do I need an empty id property to be valid? Popolo spec needs change? area = {'name': constituency, 'id': ''} create_membership(api, party_id, person['id'], area=area) create_membership(api, parliament['id'], person['id'], role='Member', label='MP for %s' % constituency, area=area)
def main(): api = PopIt(instance='professors', hostname='127-0-0-1.org.uk', port=3000, user='******', password='******') # Create print("CREATE") new = api.person.post({'name': 'Albert Keinstein'}) pprint(new) id = new['result']['_id'] # Update print("UPDATE") result = api.person(id).put({"name": "Albert Einstein"}) pprint(result) # Read print("READ") result = api.person(id).get() pprint(result) # read all results = api.person().get() pprint(results) # Delete print("DELETE") result = api.person(id).delete() pprint(result)
def handle(self, *args, **options): if options['test']: import doctest failure_count, _ = doctest.testmod(sys.modules[__name__]) sys.exit(0 if failure_count == 0 else 1) popit_option_keys = ('instance', 'hostname', 'user', 'password', 'port') popit_options = dict((k, options[k]) for k in popit_option_keys if options[k] is not None) popit_options['api_version'] = 'v1' if len(args) != 1: raise CommandError, "You must provide the base URL of the public Pombola site" try: popit = PopIt(**popit_options) base_url = args[0] parsed_url = urlparse.urlparse(base_url) message = "WARNING: this script will delete everything in the PopIt instance %s on %s.\n" message += "If you want to continue with this, type 'Yes': " response = raw_input(message % (popit.instance, popit.hostname)) if response != 'Yes': print >> sys.stderr, "Aborting." sys.exit(1) if parsed_url.path or parsed_url.params or parsed_url.query or parsed_url.fragment: raise CommandError, "You must only provide the base URL" # Remove all the "person", "organisation" and "position" # objects from PopIt. Currently there's no command to # delete all in one go, so we have to do it one-by-one. for p in popit.person.get()['results']: print >> sys.stderr, "deleting the person:", p popit.person(p['_id']).delete() for o in popit.organisation.get()['results']: print >> sys.stderr, "deleting the organisation:", o popit.organisation(o['_id']).delete() for p in popit.position.get()['results']: print >> sys.stderr, "deleting the position:", p popit.position(p['_id']).delete() # Create all the organisations found in Pombola, and get # back a dictionary mapping the Pombola organisation slug # to the PopIt ID. org_slug_to_id = create_organisations(popit) # Create a person in PopIt for each Person in Pombola: for person in Person.objects.all(): name = person.legal_name print >> sys.stderr, "creating the person:", name new_person = popit.person.post({'name': name}) person_id = new_person['result']['_id'] properties = {"personal_details": make_personal_details(person.date_of_birth, person.date_of_death)} if person.primary_image(): properties['images' ] = [{'url': base_url + person.primary_image().url}] result = popit.person(person_id).put(properties) for position in person.position_set.all(): if not (position.title and position.title.name): continue properties = {'title': position.title.name, 'person': person_id, 'start_date': date_to_popit_partial_date(position.start_date), 'end_date': date_to_popit_partial_date(position.end_date)} if position.organisation: oslug = position.organisation.slug organisation_id = org_slug_to_id[oslug] properties['organisation'] = organisation_id print >> sys.stderr, " creating the position:", position new_position = popit.position.post(properties) except slumber.exceptions.HttpClientError, e: print "Exception is:", e print "Error response content is", e.content sys.exit(1)