2022-10-08 11:21:53 +03:00
|
|
|
from rest_framework import generics
|
|
|
|
from rest_framework.generics import get_object_or_404
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from rest_framework.parsers import FormParser, MultiPartParser
|
2022-10-09 06:27:35 +03:00
|
|
|
from rest_framework.views import APIView
|
2022-10-08 11:21:53 +03:00
|
|
|
|
2022-10-09 06:27:35 +03:00
|
|
|
from blockchain.services import transact_to_admin
|
|
|
|
from conf import settings
|
2022-10-08 11:21:53 +03:00
|
|
|
from marketplace.api.serializers import ProductSerializer
|
|
|
|
from marketplace.models import Product
|
|
|
|
from common.permissions import IsManager
|
2022-10-09 06:27:35 +03:00
|
|
|
from utils.blockchain import transfer_nft
|
2022-10-08 11:21:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ListCreateProductApi(generics.ListCreateAPIView):
|
|
|
|
serializer_class = ProductSerializer
|
|
|
|
parser_classes = [FormParser, MultiPartParser]
|
|
|
|
permission_classes = [IsAuthenticated, IsManager]
|
|
|
|
queryset = Product.objects.all()
|
|
|
|
|
|
|
|
|
2022-10-09 00:42:30 +03:00
|
|
|
class RetrieveUpdateDestroyProductApi(generics.RetrieveUpdateDestroyAPIView):
|
2022-10-08 11:21:53 +03:00
|
|
|
serializer_class = ProductSerializer
|
|
|
|
parser_classes = [FormParser, MultiPartParser]
|
|
|
|
permission_classes = [IsAuthenticated, IsManager]
|
|
|
|
queryset = Product.objects.all()
|
|
|
|
|
|
|
|
def get_object(self):
|
2022-10-08 16:02:57 +03:00
|
|
|
product = get_object_or_404(
|
2022-10-08 11:21:53 +03:00
|
|
|
Product,
|
|
|
|
slug=self.request.parser_context["kwargs"]["slug"],
|
|
|
|
)
|
2022-10-08 16:02:57 +03:00
|
|
|
return product
|
2022-10-09 06:27:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BuyProductApi(generics.DestroyAPIView):
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
product = get_object_or_404(
|
|
|
|
Product,
|
|
|
|
slug=self.request.parser_context["kwargs"]["slug"],
|
|
|
|
)
|
|
|
|
transact_to_admin(self.request.user, product.price)
|
|
|
|
transfer_nft(settings.MAIN_WALLET, self.request.user.wallet_public_key, product.token)
|
|
|
|
return product
|