New API based on restframework, still non-functional
This commit is contained in:
parent
37897b6a12
commit
729eab6cf9
|
@ -0,0 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
|
@ -0,0 +1,8 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class Api3Config(AppConfig):
|
||||
name = 'api3'
|
|
@ -0,0 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
|
@ -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')
|
|
@ -0,0 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
|
@ -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'))
|
||||
]
|
|
@ -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
|
|
@ -2,3 +2,4 @@ django==1.10
|
|||
easy_thumbnails
|
||||
oauth
|
||||
django-auth-ldap
|
||||
django-restframework
|
||||
|
|
Loading…
Reference in New Issue