代码示例:
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from django.db import models
5 from django.utils.encoding import python_2_unicode_compatible
6 from apps.ncmdb.models import NcmdbRouterIpTable
7
8
9 TASK_STATUS_NOT_BEGIN = 0
10 TASK_STATUS_READY = 1
11 TASK_STATUS_RUNNING = 2
12 TASK_STATUS_COMPLETED = 3
13 TASK_STATUS_CANCEL = 4
14 TASK_STATUS = (
15 (TASK_STATUS_NOT_BEGIN, "未开始"),
16 (TASK_STATUS_READY, "准备中"),
17 (TASK_STATUS_RUNNING, "进行中"),
18 (TASK_STATUS_COMPLETED, "已完成"),
19 (TASK_STATUS_CANCEL, "已取消"),
20 )
21
22 TASK_RESULT_SUCCESS = 1
23 TASK_RESULT_FAILURE = 2
24 TASK_RESULT = (
25 (TASK_RESULT_SUCCESS, "成功"),
26 (TASK_RESULT_FAILURE, "失败"),
27 )
28
29
30 @python_2_unicode_compatible
31 class RouterTable(models.Model):
32 subnet = models.CharField(max_length=40, verbose_name="目的网段")
33 protocol = models.CharField(max_length=32, verbose_name="路由协议", null=True)
34 vrf = models.CharField(max_length=64, verbose_name="vrf名称", null=True)
35 interface = models.CharField(max_length=64, verbose_name="接口名称", null=True)
36 metric = models.CharField(max_length=16, verbose_name="metric值", null=True)
37 nexthop = models.CharField(max_length=40, verbose_name="路由下一跳", null=True)
38 preference = models.CharField(max_length=16, verbose_name="路由优先级", null=True)
39 device = models.ForeignKey(to=NcmdbRouterIpTable, null=True)
40 create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
41
42 def __str__(self):
43 return "{}_{}".format(self.__class__.__name__, self.subnet)
44
45 class Meta:
46 db_table = "rtm_router_table"
47
48
49 class RouterTableCollectTask(models.Model):
50 """
51 route table collect task table
52 """
53 device = models.ForeignKey(to=NcmdbRouterIpTable, null=True)
54 task_time = models.DateTimeField(null=True)
55 status = models.IntegerField(choices=TASK_STATUS)
56 result = models.IntegerField(choices=TASK_RESULT, null=True, blank=True)
57 task_id_celery = models.CharField(max_length=128, null=True, blank=True)
58 start_time = models.DateTimeField(auto_now_add=True)
59 finish_time = models.DateTimeField(auto_now=True)
60
61 def __str__(self):
62 return "{}:{}".format(self.__class__.__name__, self.task_id_celery)
63
64 class Meta:
65 db_table = "rtm_route_table_collect_task"
model代码
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 import django_filters
5 from rest_framework import serializers
6 from apps.rtm.models import *
7 from apps.ncmdb.uilts.uilts import format_time
8
9 __all__ = ['RouteTableSerializer', 'RouteTableFilter']
10
11 class RouteTableSerializer(serializers.ModelSerializer):
12 create_time = serializers.SerializerMethodField()
13 device = serializers.CharField(source="device.manage_ip")
14
15 def get_create_time(self, row):
16 return format_time(row.create_time) if row.create_time else None
17
18 class Meta:
19 model = RouterTable
20 fields = ["subnet", "protocol", "vrf", "interface", "metric", "nexthop",
21 "preference", "device", "create_time"]
22
23
24 class RouteTableFilter(django_filters.rest_framework.FilterSet):
25 #method后跟的函数表示自定义筛选
26 dc = django_filters.CharFilter(method='dc_filter', label="dc")
27 manage_ip = django_filters.CharFilter(method='manage_ip_filter', label="manage_ip")
28
29 def dc_filter(self, queryset, name, value):
30 return queryset.filter(device__dc=value)
31
32 def manage_ip_filter(self, queryset, name, value):
33 return queryset.filter(device__manage_ip__icontains=value)
34
35 class Meta:
36 model = RouterTable
37 fields = ("subnet", "protocol", "vrf", "nexthop", "preference", "manage_ip", "dc", "metric")
serializers及filter代码
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 from rest_framework.viewsets import ModelViewSet
5 from apps.rtm.models import RouterTable
6 from apps.rtm.views.route_table_serializers import *
7 from utils.authencation_func import BaseAuth, Pagination
8 from django_filters.rest_framework import DjangoFilterBackend
9 from rest_framework.filters import OrderingFilter
10
11 class RouteTableOperate(ModelViewSet):
12 authentication_classes = [BaseAuth]
13 queryset = RouterTable.objects.all()
14 serializer_class = RouteTableSerializer
15 pagination_class = Pagination
16 #DjangoFilterBackend表示开启筛选,OrderingFilter表示开启排序
17 filter_backends = [DjangoFilterBackend, OrderingFilter]
18 filter_class = RouteTableFilter
19 #ordering_fields='__all__'表示支持所有的字段排序, 默认是create_time降序
20 # 再给字段传排序值时ordering=-create_time表示降序,ordering=create_time表示升序
21 ordering_fields = '__all__'
22 ordering = ('-create_time',)
Views代码