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

33 lines
1.0 KiB
Python
Raw Normal View History

2014-10-23 15:38:46 +00:00
from django.utils.translation import ugettext_lazy as _
from rest_framework import viewsets, exceptions
2014-05-08 16:59:35 +00:00
2014-10-07 13:08:59 +00:00
from orchestra.api import router, SetPasswordApiMixin
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
2014-10-07 13:08:59 +00:00
class AccountViewSet(SetPasswordApiMixin, viewsets.ModelViewSet):
2014-05-08 16:59:35 +00:00
model = Account
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)