django-orchestra/orchestra/contrib/accounts/api.py

33 lines
1.1 KiB
Python
Raw Permalink Normal View History

from django.utils.translation import gettext_lazy as _
2014-10-23 15:38:46 +00:00
from rest_framework import viewsets, exceptions
2014-05-08 16:59:35 +00:00
2015-04-05 18:02:36 +00:00
from orchestra.api import router, SetPasswordApiMixin, LogApiMixin
2014-05-08 16:59:35 +00:00
from .models import Account
from .serializers import AccountSerializer
class AccountApiMixin(object):
def get_queryset(self):
qs = super(AccountApiMixin, self).get_queryset()
return qs.filter(account=self.request.user.pk)
2014-05-08 16:59:35 +00:00
2015-04-05 18:02:36 +00:00
class AccountViewSet(LogApiMixin, SetPasswordApiMixin, viewsets.ModelViewSet):
2015-04-23 14:34:04 +00:00
queryset = Account.objects.all()
2014-05-08 16:59:35 +00:00
serializer_class = AccountSerializer
singleton_pk = lambda _,request: request.user.pk
2014-05-08 16:59:35 +00:00
def get_queryset(self):
qs = super(AccountViewSet, self).get_queryset()
2014-09-30 16:39:47 +00:00
return qs.filter(id=self.request.user.pk)
2014-10-23 15:38:46 +00:00
def destroy(self, request, pk=None):
# TODO reimplement in permissions
if not request.user.is_superuser:
raise exceptions.PermissionDenied(_("Accounts can not be deleted."))
2014-10-23 21:25:44 +00:00
return super(AccountViewSet, self).destroy(request, pk=pk)
2014-05-08 16:59:35 +00:00
router.register(r'accounts', AccountViewSet)