Commit 971d5c70 by 周田

mod: 封装数据

parent d6ceee55
from rest_framework.utils.serializer_helpers import ReturnDict, OrderedDict
def tree_data(data: ReturnDict, field_name: str) -> ReturnDict:
"""
封装数据
:param data: 原本的数据
:param parent: 需要收集数据的相同字段名
:return: 收集到的数据
"""
return_data = {}
name = set()
for item in data:
if item[field_name] not in name:
name.add(item[field_name])
return_data[item[field_name]] = []
return_data[item[field_name]].append(item)
return_data['fields'] = name
return return_data
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from .models import (TableAllDevCmdDefine, TableDevCmdNamePoll,
TableSoftLimitAngle, TableXproAllDevinfo)
from .serializers import (TableAllDevCmdDefineSerializer, TableDevCmdNamePollSerializer,
TableSoftLimitAngleSerializer, TableXproAllDevinfoSerializer)
# Create your views here.
from .utils import tree_data
class TableAllDevCmdDefineView(ModelViewSet):
queryset = TableAllDevCmdDefine.objects.all()
serializer_class = TableAllDevCmdDefineSerializer
def list(self, request):
serializer = self.get_serializer(self.get_queryset(), many=True)
data = tree_data(serializer.data, 'cmd_name')
return Response(data)
class TableDevCmdNamePollView(ModelViewSet):
queryset = TableDevCmdNamePoll.objects.all()
serializer_class = TableDevCmdNamePollSerializer
def list(self, request):
serializer = self.get_serializer(self.get_queryset(), many=True)
data = tree_data(serializer.data, 'protocol_name')
return Response(data)
class TableSoftLimitAngleView(ModelViewSet):
queryset = TableSoftLimitAngle.objects.all()
......
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class ProtocolVersionManageConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'protocol_version_manage'
# Generated by Django 3.2.19 on 2023-08-10 08:56
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='AllDevCmdDefineAndVersion',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('cmd_name', models.TextField()),
('cmd_type', models.TextField()),
('fieldindex', models.IntegerField()),
('fieldname', models.TextField()),
('fieldsize', models.IntegerField()),
('value', models.TextField()),
('minvalue', models.TextField()),
('maxvalue', models.TextField()),
('datatype', models.IntegerField()),
('operation_in', models.IntegerField()),
('operation_in_num', models.IntegerField()),
('operation_out', models.IntegerField()),
('operation_out_num', models.IntegerField()),
('operabo_in', models.IntegerField()),
('operabo_out', models.IntegerField()),
('lua_script_in', models.TextField()),
('lua_script_out', models.TextField()),
('version', models.TextField()),
],
options={
'db_table': 'AllDevCmdDefineAndVersion',
},
),
migrations.CreateModel(
name='AllProtocolChangeLog',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('protocol_name', models.TextField()),
('log', models.TextField()),
],
options={
'db_table': 'AllProtocolChangeLog',
},
),
migrations.CreateModel(
name='AllProtocolDefinAndVersion',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('protocol_name', models.TextField()),
('cmd_name', models.TextField()),
('cmd_type', models.TextField()),
('encode', models.TextField()),
('timing_cmd_cycle_period', models.IntegerField()),
('cmd_explain', models.TextField()),
('version', models.TextField()),
],
options={
'db_table': 'AllProtocolDefinAndVersion',
},
),
migrations.CreateModel(
name='CurrentDevVersion',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('protocol_name', models.TextField()),
('version', models.TextField()),
],
options={
'db_table': 'CurrentDevVersion',
},
),
]
# Generated by Django 3.2.19 on 2023-08-11 07:08
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('protocol_version_manage', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Test',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('json', models.TextField()),
],
options={
'db_table': 'Test',
},
),
]
from django.db import models
# Create your models here.
class AllDevCmdDefineAndVersion(models.Model):
id = models.AutoField(primary_key=True) # Field name made lowercase.
cmd_name = models.TextField() # Field name made lowercase.
cmd_type = models.TextField() # Field name made lowercase.
fieldindex = models.IntegerField() # Field name made lowercase.
fieldname = models.TextField() # Field name made lowercase.
fieldsize = models.IntegerField() # Field name made lowercase.
value = models.TextField()
minvalue = models.TextField() # Field name made lowercase.
maxvalue = models.TextField() # Field name made lowercase.
datatype = models.IntegerField() # Field name made lowercase.
operation_in = models.IntegerField()
operation_in_num = models.IntegerField()
operation_out = models.IntegerField()
operation_out_num = models.IntegerField()
operabo_in = models.IntegerField() # Field name made lowercase.
operabo_out = models.IntegerField() # Field name made lowercase.
lua_script_in = models.TextField() # Field name made lowercase.
lua_script_out = models.TextField() # Field name made lowercase.
version = models.TextField()
class Meta:
db_table = 'AllDevCmdDefineAndVersion'
class AllProtocolDefinAndVersion(models.Model):
id = models.AutoField(primary_key=True) # Field name made lowercase.
protocol_name = models.TextField() # Field name made lowercase.
cmd_name = models.TextField() # Field name made lowercase.
cmd_type = models.TextField() # Field name made lowercase.
encode = models.TextField() # Field name made lowercase.
timing_cmd_cycle_period = models.IntegerField() # Field name made lowercase.
cmd_explain = models.TextField() # Field name made lowercase.
version = models.TextField()
class Meta:
db_table = 'AllProtocolDefinAndVersion'
class CurrentDevVersion(models.Model):
id = models.AutoField(primary_key=True) # Field name made lowercase.
protocol_name = models.TextField() # Field name made lowercase.
version = models.TextField()
class Meta:
db_table = 'CurrentDevVersion'
class AllProtocolChangeLog(models.Model):
id = models.AutoField(primary_key=True) # Field name made lowercase.
protocol_name = models.TextField() # Field name made lowercase.
log = models.TextField()
class Meta:
db_table = 'AllProtocolChangeLog'
class Test(models.Model):
id = models.AutoField(primary_key=True)
json = models.TextField()
class Meta:
db_table = 'Test'
{
[{
"current_version": "20201223",
"cmds": [
{
......@@ -21,4 +21,15 @@
"fields": ["field1", "field2", "field3"]
}
]
}
},
{
"explain": "XY置位指令格式",
"version": "20201223"
},
[
{
"version": "20201223",
"path": "/path/to/file"
}
]
]
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
......@@ -25,7 +25,7 @@ SECRET_KEY = 'django-insecure-68xzqkau2%ulw2t^tpfo0d8$+_8$5z2jixypnxv$#81l&0=)s0
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']
# Application definition
......@@ -34,6 +34,7 @@ INSTALLED_APPS = [
# 'daphne',
# 'chat',
'device_data_op',
'protocol_version_manage',
'django.contrib.admin',
'django.contrib.auth',
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment