Exemplo n.º 1
0
    def put(self, request, key, pk):
        """Create a new Purchase and return it in a seriailzed representation.

        URL Args:
            key (str): Collective key
            pk (int) Purchase ID

        POST Args:
            name (str)
            buyer (int): User id
            price (float)

        Returns:
            Serialized Purchase

        """
        collective = collective_from_key(key)

        buyer = User.objects.get(id=request.data["buyer"])
        if not collective.is_member(buyer):
            raise ValidationError(USER_MUST_BE_MEMBER)

        price_value = float(request.data["price"])
        if price_value <= 0:
            raise ValidationError(CANNOT_ADD_ZERO_MONEY)
        price = Money(price_value, EUR)

        purchase = Purchase.objects.get(pk=pk)
        purchase.name = request.data["name"]
        purchase.price = price
        purchase.buyer = buyer
        purchase.save()

        serialized_purchase = PurchaseSerializer(purchase).data
        return Response(serialized_purchase)
Exemplo n.º 2
0
def create_purchase(request, key):
    """Create a new Purchase and return it in a seriailzed representation.

    URL Args:
        key (str): Collective key

    POST Args:
        name (str)
        buyer (int): User id
        price (float)

    Returns:
        Serialized Purchase

    """
    name = request.data["name"]
    buyer_id = request.data["buyer"]

    collective = collective_from_key(key)

    buyer = User.objects.get(id=buyer_id)
    if not collective.is_member(buyer):
        raise ValidationError(USER_MUST_BE_MEMBER)

    # FIXME: Either don't allow something else than euro or handle here.
    price_value = float(request.data["price"])
    if price_value <= 0:
        raise ValidationError(CANNOT_ADD_ZERO_MONEY)
    price = Money(price_value, EUR)

    purchase = Purchase.objects.create(
        name=name,
        price=price,
        collective=collective,
        buyer=buyer,
    )
    serialized_purchase = PurchaseSerializer(purchase).data
    return Response(serialized_purchase)