def create(self, request, user_id): """ Redeem a reward """ attrs = self.flatten_dict(request.data) #print attrs user, reward, _ = self.idsValidation(user_id, attrs['reward']['id'], None) urHandler = UserRewardHandler() exists, userreward = urHandler.existsAndActive(user, reward) if not exists: raise ValueError, "reward doesn't belong to user, or reward is invalid/expired" #insert reward activity rewardActivity = RewardActivity() rewardActivity.time = datetime.now() rewardActivity.reward = reward rewardActivity.activity_type = 1 rewardActivity.description = attrs.get('description') rewardActivity.points_value = reward.equiv_points rewardActivity.from_user = user rewardActivity.to_user = None #commit to DB rewardActivity.save() userreward.delete() return rc.CREATED
def update(self, request, user_id): """ Gift out a reward to a non-member """ attrs = self.flatten_dict(request.data) #print attrs user, reward, _ = self.idsValidation(user_id, attrs['reward']['id'], None) urHandler = UserRewardHandler() exists, _ = urHandler.existsAndActive(user, reward) if not exists: raise ValueError, "reward doesn't belong to user, or reward is invalid/expired" #insert reward activity rewardActivity = RewardActivity() rewardActivity.time = datetime.now() rewardActivity.reward = reward rewardActivity.activity_type = 3 rewardActivity.description = attrs['description'] rewardActivity.points_value = reward.equiv_points rewardActivity.from_user = user rewardActivity.to_user = None #commit to DB rewardActivity.save() return {"gift_code":rewardActivity.pk}
def create(self, request, user_id): """ Buy a reward """ attrs = self.flatten_dict(request.data) #print attrs user, reward, _ = self.idsValidation(user_id, attrs['reward']['id'], None) sellerId = attrs['from_user']['id'] seller = self.userById(sellerId) if seller is None: raise LookupError, 'No Seller with this id exists: '+sellerId urHandler = UserRewardHandler() exists, userreward = urHandler.existsAndActive(seller, reward) if not exists: raise ValueError, "reward doesn't belong to user, or reward is invalid/expired" try: buyerPoint = UserPoint.objects.get(user__id=user_id) if buyerPoint.points < reward.equiv_points: return ('Buyer does not have enough points', 500) except UserPoint.DoesNotExist: return ('Buyer does not have enough points', 500) #insert reward activity rewardActivity = RewardActivity() rewardActivity.time = datetime.now() rewardActivity.reward = reward rewardActivity.activity_type = 2 rewardActivity.description = attrs.get('description') rewardActivity.points_value = reward.equiv_points rewardActivity.from_user = seller rewardActivity.to_user = user #update UserReward record userreward.user = user userreward.forsale = False #update UserPoint records buyerPoint.points -= reward.equiv_points try: sellerPoint = UserPoint.objects.get(user__id=sellerId) sellerPoint.points += reward.equiv_points except UserPoint.DoesNotExist: sellerPoint = UserPoint() sellerPoint.user = seller sellerPoint.points = reward.equiv_points #commit to DB rewardActivity.save() userreward.save() sellerPoint.save() buyerPoint.save() return rc.CREATED
def create(self, request, user_id): """ Gift out a reward """ attrs = self.flatten_dict(request.data) #print attrs user, reward, _ = self.idsValidation(user_id, attrs['reward']['id'], None) user2Id = attrs['to_user']['id'] user2 = self.userById(user2Id) if user2 is None: raise LookupError, 'No User with this to_user_id exists: '+user2Id urHandler = UserRewardHandler() exists, userreward = urHandler.existsAndActive(user, reward) if not exists: raise ValueError, "reward doesn't belong to user, or reward is invalid/expired" #insert reward activity rewardActivity = RewardActivity() rewardActivity.time = datetime.now() rewardActivity.reward = reward rewardActivity.activity_type = 3 rewardActivity.description = attrs['description'] rewardActivity.points_value = reward.equiv_points rewardActivity.from_user = user rewardActivity.to_user = user2 #update UserReward record userreward.user = user2 userreward.forsale = False #commit to DB rewardActivity.save() userreward.save() return rc.CREATED