django-orchestra-test/orchestra/contrib/domains/api.py

39 lines
1.1 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from rest_framework import viewsets
from rest_framework.decorators import action
2014-05-08 16:59:35 +00:00
from rest_framework.response import Response
2014-05-13 13:46:40 +00:00
from orchestra.api import router
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.api import AccountApiMixin
2014-05-08 16:59:35 +00:00
from . import settings
from .models import Domain
from .serializers import DomainSerializer
class DomainViewSet(AccountApiMixin, viewsets.ModelViewSet):
serializer_class = DomainSerializer
filter_fields = ('name',)
2015-04-23 14:34:04 +00:00
queryset = Domain.objects.all()
2014-05-08 16:59:35 +00:00
def get_queryset(self):
qs = super(DomainViewSet, self).get_queryset()
return qs.prefetch_related('records')
@action(detail=True)
2014-05-08 16:59:35 +00:00
def view_zone(self, request, pk=None):
domain = self.get_object()
2014-07-10 15:19:06 +00:00
return Response({
'zone': domain.render_zone()
})
2015-04-23 14:34:04 +00:00
def options(self, request):
metadata = super(DomainViewSet, self).options(request)
2014-05-13 13:46:40 +00:00
names = ['DOMAINS_DEFAULT_A', 'DOMAINS_DEFAULT_MX', 'DOMAINS_DEFAULT_NS']
2015-04-23 14:34:04 +00:00
metadata.data['settings'] = {
2014-05-13 13:46:40 +00:00
name.lower(): getattr(settings, name, None) for name in names
}
2015-04-23 14:34:04 +00:00
return metadata
2014-05-08 16:59:35 +00:00
router.register(r'domains', DomainViewSet)