예제 #1
0
# You'll always configure the api, providing your api key.
# This is required!
PassTools.configure(api_key = my_api_key)

# Let's create a new pass from a template.
# Start by retrieving a template using the same method used in Ex1_Templates.py
# The first two steps are a bit contrived, since you would know the ID of the template
# you wanted to use, but for this example we'll get the whole list and use the latest
list_response = Template.list()
template_header_list = list_response["templateHeaders"]
the_template_id = template_header_list[0]['id']

get_response = Template.get(the_template_id)

# Now create a new pass from the template.
test_pass = Pass.create(the_template_id, get_response['fieldsModel'])

print 25*"#"
print "New Pass at start"
print test_pass
print 25*"#"

# Add a location to that pass. Locations are passed as a list of dicts, length 1 or more
# A pass can have a maximum of 10 locations
print 25*"#"
print "Adding locations to new pass..."
location_list_1=[{"latitude":37.4471107, "longitude":-122.16206219999998,
                    "streetAddress1":"408 Florence St", "streetAddress2":"",
                    "city":"Palo Alto", "region":"CA", "regionCode":"94301",
                    "country":"US", "relevantText":"Palo Alto Office!"}]
add_response = Pass.add_locations(test_pass['id'], location_list_1)
# STEP 2:
# You'll always configure the api, providing your api key.
# This is required!
PassTools.configure(api_key = my_api_key)

# Our model DB...
user_db = [{"first_name": "James", "last_name":"Bond"},
           {"first_name": "Jimi", "last_name":"Hendrix"},
           {"first_name": "Johnny", "last_name":"Appleseed"}]

# You'll have selected the template you want to use...you can find the template ID in the Template Builder UI
selected_template_id = 604

# Retrieve your template, so you can modify the data and create passes from it
get_response = Template.get(selected_template_id)

the_fields_model = get_response["fieldsModel"]

# Now for each user in your DB, grab the user data, modify the template.fields_model, create a pass and download it:
for user_record in user_db:
    the_fields_model["fname"]["value"] = user_record["first_name"]
    the_fields_model["lname"]["value"] = user_record["last_name"]

    create_response = Pass.create(selected_template_id, the_fields_model)

    new_pass_id = create_response["id"]
    print "NEW PASS CREATED. ID: %s, First: %s, Last: %s" % (new_pass_id, user_record["first_name"], user_record["last_name"])
    Pass.download(new_pass_id, "/tmp/%s_%s.pkpass" % (user_record["first_name"], user_record["last_name"]))

# Now distribute the passes to your users!
예제 #3
0
#    the_template_fields_model["primary1"]["value"] = "10% Off!"
# And in this one, we set 'user_first_name' as a custom key name when we created the template
#    the_template_fields_model["user_first_name"]["value"] = "John"

# Keep in mind:
#   - If you marked a field as "Required", you'll have to give it a value here, unless you gave it a default
#   - If you marked a field as 'Don't show if empty', then if you don't give it a value here, it won't show on the pass

# Now create a new pass from the template.
# Of course, we haven't changed any fields (since we don't know what's in your template!),
# so this pass will look like the template, but by changing the fields as described above,
# you'll be able to generate one form for many customers, or a unique pass for each customer, or anything in between.

print 25*"#"
print "Creating new Pass from template ID %s" % the_template_id
create_response = Pass.create(the_template_id, the_template_fields_model)
print create_response
print 25*"#"

# There are a few ways to deliver passes to customers (and more to come), several of which involve you distributing
# the pass file itself...so you'll want to download passes you create.
# Let's do that, using the 'download' method of a pass.
# IMPORTANT: you'll want to be sure to give your passes the '.pkpass' extension, or they will not be properly recognized
# when your customer receives them.
print 25*"#"
new_pass_id = create_response['id']
print "Downloading an id-specified Pass from the service..."
Pass.download(new_pass_id, "/tmp/New_Pass.pkpass")
print 25*"#"

# Next, we'll update an existing pass, using--surprise!--the 'update' method. In this case, we use the fields