Skip to content

Commit

Permalink
Merge pull request #58 from ral-facilities/54_use_enum_for_parameter_…
Browse files Browse the repository at this point in the history
…type

Use enum for parameter type
  • Loading branch information
keiranjprice101 authored Sep 12, 2019
2 parents 5695e3f + ba49d1e commit db5330d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
9 changes: 4 additions & 5 deletions common/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import logging

log = logging.getLogger()


class ApiError(Exception):
pass

Expand All @@ -21,3 +16,7 @@ class AuthenticationError(ApiError):

class BadRequestError(ApiError):
pass


class DatabaseError(ApiError):
pass
39 changes: 36 additions & 3 deletions common/models/db_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
from sqlalchemy import Index, Column, BigInteger, String, DateTime, ForeignKey, Integer, Float, FetchedValue
import enum

from sqlalchemy import Index, Column, BigInteger, String, DateTime, ForeignKey, Integer, Float, FetchedValue, \
TypeDecorator
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import InstrumentedList

from common.exceptions import BadFilterError
from common.exceptions import BadFilterError, DatabaseError

Base = declarative_base()


class EnumAsInteger(TypeDecorator):
"""
Column type for storing Python enums in a database INTEGER column.
"""
impl = Integer

def __init__(self, enum_type):
super(EnumAsInteger, self).__init__()
self.enum_type = enum_type

def process_bind_param(self, value, dialect):
if isinstance(value, self.enum_type):
return value.value
raise DatabaseError(f"value {value} not in {self.enum_type.__name__}")

def process_result_value(self, value, dialect):
try:
return f"{self.enum_type(value)}".replace(f"{self.enum_type.__name__}.", "") # Strips the enum class name
except ValueError:
raise DatabaseError(f"value {value} not in {self.enum_type.__name__}") # This will force a 500 response

def copy(self, **kwargs):
return EnumAsInteger(self.enum_type)


class EntityHelper(object):
"""
EntityHelper class that contains methods to be shared across all entities
Expand Down Expand Up @@ -606,6 +634,11 @@ class PARAMETERTYPE(Base, EntityHelper):
Index('UNQ_PARAMETERTYPE_0', 'FACILITY_ID', 'NAME', 'UNITS'),
)

class ValueTypeEnum(enum.Enum):
DATE_AND_TIME = 0
NUMERIC = 1
STRING = 2

ID = Column(BigInteger, primary_key=True)
APPLICABLETODATACOLLECTION = Column(Integer, server_default=FetchedValue())
APPLICABLETODATAFILE = Column(Integer, server_default=FetchedValue())
Expand All @@ -623,7 +656,7 @@ class PARAMETERTYPE(Base, EntityHelper):
NAME = Column(String(255), nullable=False)
UNITS = Column(String(255), nullable=False)
UNITSFULLNAME = Column(String(255))
VALUETYPE = Column(Integer, nullable=False)
VALUETYPE = Column(EnumAsInteger(ValueTypeEnum), nullable=False)
VERIFIED = Column(Integer, server_default=FetchedValue())
FACILITY_ID = Column(ForeignKey('FACILITY.ID'), nullable=False)

Expand Down

0 comments on commit db5330d

Please sign in to comment.