def get(self): geopoints = Appliance().get_geopoints() # return JSON response params = { 'geopoints': geopoints } self.response.headers['Content-Type'] = "application/json" return self.render_template('api/geopoints.json', **params)
def get(self): appliances = Appliance().appliances_with_instances_on_sale() # add gravatar URLs for appliance in appliances: email = appliance.owner.get().email gravatar_hash = md5.new(email.lower().strip()).hexdigest() appliance.gravatar_url = "https://www.gravatar.com/avatar/%s" % gravatar_hash # return JSON response params = { 'appliances': appliances, 'message': "This is a list of all active appliances with instances for sale." } self.response.headers['Content-Type'] = "application/json" return self.render_template('api/appliances.json', **params)
def post(self): # lookup user's auth info user_info = User.get_by_id(long(self.user_id)) # initialize form choices for group self.form.group.choices = [] # add list of user's groups, if any groups = GroupMembers.get_user_groups(user_info.key) for group in groups: self.form.group.choices.insert(0, (str(group.key.id()), group.name)) # public group self.form.group.choices.insert(0, ('public', "Public")) # check if we are getting a custom group entry if self.form.group.data == "custom": # check if the group exists if Group.get_by_name(self.form.custom.data.strip()): self.add_message("A group with that name already exists!", "error") return self.redirect_to('account-appliances') # make the new group group = Group(name=self.form.custom.data.strip(), owner=user_info.key) group.put() group_key = group.key # create the group member entry groupmember = GroupMembers( group=group_key, member=user_info.key, invitor=user_info.key, # same same active=True) groupmember.put() # hack the form with new group self.form.group.choices.insert(0, ('custom', "Custom")) else: # grab an existing group if self.form.group.data.strip() == 'public': # no group for public appliances group_key = None else: # check membership group = Group.get_by_id(int(self.form.group.data.strip())) if GroupMembers.is_member(user_info.key, group.key): group_key = group.key else: group_key = None # check what was returned from the rest of the form validates if not self.form.validate(): self.add_message("The new appliance form did not validate.", "error") return self.get() # load form values name = self.form.name.data.strip() token = self.form.token.data.strip() # check if we have it already - all that work bitches? if Appliance.get_by_token(token): self.add_message("An appliance with that token already exists!", "error") return self.redirect_to('account-appliances') # save the new appliance in our database appliance = Appliance(name=name, token=token, group=group_key, owner=user_info.key) appliance.put() # log to alert self.add_message("Appliance %s successfully created!" % name, "success") # give it a few seconds to update db, then redirect time.sleep(1) return self.redirect_to('account-appliances')