Last active
September 18, 2017 17:23
-
-
Save inhumantsar/7e45b2ee19bc8703af484d8b66a9a204 to your computer and use it in GitHub Desktop.
nationbuilder python exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!pip install rauth | |
from rauth import OAuth2Service | |
import json | |
# init work | |
NATION_SLUG = 'somenation' | |
SITE_SLUG = 'somesite' | |
OAUTH_ID = 'someid' | |
OAUTH_SECRET = 'somesecret' | |
REDIRECT_URI = 'http://somecallback.url' | |
access_token_url = "http://" + NATION_SLUG + ".nationbuilder.com/oauth/token" | |
authorize_url = NATION_SLUG + ".nationbuilder.com/oauth/authorize" | |
service = OAuth2Service( | |
client_id = OAUTH_ID, | |
client_secret = OAUTH_SECRET, | |
name = "any name", | |
authorize_url = authorize_url, | |
access_token_url = access_token_url, | |
base_url = NATION_SLUG + ".nationbuilder.com") | |
session = service.get_session(service.get_access_token( | |
decoder=json.loads, | |
data = { | |
"code": code, | |
"redirect_uri": REDIRECT_URI, | |
"grant_type": "authorization_code" | |
})) | |
# create a person | |
create_person = { | |
"person": { | |
"last_name": "Snow", | |
"first_name": "Jane", | |
"email": "[email protected]", | |
"sex": "F", | |
"signup_type": 0, | |
"employer": "Free Folk Inc", | |
"party": "C", | |
"registered_address": { | |
"state": "BC", | |
"country_code": "CA" | |
} | |
} | |
} | |
response = session.post( | |
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/people", | |
params={'format': 'json', 'person': create_person}, | |
headers={'content-type': 'application/json'} | |
) | |
# update a person | |
update_person = { | |
"person": { | |
"employer": "Knee Benders Co-op" | |
} | |
} | |
response = session.post( | |
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/people/" + response['person']['id'], | |
params={'format': 'json', 'person': update_person}, | |
headers={'content-type': 'application/json'} | |
) | |
# delete a person | |
response = session.delete( | |
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/people/" + response['person']['id'], | |
params={'format': 'json'}, | |
headers={'content-type': 'application/json'} | |
) | |
# create an event | |
create_event = { | |
"status": "unlisted", | |
"name": "Election Day", | |
"intro": "Get out the vote!", | |
"time_zone": "Pacific Time (US & Canada)", | |
"start_time": "2018-11-08T07:00:00-00:00", | |
"end_time": "2018-11-08T19:00:00-00:00", | |
"contact": { | |
"name": "John Doe", | |
"contact_phone": "6046646464", | |
"show_phone": true, | |
}, | |
"rsvp_form": { | |
"phone": "required", | |
"address": "optional", | |
"allow_guests": true, | |
"accept_rsvps": true, | |
"gather_volunteers": false | |
}, | |
"show_guests": true, | |
"capacity": 10000} | |
response = session.post( | |
"https://" + NATION_SLUG + ".nationbuilder.com/api/v1/sites/" + SITE_SLUG + "/events", | |
params={'format': 'json', 'event': create_event}, | |
headers={'content-type': 'application/json'} | |
) | |
# HTML form for event update | |
<html> | |
<!-- ... --> | |
<body> | |
<!-- ... --> | |
<form action="https://NATION_SLUG.nationbuilder.com/api/v1/sites/SITE_SLUG/pages/events/EVENT_ID" method="POST"> | |
<label for="event_title">Title: <input type="text" name="event_title"></label><br /> | |
<label for="event_start">Start: <input type="text" name="event_start" placeholder="2013-02-07T18:00:00-00:00"></label><br /> | |
<label for="event_end">End: <input type="text" name="event_end" placeholder="2013-02-07T20:00:00-00:00"></label><br /> | |
<label for="event_venue_name">Venue Name: <input type="text" name="event_venue_name"></label><br /> | |
<label for="event_venue_address">Venue Address: <input type="text" name="event_venue_address"></label><br /> | |
<button type="submit">Update</button> | |
</form> | |
<!-- ... --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment