134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
"""
|
|
Project: PARTS Website
|
|
Author: Edward Middleton-Smith
|
|
Precision And Research Technology Systems Limited
|
|
|
|
Technology: Business Objects
|
|
Feature: Contact_Form Business Object
|
|
"""
|
|
|
|
# internal
|
|
from business_objects.base import Base
|
|
from business_objects.db_base import SQLAlchemy_ABC
|
|
import lib.argument_validation as av
|
|
from extensions import db
|
|
from helpers.helper_app import Helper_App
|
|
# external
|
|
from dataclasses import dataclass
|
|
from typing import ClassVar
|
|
|
|
|
|
class Contact_Form(SQLAlchemy_ABC, Base):
|
|
FLAG_ALTCHA: ClassVar[str] = 'altcha'
|
|
FLAG_CONTACT_FORM: ClassVar[str] = 'contact-form'
|
|
FLAG_NAME_COMPANY: ClassVar[str] = 'name-company'
|
|
FLAG_NAME_CONTACT: ClassVar[str] = 'name-contact'
|
|
FLAG_MESSAGE: ClassVar[str] = 'message'
|
|
FLAG_RECEIVE_MARKETING_COMMUNICATIONS: ClassVar[str] = 'receive-marketing-communications'
|
|
NAME_ATTR_OPTION_VALUE: ClassVar[str] = FLAG_CONTACT_FORM
|
|
NAME_ATTR_OPTION_TEXT: ClassVar[str] = Base.FLAG_EMAIL
|
|
|
|
__tablename__ = 'PH_Contact_Form'
|
|
__table_args__ = { 'extend_existing': True }
|
|
|
|
id_contact_form = db.Column(db.Integer, primary_key=True)
|
|
email = db.Column(db.String(255))
|
|
name_contact = db.Column(db.String(255))
|
|
name_company = db.Column(db.String(255))
|
|
message = db.Column(db.Text)
|
|
receive_marketing_communications = db.Column(db.Boolean)
|
|
active = db.Column(db.Boolean)
|
|
created_on = db.Column(db.DateTime)
|
|
|
|
def __init__(self):
|
|
self.id_contact_form = 0
|
|
self.is_new = False
|
|
super().__init__()
|
|
|
|
@classmethod
|
|
def from_db_contact_form(cls, query_row):
|
|
_m = f'{cls.__qualname__}.from_db_contact_form'
|
|
form = cls()
|
|
form.id_contact_form = query_row[0]
|
|
form.email = query_row[1]
|
|
form.name_contact = query_row[2]
|
|
form.name_company = query_row[3]
|
|
form.message = query_row[4]
|
|
form.receive_marketing_communications = av.input_bool(query_row[5], 'receive_marketing_communications', _m)
|
|
form.active = av.input_bool(query_row[6], 'active', _m)
|
|
form.created_on = query_row[7]
|
|
return form
|
|
|
|
@classmethod
|
|
def from_json(cls, json):
|
|
_m = f'{cls.__qualname__}.from_json'
|
|
form = cls()
|
|
if json is None: return form
|
|
form.id_contact_form = -1
|
|
form.email = json[cls.FLAG_EMAIL]
|
|
form.name_contact = json.get(cls.FLAG_NAME_CONTACT, 'No Contact Name')
|
|
form.name_company = json.get(cls.FLAG_NAME_COMPANY, 'No Company Name')
|
|
form.message = json.get(cls.FLAG_MESSAGE, 'No Message')
|
|
form.receive_marketing_communications = json.get(cls.FLAG_RECEIVE_MARKETING_COMMUNICATIONS, True)
|
|
form.active = av.input_bool(json.get(cls.FLAG_ACTIVE, True), cls.FLAG_ACTIVE, _m)
|
|
form.created_on = json.get(cls.FLAG_CREATED_ON, None)
|
|
return form
|
|
|
|
|
|
def to_json(self):
|
|
as_json = {
|
|
self.FLAG_CONTACT_FORM: self.id_contact_form
|
|
, self.FLAG_EMAIL: self.email
|
|
, self.FLAG_NAME_CONTACT: self.name_contact
|
|
, self.FLAG_NAME_COMPANY: self.name_company
|
|
, self.FLAG_MESSAGE: self.message
|
|
, self.FLAG_RECEIVE_MARKETING_COMMUNICATIONS: self.receive_marketing_communications
|
|
, self.FLAG_ACTIVE: self.active
|
|
, self.FLAG_CREATED_ON: self.created_on
|
|
}
|
|
return as_json
|
|
|
|
def __repr__(self):
|
|
return f'''
|
|
{self.__class__.__name__}(
|
|
{self.FLAG_CONTACT_FORM}: {self.id_contact_form}
|
|
{self.FLAG_EMAIL}: {self.email}
|
|
{self.FLAG_NAME_CONTACT}: {self.name_contact}
|
|
{self.FLAG_NAME_COMPANY}: {self.name_company}
|
|
{self.FLAG_MESSAGE}: {self.message}
|
|
{self.FLAG_RECEIVE_MARKETING_COMMUNICATIONS}: {self.receive_marketing_communications}
|
|
{self.FLAG_ACTIVE}: {self.active}
|
|
{self.FLAG_CREATED_ON}: {self.created_on}
|
|
)
|
|
'''
|
|
|
|
class Contact_Form_Temp(db.Model, Base):
|
|
__tablename__ = 'PH_Contact_Form_Temp'
|
|
__table_args__ = { 'extend_existing': True }
|
|
id_temp = db.Column(db.Integer, primary_key=True)
|
|
id_contact_form = db.Column(db.Integer)
|
|
email = db.Column(db.String(255))
|
|
name_contact = db.Column(db.String(255))
|
|
name_company = db.Column(db.String(255))
|
|
message = db.Column(db.Text)
|
|
receive_marketing_communications = db.Column(db.Boolean)
|
|
active = db.Column(db.Boolean)
|
|
created_on = db.Column(db.DateTime)
|
|
guid: str = db.Column(db.String(36))
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
@classmethod
|
|
def from_contact_form(cls, contact_form):
|
|
_m = f'{cls.__qualname__}.from_contact_form'
|
|
temp = cls()
|
|
temp.id_contact_form = contact_form.id_contact_form
|
|
temp.email = contact_form.email
|
|
temp.name_contact = contact_form.name_contact
|
|
temp.name_company = contact_form.name_company
|
|
temp.message = contact_form.message
|
|
temp.receive_marketing_communications = contact_form.receive_marketing_communications
|
|
temp.active = contact_form.active
|
|
temp.created_on = contact_form.created_on
|
|
return temp |