forked from mysociety/yournextrepresentative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reproduce-missing-memberships.py
executable file
·114 lines (92 loc) · 3.5 KB
/
reproduce-missing-memberships.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
from collections import defaultdict
from datetime import date, timedelta
import json
from optparse import OptionParser
import os
from os.path import join, dirname, realpath
from random import randint
import re
import requests
import slumber
import sys
import time
from popit_api import PopIt
from slugify import slugify
import yaml
directory = realpath(os.path.dirname(__file__))
configuration_file = join(directory, 'conf', 'general.yml')
with open(configuration_file) as f:
conf = yaml.load(f)
parser = OptionParser()
parser.add_option("--create", action="store_true", default=False,
help="Create the example person and organization")
parser.add_option("--delete", action="store_true", default=False,
help="Delete the example person and organization")
options, args = parser.parse_args()
try:
api_properties = {
'instance': conf['POPIT_INSTANCE'],
'hostname': conf['POPIT_HOSTNAME'],
'port': conf.get('POPIT_PORT', 80),
'api_version': 'v0.1',
}
popit_api_key = conf.get('POPIT_API_KEY')
popit_user = conf.get('POPIT_USER')
popit_password = conf.get('POPIT_PASSWORD')
if popit_api_key:
api_properties['api_key'] = popit_api_key
else:
api_properties['user'] = popit_user
api_properties['password'] = popit_password
api = PopIt(**api_properties)
person_id = 'joe-example-bloggs'
organization_id = 'example-institute'
if options.create:
print "Creating person:", person_id
api.persons.post({
'id': person_id,
'name': 'Joe Example Bloggs',
})
print "Creating organization:", organization_id
api.organizations.post({
'id': organization_id,
'name': 'institute-of-examples',
})
print "Creating membership:", person_id, "member of", organization_id
api.memberships.post({
'person_id': person_id,
'organization_id': organization_id
})
if options.delete:
person = api.persons(person_id).get()
for m in person['result']['memberships']:
print "Deleting membership:", m['person_id'], "member of", m['organization_id']
api.memberships(m['id']).delete()
print "Deleting organization:", organization_id
api.organizations(organization_id).delete()
print "Deleting person:", person_id
api.persons(person_id).delete()
sys.exit(0)
print "Getting person data with GET:"
person_data = api.persons(person_id).get()
print "The number of memberships is:", len(person_data['result'].get('memberships', []))
print "Now updating the person with PUT:"
birth_date = "1970-04-" + str(randint(1, 30))
put_result = api.persons(person_id).put({
'id': person_id,
'name': 'Joe Example Bloggs',
'birth_date': birth_date
})
print "The number of memberships is:", len(put_result['result'].get('memberships', []))
print "Now re-getting person data with GET:"
person_data = api.persons(person_id).get()
print "The number of memberships is:", len(person_data['result'].get('memberships', []))
print "Now re-getting person data yet again with GET:"
person_data = api.persons(person_id).get()
print "The number of memberships is:", len(person_data['result'].get('memberships', []))
except Exception as e:
print "Got exception:", e
if hasattr(e, 'content'):
print "The exception body content was:", e.content
raise