Skip to content

Commit

Permalink
change order of raffles
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamad Bastin committed Oct 14, 2023
1 parent be494fd commit 0f28db2
Showing 1 changed file with 66 additions and 79 deletions.
145 changes: 66 additions & 79 deletions prizetap/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,51 @@
from faucet.constraints import *
from .models import Raffle, RaffleEntry, Constraint, LineaRaffleEntries
from .serializers import (
RaffleSerializer,
RaffleSerializer,
RaffleEntrySerializer,
ConstraintSerializer,
CreateRaffleSerializer,
LineaRaffleEntrySerializer
LineaRaffleEntrySerializer,
)
from .validators import (
RaffleEnrollmentValidator,
SetRaffleEntryTxValidator,
SetClaimingPrizeTxValidator,
SetRaffleTxValidator
SetRaffleTxValidator,
)
from .constraints import *
from .constants import CONTRACT_ADDRESSES


class RaffleListView(ListAPIView):
queryset = Raffle.objects.filter(is_active=True).order_by("pk")
queryset = Raffle.objects.filter(is_active=True).order_by("-pk")
serializer_class = RaffleSerializer

def get(self, request):
queryset = self.get_queryset()
serializer = RaffleSerializer(queryset, many=True, context={
'user': request.user.profile if request.user.is_authenticated else None
})
serializer = RaffleSerializer(
queryset,
many=True,
context={
"user": request.user.profile if request.user.is_authenticated else None
},
)
return Response(serializer.data)


class RaffleEnrollmentView(CreateAPIView):
permission_classes = [IsAuthenticated]

def post(self, request, pk):
user_profile = request.user.profile
raffle = get_object_or_404(Raffle, pk=pk)

validator = RaffleEnrollmentValidator(
user_profile=user_profile,
raffle=raffle
)


validator = RaffleEnrollmentValidator(user_profile=user_profile, raffle=raffle)

validator.is_valid(self.request.data)

try:
raffle_entry = raffle.entries.get(
user_profile=user_profile
)
raffle_entry = raffle.entries.get(user_profile=user_profile)
except RaffleEntry.DoesNotExist:
raffle_entry = RaffleEntry.objects.create(
user_profile=user_profile,
Expand All @@ -69,7 +69,8 @@ def post(self, request, pk):
},
status=200,
)



class SetEnrollmentTxView(APIView):
permission_classes = [IsAuthenticated]

Expand All @@ -78,12 +79,11 @@ def post(self, request, pk):
raffle_entry = get_object_or_404(RaffleEntry, pk=pk)

validator = SetRaffleEntryTxValidator(
user_profile=user_profile,
raffle_entry=raffle_entry
user_profile=user_profile, raffle_entry=raffle_entry
)

validator.is_valid(self.request.data)

tx_hash = self.request.data.get("tx_hash", None)
raffle_entry.tx_hash = tx_hash
raffle_entry.save()
Expand All @@ -92,25 +92,26 @@ def post(self, request, pk):
{
"detail": "Raffle entry updated successfully",
"success": True,
"entry": RaffleEntrySerializer(raffle_entry).data
"entry": RaffleEntrySerializer(raffle_entry).data,
},
status=200,
)



class SetClaimingPrizeTxView(APIView):
permission_classes = [IsAuthenticated]

def post(self, request, pk):
user_profile = request.user.profile
raffle = get_object_or_404(Raffle, pk=pk)
raffle_entry = get_object_or_404(
RaffleEntry, raffle=raffle, user_profile=user_profile)
RaffleEntry, raffle=raffle, user_profile=user_profile
)

validator = SetClaimingPrizeTxValidator(
user_profile=user_profile,
raffle_entry=raffle_entry
user_profile=user_profile, raffle_entry=raffle_entry
)

validator.is_valid(self.request.data)

tx_hash = self.request.data.get("tx_hash", None)
Expand All @@ -121,23 +122,22 @@ def post(self, request, pk):
{
"detail": "Raffle entry updated successfully",
"success": True,
"entry": RaffleEntrySerializer(raffle_entry).data
"entry": RaffleEntrySerializer(raffle_entry).data,
},
status=200,
)


class GetRaffleEntryView(APIView):
def get(self, request, pk):
raffle_entry = get_object_or_404(RaffleEntry, pk=pk)

return Response(
{
"success": True,
"entry": RaffleEntrySerializer(raffle_entry).data
},
{"success": True, "entry": RaffleEntrySerializer(raffle_entry).data},
status=200,
)


class GetRaffleConstraintsView(APIView):
permission_classes = [IsAuthenticated]

Expand All @@ -161,33 +161,26 @@ def get(self, request, raffle_pk):
is_verified = False
if constraint.is_observed():
is_verified = True
response_constraints.append({
**ConstraintSerializer(c).data,
"is_verified": is_verified
})

response_constraints.append(
{**ConstraintSerializer(c).data, "is_verified": is_verified}
)

return Response(
{
"success": True,
"constraints": response_constraints
},
status=200
{"success": True, "constraints": response_constraints}, status=200
)


class CreateRaffleView(CreateAPIView):
permission_classes = [IsAuthenticated]
serializer_class = CreateRaffleSerializer

def post(self, request: Request):
serializer: CreateRaffleSerializer = self.get_serializer(data=request.data, context={
'user_profile': request.user.profile
})
serializer: CreateRaffleSerializer = self.get_serializer(
data=request.data, context={"user_profile": request.user.profile}
)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response({
'success': True,
"data": serializer.data
})
return Response({"success": True, "data": serializer.data})


class SetRaffleTXView(APIView):
Expand All @@ -197,13 +190,10 @@ def post(self, request, pk):
user_profile = request.user.profile
raffle = get_object_or_404(Raffle, pk=pk)

validator = SetRaffleTxValidator(
user_profile=user_profile,
raffle=raffle
)

validator = SetRaffleTxValidator(user_profile=user_profile, raffle=raffle)

validator.is_valid(self.request.data)

tx_hash = self.request.data.get("tx_hash", None)
raffle.tx_hash = tx_hash
raffle.save()
Expand All @@ -212,13 +202,14 @@ def post(self, request, pk):
{
"detail": "Raffle updated successfully",
"success": True,
"raffle": RaffleSerializer(raffle, context={
'user': request.user.profile
}).data
"raffle": RaffleSerializer(
raffle, context={"user": request.user.profile}
).data,
},
status=200,
)


class ValidChainsView(ListAPIView):
queryset = Chain.objects.filter(chain_id__in=list(CONTRACT_ADDRESSES.keys()))
serializer_class = SmallChainSerializer
Expand All @@ -228,25 +219,23 @@ def get(self, request):
serializer = SmallChainSerializer(queryset, many=True)
response = []
for chain in serializer.data:
response.append({
**chain,
**CONTRACT_ADDRESSES[chain['chain_id']]
})
return Response({
"success": True,
"data": response
})
response.append({**chain, **CONTRACT_ADDRESSES[chain["chain_id"]]})
return Response({"success": True, "data": response})


class UserRafflesListView(ListAPIView):
permission_classes=[IsAuthenticated]
permission_classes = [IsAuthenticated]

def get(self, request):
queryset = Raffle.objects.filter(creator_profile=request.user.profile).order_by("pk")
serializer = RaffleSerializer(queryset, many=True, context={
'user': request.user.profile
})
queryset = Raffle.objects.filter(creator_profile=request.user.profile).order_by(
"pk"
)
serializer = RaffleSerializer(
queryset, many=True, context={"user": request.user.profile}
)
return Response(serializer.data)


class ConstraintsListView(ListAPIView):
queryset = Constraint.objects.all()
serializer_class = ConstraintSerializer
Expand All @@ -257,17 +246,15 @@ class LineaRaffleView(ListAPIView):

def get_queryset(self):
return LineaRaffleEntries.objects.all()


class SetLineaTxHashView(CreateAPIView):

class SetLineaTxHashView(CreateAPIView):
def post(self, request, *args, **kwargs):
pk = self.kwargs.get('pk')
tx_hash = request.data.get('tx_hash')
pk = self.kwargs.get("pk")
tx_hash = request.data.get("tx_hash")
raffle_entry = get_object_or_404(LineaRaffleEntries, pk=pk)
raffle_entry.claim_tx = tx_hash
raffle_entry.save()
return Response({
'success': True,
'data': LineaRaffleEntrySerializer(raffle_entry).data
})
return Response(
{"success": True, "data": LineaRaffleEntrySerializer(raffle_entry).data}
)

0 comments on commit 0f28db2

Please sign in to comment.