Skip to content

Commit e6809dd

Browse files
committed
refactor: Change Primary Key from ID to UUID for Locale
1 parent d86e917 commit e6809dd

File tree

6 files changed

+40
-29
lines changed

6 files changed

+40
-29
lines changed

packages/dsw-database/dsw/database/database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Database:
8888
' enabled is TRUE AND '
8989
' tenant_uuid = %(tenant_uuid)s;')
9090
SELECT_LOCALE = ('SELECT * FROM locale '
91-
'WHERE id = %(locale_id)s AND tenant_uuid = %(tenant_uuid)s;')
91+
'WHERE uuid = %(locale_uuid)s AND tenant_uuid = %(tenant_uuid)s;')
9292

9393
def __init__(self, cfg: DatabaseConfig, connect: bool = True,
9494
with_queue: bool = True):
@@ -481,22 +481,22 @@ def get_default_locale(self, tenant_uuid: str) -> DBLocale | None:
481481
before=tenacity.before_log(LOG, logging.DEBUG),
482482
after=tenacity.after_log(LOG, logging.DEBUG),
483483
)
484-
def get_locale(self, locale_id: str, tenant_uuid: str) -> DBLocale | None:
484+
def get_locale(self, locale_uuid: str, tenant_uuid: str) -> DBLocale | None:
485485
if not self._check_table_exists(table_name='locale'):
486486
return None
487487
with self.conn_query.new_cursor(use_dict=True) as cursor:
488488
try:
489489
cursor.execute(
490490
query=self.SELECT_LOCALE,
491-
params={'locale_id': locale_id, 'tenant_uuid': tenant_uuid},
491+
params={'locale_uuid': locale_uuid, 'tenant_uuid': tenant_uuid},
492492
)
493493
result = cursor.fetchone()
494494
if result is None:
495495
return None
496496
return DBLocale.from_dict_row(data=result)
497497
except Exception as e:
498498
LOG.warning('Could not retrieve locale "%s" for tenant "%s": %s',
499-
locale_id, tenant_uuid, str(e))
499+
locale_uuid, tenant_uuid, str(e))
500500
return None
501501

502502
@tenacity.retry(

packages/dsw-database/dsw/database/model.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,10 @@ def from_dict_row(data: dict):
408408
class DBLocale:
409409
TABLE_NAME = 'locale'
410410

411-
id: str
411+
uuid: str
412+
organization_id: str
413+
locale_id: str
414+
version: str
412415
name: str
413416
code: str
414417
default_locale: bool
@@ -417,13 +420,20 @@ class DBLocale:
417420
@staticmethod
418421
def from_dict_row(data: dict):
419422
return DBLocale(
420-
id=str(data['id']),
423+
uuid=str(data['uuid']),
424+
organization_id=data['organization_id'],
425+
locale_id=data['locale_id'],
426+
version=data['version'],
421427
name=data['name'],
422428
code=data['code'],
423429
default_locale=data['default_locale'],
424430
enabled=data['enabled'],
425431
)
426432

433+
@property
434+
def id(self) -> str:
435+
return f'{self.organization_id}:{self.locale_id}:{self.version}'
436+
427437

428438
@dataclasses.dataclass
429439
class DBInstanceConfigMail:

packages/dsw-mailer/dsw/mailer/mailer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def run_once(self):
9494
queue = self._run_preparation()
9595
queue.run_once()
9696

97-
def _get_locale_id(self, recipient_uuid: str, tenant_uuid: str) -> str | None:
97+
def _get_locale_uuid(self, recipient_uuid: str, tenant_uuid: str) -> str | None:
9898
app_ctx = Context.get().app
9999
user = app_ctx.db.get_user(
100100
user_uuid=recipient_uuid,
@@ -116,13 +116,13 @@ def _get_msg_request(self, command: PersistentCommand) -> MessageRequest:
116116
if len(mc.recipients) == 0:
117117
raise RuntimeError('No recipients specified')
118118
first_recipient = mc.recipients[0]
119-
locale_id = None
119+
locale_uuid = None
120120
if first_recipient.uuid is not None:
121-
locale_id = self._get_locale_id(first_recipient.uuid, command.tenant_uuid)
121+
locale_uuid = self._get_locale_uuid(first_recipient.uuid, command.tenant_uuid)
122122
return mc.to_request(
123123
msg_id=command.uuid,
124124
trigger='PersistentComment',
125-
locale_id=locale_id,
125+
locale_uuid=locale_uuid,
126126
)
127127

128128
def _get_mail_config(self, command: PersistentCommand) -> MailConfig:
@@ -225,12 +225,12 @@ def __init__(self, *, recipients: list[MessageRecipient], mode: str,
225225
self.cmd_uuid = cmd_uuid
226226
self._enrich_context()
227227

228-
def to_request(self, msg_id: str, locale_id: str | None, trigger: str) -> MessageRequest:
228+
def to_request(self, msg_id: str, locale_uuid: str | None, trigger: str) -> MessageRequest:
229229
rq = MessageRequest(
230230
message_id=msg_id,
231231
template_name=f'{self.mode}:{self.template}',
232232
tenant_uuid=self.tenant_uuid,
233-
locale_id=locale_id,
233+
locale_uuid=locale_uuid,
234234
trigger=trigger,
235235
ctx=self.ctx,
236236
recipients=self.recipients,

packages/dsw-mailer/dsw/mailer/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ def from_dict(cls, data: dict):
203203

204204
class MessageRequest:
205205

206-
def __init__(self, *, message_id: str, locale_id: str | None, template_name: str,
206+
def __init__(self, *, message_id: str, locale_uuid: str | None, template_name: str,
207207
trigger: str, ctx: dict, recipients: list[MessageRecipient], tenant_uuid: str,
208208
style: StyleConfig | None = None):
209209
self.id = message_id
210210
self.template_name = template_name
211211
self.tenant_uuid = tenant_uuid
212-
self.locale_id = locale_id
212+
self.locale_uuid = locale_uuid
213213
self.trigger = trigger
214214
self.ctx = ctx
215215
self.recipients = recipients
@@ -228,7 +228,7 @@ def load_from_file(data: dict) -> 'MessageRequest':
228228
message_id=data['id'],
229229
template_name=data['type'],
230230
tenant_uuid=data['tenantUuid'],
231-
locale_id=data['localeId'],
231+
locale_uuid=data['localeUuid'],
232232
trigger=data.get('trigger', 'input_file'),
233233
ctx=data.get('ctx', {}),
234234
recipients=data.get('recipients', []),

packages/dsw-mailer/dsw/mailer/templates.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,40 +201,41 @@ def _install_translations(self, translations: gettext.GNUTranslations):
201201
# pylint: disable-next=no-member
202202
self.j2_env.install_gettext_translations(translations)
203203

204-
def _load_locale(self, tenant_uuid: str, locale_id: str | None, app_ctx,
204+
def _load_locale(self, tenant_uuid: str, locale_uuid: str | None, app_ctx,
205205
locale_root_dir: pathlib.Path):
206-
LOG.info('Loading locale: %s (for tenant %s)', locale_id, tenant_uuid)
206+
LOG.info('Loading locale: %s (for tenant %s)', locale_uuid, tenant_uuid)
207207
self._uninstall_translations()
208-
if locale_id is None:
208+
if locale_uuid is None:
209209
LOG.info('No locale specified - using null translations')
210210
self._install_null_translations()
211-
elif locale_id == self.DEFAULT_LOCALE:
212-
LOG.info('Default locale - using null translations')
213-
self._install_null_translations()
214211
else:
215212
# fetch locale from DB
216213
locale = app_ctx.db.get_locale(
217214
tenant_uuid=tenant_uuid,
218-
locale_id=locale_id,
215+
locale_uuid=locale_uuid,
219216
)
217+
if locale.id == self.DEFAULT_LOCALE:
218+
LOG.info('Locale is default locale - using null translations')
219+
self._install_null_translations()
220+
return
220221
if locale is None:
221222
LOG.error('Could not find locale for tenant %s', tenant_uuid)
222-
raise RuntimeError(f'Locale not found in DB: {locale_id}')
223+
raise RuntimeError(f'Locale not found in DB: {locale_uuid}')
223224
# fetch locale from S3
224225
locale_dir = locale_root_dir / locale.code / 'LC_MESSAGES'
225226
locale_dir.mkdir(parents=True, exist_ok=True)
226227
locale_po_path = locale_dir / 'default.po'
227228
locale_mo_path = locale_dir / 'default.mo'
228229
downloaded = app_ctx.s3.download_locale(
229230
tenant_uuid=tenant_uuid,
230-
locale_id=locale_id,
231+
locale_uuid=locale_uuid,
231232
file_name='mail.po',
232233
target_path=locale_po_path,
233234
)
234235
if not downloaded:
235236
LOG.error('Cannot download locale file (mail.po) from %s to %s',
236-
locale_id, locale_po_path)
237-
raise RuntimeError(f'Failed to download locale file (mail.po) from {locale_id}')
237+
locale_uuid, locale_po_path)
238+
raise RuntimeError(f'Failed to download locale file (mail.po) from {locale_uuid}')
238239
LOG.debug('Saved PO file to %s', locale_po_path)
239240
# convert po to mo
240241
po = polib.pofile(locale_po_path.absolute().as_posix())
@@ -256,7 +257,7 @@ def render(self, rq: MessageRequest, cfg: MailConfig, app_ctx) -> MailMessage:
256257
with tempfile.TemporaryDirectory() as tmpdir:
257258
try:
258259
locale_root_dir = pathlib.Path(tmpdir) / 'locale'
259-
self._load_locale(rq.tenant_uuid, rq.locale_id, app_ctx, locale_root_dir)
260+
self._load_locale(rq.tenant_uuid, rq.locale_uuid, app_ctx, locale_root_dir)
260261
except Exception as e:
261262
LOG.warning('Cannot load locale for tenant %s: %s', rq.tenant_uuid, str(e))
262263
LOG.warning('Rendering without locale')

packages/dsw-storage/dsw/storage/s3storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ def download_template_asset(self, *, tenant_uuid: str, template_id: str,
121121
before=tenacity.before_log(LOG, logging.DEBUG),
122122
after=tenacity.after_log(LOG, logging.DEBUG),
123123
)
124-
def download_locale(self, *, tenant_uuid: str, locale_id: str,
124+
def download_locale(self, *, tenant_uuid: str, locale_uuid: str,
125125
file_name: str, target_path: pathlib.Path) -> bool:
126126
return self._download_file(
127127
tenant_uuid=tenant_uuid,
128-
file_name=f'locales/{locale_id}/{file_name}',
128+
file_name=f'locales/{locale_uuid}/{file_name}',
129129
target_path=target_path,
130130
)
131131

0 commit comments

Comments
 (0)