General clean up of models and methods.
This commit is contained in:
127
app/models/contact_models.py
Normal file
127
app/models/contact_models.py
Normal file
@@ -0,0 +1,127 @@
|
||||
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 ..db_sql import redis_lookup_id_random
|
||||
from ..lib_general import *
|
||||
|
||||
from .common_field_schema import base_fields, default_num_bytes
|
||||
#from .account_models import Account_Base
|
||||
from .address_models import Address_Base
|
||||
|
||||
|
||||
class Contact_Base(BaseModel):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
id_random: Optional[str] = Field(
|
||||
**base_fields['contact_id_random'],
|
||||
alias='contact_id_random',
|
||||
default_factory=lambda:secrets.token_urlsafe(default_num_bytes),
|
||||
)
|
||||
id: Optional[int] = Field(
|
||||
#alias='contact_id'
|
||||
)
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int]
|
||||
address_id_random: Optional[str]
|
||||
address_id: Optional[int]
|
||||
|
||||
for_type: Optional[str]
|
||||
for_id_random: Optional[str]
|
||||
for_id: Optional[int]
|
||||
|
||||
name: Optional[str]
|
||||
title: Optional[str]
|
||||
tagline: Optional[str]
|
||||
|
||||
description: Optional[str]
|
||||
lu_time_zone_id: Optional[str]
|
||||
timezone: Optional[str]
|
||||
|
||||
email: Optional[str]
|
||||
website_url: Optional[str]
|
||||
website_name: Optional[str]
|
||||
|
||||
phone_mobile: Optional[str]
|
||||
phone_home: Optional[str]
|
||||
phone_office: Optional[str]
|
||||
phone_land: Optional[str]
|
||||
phone_fax: Optional[str]
|
||||
|
||||
facebook_url: Optional[str]
|
||||
instagram_url: Optional[str]
|
||||
twitter_url: Optional[str]
|
||||
linkedin_url: Optional[str]
|
||||
|
||||
other_site_url: Optional[str]
|
||||
other_site_name: Optional[str]
|
||||
|
||||
other_text: Optional[str]
|
||||
other_json: Optional[Json]
|
||||
|
||||
priority: Optional[int]
|
||||
sort: Optional[int]
|
||||
group: Optional[str]
|
||||
|
||||
#account: Optional[Account_Base] = Account_Base()
|
||||
address: Optional[Address_Base] = Address_Base()
|
||||
|
||||
created_on: Optional[datetime.datetime] = None
|
||||
updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
_processed_at: datetime.datetime = PrivateAttr(default_factory=datetime.datetime.now)
|
||||
|
||||
#@validator('contact_id_random', always=True)
|
||||
def contact_id_random_copy(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['id_random']:
|
||||
return values['id_random']
|
||||
return None
|
||||
|
||||
@validator('id', always=True)
|
||||
def contact_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['id_random']:
|
||||
log.debug(values['id_random'])
|
||||
return redis_lookup_id_random(record_id_random=values['id_random'], table_name='contact')
|
||||
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('address_id', always=True)
|
||||
def address_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values.get('address_id_random', None):
|
||||
return redis_lookup_id_random(record_id_random=values['address_id_random'], table_name='address')
|
||||
return None
|
||||
|
||||
@validator('for_id', always=True)
|
||||
def for_id_lookup(cls, v, values, **kwargs):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['for_id_random'] and values['for_type']:
|
||||
return redis_lookup_id_random(record_id_random=values['for_id_random'], table_name=values['for_type'])
|
||||
return None
|
||||
|
||||
class Config:
|
||||
underscore_attrs_are_private = True
|
||||
fields = base_fields
|
||||
|
||||
#Contact_Base.update_forward_refs()
|
||||
Reference in New Issue
Block a user