1
0
Forkkaa 0
peili kohteelle https://github.com/yweber/lodel2.git synkronoitu 2025-11-27 15:56:53 +01:00
lodel2_mirror/lodel/leapi/datahandlers/references.py
Yann 61e892fe61 Implements an empty() classmethod for multiref datahandlers
Return the value of this datahandler when empty
2016-09-07 10:22:42 +02:00

109 riviä
4,1 KiB
Python
Raaka Syyllistynyt Historia

Tämä tiedosto sisältää näkymättömiä Unicode-merkkejä

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef, SingleRef
from lodel.exceptions import *
from lodel import logger
class Link(SingleRef):
pass
##@brief Child class of MultipleRef where references are represented in the form of a python list
class List(MultipleRef):
##@brief instanciates a list reference
# @param allowed_classes list | None : list of allowed em classes if None no restriction
# @param internal bool
# @param kwargs
def __init__(self, max_length = None, **kwargs):
super().__init__(**kwargs)
@classmethod
def empty(cls):
return list()
##@brief Check and cast value in appropriate type
#@param value *
#@throw FieldValidationError if value is unappropriate or can not be cast
#@return value
def _check_data_value(self, value):
value = super()._check_data_value(value)
if not(isinstance(value, list) or isinstance(value, str)):
raise FieldValidationError("List or string expected for a set field")
return value
##@brief Child class of MultipleRef where references are represented in the form of a python set
class Set(MultipleRef):
##@brief instanciates a set reference
# @param allowed_classes list | None : list of allowed em classes if None no restriction
# @param internal bool : if False, the field is not internal
# @param kwargs : Other named arguments
def __init__(self, **kwargs):
super().__init__(**kwargs)
@classmethod
def empty(cls):
return set()
##@brief Check and cast value in appropriate type
#@param value *
#@throw FieldValidationError if value is unappropriate or can not be cast
#@return value
def _check_data_value(self, value):
value = super()._check_data_value(value)
if not (isinstance(value, set) or isinstance(value, str)):
raise FieldValidationError("Set or string expected for a set field")
return value
##@brief Child class of MultipleRef where references are represented in the form of a python dict
class Map(MultipleRef):
##@brief instanciates a dict reference
# @param allowed_classes list | None : list of allowed em classes if None no restriction
# @param internal bool : if False, the field is not internal
# @param kwargs : Other named arguments
def __init__(self, **kwargs):
super().__init__(**kwargs)
@classmethod
def empty(cls):
return dict()
##@brief Check and cast value in appropriate type
#@param value *
#@throw FieldValidationError if value is unappropriate or can not be cast
#@return value
def _check_data_value(self, value):
value = super()._check_data_value(value)
if not isinstance(value, dict):
raise FieldValidationError("Values for dict fields should be dict")
return value
##@brief This Reference class is designed to handler hierarchy with some constraint
class Hierarch(MultipleRef):
directly_editable = False
##@brief Instanciate a data handler handling hierarchical relation with constraints
# @param back_reference tuple : Here it is mandatory to have a back ref (like a parent field)
# @param max_depth int | None : limit of depth
# @param max_childs int | Nine : maximum number of childs by nodes
def __init__(self, back_reference, max_depth = None, max_childs = None, **kwargs):
super().__init__( back_reference = back_reference,
max_depth = max_depth,
max_childs = max_childs,
**kwargs)
@classmethod
def empty(cls):
return tuple()
##@brief Check and cast value in appropriate type
#@param value *
#@throw FieldValidationError if value is unappropriate or can not be cast
#@return value
def _check_data_value(self, value):
value = super()._check_data_value(value)
if not (isinstance(value, list) or isinstance(value, str)):
raise FieldValidationError(
"List or string expected for a set field")
return value