Commit 672dab88 by 周田

feat:通过观察是否有协议版本下拉列表,来初始化协议信息

parent d259f1a2
# Generated by Django 3.2.19 on 2023-08-21 01:52
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('protocol_version_manage', '0002_test'),
]
operations = [
migrations.RenameModel(
old_name='AllProtocolChangeLog',
new_name='AllProtocolVersion',
),
migrations.DeleteModel(
name='Test',
),
migrations.RenameField(
model_name='allprotocolversion',
old_name='log',
new_name='vesrions_path',
),
migrations.AlterModelTable(
name='allprotocolversion',
table='AllProtocolVersion',
),
]
# Generated by Django 3.2.19 on 2023-08-21 02:19
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('protocol_version_manage', '0003_auto_20230821_0952'),
]
operations = [
migrations.RemoveField(
model_name='allprotocolversion',
name='id',
),
migrations.RemoveField(
model_name='currentdevversion',
name='id',
),
migrations.AlterField(
model_name='allprotocolversion',
name='protocol_name',
field=models.TextField(primary_key=True, serialize=False),
),
migrations.AlterField(
model_name='currentdevversion',
name='protocol_name',
field=models.TextField(primary_key=True, serialize=False),
),
]
# Generated by Django 3.2.19 on 2023-08-21 05:22
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('protocol_version_manage', '0004_auto_20230821_1019'),
]
operations = [
migrations.RenameField(
model_name='allprotocolversion',
old_name='vesrions_path',
new_name='version_paths',
),
]
...@@ -46,8 +46,7 @@ class AllProtocolDefinAndVersion(models.Model): ...@@ -46,8 +46,7 @@ class AllProtocolDefinAndVersion(models.Model):
class CurrentDevVersion(models.Model): class CurrentDevVersion(models.Model):
id = models.AutoField(primary_key=True) # Field name made lowercase. protocol_name = models.TextField(primary_key=True) # Field name made lowercase.
protocol_name = models.TextField() # Field name made lowercase.
version = models.TextField() version = models.TextField()
class Meta: class Meta:
...@@ -59,12 +58,12 @@ class AllProtocolVersion(models.Model): ...@@ -59,12 +58,12 @@ class AllProtocolVersion(models.Model):
versions_path 格式 versions_path 格式
版本号:版本原始文件所在的路径 版本号:版本原始文件所在的路径
{ {
"20240229": "path/to/file", "vesrion": "20230708",
"path": "/path/to/file"
} }
""" """
id = models.AutoField(primary_key=True) protocol_name = models.TextField(primary_key=True)
protocol_name = models.TextField() version_paths = models.TextField()
vesrions_path = models.TextField()
class Meta: class Meta:
db_table = 'AllProtocolVersion' db_table = 'AllProtocolVersion'
......
from .models import (CurrentDevVersion, AllDevCmdDefineAndVersion,
AllProtocolDefinAndVersion, AllProtocolVersion)
from rest_framework import serializers
class AllProtocolVersionSerializer(serializers.ModelSerializer):
class Meta:
model = AllProtocolVersion
fields = '__all__'
class AllDevCmdDefineAndVersionSerializer(serializers.ModelSerializer):
class Meta:
model = AllDevCmdDefineAndVersion
fields = '__all__'
class AllProtocolDefinAndVersionSerializer(serializers.ModelSerializer):
class Meta:
model = AllProtocolDefinAndVersion
fields = '__all__'
class CurrentDevVersionSerializer(serializers.ModelSerializer):
class Meta:
model = CurrentDevVersion
fields = '__all__'
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^protocol_version_manage/init/$', views.init),
re_path(r'^all_protocol_version/$', views.AllProtocolVersionViewSet.as_view({'get': 'list'})),
]
import json
from device_data_op.models import TableAllDevCmdDefine, TableDevCmdNamePoll
from device_data_op.serializers import TableAllDevCmdDefineSerializer, TableDevCmdNamePollSerializer
from django.db.models import QuerySet
from .models import (AllDevCmdDefineAndVersion, AllProtocolDefinAndVersion,
AllProtocolVersion, CurrentDevVersion)
INIT_VERSION = "init"
def init_protocol_version_manage(protocol_name) -> None:
"""
将协议版本信息初始化,即先将 device 已经在用的表的数据添加到数据库表中
:param protocol_name: 协议名
"""
protocol_cmds = TableDevCmdNamePoll.objects.filter(protocol_name=protocol_name).all()
protocol_cmds_serializer = TableDevCmdNamePollSerializer(protocol_cmds, many=True)
all_protocol_objects: list[AllProtocolDefinAndVersion] = []
for protocol in protocol_cmds_serializer.data:
protocol['version'] = json.dumps([INIT_VERSION])
del protocol['id']
all_protocol_objects.append(AllProtocolDefinAndVersion(**protocol))
AllProtocolDefinAndVersion.objects.bulk_create(all_protocol_objects)
cmd_fields: list[QuerySet] = []
for protocol in protocol_cmds:
cmd_fields.extend(TableAllDevCmdDefine.objects.filter(cmd_name=protocol.cmd_name).all())
cmd_fields_serializer = TableAllDevCmdDefineSerializer(cmd_fields, many=True)
all_cmd_fields_objects: list[AllDevCmdDefineAndVersion] = []
for cmd_field in cmd_fields_serializer.data:
cmd_field['version'] = json.dumps([INIT_VERSION])
del cmd_field['id']
all_cmd_fields_objects.append(AllDevCmdDefineAndVersion(**cmd_field))
AllDevCmdDefineAndVersion.objects.bulk_create(all_cmd_fields_objects)
version_path = {
"version": INIT_VERSION
}
AllProtocolVersion.objects.create(protocol_name=protocol_name,
version_paths=json.dumps([version_path]))
CurrentDevVersion.objects.create(protocol_name=protocol_name,
version=INIT_VERSION)
from django.shortcuts import render import json
from rest_framework.decorators import api_view from rest_framework.decorators import api_view
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import status from rest_framework import status
# Create your views here. from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from .models import (AllDevCmdDefineAndVersion, AllProtocolDefinAndVersion,
AllProtocolVersion, CurrentDevVersion)
from .serializers import (AllDevCmdDefineAndVersionSerializer, AllProtocolDefinAndVersionSerializer,
AllProtocolVersionSerializer, CurrentDevVersionSerializer)
from .utils import init_protocol_version_manage
@api_view(['POST'])
def init(request):
protocol_name = request.data.get('protocol_name')
assert protocol_name is not None, Response(status=status.HTTP_400_BAD_REQUEST)
all_protocol_version = AllProtocolVersion.objects.filter(protocol_name=protocol_name).first()
try:
if all_protocol_version is None:
# 该协议为空,即没有协议版本信息
init_protocol_version_manage(protocol_name)
all_protocol_version = AllProtocolVersion.objects.filter(protocol_name=protocol_name).first()
# 该协议不为空,即有协议版本信息
current_protocol_version = CurrentDevVersion.objects.filter(protocol_name=protocol_name).first()
data = json.loads(all_protocol_version.version_paths)
res_data = {
'version_paths': data,
'current_version': current_protocol_version.version
}
except:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response(data=res_data, status=status.HTTP_200_OK)
class AllProtocolVersionViewSet(GenericViewSet, ListModelMixin):
queryset = AllProtocolVersion.objects.all()
serializer_class = AllProtocolVersionSerializer
...@@ -40,6 +40,7 @@ urlpatterns = [ ...@@ -40,6 +40,7 @@ urlpatterns = [
path("chat/", include("chat.urls")), path("chat/", include("chat.urls")),
path("op/", include("device_data_op.urls")), path("op/", include("device_data_op.urls")),
path("api/", include("download_db.urls")), path("api/", include("download_db.urls")),
path("api/", include("protocol_version_manage.urls")),
re_path(r'^assets/(?P<path>.*)/$', serve, {'document_root': settings.STATIC_ROOT}), re_path(r'^assets/(?P<path>.*)/$', serve, {'document_root': settings.STATIC_ROOT}),
......
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