diff --git a/myproject/myapp/payments.py b/myproject/myapp/payments.py index 42254cda177c05736b2821c5b13a0c6f5b9dc13c..b1108fd9e985622974b0b69dfa042f426da12ac8 100644 --- a/myproject/myapp/payments.py +++ b/myproject/myapp/payments.py @@ -7,9 +7,12 @@ from django.shortcuts import redirect, render from django.urls import reverse from .models import Action, UserTokenCount, Payment from .views import get_log_data, create_log +from .models import UserTokenCount, Action +from rest_framework.response import Response +from rest_framework import status + # Create a payment that can be made via the PayPal API -# Create a payment that can be made via the PayPal API -def create_payment(request): +def create_payment(request, purchase_type): # Configure PayPal SDK paypalrestsdk.configure({ "mode": settings.PAYPAL_MODE, @@ -17,6 +20,26 @@ def create_payment(request): "client_secret": settings.PAYPAL_CLIENT_SECRET }) + # We need to check what kind of payment it is first, how many tokens are being bought? + if purchase_type == "single": + name = "Single purchase" + sku = "sku01" + quantity = 1 + price = "9.99" + description = "Single purchase of 1 token" + + elif purchase_type == "bulk": + name = "Bulk purchase" + sku = "sku02" + quantity = 10 + price = "44.99" + description = "Bulk purchase of 10 tokens" + else: + return JsonResponse({"error": "Invalid purchase type"}) + + # Pass the quantity to the session to later change the payment execution type + request.session['purchase_quantity'] = quantity + # Create payment object payment = paypalrestsdk.Payment({ "intent": "sale", @@ -30,18 +53,18 @@ def create_payment(request): "transactions" : [{ "item_list" : { "items" : [{ - "name": "Test item", - "sku": "test item", - "price": "9.99", + "name": name, + "sku": sku, + "price": price, "currency": "GBP", "quantity": 1, }] }, "amount" : { - "total": "9.99", + "total": price, "currency": "GBP" }, - "description": "Test payment description" + "description": description }] }) @@ -111,10 +134,10 @@ def execute_payment(request): # If neither ID, error, restart if not payment_id or not payer_id: - print("No payment") - return redirect('handler404') - - # Configure API + print("no payment id or payer_id") + return Response({"error": "Error: No payment id or payer id was found."}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + # configure API paypalrestsdk.configure({ "mode": settings.PAYPAL_MODE, "client_id": settings.PAYPAL_CLIENT_ID, @@ -127,34 +150,30 @@ def execute_payment(request): # If it we do and the payer IDs match if payment.execute({"payer_id": payer_id}): print("Payment executed successfully!") + print(f"Payment: {payment}") # Allocate some tokens - user = request.user - tokens_purchased = 1 - - if request.user.is_authenticated: - add_tokens(user, tokens_purchased) - - # Save payment details to the database - Payment.objects.create( - user=user, - amount=payment.transactions[0].amount.total, - payment_id=payment_id, - payer_id=payer_id - ) - - return redirect('success') - else: - return redirect('handler404') + tokens_purchased = request.session.get("purchase_quantity") + + add_tokens(request.user, tokens_purchased) + # log_data = { + # 'action': 'Tokens purchased', + + # } + log_data = get_log_data(request.user, Action.PAYMENT_SUCCESSFUL, 'success', description=f"Purchased {tokens_purchased} tokens") + create_log(request.user if request.user.is_authenticated else None, log_data) + + + return redirect('success') else: - print("Payment execution failed") - return redirect('handler404') + print("exiting at the end of execute_payment(), incorrect payer id") + return Response({"error": "Error: Payment failed to execute, incorrect payer id"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def add_tokens(user, tokens): token_count_instance, created = UserTokenCount.objects.get_or_create(user=user) - token_count_instance.token_count += tokens + token_count_instance.token_count += int(tokens) token_count_instance.save() - + def payment_cancelled(request): return render(request, 'payment_cancelled.html') diff --git a/myproject/myapp/templates/payment_success.html b/myproject/myapp/templates/payment_success.html index 8496abe27218036de5f59812760e16c1b43ba7c9..51a411039e2529af3f8d003f8490d8531ffcc352 100644 --- a/myproject/myapp/templates/payment_success.html +++ b/myproject/myapp/templates/payment_success.html @@ -1,10 +1,14 @@ {% extends "_base.html" %}{% block content %} -<div class="grid grid-cols-1 gap-16 items-center py-8 px-4 mx-auto w-70 lg:grid"> - <div class="font-light text-gray-500 sm:text-lg dark:text-gray-400"> - <h2 class="mb-4 text-4xl tracking-tight font-extrabold text-gray-900 dark:text-white">Success!</h2> - <p class="mb-4">Your payment was successful and you have been credited with 1 token.</p> +<div class="flex mt-6"> + <div class="grid grid-cols-1 gap-12 items-center py-8 px-4 mx-auto w-70 lg:grid"> + <div class="font-light text-gray-500 sm:text-lg dark:text-gray-400 text-center"> + <h2 class="mb-4 text-4xl tracking-tight font-extrabold text-gray-900 dark:text-white">Transaction completed</h2> + <p class=" mt-6">Your payment was successful.</p> + </div> + <button class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-900 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex justify-center text-center"><a href="{% url 'users' %}">Return</a></button> </div> </div> -<button class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-900 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex justify-center text-center"><a href="{% url 'users' %}">Return</a></button> + + {% endblock content%} diff --git a/myproject/myapp/templates/pricing.html b/myproject/myapp/templates/pricing.html index 78b60c2de6bfdf0b8bc7348ba8439a66e76fbcc0..7f72582cc008f5f724d4beb40dab2c2ae18c1b11 100644 --- a/myproject/myapp/templates/pricing.html +++ b/myproject/myapp/templates/pricing.html @@ -1,7 +1,7 @@ {% extends "_base.html" %}{% block content %} <div class="grid grid-cols-1 gap-16 items-center py-8 px-4 mx-auto w-70 lg:grid"> - <div class="font-light text-gray-500 sm:text-lg dark:text-gray-400"> + <div class="font-light text-gray-500 sm:text-lg dark:text-gray-400 my-10"> <h2 class="mb-4 text-4xl tracking-tight font-extrabold text-gray-900 dark:text-white">An Intelligent System for Instrument Detection</h2> <p class="mb-4">*placeholder input* We present to you a intelligent system for instrument detection. Using audio processing techinques and a convolutionsal neural network we are able to classify instruments used in a song. other exciting words that might catch peoples attention and make them use our product. @@ -38,62 +38,35 @@ <span class="text-base font-normal leading-tight text-gray-500 ms-3">Email support</span> </li> </ul> - <button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-900 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex justify-center w-full text-center"><a href="{% url 'create_payment' %}">Purchase via PayPal</a></button> + <button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-900 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex justify-center w-full text-center"><a href="{% url 'create_payment' 'single' %}">Purchase via PayPal</a></button> </div> <div class="w-full mx-auto max-w-sm p-4 bg-white border border-gray-200 rounded-lg shadow sm:p-8 dark:bg-gray-800 dark:border-gray-700"> <h5 class="mb-4 text-xl font-medium text-gray-500 dark:text-gray-400">Bulk Purchase</h5> <div class="flex items-baseline text-gray-900 dark:text-white"> - <span class="text-3xl font-semibold">£</span> - <span class="text-5xl font-extrabold tracking-tight">49</span> - <span class="ms-1 text-xl font-normal text-gray-500 dark:text-gray-400">/month</span> + <span class="text-5xl font-extrabold tracking-tight">£44.99</span> </div> <ul role="list" class="space-y-5 my-7"> <li class="flex items-center"> <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-blue-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> </svg> - <span class="text-base font-normal leading-tight text-gray-500 dark:text-gray-400 ms-3">2 team members</span> + <span class="text-base font-normal leading-tight text-gray-500 dark:text-gray-400 ms-3">10 tokens</span> </li> - <li class="flex"> - <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-blue-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> - <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> - </svg> - <span class="text-base font-normal leading-tight text-gray-500 dark:text-gray-400 ms-3">20GB Cloud storage</span> - </li> - <li class="flex decoration-gray-500"> - <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> - <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> - </svg> - <span class="text-base font-normal leading-tight text-gray-500 ms-3">100 File Uploads</span> - <span class="ms-1 text-sm font-normal text-gray-500 dark:text-gray-400">/month</span> - </li> - <li class="flex"> - <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-blue-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> - <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> - </svg> - <span class="text-base font-normal leading-tight text-gray-500 dark:text-gray-400 ms-3">Integration help</span> - </li> - <li class="flex decoration-gray-500"> - <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> + <li class="flex decoration-gray-500"> + <svg class="flex-shrink-0 w-4 h-4 text-gray-400 dark:text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> </svg> <span class="text-base font-normal leading-tight text-gray-500 ms-3">API Access</span> </li> - <li class="flex decoration-gray-500"> - <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> - <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> - </svg> - <span class="text-base font-normal leading-tight text-gray-500 ms-3">Complete documentation</span> - </li> - <li class="flex decoration-gray-500"> - <svg class="flex-shrink-0 w-4 h-4 text-blue-700 dark:text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> + <li class="flex decoration-gray-500"> + <svg class="flex-shrink-0 w-4 h-4 text-gray-400 dark:text-gray-500" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 20 20"> <path d="M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z"/> </svg> - <span class="text-base font-normal leading-tight text-gray-500 ms-3">24×7 phone & email support</span> + <span class="text-base font-normal leading-tight text-gray-500 ms-3">Email support</span> </li> </ul> - <button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-900 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex justify-center w-full text-center">Choose plan</button> + <button type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-200 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-900 font-medium rounded-lg text-sm px-5 py-2.5 inline-flex justify-center w-full text-center"><a href="{% url 'create_payment' 'bulk' %}">Purchase via PayPal</a></button> </div> </div> diff --git a/myproject/myapp/templates/user_page.html b/myproject/myapp/templates/user_page.html index e8b40510bcbadf9262bd3a19cf2109794d77864c..987c029969c603ccae4006aaed7ba29cd5c26af4 100644 --- a/myproject/myapp/templates/user_page.html +++ b/myproject/myapp/templates/user_page.html @@ -161,7 +161,7 @@ id="first-name" class="shadow-sm bg-gray-50 border border-gray-300 text-grey-300 sm:text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="example@gmail.com" - required + required> {% if user.email %} <span>{{ user.email }}</span> {% else %} @@ -227,15 +227,13 @@ {{entry.date}} </th> <td class="px-6 py-4">{{entry.action}}</td> <td class="px-6 py-4"> - <a - href="#" - class="font-medium text-blue-600 dark:text-blue-500 hover:underline" - >{{entry.file}}</a - > - {% if entry.description %} - <button type="button" class="ml-4 px-2 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600" onclick="showModal('{{entry.description|join:'\n'}}')"> + {% if entry.file %} + <span>Filename: {{entry.file}}</span> + {% elif entry.description %} + <!-- <button type="button" class="ml-4 px-2 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600" onclick="showModal('{{entry.description|join:'\n'}}')"> Show Results - </button> + </button> --> + <span>{{ entry.description }}</span> {% endif %} </td> </tr> diff --git a/myproject/myapp/urls.py b/myproject/myapp/urls.py index 2e0f0cacafc1b0f8ab13e4b912973c3b9b721c4b..a7c38d019e636e988f655430977fb3a3eca36243 100644 --- a/myproject/myapp/urls.py +++ b/myproject/myapp/urls.py @@ -43,7 +43,7 @@ urlpatterns = [ path('register/', RegisterView.as_view(), name='register'), path('user_logout/', auth_views.LogoutView.as_view(next_page='index'), name='user_logout'), # Payment - path('payment/create/', create_payment, name='create_payment'), + path('payment/create/<str:purchase_type>', create_payment, name='create_payment'), path('payment/execute/', execute_payment, name='execute_payment'), path('payment/cancel/', payment_cancelled, name='payment_cancelled'), path('payment_success/', payment_success, name='success') diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index c7126e55bd284f3c2808221a9e9e9d429b2507ba..e65ac6c9f5be9c7e24b166b5748322c5aaa0721a 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -92,6 +92,44 @@ def submit_feedback(request): return redirect('index') +@csrf_exempt +def log_fileupload(request): + if request.method == 'POST': + data = json.loads(request.body) + status = data.get('status') + file = data.get('file') + + if request.user.is_authenticated: + log_data = get_log_data(request.user, Action.UPLOAD_FILE, status, file) + create_log(request.user, log_data) + + return JsonResponse({'message': 'Log created successfully'}, status=201) + + return JsonResponse({'error': 'Invalid request'}, status=400) + +def submit_feedback(request): + if request.method == 'POST' and request.user.is_authenticated: + prediction = request.POST.get('prediction') + liked = request.POST.get('feedback') == 'true' + file_name = request.POST.get('file_name') # Get the filename from the form data + + # Create log data using the get_log_data function + log_data = get_log_data( + user=request.user, + action=Action.FEEDBACK_SUBMITTED, + status='success', + file=file_name, # Use the filename obtained from the form + description=prediction, + feedback=liked + ) + + # Create the Log entry using the create_log function + create_log(request.user, log_data) + + return redirect('index') + + return redirect('index') + def admin_table(request): if request.user.is_authenticated: if request.user.profile.user_type != 0 or request.user.is_superuser: