83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
from __future__ import annotations
|
|
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
|
|
from app.models.product_models import Product_Base
|
|
|
|
|
|
class Cont_Edu_Cert_Base(BaseModel):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
id_random: Optional[str] = Field(
|
|
**base_fields['cont_edu_cert_id_random'],
|
|
alias = 'cont_edu_cert_id_random',
|
|
default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
|
)
|
|
id: Optional[int] = Field(
|
|
alias = 'cont_edu_cert_id'
|
|
)
|
|
|
|
account_id_random: Optional[str]
|
|
account_id: Optional[int]
|
|
|
|
product_id_random: Optional[str]
|
|
product_id: Optional[int]
|
|
|
|
name: Optional[str]
|
|
description: Optional[str]
|
|
title: Optional[str]
|
|
datetime_start: Optional[datetime.datetime]
|
|
datetime_end: Optional[datetime.datetime]
|
|
location: Optional[str]
|
|
|
|
enable: Optional[bool] = False
|
|
|
|
notes: Optional[str]
|
|
created_on: Optional[datetime.datetime] = None
|
|
updated_on: Optional[datetime.datetime] = None
|
|
|
|
# Including other related objects
|
|
product: Optional[Union[Product_Base, None]]
|
|
|
|
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
|
|
|
@validator('id', always=True)
|
|
def cont_edu_cert_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values.get('id_random', None): # 'id_random' in values and values['id_random']:
|
|
log.debug(values['id_random'])
|
|
return redis_lookup_id_random(record_id_random=values['id_random'], table_name='cont_edu_cert')
|
|
return None
|
|
|
|
@validator('account_id', always=True)
|
|
def account_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values['account_id_random']:
|
|
return redis_lookup_id_random(record_id_random=values['account_id_random'], table_name='account')
|
|
return None
|
|
|
|
@validator('product_id', always=True)
|
|
def product_id_lookup(cls, v, values, **kwargs):
|
|
log.setLevel(logging.WARNING)
|
|
log.debug(locals())
|
|
|
|
if values.get('product_id_random', None): # values['product_id_random']:
|
|
return redis_lookup_id_random(record_id_random=values['product_id_random'], table_name='product')
|
|
return None
|
|
|
|
class Config:
|
|
underscore_attrs_are_private = True
|
|
allow_population_by_field_name = True
|
|
fields = base_fields
|