def coupon_create(discount_key_str, user_id): """ It creates a coupon, letting a user to take advantage of a discount. Parameters: - discount_key_str: the urlsafe key of the discount the user is interested in - user_id: the id of the user who is buying the coupon It returns a tuple: - the created coupon (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: res = Discount.add_coupon( Discount.make_key(None, discount_key_str), user_id) except TypeError, e: return None, str(e), 400
def discount_publish(discount_key_str, requester_id): """ It make a discount public. Parameters: - discount_key_str: a urlsafe key for identifying the discount - requester_id: the id of the user makeing the request It returns a tuple: - the discount with updated information (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: discount = Discount.publish( Discount.make_key(None, discount_key_str), requester_id) except (TypeError, InvalidKeyException), e: return None, str(e), 400
def discount_get(discount_key_str, requester_id): """ It gets the Discount identified by the input key. Parameters: - discount_key_str: the urlsafe key of the Discount to retrieve - requester_id: id of the user which is making the request. Only the place owner can access the past discounts and the ones that have not been published yet. It returns a tuple: - the requested discount (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: res = Discount.get_by_key( Discount.make_key(None, discount_key_str), requester_id) except TypeError, e: return None, str(e), 400
def discount_delete(discount_key_str, requester_id): """ It deletes a discount, removing it from the datastore. Only discounts that have not been published can be deleted. Only the owner of the place the discount refers to can delete a discount. Parameters: - discount_key_str: the urlsafe key of the discount to delete - requester_id: the id of the user that is making the request It returns a tuple: - the deleted discount (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: res = Discount.delete( Discount.make_key(None, discount_key_str), requester_id) except TypeError, e: return None, str(e), 400
def coupon_get_by_code(discount_key_str, requester_id, code): """ It retrieves a coupon. The user must have permission to see it. Parameters: - discount_key_str: the urlsafe key of the discount the coupon belongs to - requester_id: the id of the user who is making the request - code: the code identifying the coupon to be marked as deleted It returns a tuple: - the requested coupon (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: res = Discount.get_coupon( Discount.make_key(None, discount_key_str), requester_id, code) except (TypeError, ValueError, InvalidKeyException) as e: return None, str(e), 400 except UnauthorizedException, e: return None, str(e), 403
def coupon_use(discount_key_str, requester_id, code): """ It marks a coupon as used, so it cannot be used again. Only place owners can mark coupons as used. Parameters: - discount_key_str: the urlsafe key of the discount the coupon refers to - requester_id: the id of the user who is making the request - code: the code identifying the coupon to be marked as used It returns a tuple: - the coupon with updated information (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: res = Discount.use_coupon( Discount.make_key(None, discount_key_str), requester_id, code) except (TypeError, ValueError, InvalidKeyException, InvalidCouponException) as e: return None, str(e), 400 except UnauthorizedException, e: return None, str(e), 403
def discount_update(discount, discount_key_str, requester_id): """ It updates an existing Discount. Parameters: - discount: the Discount containing the new information to store. - discount_key_str: the urlsafe key of the Discount to update - requester_id: the string id of the user that is making the request It returns a tuple: - the updated discount (or None in case of errors in the input), - the status (a string indicating whether an error occurred), - the http code indicating the type of error, if any """ try: res = Discount.store( discount, Discount.make_key(None, discount_key_str), requester_id) except (TypeError, ValueError, InvalidKeyException) as e: return None, str(e), 400 except UnauthorizedException, e: return None, str(e), 403