Move user view to uncloud_auth

This commit is contained in:
Nico Schottelius 2020-03-18 14:36:40 +01:00
parent 2f1aee8181
commit cd01f62fde
6 changed files with 35 additions and 24 deletions

View file

@ -22,6 +22,7 @@ from uncloud_vm import views as vmviews
from uncloud_pay import views as payviews
from ungleich_service import views as serviceviews
from opennebula import views as oneviews
from uncloud_auth import views as authviews
router = routers.DefaultRouter()
@ -56,7 +57,6 @@ router.register(r'service/matrix', serviceviews.MatrixServiceProductViewSet, bas
# Pay
router.register(r'user', payviews.UserViewSet, basename='user')
router.register(r'payment-method', payviews.PaymentMethodViewSet, basename='payment-method')
router.register(r'bill', payviews.BillViewSet, basename='bill')
router.register(r'order', payviews.OrderViewSet, basename='order')
@ -73,6 +73,9 @@ router.register(r'admin/order', payviews.AdminOrderViewSet, basename='admin/orde
router.register(r'admin/vmhost', vmviews.VMHostViewSet)
router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula')
# User/Account
router.register(r'user', authviews.UserViewSet, basename='user')
urlpatterns = [
path('', include(router.urls)),

View file

@ -0,0 +1,14 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers
from uncloud_pay.models import get_balance_for_user
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ['username', 'email', 'balance']
# Display current 'balance'
balance = serializers.SerializerMethodField('get_balance')
def get_balance(self, user):
return get_balance_for_user(user)

View file

@ -0,0 +1,16 @@
from rest_framework import viewsets, permissions, status
from .serializers import *
class UserViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
if self.request.user.is_superuser:
obj = get_user_model().objects.all()
else:
# This is a bit stupid: we have a user, we create a queryset by
# matching on the username.
obj = get_user_model().objects.filter(username=self.request.user.username)
return obj

View file

@ -44,10 +44,8 @@ class ProductStatus(models.TextChoices):
ACTIVE = 'ACTIVE', _('Active')
DELETED = 'DELETED', _('Deleted')
###
# Users.
def get_balance_for(user):
def get_balance_for_user(user):
bills = reduce(
lambda acc, entry: acc + entry.total,
Bill.objects.filter(owner=user),

View file

@ -2,20 +2,6 @@ from django.contrib.auth import get_user_model
from rest_framework import serializers
from .models import *
###
# Users.
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ['username', 'email', 'balance']
# Display current 'balance'
balance = serializers.SerializerMethodField('get_balance')
def get_balance(self, user):
return get_balance_for(user)
###
# Payments and Payment Methods.

View file

@ -48,12 +48,6 @@ class OrderViewSet(viewsets.ReadOnlyModelViewSet):
def get_queryset(self):
return Order.objects.filter(owner=self.request.user)
class UserViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = UserSerializer
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
return get_user_model().objects.all()
class PaymentMethodViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated]