From 36fcff5149c10e972116b9b64cfb5e9bc41f26ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Thu, 27 Feb 2020 15:15:12 +0100 Subject: [PATCH] Add initial structure for payment methods --- uncloud/uncloud/urls.py | 1 + .../migrations/0002_auto_20200227_1404.py | 32 +++++++++++++++++++ .../migrations/0003_auto_20200227_1414.py | 28 ++++++++++++++++ uncloud/uncloud_pay/models.py | 15 +++++++++ uncloud/uncloud_pay/serializers.py | 7 +++- uncloud/uncloud_pay/views.py | 19 +++++++++-- 6 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 uncloud/uncloud_pay/migrations/0002_auto_20200227_1404.py create mode 100644 uncloud/uncloud_pay/migrations/0003_auto_20200227_1414.py diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index 5ee9f07..40b1be5 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -33,6 +33,7 @@ router.register(r'user', payviews.UserViewSet, basename='user') router.register(r'bill', payviews.BillViewSet, basename='bill') router.register(r'order', payviews.OrderViewSet, basename='order') router.register(r'payment', payviews.PaymentViewSet, basename='payment') +router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-methods') # admin/staff urls router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill') diff --git a/uncloud/uncloud_pay/migrations/0002_auto_20200227_1404.py b/uncloud/uncloud_pay/migrations/0002_auto_20200227_1404.py new file mode 100644 index 0000000..4a6e776 --- /dev/null +++ b/uncloud/uncloud_pay/migrations/0002_auto_20200227_1404.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.3 on 2020-02-27 14:04 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('uncloud_pay', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='payment', + name='source', + field=models.CharField(choices=[('wire', 'Wire Transfer'), ('stripe', 'Stripe'), ('voucher', 'Voucher'), ('referral', 'Referral'), ('unknown', 'Unknown')], default='unknown', max_length=256), + ), + migrations.CreateModel( + name='PaymentMethod', + fields=[ + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), + ('source', models.CharField(choices=[('stripe', 'Stripe'), ('unknown', 'Unknown')], default='stripe', max_length=256)), + ('description', models.TextField()), + ('default', models.BooleanField()), + ('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/uncloud/uncloud_pay/migrations/0003_auto_20200227_1414.py b/uncloud/uncloud_pay/migrations/0003_auto_20200227_1414.py new file mode 100644 index 0000000..1e16235 --- /dev/null +++ b/uncloud/uncloud_pay/migrations/0003_auto_20200227_1414.py @@ -0,0 +1,28 @@ +# Generated by Django 3.0.3 on 2020-02-27 14:14 + +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('uncloud_pay', '0002_auto_20200227_1404'), + ] + + operations = [ + migrations.AddField( + model_name='paymentmethod', + name='primary', + field=models.BooleanField(default=True), + ), + migrations.AlterUniqueTogether( + name='paymentmethod', + unique_together={('owner', 'primary')}, + ), + migrations.RemoveField( + model_name='paymentmethod', + name='default', + ), + ] diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 6a33fd5..643361a 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -68,7 +68,22 @@ class Order(models.Model): # return amount # you get the picture +class PaymentMethod(models.Model): + uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) + owner = models.ForeignKey(get_user_model(), + on_delete=models.CASCADE, + editable=False) + source = models.CharField(max_length=256, + choices = ( + ('stripe', 'Stripe'), + ('unknown', 'Unknown'), + ), + default='stripe') + description = models.TextField() + primary = models.BooleanField(default=True) + class Meta: + unique_together = [['owner', 'primary']] class Payment(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) diff --git a/uncloud/uncloud_pay/serializers.py b/uncloud/uncloud_pay/serializers.py index 130f683..93a3031 100644 --- a/uncloud/uncloud_pay/serializers.py +++ b/uncloud/uncloud_pay/serializers.py @@ -1,6 +1,6 @@ from django.contrib.auth import get_user_model from rest_framework import serializers -from .models import Bill, Payment, Order +from .models import * class BillSerializer(serializers.ModelSerializer): class Meta: @@ -13,6 +13,11 @@ class PaymentSerializer(serializers.ModelSerializer): model = Payment fields = ['owner', 'amount', 'source', 'timestamp'] +class PaymentMethodSerializer(serializers.ModelSerializer): + class Meta: + model = PaymentMethod + fields = ['owner', 'primary', 'source', 'description'] + class OrderSerializer(serializers.ModelSerializer): class Meta: model = Order diff --git a/uncloud/uncloud_pay/views.py b/uncloud/uncloud_pay/views.py index ae88861..0b39ff3 100644 --- a/uncloud/uncloud_pay/views.py +++ b/uncloud/uncloud_pay/views.py @@ -4,8 +4,8 @@ from rest_framework import viewsets, permissions, status from rest_framework.response import Response from rest_framework.decorators import action -from .models import Bill, Payment, Order -from .serializers import BillSerializer, PaymentSerializer, UserSerializer, OrderSerializer +from .models import * +from .serializers import * from datetime import datetime ### @@ -58,6 +58,21 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): def balance(self, request): return Response(status=status.HTTP_204_NO_CONTENT) +class PaymentMethodViewSet(viewsets.ModelViewSet): + serializer_class = PaymentMethodSerializer + permission_classes = [permissions.IsAuthenticated] + + def get_queryset(self): + return PaymentMethod.objects.filter(owner=self.request.user) + + def create(self, request): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + serializer.save(owner=request.user) + + headers = self.get_success_headers(serializer.data) + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + ### # Admin views.