class Lead(db.Model): id = db.Column(db.Integer, db.Sequence('lead_id_seq'), primary_key=True) title = db.Column(db.String(100), nullable=True) first_name = db.Column(db.String(40), nullable=True) last_name = db.Column(db.String(40), nullable=False) company_name = db.Column(db.String(40), nullable=False) email = db.Column(db.String(120), nullable=True) phone = db.Column(db.String(20), nullable=True) mobile = db.Column(db.String(20), nullable=True) address_line = db.Column(db.String(40), nullable=True) addr_state = db.Column(db.String(40), nullable=True) addr_city = db.Column(db.String(40), nullable=True) post_code = db.Column(db.String(20), nullable=True) country = db.Column(db.String(40), nullable=True) notes = db.Column(db.String(200), nullable=True) lead_source_id = db.Column(db.Integer, db.ForeignKey('lead_source.id', ondelete='SET NULL'), nullable=True) lead_status_id = db.Column(db.Integer, db.ForeignKey('lead_status.id', ondelete='SET NULL'), nullable=True) owner_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='SET NULL'), nullable=True) date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) @staticmethod def get_by_id(lead_id): return Lead.query.filter_by(id=lead_id).first() def __repr__(self): return f"Lead('{self.last_name}', '{self.email}', '{self.company_name}')"
class AppConfig(db.Model): id = db.Column(db.Integer, db.Sequence('app_config_id_seq'), primary_key=True) default_currency = db.Column(db.Integer, db.ForeignKey('currency.id', ondelete='SET NULL'), nullable=False) default_timezone = db.Column(db.Integer, db.ForeignKey('time_zone.id', ondelete='SET NULL'), nullable=False) date_format = db.Column(db.String(20), nullable=True, default='%Y-%m-d%') address_format = db.Column(db.String(200), nullable=True, default=DEFAULT_ADDRESS) smtp_server = db.Column(db.String(50), nullable=True) smtp_encryption = db.Column(db.String(5), nullable=True) smtp_port = db.Column(db.String(5), nullable=True) smtp_charset = db.Column(db.String(5), nullable=True, default='utf-8') sender_name = db.Column(db.String(50), nullable=True) sender_email = db.Column(db.String(100), nullable=True) currency = db.relationship('Currency', backref='app_config', uselist=False, lazy=True) time_zone = db.relationship('TimeZone', backref='app_config', uselist=False, lazy=True)
class Resource(db.Model): id = db.Column(db.Integer, db.Sequence('resource_id_seq'), primary_key=True) name = db.Column(db.String(20), nullable=False) can_view = db.Column(db.Boolean, nullable=False) can_edit = db.Column(db.Boolean, nullable=False) can_create = db.Column(db.Boolean, nullable=False) can_delete = db.Column(db.Boolean, nullable=False)
class Contact(db.Model): __tablename__ = "contact" id = db.Column(db.Integer, db.Sequence('contact_id_seq'), primary_key=True) first_name = db.Column(db.String(20)) last_name = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) avatar = db.Column(db.String(25)) phone = db.Column(db.String(20), nullable=False) mobile = db.Column(db.String(20)) address_line = db.Column(db.String(40)) addr_state = db.Column(db.String(40)) addr_city = db.Column(db.String(40)) post_code = db.Column(db.String(20)) country = db.Column(db.String(40)) notes = db.Column(db.String(200)) account_id = db.Column(db.Integer, db.ForeignKey('account.id', ondelete='cascade'), nullable=False) owner_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='SET NULL'), nullable=True) deals = db.relationship('Deal', backref='contact', lazy=True) date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) @staticmethod def contact_list_query(): account = request.args.get('acc', None, type=int) if current_user.is_admin: contacts = Contact.query\ .filter(Contact.account_id == account if account else True) else: contacts = Contact.query \ .filter(Contact.account_id == account if account else True) \ .filter(Contact.owner_id == current_user.id) return contacts @staticmethod def get_label(contact): return contact.first_name + ' ' + contact.last_name @staticmethod def get_contact(contact_id): return Contact.query.filter_by(id=contact_id).first() def get_contact_name(self): if self.last_name: return self.first_name + ' ' + self.last_name else: return None def __repr__(self): return f"Account('{self.last_name}', '{self.email}', '{self.phone}')"
class User(db.Model): id = db.Column(db.Integer, db.Sequence('user_id_seq'), primary_key=True) first_name = db.Column(db.String(20), nullable=True) last_name = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) avatar = db.Column(db.String(25), nullable=True) password = db.Column(db.String(60), nullable=False) is_admin = db.Column(db.Boolean, nullable=False, default=False) is_first_login = db.Column(db.Boolean, nullable=False, default=True) is_user_active = db.Column(db.Boolean, nullable=False, default=False) def __repr__(self): return f"User('{self.first_name}', '{self.last_name}', '{self.email}')"
class LeadStatus(db.Model): id = db.Column(db.Integer, db.Sequence('lead_status_id_seq'), primary_key=True) status_name = db.Column(db.String(40), unique=True, nullable=False) leads = db.relationship('Lead', backref='status', lazy=True) @staticmethod def lead_status_query(): return LeadStatus.query @staticmethod def get_by_id(lead_status_id): return LeadStatus.query.filter_by(id=lead_status_id).first() def __repr__(self): return f"LeadStatus('{self.status_name}')"
class Account(db.Model): id = db.Column(db.Integer, db.Sequence('account_id_seq'), primary_key=True) name = db.Column(db.String(100), nullable=False) website = db.Column(db.String(100)) email = db.Column(db.String(120), nullable=False) address_line = db.Column(db.String(40)) addr_state = db.Column(db.String(40)) addr_city = db.Column(db.String(40)) post_code = db.Column(db.String(20)) country = db.Column(db.String(40)) phone = db.Column(db.String(20)) is_active = db.Column(db.Boolean, default=True) owner_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='SET NULL'), nullable=True) contacts = db.relationship('Contact', cascade='all,delete', backref='account', passive_deletes=True, lazy=True) deals = db.relationship('Deal', cascade='all,delete', backref='account', passive_deletes=True, lazy=True) notes = db.Column(db.String(200)) date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) @staticmethod def account_list_query(): if current_user.is_admin: return Account.query else: return Account.query.filter_by(owner_id=current_user.id) @staticmethod def get_label(account): return account.name @staticmethod def get_account(account_id): return Account.query.filter_by(id=account_id).first() def __repr__(self): return f"Account('{self.name}', '{self.email}', '{self.website}')"
class Currency(db.Model): id = db.Column(db.Integer, db.Sequence('currency_id_seq'), primary_key=True) name = db.Column(db.String(100), nullable=False) iso_code = db.Column(db.String(10), nullable=False) symbol = db.Column(db.String(10), nullable=True) @staticmethod def get_list_query(): return Currency.query @staticmethod def get_currency_by_id(currency_id): return Currency.query.filter_by(id=currency_id).first() def __repr__(self): return f"Currency('{self.name}', '{self.iso_code}')"
class TimeZone(db.Model): id = db.Column(db.Integer, db.Sequence('timezone_id_seq'), primary_key=True) name = db.Column(db.String(100), nullable=False) @staticmethod def get_list_query(): return TimeZone.query @staticmethod def get_tz_by_id(tz_id): return TimeZone.query.filter_by(id=tz_id).first() @staticmethod def get_tz_by_name(tz_name): return TimeZone.query.filter_by(name=tz_name).first() def __repr__(self): return f"TimeZone('{self.name}')"
class Deal(db.Model): id = db.Column(db.Integer, db.Sequence('deal_id_seq'), primary_key=True) title = db.Column(db.String(100), nullable=True) expected_close_price = db.Column(db.Float, nullable=False) expected_close_date = db.Column(db.DateTime, nullable=True) deal_stage_id = db.Column(db.Integer, db.ForeignKey('deal_stage.id', ondelete='SET NULL'), nullable=False) account_id = db.Column(db.Integer, db.ForeignKey('account.id', ondelete='cascade'), nullable=False) contact_id = db.Column(db.Integer, db.ForeignKey('contact.id', ondelete='SET NULL'), nullable=True) owner_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='SET NULL'), nullable=True) notes = db.Column(db.String(200), nullable=True) date_created = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) deal_stage = db.relationship('DealStage', backref='deal', uselist=False, lazy=True) owner = db.relationship('User', backref='deal', uselist=False, lazy=True) def is_expired(self): today = datetime.today() if self.expected_close_date < today: return True return False @staticmethod def get_deal(deal_id): return Deal.query.filter_by(id=deal_id).first() def __repr__(self): return f"Deal('{self.title}', '{self.deal_stage_id}', '{self.account_id}', '{self.contact_id}', '{self.owner_id}')"
class User(db.Model, UserMixin): id = db.Column(db.Integer, db.Sequence('user_id_seq'), primary_key=True) first_name = db.Column(db.String(20), nullable=True) last_name = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) avatar = db.Column(db.String(25), nullable=True) password = db.Column(db.String(60), nullable=False) is_admin = db.Column(db.Boolean, nullable=False, default=False) is_first_login = db.Column(db.Boolean, nullable=False, default=True) is_user_active = db.Column(db.Boolean, nullable=False, default=False) role_id = db.Column(db.Integer, db.ForeignKey('role.id', ondelete='SET NULL'), nullable=True) leads = db.relationship('Lead', backref='owner', lazy=True) accounts = db.relationship('Account', backref='account_owner', lazy=True) contacts = db.relationship('Contact', backref='contact_owner', lazy=True) deals = db.relationship('Deal', backref='deal_owner', lazy=True) @staticmethod def get_label(user): return user.get_name() @staticmethod def user_list_query(): return User.query @staticmethod def get_current_user(): return User.query.filter_by(id=current_user.id).first() @staticmethod def get_by_id(user_id): return User.query.filter_by(id=user_id).first() def get_name(self): return self.first_name + ' ' + self.last_name def __repr__(self): return f"User('{self.first_name}', '{self.last_name}', '{self.email}', '{self.avatar}')"
class DealStage(db.Model): id = db.Column(db.Integer, db.Sequence('deal_stage_id_seq'), primary_key=True) stage_name = db.Column(db.String(20), nullable=False) display_order = db.Column(db.Integer, nullable=False) close_type = db.Column(db.String(10), nullable=True, default=None) deals = db.relationship('Deal', backref='dealstage', lazy=True) @staticmethod def deal_stage_list_query(): return DealStage.query @staticmethod def get_label(deal_stage): return deal_stage.stage_name @staticmethod def get_deal_stage(deal_stage_id): return DealStage.query.filter_by(id=deal_stage_id).first() def __repr__(self): return f"DealStage('{self.stage_name}')"
class Role(db.Model): id = db.Column(db.Integer, db.Sequence('role_id_seq'), primary_key=True) name = db.Column(db.String(20), unique=True, nullable=False) user = db.relationship('User', uselist=False, backref='role', lazy=True) resources = db.relationship('Resource', secondary=roles_resources, backref=db.backref('resources', lazy='dynamic')) @staticmethod def get_by_name(name): return Role.query.filter_by(name=name).first() @staticmethod def get_by_id(role_id): return Role.query.filter_by(id=role_id).first() def set_permissions(self, resources): for ind in range(len(resources)): self.resources[ind].can_view = resources[ind].can_view.data self.resources[ind].can_create = resources[ind].can_create.data self.resources[ind].can_edit = resources[ind].can_edit.data self.resources[ind].can_delete = resources[ind].can_delete.data