diff --git a/myproject/myapp/models.py b/myproject/myapp/models.py index 7dc56b8baaceda6431a612b5bf489da923978f3a..663f7ba01fbff5ba31935f8efe32cd1f4aec32c5 100644 --- a/myproject/myapp/models.py +++ b/myproject/myapp/models.py @@ -3,6 +3,8 @@ from django.contrib.auth import get_user_model from django.contrib.auth.models import User, Group, Permission from django.contrib.contenttypes.models import ContentType from enum import Enum +from django.dispatch import receiver +from django.db.models.signals import post_save # class UserTypes(User): # USER_TYPE_CHOICES = ( @@ -47,6 +49,24 @@ class Profile(models.Model): return f'{self.user.username} Profile' +# Model to hold the user token count +class UserTokenCount(models.Model): + # User + user = models.OneToOneField(User, on_delete=models.CASCADE) + # Token count + token_count = models.IntegerField(default=0) + + def __str__(self): + return f'{self.user.username}\'s token count: {self.token_count}' + +# Automatically create a UserTokenCount entry for each user on user creation +@receiver(post_save, sender=User) +def create_or_update_user_profile(sender, instance, created, **kwargs): + if created: + UserTokenCount.objects.create(user=instance) + # Profile.objects.create(user=instance) + # instance.profile.save() + class Action(Enum): UPLOAD_FILE = "The user has successfully uploaded a file." LOGIN = "The user has logged in to their account." diff --git a/myproject/myapp/payments.py b/myproject/myapp/payments.py index 65a36827e99d129aa7b961a9fb97e9d848aa40df..6cf511b5127dd2725d420ecc4d6cc9a32dd8adf2 100644 --- a/myproject/myapp/payments.py +++ b/myproject/myapp/payments.py @@ -5,12 +5,11 @@ from django.shortcuts import redirect, render from django.urls import reverse from django.shortcuts import redirect, render from django.urls import reverse +from .models import UserTokenCount # 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): - - # Configure PayPal SDK paypalrestsdk.configure({ "mode": settings.PAYPAL_MODE, @@ -92,13 +91,23 @@ def execute_payment(request): # increment user_tokens # commit changes + # TODO: Change something here such that the token amount added depends on a detail of the transaction, + # i.e. £9.99 payment for one token or £24.99 for + # + add_tokens(user, tokens_purchased) return redirect('success') else: #TODO: Change this to a more appropriate error message print("exiting at the end of execute_payment()") return redirect('handler404') - + + +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.save() + def payment_cancelled(request): return render(request, 'payment_cancelled.html') diff --git a/myproject/myapp/templates/user_page.html b/myproject/myapp/templates/user_page.html index 1cd0f06ce218418b051251efaa37eb531eb4d0bd..2f17994f4975e0982b7d7a1b19e7058d88641b8e 100644 --- a/myproject/myapp/templates/user_page.html +++ b/myproject/myapp/templates/user_page.html @@ -11,7 +11,7 @@ <div class="col-span-full xl:col-auto"> <div class="p-4 mb-4 bg-white border boder-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800"> <h3 class="mb-4 text-xl font-semibold dark:text-white">Tokens:</h3> - <p class="mb-4 text-xl font-semibold dark:text-white">Token Count</p> + <p class="mb-4 text-xl font-semibold dark:text-white">{{ token_count }}</p> </div> <div class="p-4 mb-4 bg-white border boder-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800"> <h3 class="mb-4 text-xl font-semibold dark:text-white">Change your password</h3> diff --git a/myproject/myapp/views.py b/myproject/myapp/views.py index 34e3e538fceba7e9c1439b0ccc81bcfa817fbc14..90742248de9a0ca26b471d74e34c2fd6cb8bd905 100644 --- a/myproject/myapp/views.py +++ b/myproject/myapp/views.py @@ -12,7 +12,7 @@ import json from datetime import datetime from .forms import InstrumentDetectionForm -from .models import Log, Action, User +from .models import Log, Action, User, UserTokenCount from django.http import JsonResponse from django.db import connection @@ -150,10 +150,12 @@ def users(request): data_user = user_table(request) admin_dict = json.loads(data_admin.content) user_dict = json.loads(data_user.content) + token_count = UserTokenCount.objects.get(user=request.user).token_count # Pass the data as a context variable to the template # !!! ADMIN DATA ONLY DISPLAYED AND GET IF USER IS ADMIN !!! context['admin_data'] = admin_dict['data'] context['user_data'] = user_dict['data'] + context['token_count'] = token_count return render(request, 'user_page.html', context)