New API based on restframework, still non-functional

This commit is contained in:
Sebastian Lohff 2019-12-03 20:50:49 +01:00
parent 37897b6a12
commit 729eab6cf9
10 changed files with 162 additions and 0 deletions

0
k4ever/api3/__init__.py Normal file
View File

6
k4ever/api3/admin.py Normal file
View File

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
# Register your models here.

8
k4ever/api3/apps.py Normal file
View File

@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.apps import AppConfig
class Api3Config(AppConfig):
name = 'api3'

View File

6
k4ever/api3/models.py Normal file
View File

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.

View File

@ -0,0 +1,60 @@
from django.contrib.auth.models import User
from rest_framework import serializers
from buyable.models import BuyableType, Buyable, Order, Purchase
from transaction.models import TransactionType, Transaction, VirtualTransaction
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('id', 'username',)
class BuyableTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = BuyableType
fields = ['url', 'id', 'name']
class BuyableSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Buyable
fields = ['url', 'id', 'name', 'description', 'price', 'barcode', 'deposit', 'image', 'buyableType']
class PurchaseSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Purchase
fields = ['id', 'price', 'isDeposit', 'buyable']
class OrderSerializer(serializers.HyperlinkedModelSerializer):
purchases = PurchaseSerializer(source='purchase_set', many=True)
class Meta:
model = Order
fields = ['url', 'id', 'price', 'dateTime', 'purchases']
class TransactionTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = TransactionType
fields = ('url', 'id', 'name')
class TransactionSerializer(serializers.HyperlinkedModelSerializer):
transactionType = TransactionTypeSerializer()
class Meta:
model = Transaction
fields = ('url', 'id', 'amount', 'dateTime', 'checked', 'transactionType')
class VirtualTransactionSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer()
recipient = UserSerializer()
class Meta:
model = VirtualTransaction
fields = ('url', 'id', 'amount', 'dateTime', 'comment', 'user', 'recipient')

6
k4ever/api3/tests.py Normal file
View File

@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
# Create your tests here.

20
k4ever/api3/urls.py Normal file
View File

@ -0,0 +1,20 @@
from django.conf.urls import include, url
from rest_framework import routers
from .views import BuyableTypeViewSet, BuyableViewSet, OrderViewSet
from .views import TransactionTypeViewSet, TransactionViewSet, VirtualTransactionViewSet
router = routers.DefaultRouter()
router.register('buyabletypes', BuyableTypeViewSet)
router.register('buyables', BuyableViewSet)
router.register('orders', OrderViewSet, basename='order')
router.register('transactiontypes', TransactionTypeViewSet)
router.register('transactions', TransactionViewSet, 'transaction')
router.register('virtualtransactions', VirtualTransactionViewSet, 'virtualtransaction')
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

55
k4ever/api3/views.py Normal file
View File

@ -0,0 +1,55 @@
from django.db.models import Q
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from .serializers import BuyableTypeSerializer, BuyableSerializer, OrderSerializer
from .serializers import TransactionTypeSerializer, TransactionSerializer, VirtualTransactionSerializer
from buyable.models import BuyableType, Buyable, Order
from transaction.models import TransactionType, Transaction, VirtualTransaction
class BuyableTypeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = BuyableType.objects.all()
serializer_class = BuyableTypeSerializer
class BuyableViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Buyable.objects.all()
serializer_class = BuyableSerializer
class OrderViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAuthenticated]
serializer_class = OrderSerializer
def get_queryset(self):
return Order.objects.filter(user=self.request.user)
class TransactionTypeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = TransactionType.objects.all()
serializer_class = TransactionTypeSerializer
class TransactionViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAuthenticated]
serializer_class = TransactionSerializer
def get_queryset(self):
return Transaction.objects.filter(user=self.request.user).order_by("-dateTime")
class VirtualTransactionViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAuthenticated]
serializer_class = VirtualTransactionSerializer
def get_queryset(self):
return (VirtualTransaction.objects.filter(Q(user=self.request.user) | Q(recipient=self.request.user))
.order_by("-dateTime"))
# class BalanceView(APIView):
# def get(self, request):
# return Response({'balance': request.user.userprofile.balance}
class BalanceViewSet(viewsets.ReadOnlyModelViewSet):
pass

View File

@ -2,3 +2,4 @@ django==1.10
easy_thumbnails
oauth
django-auth-ldap
django-restframework