def NewGiftJson(request): data = request.GET giftInfo = {} apikey = data.get("apikey", None) product_id = data.get("product", None) receiver_id = data.get("receiver", None) receiver_email = data.get("email", None) if apikey and product_id and (receiver_id or receiver_email): sender = User.select(apikey=apikey) if sender: if receiver_id: recipient = User.select(id=receiver_id) else: try: recipient = User.select(email=receiver_email) except User.DoesNotExist: # create dummy user for non-registered user here giftInfo = {"error": "Recipient is not a member yet"} if recipient and sender != recipient: try: product = Product.objects.get(id=product_id) gift = Gift(sender=sender, receiver=recipient, product=product) gift.save() giftInfo = gift.to_dict() except: giftInfo = {"error": "Invalid product"} else: giftInfo = {"error": "Invalid receiver"} else: giftInfo = {"error": "Invalid apikey"} return HttpResponse(json.dumps(giftInfo, cls=DjangoJSONEncoder))
def UserLoginJson(request): data = request.GET email = data.get("email", None) fbemail = data.get("fbemail", None) password = data.get("password", None) response = {"error": "Invalid username/password"} if password and (email or fbemail): if email: user = User.select(email=email) else: user = User.select(fbemail=fbemail) if user and user.user.check_password(password): response = user.to_dict() return HttpResponse(json.dumps(response, cls=DjangoJSONEncoder))
def RedeemGiftJson(request): data = request.GET redemptionInfo = {} apikey = data.get("apikey", None) giftid = data.get("gift", None) if apikey and giftid: recipient = User.select(apikey=apikey) if recipient: gift = Gift.objects.get(id=giftid) if gift and gift.receiver == recipient and gift.status != Gift.GIFT_STATUS_REDEEMED: card, expires = gift.Redeem() if card: redemptionInfo={ "redemptionInfo": card.ProxyCard().RedemptionInfo(), "expires": expires, "product": gift.product.to_dict(fields=["id", "name", "icon", "vendor"]), "sender": gift.sender.to_dict(fields=["first", "last", "id"]), } else: redemptionInfo = { "error": "A payment barcode could not be generated at this time, please try again later" } elif gift.status == Gift.GIFT_STATUS_REDEEMED: redemptionInfo = {"error": "You have already redeemed this gift"} else: redemptionInfo = {"error": "Invalid gift"} else: redemptionInfo = {"error": "Invalid apikey"} return HttpResponse(json.dumps(redemptionInfo, cls=DjangoJSONEncoder))
def setUpClass(cls): cls.user1Data = { "email": "*****@*****.**", "first": "John", "last": "Smith", "password": "******", "fbemail": "*****@*****.**", } cls.user2Data = { "email": "*****@*****.**", "first": "Mark", "last": "Zuckerburg", "password": "******", "bday": "1984-05-14", } cls.user1 = User.create(cls.user1Data) cls.user2 = User.create(cls.user2Data)
def ListUserGiftJson(request): data = request.GET apikey = data.get("apikey", None) giftInfo = {} if apikey: user = User.select(apikey=apikey) if user: giftInfo["received"] = [] giftInfo["sent"] = [] for gift in user.received.all(): giftInfo["received"].append(gift.to_dict(fields=["id", "product"])) for gift in user.sent.all(): giftInfo["sent"].append(gift.to_dict(fields=["id", "product"])) else: giftInfo = {"error": "Bad apikey"} return HttpResponse(json.dumps(giftInfo, cls=DjangoJSONEncoder))
def NewUserJson(request): data = request.GET userInfo = {} userData = { "email": data.get("email", None), "first": data.get("first", None), "last": data.get("last", None), "password": data.get("password", None), "fbemail": data.get("fbemail", None), "bday": data.get("bday", None), } try: newUser = User.create(userData) userInfo = newUser.to_dict() except Exception, err: userInfo = {"error": str(err)}
def test_model_user_unique_fbemail(self): self.testUserData["fbemail"] = "*****@*****.**" with self.assertRaises(Exception): testUser = User.create(self.testUserData)
def test_model_user_unique_email(self): self.testUserData["email"] = "*****@*****.**" with self.assertRaises(Exception): testUser = User.create(self.testUserData)
def test_model_user_password_required(self): del self.testUserData["password"] with self.assertRaises(Exception): testUser = User.create(self.testUserData)
def test_model_user_first_required(self): del self.testUserData["first"] with self.assertRaises(Exception): testUser = User.create(self.testUserData)