def update(self, request, user_id):
        """
        Gift out a reward to a non-member
        """
        attrs = self.flatten_dict(request.data)
        # print attrs

        userrewardId = attrs["userreward_id"]
        try:
            userreward = UserReward.objects.get(id=userrewardId)
            if userreward.user.id != int(user_id):
                raise ValueError, "reward does not belong to this user"
        except UserReward.DoesNotExist:
            raise ValueError, "invalid userreward"

        user = userreward.user
        reward = userreward.reward

        # 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.id}
    def create(self, request, user_id):
        """
        Redeem a reward
        """
        attrs = self.flatten_dict(request.data)
        # print attrs

        userrewardId = attrs["userreward_id"]
        try:
            userreward = UserReward.objects.get(id=userrewardId)
            if userreward.user.id != int(user_id):
                raise ValueError, "reward to redeem does not belong to this user"
        except UserReward.DoesNotExist:
            raise ValueError, "invalid userreward"

        user, _, _ = self.idsValidation(user_id, None, None)
        reward = userreward.reward

        # 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 {"id": rewardActivity.id}
    def create(self, request, user_id):
        """
        Buy a reward
        """
        attrs = self.flatten_dict(request.data)
        # print attrs

        userrewardId = attrs["userreward_id"]
        try:
            userreward = UserReward.objects.get(id=userrewardId)
            if userreward.user.id == int(user_id):
                raise ValueError, "reward belongs to this user already"
        except UserReward.DoesNotExist:
            raise ValueError, "invalid userreward"

        buyer, _, _ = self.idsValidation(user_id, None, None)
        seller = userreward.user
        sellerId = seller.id

        reward = userreward.reward

        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 = buyer

        # update UserReward record
        userreward.user = buyer
        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 {"id": rewardActivity.id}
    def create(self, request, user_id):
        """
        Gift out a reward to a member
        """
        attrs = self.flatten_dict(request.data)
        # print attrs

        userrewardId = attrs["userreward_id"]
        try:
            userreward = UserReward.objects.get(id=userrewardId)
            if userreward.user.id != int(user_id):
                raise ValueError, "reward does not belong to this user"
        except UserReward.DoesNotExist:
            raise ValueError, "invalid userreward"

        user = userreward.user
        reward = userreward.reward

        user2Id = attrs["to_user"]["id"]
        user2 = self.userById(user2Id)
        if user2 is None:
            raise LookupError, "No User with this to_user_id exists: %d" % user2Id

        # 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 {"gift_code": rewardActivity.id}