diff --git a/apps/assets/api.py b/apps/assets/api.py index 2aa8c564c..05ca46331 100644 --- a/apps/assets/api.py +++ b/apps/assets/api.py @@ -1,33 +1,16 @@ # ~*~ coding: utf-8 ~*~ -from rest_framework import serializers -from rest_framework import viewsets, serializers, generics +from rest_framework import viewsets, generics, mixins from rest_framework.response import Response from rest_framework.views import APIView from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin, ListBulkCreateUpdateDestroyAPIView +from django.shortcuts import get_object_or_404 from common.mixins import BulkDeleteApiMixin from common.utils import get_object_or_none, signer from .hands import IsSuperUserOrTerminalUser, IsSuperUser -from .models import AssetGroup, Asset, IDC, SystemUser -from .serializers import AssetBulkUpdateSerializer - - -class AssetGroupSerializer(serializers.ModelSerializer): - class Meta: - model = AssetGroup - - -class AssetSerializer(serializers.ModelSerializer): - class Meta: - model = Asset - # fields = ('id', 'title', 'code', 'linenos', 'language', 'style') - - -class IDCSerializer(serializers.ModelSerializer): - class Meta: - model = IDC - # fields = ('id', 'title', 'code', 'linenos', 'language', 'style') +from .models import AssetGroup, Asset, IDC, SystemUser, AdminUser +from . import serializers class AssetGroupViewSet(viewsets.ModelViewSet): @@ -35,26 +18,50 @@ class AssetGroupViewSet(viewsets.ModelViewSet): some other comment """ queryset = AssetGroup.objects.all() - serializer_class = AssetGroupSerializer + serializer_class = serializers.AssetGroupSerializer class AssetViewSet(viewsets.ModelViewSet): """API endpoint that allows Asset to be viewed or edited.""" queryset = Asset.objects.all() - serializer_class = AssetSerializer + serializer_class = serializers.AssetSerializer -class IDCViewSet(viewsets.ReadOnlyModelViewSet): +class IDCViewSet(viewsets.ModelViewSet): """API endpoint that allows IDC to be viewed or edited.""" queryset = IDC.objects.all() - serializer_class = IDCSerializer + serializer_class = serializers.IDCSerializer permission_classes = (IsSuperUser,) +class AdminUserViewSet(viewsets.ModelViewSet): + queryset = AdminUser.objects.all() + serializer_class = serializers.AdminUserSerializer + permission_classes = (IsSuperUser,) + + +class SystemUserViewSet(viewsets.ModelViewSet): + queryset = SystemUser.objects.all() + serializer_class = serializers.SystemUserSerializer + permission_classes = (IsSuperUser,) + + +class IDCAssetsApi(generics.ListAPIView): + model = IDC + serializer_class = serializers.AssetSerializer + + def get(self, request, *args, **kwargs): + filter_kwargs = {self.lookup_field: self.kwargs[self.lookup_field]} + self.object = get_object_or_404(self.model, **filter_kwargs) + return super(IDCAssetsApi, self).get(request, *args, **kwargs) + + def get_queryset(self): + return self.object.assets.all() + class AssetListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView): queryset = Asset.objects.all() - serializer_class = AssetBulkUpdateSerializer + serializer_class = serializers.AssetBulkUpdateSerializer permission_classes = (IsSuperUser,) diff --git a/apps/assets/models.py b/apps/assets/models.py index 1103d0b4c..fdb298e3c 100644 --- a/apps/assets/models.py +++ b/apps/assets/models.py @@ -329,6 +329,7 @@ class Asset(models.Model): def __unicode__(self): return '%(ip)s:%(port)s' % {'ip': self.ip, 'port': self.port} + @property def is_valid(self): warning = '' if not self.is_active: diff --git a/apps/assets/serializers.py b/apps/assets/serializers.py index f5b53fb68..cc00144ec 100644 --- a/apps/assets/serializers.py +++ b/apps/assets/serializers.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from django.utils.translation import ugettext_lazy as _ from rest_framework import viewsets, serializers,generics -from .models import AssetGroup, Asset, IDC, AssetExtend +from .models import AssetGroup, Asset, IDC, AssetExtend, AdminUser, SystemUser from common.mixins import BulkDeleteApiMixin from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin @@ -14,11 +14,47 @@ class AssetBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer class Meta(object): model = Asset list_serializer_class = BulkListSerializer - fields = ['id', 'port', 'idc'] + fields = ('id', 'port', 'idc') # def get_group_display(self, obj): # return " ".join([group.name for group in obj.groups.all()]) # # def get_active_display(self, obj): # # TODO: user ative state - # return not (obj.is_expired and obj.is_active) \ No newline at end of file + # return not (obj.is_expired and obj.is_active) + + +class AssetGroupSerializer(serializers.ModelSerializer): + class Meta: + model = AssetGroup + + +class AssetSerializer(serializers.ModelSerializer): + class Meta: + model = Asset + + +class AdminUserSerializer(serializers.ModelSerializer): + class Meta: + model = AdminUser + + +class SystemUserSerializer(serializers.ModelSerializer): + class Meta: + model = SystemUser + + +class IDCSerializer(serializers.ModelSerializer): + assets_amount = serializers.SerializerMethodField() + + class Meta: + model = IDC + + @staticmethod + def get_assets_amount(obj): + return obj.assets.count() + + def get_field_names(self, declared_fields, info): + fields = super(IDCSerializer, self).get_field_names(declared_fields, info) + fields.append('assets_amount') + return fields diff --git a/apps/assets/templates/assets/admin_user_list.html b/apps/assets/templates/assets/admin_user_list.html index ed41a5cfe..e204b2e15 100644 --- a/apps/assets/templates/assets/admin_user_list.html +++ b/apps/assets/templates/assets/admin_user_list.html @@ -1,41 +1,118 @@ {% extends '_base_list.html' %} -{% load i18n %} -{% load common_tags %} -{% block content_left_head %} - {% trans "Create admin user" %} +{#{% load i18n %}#} +{#{% load common_tags %}#} +{#{% block content_left_head %}#} +{# {% trans "Create admin user" %} #} +{#{% endblock %}#} +{##} +{#{% block table_head %}#} +{#
+ + | +{% trans 'Name' %} | +{% trans 'Username' %} | +{% trans 'Asset num' %} | +{% trans 'Lost connection' %} | +{% trans 'Comment' %} | ++ |
---|