Add initial structure for payment methods

This commit is contained in:
fnux 2020-02-27 15:15:12 +01:00
parent 70a4fe4d90
commit 36fcff5149
6 changed files with 99 additions and 3 deletions

View file

@ -33,6 +33,7 @@ router.register(r'user', payviews.UserViewSet, basename='user')
router.register(r'bill', payviews.BillViewSet, basename='bill') router.register(r'bill', payviews.BillViewSet, basename='bill')
router.register(r'order', payviews.OrderViewSet, basename='order') router.register(r'order', payviews.OrderViewSet, basename='order')
router.register(r'payment', payviews.PaymentViewSet, basename='payment') router.register(r'payment', payviews.PaymentViewSet, basename='payment')
router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-methods')
# admin/staff urls # admin/staff urls
router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill') router.register(r'admin/bill', payviews.AdminBillViewSet, basename='admin/bill')

View file

@ -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)),
],
),
]

View file

@ -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',
),
]

View file

@ -68,7 +68,22 @@ class Order(models.Model):
# return amount # you get the picture # 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): class Payment(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

View file

@ -1,6 +1,6 @@
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from rest_framework import serializers from rest_framework import serializers
from .models import Bill, Payment, Order from .models import *
class BillSerializer(serializers.ModelSerializer): class BillSerializer(serializers.ModelSerializer):
class Meta: class Meta:
@ -13,6 +13,11 @@ class PaymentSerializer(serializers.ModelSerializer):
model = Payment model = Payment
fields = ['owner', 'amount', 'source', 'timestamp'] fields = ['owner', 'amount', 'source', 'timestamp']
class PaymentMethodSerializer(serializers.ModelSerializer):
class Meta:
model = PaymentMethod
fields = ['owner', 'primary', 'source', 'description']
class OrderSerializer(serializers.ModelSerializer): class OrderSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Order model = Order

View file

@ -4,8 +4,8 @@ from rest_framework import viewsets, permissions, status
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.decorators import action from rest_framework.decorators import action
from .models import Bill, Payment, Order from .models import *
from .serializers import BillSerializer, PaymentSerializer, UserSerializer, OrderSerializer from .serializers import *
from datetime import datetime from datetime import datetime
### ###
@ -58,6 +58,21 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet):
def balance(self, request): def balance(self, request):
return Response(status=status.HTTP_204_NO_CONTENT) 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. # Admin views.