Skip to content

Commit

Permalink
adding more routes, add requirements requests, adding Models (not
Browse files Browse the repository at this point in the history
working yet)
  • Loading branch information
ShiroUsagi-san committed Oct 9, 2023
1 parent fff731f commit 118d8da
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 47 deletions.
2 changes: 1 addition & 1 deletion insalan/payment/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class PaymentConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'payment'
name = 'insalan.payment'
13 changes: 8 additions & 5 deletions insalan/payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ class TransactionStatus(models.TextChoices):
SUCCEDED = "SUCCEEDED", _("Success")


class Product(models.Model):
""" Object to represent in database anything sellable"""
price = models.DecimalField(null=False, max_digits=5, decimal_places=2)
name = models.CharField(max_length=50)
desc = models.CharField(max_length=50, verbose_name=_("description"))

class Transaction(models.Model):
"""A transaction"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
payer = models.ForeignKey(User, on_delete=models.CASCADE)
amount = models.DecimalField(null=False)
products = models.ManyToManyField(Product) # A transaction can be composed of n products
payment_status = models.CharField(
max_length=10,
blank=True,
default=TransactionStatus.FAILED,
choices=TransactionStatus.choices,
null=False,
verbose_name="Transaction status",
verbose_name=_("Transaction status"),
)
date = models.DateField()

class Product(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
amount = models.DecimalField(null=False)
10 changes: 8 additions & 2 deletions insalan/payment/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from rest_framework import serializers
from .models import Transaction, TransactionStatus
from .models import Transaction, TransactionStatus, Product

class TransactionSerializer(serializers.ModelSerializer):
products = serializers.ListField(required=True)
class Meta:
model=Transaction
fields = ['payer', 'amount', 'payment_status', 'date']
fields = ['payer', 'amount', 'payment_status', 'date']

class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = "__all__"
26 changes: 0 additions & 26 deletions insalan/payment/static_urls.py

This file was deleted.

5 changes: 2 additions & 3 deletions insalan/payment/tokens.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import json
import requests
from os import getenv
from .static_urls import static_urls

class tokens :
instance=None
def __init__(self):
if tokens.instance is None:
tokens.instance = self
request = requests.post(
url=static_urls.get_tokens_url(),
url="https://google.fr",
headers={'Content-Type': "application/x-www-form-urlencoded"},
data={
'client_id': getenv("CLIENT_ID"),
Expand All @@ -33,4 +32,4 @@ def refresh(self):
},
)
self.bearer_token=json.loads(request.text)["access_token"]
self.refresh_token=json.loads(request.text)["refresh_token"]
self.refresh_token=json.loads(request.text)["refresh_token"]
6 changes: 6 additions & 0 deletions insalan/payment/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@

app_name = "payment"
urlpatterns = [
path("pay/", views.PayView.as_view(), name="pay"),
path("product/", views.ProductList.as_view(), name="list-product"),
path("product/<int:pk>/", views.ProductDetails.as_view(), name="product-details"),
path("new/", views.CreateProduct.as_view(), name="new-product"),
path("transaction/", views.TransactionList.as_view(), name="transactions"),
path("transaction/<int:pk>", views.TransactionPerId.as_view(), name="transactions/id"),
]
45 changes: 36 additions & 9 deletions insalan/payment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,46 @@
from .serializers import TransactionSerializer
from datetime import date
from .tokens import tokens
from .static_urls import static_urls
from rest_framework import generics, permissions

from django.shortcuts import render
import insalan.payment.serializers as serializers
from .models import Product, Transaction

# Create your views here.

class ProductList(generics.ListAPIView):
pagination = None
serializer_class = serializers.ProductSerializer
queryset = Product.objects.all()
permission_classes = [permissions.IsAdminUser]

def pay(request):
class ProductDetails(generics.RetrieveUpdateDestroyAPIView):
pagination = None
serializer_class= serializers.ProductSerializer
queryset = Product.objects.all()
permission_classes = [permissions.IsAdminUser]

class TransactionList(generics.ListAPIView):
pagination = None
serializer_class =serializers.TransactionSerializer
queryset = Transaction.objects.all()
permission_classes = [permissions.IsAdminUser]

class TransactionPerId(generics.RetrieveAPIView):
pagination = None
serializer_class = serializers.TransactionSerializer
queryset = Transaction.objects.all().order_by('date')
permission_classes = [permissions.IsAdminUser]


class CreateProduct(generics.CreateAPIView):
pass

class PayView(generics.CreateAPIView):
pass
"""
# lets parse the request
user_request_body = json.loads(request.body)
product_list=[]
amount=0
name=""
user=request.user
for asked_product in user_request_body:
try:
Expand Down Expand Up @@ -73,14 +100,14 @@ def pay(request):
pass
return HttpResponseRedirect(redirect_to=json.loads(checkout_init.text)['id'])
@csrf_exempt
def validate_payment(request, id):
class validate_payment(request, id):
Transaction.objects.get(id=id).payment_status=TransactionStatus.SUCCEDED
def get_transactions(request):
class get_transactions(request):
transactions=TransactionSerializer(Transaction.objects.all(), many=True)
return JsonResponse(transactions.data)

"""



1 change: 1 addition & 0 deletions insalan/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"insalan.partner",
"insalan.tournament",
"insalan.tickets",
"insalan.payment",
]

MIDDLEWARE = [
Expand Down
4 changes: 3 additions & 1 deletion insalan/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
path("v1/langate/authenticate", langate_views.LangateUserView.as_view()),
#path("v1/api-auth/", include("rest_framework.urls", namespace="rest_framework")),
path("v1/admin/", admin.site.urls),
]
path("v1/payment/", include("insalan.payment.urls")),
]


# Set admin site url correctly for the admin panel
admin.site.site_url = "/v1/"
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ gunicorn==20.1.0
django-cors-headers==3.14
tzdata==2023.3
qrcode==7.4.2
requests==2.31.0

0 comments on commit 118d8da

Please sign in to comment.