"""Database main objects, functions and constants."""
from __future__ import annotations
from os import makedirs
from os.path import join, exists
from shutil import copy2
from json import loads
from sqlalchemy import Column
from ..lib.i18n import translate_field
# =============================================================================
[docs]class DBBaseClass():
"""Base class for prime SQLAlchemy table."""
suffix: str = 'cio'
attachments_dir: str | None = None
_settings_tabs: tuple | None = None
i18n_label: Column | str | None = None
i18n_description: Column | None = None
attachments_key: Column | None = None
picture: Column | None = None
# -------------------------------------------------------------------------
[docs] def label(self, request):
"""Return a translated label.
:type request: pyramid.request.Request
:param request:
Current request.
:rtype: str
"""
return translate_field(request, loads(self.i18n_label)) \
if self.i18n_label else ''
# -------------------------------------------------------------------------
[docs] def description(self, request):
"""Return a translated description.
:type request: pyramid.request.Request
:param request:
Current request.
:rtype: str
"""
# pylint: disable = using-constant-test
return translate_field(request, self.i18n_description) \
if self.description else ''
# -------------------------------------------------------------------------
[docs] def attachments2directory(self, attachments, directory):
"""Copy from attachments directory the file corresponding to the user.
:param str attachments:
Absolute path to the attachments directory.
:param str directory:
The backup directory.
"""
# pylint: disable = no-member
if not self.attachments_dir or not self.attachments_key or \
not self.picture:
return
picture = join(
attachments, self.attachments_dir, self.attachments_key,
self.picture)
if not exists(picture):
return
destination = join(
directory, self.attachments_dir, self.attachments_key)
if not exists(destination):
makedirs(destination)
copy2(picture, destination)
# -------------------------------------------------------------------------
[docs] @classmethod
def settings_tabs(cls, request):
"""Return a tuple of tab labels to view or edit the object.
:type request: pyramid.request.Request
:param request:
Current request.
:rtype: tuple
"""
# pylint: disable = unused-argument
return cls._settings_tabs