Package passtools :: Module pt_template
[hide private]
[frames] | no frames]

Source Code for Module passtools.pt_template

  1  ########################################## 
  2  # pt_template.py 
  3  # 
  4  # Models PassTools Template 
  5  # 
  6  # Copyright 2013, Urban Airship, Inc. 
  7  ########################################## 
  8   
  9  """ 
 10  Define and provide methods for manipulating PassTools Template objects. 
 11   
 12  """ 
 13  try: 
 14      import simplejson as json 
 15  except ImportError: 
 16      import json 
 17   
 18  import pt_client 
19 20 -class Template(object):
21
22 - def __init__(self, template_id = None):
23 """ 24 Init, optionally populate, new pt_template.Template instance 25 If template_id is supplied, will retrieve complete instance, 26 otherwise just create new empty instance. 27 28 API call used is v1/template (GET) 29 30 @type template_id: int 31 @param template_id: ID of the desired template [Optional] 32 @return: None 33 """ 34 self.header = {} 35 self.fields_model = {} 36 if template_id: 37 new_template = self.get(template_id) 38 if new_template: 39 self.header = new_template.header 40 self.fields_model = new_template.fields_model
41 42 @property
43 - def id(self):
44 the_id = None 45 if self.header and "id" in self.header: 46 the_id = int(self.header["id"]) 47 return the_id
48
49 - def __str__(self):
50 pretty_header_fields = json.dumps(self.header, sort_keys = True, indent = 2, encoding = "ISO-8859-1") 51 pretty_template_fields = json.dumps(self.fields_model, sort_keys = True, indent = 2, encoding = "ISO-8859-1") 52 return "header:%s\nfields_model:%s" % (pretty_header_fields, pretty_template_fields)
53 54 @classmethod
55 - def get(cls, template_id):
56 """ 57 Retrieve full-form template specified by template_id 58 59 API call used is v1/template/<template_id> (GET) 60 61 @type template_id: int 62 @param template_id: ID of the template to retrieve 63 @return: json form of template full-form description 64 """ 65 request_url = "/template/%s" % (int(template_id)) 66 return pt_client.pt_get(request_url)
67 68 @classmethod
69 - def list(cls, **kwargs):
70 """ 71 Retrieve list of existing templates created by owner of API-key 72 Optional parameters are translated into query-modifiers 73 74 Note that list() returns header-only form of templates. Use get() to retrieve full template. 75 76 API call used is v1/template/headers (GET) 77 78 @type pageSize: int 79 @param pageSize: Maximum length of list to return [Optional; Default = 10] 80 @type page: int 81 @param page: 1-based index of page into list, based on page_size [Optional; Default = 1] 82 @type order: string 83 @param order: Name of field on which to sort list [Optional; From (ID, Name, Created, Updated)] 84 @type direction: string 85 @param direction: Direction which to sort list [Optional; From (ASC, DESC); Default = DESC] 86 @return: json form of list of template header descriptions 87 """ 88 request_dict = kwargs 89 request_url = "/template/headers" 90 return pt_client.pt_get(request_url, request_dict)
91 92 @classmethod
93 - def delete(cls, template_id):
94 """ 95 delete existing template 96 97 API call used is v1/template/<template_id> (DELETE) 98 99 @type template_id: int 100 @param template_id: ID of the template to delete 101 @return: json form of response data 102 """ 103 104 request_url = "/template/%d" % int(template_id) 105 return pt_client.pt_delete(request_url, {})
106