Skip to content
Snippets Groups Projects
Commit 04f4e332 authored by Mattia Mancini's avatar Mattia Mancini
Browse files

OSB-28: moved the code to process the station test into the serializers

parent 6059895b
No related branches found
No related tags found
2 merge requests!89Monitoring maintenance Epic branch merge,!1Resolve OSB-13 "Monitoringmaintenance "
import logging
from rest_framework.serializers import PrimaryKeyRelatedField, CharField, IntegerField from rest_framework.serializers import PrimaryKeyRelatedField, CharField, IntegerField
from ..models.component_error import ComponentError
from .utils import NotNullModelSerializer from .utils import NotNullModelSerializer
from ..models.component import Component
from ..models.component_error import ComponentError
logger = logging.getLogger(__name__)
class ComponentErrorSerializer(NotNullModelSerializer): class ComponentErrorSerializer(NotNullModelSerializer):
...@@ -16,3 +22,48 @@ class ComponentErrorSerializer(NotNullModelSerializer): ...@@ -16,3 +22,48 @@ class ComponentErrorSerializer(NotNullModelSerializer):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.Meta.depth = kwargs.pop('depth', 2) self.Meta.depth = kwargs.pop('depth', 2)
super(ComponentErrorSerializer, self).__init__(*args, **kwargs) super(ComponentErrorSerializer, self).__init__(*args, **kwargs)
@staticmethod
def insert_component_error(station_test_entry,
station_entry,
component_error):
component = component_error.pop('component')
component_entry = Component.objects.filter(station=station_entry,
**component).first()
if component_entry is None:
logger.debug('Component entry is not present, inserting ...')
component_entry = Component(station=station_entry,
**component)
logger.debug(component_entry, component_error)
component_entry.save()
component_error_entry = ComponentError.objects.filter(component=component_entry,
station_test=station_test_entry,
**component_error).first()
if component_error_entry is None:
logger.debug('Component error entry is not present, inserting ...')
component_error_entry = ComponentError(component=component_entry,
station_test=station_test_entry,
**component_error)
component_error_entry.save()
return component_error_entry
@staticmethod
def insert_component_errors(station_test_entry,
station_entry,
component_errors):
results = []
for component_error in component_errors:
try:
results.append(ComponentErrorSerializer.insert_component_error(
station_test_entry, station_entry, component_error))
except Exception as e:
logger.exception(
'Cannot insert component error %s'
' for station_test_entry %s and station_entry %s: %s',
component_error, station_test_entry, station_entry)
raise e
return results
...@@ -7,3 +7,13 @@ class ElementSerializer(ModelSerializer): ...@@ -7,3 +7,13 @@ class ElementSerializer(ModelSerializer):
model = Element model = Element
fields = '__all__' fields = '__all__'
depth = 1 depth = 1
@staticmethod
def insert_element(component, element):
element_entry = Element.objects.filter(component=component,
**element).first()
if element_entry is None:
element_entry = Element(component=component,
**element)
element_entry.save()
return element_entry
\ No newline at end of file
from rest_framework.serializers import ModelSerializer from rest_framework.serializers import ModelSerializer
from .component_error import ComponentErrorSerializer
from .element import ElementSerializer
from ..models.element_error import ElementError from ..models.element_error import ElementError
from .error_details import ErrorDetailsSerializer import logging
logger = logging.getLogger(__name__)
class ElementErrorSerializer(ModelSerializer):
class ElementErrorSerializer(ModelSerializer):
class Meta: class Meta:
model = ElementError model = ElementError
fields = '__all__' fields = '__all__'
depth = 1 depth = 1
\ No newline at end of file
@staticmethod
def insert_element_error(station_test_entry,
station_entry,
element_error):
component_error = element_error.pop('component_error')
component_error_entry = ComponentErrorSerializer(station_test_entry,
station_entry,
component_error)
component = component_error_entry.component
element = element_error.pop('element')
element_error_details = element_error.pop('details')
element_entry = ElementSerializer.insert_element(component, element)
element_error_entry = ElementError.objects.filter(element=element_entry,
component_error=component_error_entry,
**element_error).first()
if element_error_entry is None:
element_error_entry = ElementError(element=element_entry,
details=element_error_details,
component_error=component_error_entry,
**element_error)
element_error_entry.save()
return element_error_entry
@staticmethod
def insert_element_errors(station_test_entry,
station_entry,
element_errors):
results = []
for element_error in element_errors:
try:
results.append(ElementErrorSerializer.insert_element_error(station_test_entry,
station_entry,
element_error))
except Exception as e:
logger.exception(
'Cannot insert element error %s'
' for station_test_entry %s and station_entry %s: %s',
element_error, station_test_entry, station_entry)
raise e
from rest_framework.serializers import ModelSerializer from rest_framework.serializers import ModelSerializer
from ..models.station_test import StationTest from ..models.station_test import StationTest
from ..models.station import Station
from .component_error import ComponentErrorSerializer from .component_error import ComponentErrorSerializer
from .element_error import ElementErrorSerializer
import logging
logger = logging.getLogger(__name__)
class StationTestSerializer(ModelSerializer): class StationTestSerializer(ModelSerializer):
component_errors = ComponentErrorSerializer(many=True, read_only=True, depth=0) component_errors = ComponentErrorSerializer(many=True, read_only=True, depth=0)
class Meta: class Meta:
model = StationTest model = StationTest
fields = '__all__' fields = '__all__'
depth = 2 depth = 2
\ No newline at end of file
@staticmethod
def insert_station_test(station_test):
"""
Insert the station test in the database and if necessary the component errors
and the element errors
:param station_test: json representation of the station test
:type station_test: dict
:return: the station test entry inserted
:rtype: StationTest
"""
station = station_test.pop('station')
component_errors = station_test.pop('component_errors')
element_errors = station_test.pop('element_errors')
station_entry = Station.objects.filter(**station).first()
if station_entry is None:
logger.debug('Station is not present, inserting ...')
station_entry = Station(**station)
station_entry.save()
station_test_entry = StationTest.objects.filter(**station_test).first()
if station_test_entry is None:
logger.debug('Station test is not present, inserting ...')
station_test_entry = StationTest(station=station_entry,
**station_test)
station_test_entry.save()
ComponentErrorSerializer.insert_component_errors(station_test_entry,
station_entry,
component_errors)
ElementErrorSerializer.insert_element_errors(station_test_entry,
station_entry,
element_errors)
return station_test_entry
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment