General clean up. Work on event device. Work on creating a common core object model.
This commit is contained in:
@@ -18,7 +18,7 @@ class Event_Device_Base(BaseModel):
|
|||||||
id_random: Optional[str] = Field(
|
id_random: Optional[str] = Field(
|
||||||
**base_fields['event_device_id_random'],
|
**base_fields['event_device_id_random'],
|
||||||
alias = 'event_device_id_random',
|
alias = 'event_device_id_random',
|
||||||
default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
# default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
||||||
)
|
)
|
||||||
id: Optional[int] = Field(
|
id: Optional[int] = Field(
|
||||||
alias = 'event_device_id'
|
alias = 'event_device_id'
|
||||||
@@ -80,6 +80,10 @@ class Event_Device_Base(BaseModel):
|
|||||||
alert: Optional[bool]
|
alert: Optional[bool]
|
||||||
alert_msg: Optional[str]
|
alert_msg: Optional[str]
|
||||||
|
|
||||||
|
info_hostname: Optional[bool]
|
||||||
|
info_ip: Optional[bool]
|
||||||
|
info_os: Optional[bool]
|
||||||
|
|
||||||
enable: Optional[bool]
|
enable: Optional[bool]
|
||||||
|
|
||||||
# hide: Optional[bool]
|
# hide: Optional[bool]
|
||||||
@@ -107,13 +111,13 @@ class Event_Device_Base(BaseModel):
|
|||||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||||
|
|
||||||
#@validator('event_device_id_random', always=True)
|
#@validator('event_device_id_random', always=True)
|
||||||
def event_device_id_random_copy(cls, v, values, **kwargs):
|
# def event_device_id_random_copy(cls, v, values, **kwargs):
|
||||||
log.setLevel(logging.WARNING)
|
# log.setLevel(logging.WARNING)
|
||||||
log.debug(locals())
|
# log.debug(locals())
|
||||||
|
|
||||||
if values['id_random']:
|
# if values['id_random']:
|
||||||
return values['id_random']
|
# return values['id_random']
|
||||||
return None
|
# return None
|
||||||
|
|
||||||
@validator('id', always=True)
|
@validator('id', always=True)
|
||||||
def event_device_id_lookup(cls, v, values, **kwargs):
|
def event_device_id_lookup(cls, v, values, **kwargs):
|
||||||
|
|||||||
110
app/models/obj_models.py
Normal file
110
app/models/obj_models.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import datetime, hashlib, logging, os, pytz, redis, secrets
|
||||||
|
|
||||||
|
from typing import Dict, List, Optional, Set, Union
|
||||||
|
from pydantic import BaseModel, EmailStr, Field, Json, PrivateAttr, ValidationError, validator
|
||||||
|
|
||||||
|
from app.db_sql import redis_lookup_id_random
|
||||||
|
from app.lib_general import log, logging
|
||||||
|
|
||||||
|
from app.models.common_field_schema import base_fields, default_num_bytes
|
||||||
|
|
||||||
|
|
||||||
|
# ### BEGIN ### API Common Core Object Models ### Obj_Base() ###
|
||||||
|
class Obj_Base(BaseModel):
|
||||||
|
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
|
log.debug(locals())
|
||||||
|
|
||||||
|
id_random: Optional[str] = Field(
|
||||||
|
**base_fields['obj_id_random'],
|
||||||
|
alias = 'obj_id_random',
|
||||||
|
# default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
||||||
|
)
|
||||||
|
id: Optional[int] = Field(
|
||||||
|
alias = 'obj_id'
|
||||||
|
)
|
||||||
|
|
||||||
|
code: Optional[str]
|
||||||
|
|
||||||
|
account_id_random: Optional[str]
|
||||||
|
account_id: Optional[int]
|
||||||
|
|
||||||
|
person_id_random: Optional[str]
|
||||||
|
person_id: Optional[int]
|
||||||
|
|
||||||
|
user_id_random: Optional[str]
|
||||||
|
user_id: Optional[int]
|
||||||
|
|
||||||
|
for_type: Optional[str]
|
||||||
|
for_id: Optional[int] # NOTE: id_random should no longer be needed.
|
||||||
|
|
||||||
|
name: Optional[str]
|
||||||
|
description: Optional[str]
|
||||||
|
|
||||||
|
alert: Optional[bool]
|
||||||
|
alert_msg: Optional[str]
|
||||||
|
|
||||||
|
enable: Optional[bool]
|
||||||
|
|
||||||
|
restricted: Optional[bool]
|
||||||
|
hide: Optional[bool]
|
||||||
|
priority: Optional[bool]
|
||||||
|
sort: Optional[int]
|
||||||
|
group: Optional[str]
|
||||||
|
|
||||||
|
ver: Optional[int]
|
||||||
|
|
||||||
|
staff_notes: Optional[str]
|
||||||
|
|
||||||
|
meta_json: Optional[str]
|
||||||
|
other_json: Optional[str]
|
||||||
|
|
||||||
|
notes: Optional[str]
|
||||||
|
|
||||||
|
created_on: Optional[datetime.datetime] = None
|
||||||
|
updated_on: Optional[datetime.datetime] = None
|
||||||
|
|
||||||
|
# Including convenience data
|
||||||
|
# This is only for convenience. Probably going to keep unless it causes a problem.
|
||||||
|
|
||||||
|
# Including JSON data
|
||||||
|
# other_json: Optional[Json]
|
||||||
|
# meta_json: Optional[Json]
|
||||||
|
|
||||||
|
# Including other related objects
|
||||||
|
example: Optional[Example_Base]
|
||||||
|
|
||||||
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||||
|
|
||||||
|
@validator('id', always=True)
|
||||||
|
def obj_id_lookup(cls, v, values, **kwargs):
|
||||||
|
if isinstance(v, int) and v > 0: return v
|
||||||
|
elif id_random := values.get('id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='obj')
|
||||||
|
return None
|
||||||
|
|
||||||
|
@validator('account_id', always=True)
|
||||||
|
def account_id_lookup(cls, v, values, **kwargs):
|
||||||
|
if isinstance(v, int) and v > 0: return v
|
||||||
|
elif id_random := values.get('account_id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='account')
|
||||||
|
return None
|
||||||
|
|
||||||
|
@validator('person_id', always=True)
|
||||||
|
def person_id_lookup(cls, v, values, **kwargs):
|
||||||
|
if isinstance(v, int) and v > 0: return v
|
||||||
|
elif id_random := values.get('person_id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='person')
|
||||||
|
return None
|
||||||
|
|
||||||
|
@validator('user_id', always=True)
|
||||||
|
def user_id_lookup(cls, v, values, **kwargs):
|
||||||
|
if isinstance(v, int) and v > 0: return v
|
||||||
|
elif id_random := values.get('user_id_random'):
|
||||||
|
return redis_lookup_id_random(record_id_random=id_random, table_name='user')
|
||||||
|
return None
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
underscore_attrs_are_private = True
|
||||||
|
allow_population_by_field_name = True
|
||||||
|
fields = base_fields
|
||||||
|
# ### END ### API Common Core Object Models ### Obj_Base() ###
|
||||||
Reference in New Issue
Block a user