oxutils 0.1.2__py3-none-any.whl → 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- oxutils/__init__.py +1 -1
- oxutils/audit/settings.py +1 -16
- oxutils/audit/utils.py +22 -0
- oxutils/conf.py +1 -3
- oxutils/constants.py +2 -0
- oxutils/context/__init__.py +0 -0
- oxutils/context/site_name_processor.py +11 -0
- oxutils/currency/__init__.py +0 -0
- oxutils/currency/admin.py +57 -0
- oxutils/currency/apps.py +7 -0
- oxutils/currency/controllers.py +79 -0
- oxutils/currency/enums.py +7 -0
- oxutils/currency/migrations/0001_initial.py +41 -0
- oxutils/currency/migrations/__init__.py +0 -0
- oxutils/currency/models.py +100 -0
- oxutils/currency/schemas.py +38 -0
- oxutils/currency/tests.py +3 -0
- oxutils/currency/utils.py +69 -0
- oxutils/functions.py +5 -2
- oxutils/logger/receivers.py +0 -2
- oxutils/oxiliere/__init__.py +0 -0
- oxutils/oxiliere/admin.py +3 -0
- oxutils/oxiliere/apps.py +6 -0
- oxutils/oxiliere/cacheops.py +7 -0
- oxutils/oxiliere/caches.py +33 -0
- oxutils/oxiliere/controllers.py +36 -0
- oxutils/oxiliere/enums.py +10 -0
- oxutils/oxiliere/management/__init__.py +0 -0
- oxutils/oxiliere/management/commands/__init__.py +0 -0
- oxutils/oxiliere/management/commands/init_oxiliere_system.py +86 -0
- oxutils/oxiliere/middleware.py +97 -0
- oxutils/oxiliere/migrations/__init__.py +0 -0
- oxutils/oxiliere/models.py +55 -0
- oxutils/oxiliere/permissions.py +104 -0
- oxutils/oxiliere/schemas.py +65 -0
- oxutils/oxiliere/settings.py +17 -0
- oxutils/oxiliere/tests.py +3 -0
- oxutils/oxiliere/utils.py +76 -0
- oxutils/pdf/__init__.py +10 -0
- oxutils/pdf/printer.py +81 -0
- oxutils/pdf/utils.py +94 -0
- oxutils/pdf/views.py +208 -0
- oxutils/settings.py +2 -0
- oxutils/users/__init__.py +0 -0
- oxutils/users/admin.py +3 -0
- oxutils/users/apps.py +6 -0
- oxutils/users/migrations/__init__.py +0 -0
- oxutils/users/models.py +88 -0
- oxutils/users/tests.py +3 -0
- oxutils/users/utils.py +15 -0
- {oxutils-0.1.2.dist-info → oxutils-0.1.6.dist-info}/METADATA +99 -11
- oxutils-0.1.6.dist-info/RECORD +88 -0
- {oxutils-0.1.2.dist-info → oxutils-0.1.6.dist-info}/WHEEL +1 -1
- oxutils/locale/fr/LC_MESSAGES/django.mo +0 -0
- oxutils-0.1.2.dist-info/RECORD +0 -45
oxutils/__init__.py
CHANGED
oxutils/audit/settings.py
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
1
|
# Oxiliere Audit settings
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
AUDITLOG_MASK_TRACKING_FIELDS = (
|
|
5
|
-
"password",
|
|
6
|
-
"api_key",
|
|
7
|
-
"secret_token",
|
|
8
|
-
"token",
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
AUDITLOG_EXCLUDE_TRACKING_FIELDS = (
|
|
12
|
-
"created_at",
|
|
13
|
-
"updated_at",
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
CID_GENERATE = False
|
|
17
|
-
|
|
18
|
-
AUDITLOG_CID_GETTER = "cid.locals.get_cid"
|
|
3
|
+
AUDITLOG_CID_GETTER = "oxutils.audit.utils.get_request_id"
|
|
19
4
|
AUDITLOG_LOGENTRY_MODEL = "auditlog.LogEntry"
|
oxutils/audit/utils.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Utility functions for audit logging.
|
|
3
|
+
"""
|
|
4
|
+
import structlog
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_request_id():
|
|
8
|
+
"""
|
|
9
|
+
Get the request_id from django-structlog context.
|
|
10
|
+
|
|
11
|
+
This function retrieves the request_id that was set by
|
|
12
|
+
django-structlog's RequestMiddleware and returns it for use
|
|
13
|
+
in auditlog's correlation ID field.
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
str: The request_id from the current request context, or None if not available.
|
|
17
|
+
"""
|
|
18
|
+
try:
|
|
19
|
+
context = structlog.contextvars.get_contextvars()
|
|
20
|
+
return context.get('request_id')
|
|
21
|
+
except Exception:
|
|
22
|
+
return None
|
oxutils/conf.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
UTILS_APPS = (
|
|
2
2
|
'django_structlog',
|
|
3
3
|
'auditlog',
|
|
4
|
-
'cid.apps.CidAppConfig',
|
|
5
4
|
'django_celery_results',
|
|
6
5
|
'oxutils.audit',
|
|
7
6
|
)
|
|
8
7
|
|
|
9
8
|
AUDIT_MIDDLEWARE = (
|
|
10
|
-
'cid.middleware.CidMiddleware',
|
|
11
|
-
'auditlog.middleware.AuditlogMiddleware',
|
|
12
9
|
'django_structlog.middlewares.RequestMiddleware',
|
|
10
|
+
'auditlog.middleware.AuditlogMiddleware',
|
|
13
11
|
)
|
oxutils/constants.py
ADDED
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from django.contrib import admin
|
|
2
|
+
from .models import CurrencyState, Currency
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class CurrencyInline(admin.TabularInline):
|
|
6
|
+
model = Currency
|
|
7
|
+
extra = 0
|
|
8
|
+
readonly_fields = ('id', 'code', 'rate')
|
|
9
|
+
can_delete = False
|
|
10
|
+
|
|
11
|
+
def has_add_permission(self, request, obj=None):
|
|
12
|
+
return False
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@admin.register(CurrencyState)
|
|
16
|
+
class CurrencyStateAdmin(admin.ModelAdmin):
|
|
17
|
+
list_display = ('id', 'source', 'currency_count', 'created_at', 'updated_at')
|
|
18
|
+
list_filter = ('source', 'created_at')
|
|
19
|
+
readonly_fields = ('id', 'created_at', 'updated_at')
|
|
20
|
+
search_fields = ('id', 'source')
|
|
21
|
+
ordering = ('-created_at',)
|
|
22
|
+
inlines = [CurrencyInline]
|
|
23
|
+
|
|
24
|
+
def currency_count(self, obj):
|
|
25
|
+
return obj.currencies.count()
|
|
26
|
+
currency_count.short_description = 'Currencies'
|
|
27
|
+
|
|
28
|
+
def has_add_permission(self, request):
|
|
29
|
+
return False
|
|
30
|
+
|
|
31
|
+
def has_delete_permission(self, request, obj=None):
|
|
32
|
+
return request.user.is_superuser
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@admin.register(Currency)
|
|
36
|
+
class CurrencyAdmin(admin.ModelAdmin):
|
|
37
|
+
list_display = ('id', 'code', 'rate', 'state_source', 'state_created_at')
|
|
38
|
+
list_filter = ('code', 'state__source', 'state__created_at')
|
|
39
|
+
readonly_fields = ('id', 'code', 'rate', 'state')
|
|
40
|
+
search_fields = ('code', 'state__id')
|
|
41
|
+
ordering = ('code',)
|
|
42
|
+
|
|
43
|
+
def state_source(self, obj):
|
|
44
|
+
return obj.state.source
|
|
45
|
+
state_source.short_description = 'Source'
|
|
46
|
+
state_source.admin_order_field = 'state__source'
|
|
47
|
+
|
|
48
|
+
def state_created_at(self, obj):
|
|
49
|
+
return obj.state.created_at
|
|
50
|
+
state_created_at.short_description = 'State Created'
|
|
51
|
+
state_created_at.admin_order_field = 'state__created_at'
|
|
52
|
+
|
|
53
|
+
def has_add_permission(self, request):
|
|
54
|
+
return False
|
|
55
|
+
|
|
56
|
+
def has_delete_permission(self, request, obj=None):
|
|
57
|
+
return request.user.is_superuser
|
oxutils/currency/apps.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from django.http import HttpRequest
|
|
2
|
+
from django.core.exceptions import ObjectDoesNotExist
|
|
3
|
+
from ninja_extra import (
|
|
4
|
+
ControllerBase,
|
|
5
|
+
api_controller,
|
|
6
|
+
http_get,
|
|
7
|
+
)
|
|
8
|
+
from ninja_extra.pagination import (
|
|
9
|
+
paginate, PageNumberPaginationExtra, PaginatedResponseSchema
|
|
10
|
+
)
|
|
11
|
+
from ninja.errors import HttpError
|
|
12
|
+
from uuid import UUID
|
|
13
|
+
import structlog
|
|
14
|
+
from oxutils.currency.models import CurrencyState
|
|
15
|
+
from oxutils.currency.schemas import (
|
|
16
|
+
CurrencyStateSchema,
|
|
17
|
+
CurrencyStateDetailSchema,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
logger = structlog.get_logger(__name__)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@api_controller('/currency', tags=['Currency'], auth=None)
|
|
25
|
+
class CurrencyController(ControllerBase):
|
|
26
|
+
|
|
27
|
+
@http_get('/states', response=PaginatedResponseSchema[CurrencyStateSchema])
|
|
28
|
+
@paginate(PageNumberPaginationExtra, page_size=20)
|
|
29
|
+
def list_states(self, request: HttpRequest):
|
|
30
|
+
return CurrencyState.objects.all().order_by('-created_at')
|
|
31
|
+
|
|
32
|
+
@http_get('/states/latest', response=CurrencyStateDetailSchema)
|
|
33
|
+
def get_latest_state(self, request: HttpRequest):
|
|
34
|
+
try:
|
|
35
|
+
state = CurrencyState.objects.latest()
|
|
36
|
+
except ObjectDoesNotExist:
|
|
37
|
+
logger.error("currency_state_not_found", message="No currency state found in database")
|
|
38
|
+
raise HttpError(404, "No currency state found in database")
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
'id': state.id,
|
|
42
|
+
'source': state.source,
|
|
43
|
+
'created_at': state.created_at,
|
|
44
|
+
'updated_at': state.updated_at,
|
|
45
|
+
'currencies': {c.code: float(c.rate) for c in state.currencies.all()}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@http_get('/states/{state_id}', response=CurrencyStateDetailSchema)
|
|
49
|
+
def get_state(self, request: HttpRequest, state_id: UUID):
|
|
50
|
+
state = CurrencyState.objects.prefetch_related('currencies').get(id=state_id)
|
|
51
|
+
return {
|
|
52
|
+
'id': state.id,
|
|
53
|
+
'source': state.source,
|
|
54
|
+
'created_at': state.created_at,
|
|
55
|
+
'updated_at': state.updated_at,
|
|
56
|
+
'currencies': {c.code: float(c.rate) for c in state.currencies.all()}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@http_get('/rates', response=dict[str, float])
|
|
60
|
+
def get_current_rates(self, request: HttpRequest):
|
|
61
|
+
try:
|
|
62
|
+
state = CurrencyState.objects.latest()
|
|
63
|
+
except ObjectDoesNotExist:
|
|
64
|
+
logger.error("currency_state_not_found", message="No currency state found in database")
|
|
65
|
+
raise HttpError(404, "No currency rates available")
|
|
66
|
+
|
|
67
|
+
currencies = state.currencies.all()
|
|
68
|
+
return {c.code: float(c.rate) for c in currencies}
|
|
69
|
+
|
|
70
|
+
@http_get('/rates/{code}', response=dict[str, float])
|
|
71
|
+
def get_rate_by_code(self, request: HttpRequest, code: str):
|
|
72
|
+
try:
|
|
73
|
+
state = CurrencyState.objects.latest()
|
|
74
|
+
currency = state.currencies.get(code=code.upper())
|
|
75
|
+
except ObjectDoesNotExist:
|
|
76
|
+
logger.error("currency_rate_not_found", code=code.upper())
|
|
77
|
+
raise HttpError(404, f"Currency rate for {code.upper()} not found")
|
|
78
|
+
|
|
79
|
+
return {currency.code: float(currency.rate)}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Generated by Django 5.2.9 on 2025-12-19 14:34
|
|
2
|
+
|
|
3
|
+
import django.db.models.deletion
|
|
4
|
+
import uuid
|
|
5
|
+
from django.db import migrations, models
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Migration(migrations.Migration):
|
|
9
|
+
|
|
10
|
+
initial = True
|
|
11
|
+
|
|
12
|
+
dependencies = [
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
operations = [
|
|
16
|
+
migrations.CreateModel(
|
|
17
|
+
name='CurrencyState',
|
|
18
|
+
fields=[
|
|
19
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, help_text='Unique identifier for this record', primary_key=True, serialize=False)),
|
|
20
|
+
('created_at', models.DateTimeField(auto_now_add=True, help_text='Date and time when this record was created')),
|
|
21
|
+
('updated_at', models.DateTimeField(auto_now=True, help_text='Date and time when this record was last updated')),
|
|
22
|
+
('source', models.CharField(choices=[('bcc', 'BCC'), ('oxr', 'Open Exchange Rates')], max_length=10)),
|
|
23
|
+
],
|
|
24
|
+
options={
|
|
25
|
+
'abstract': False,
|
|
26
|
+
},
|
|
27
|
+
),
|
|
28
|
+
migrations.CreateModel(
|
|
29
|
+
name='Currency',
|
|
30
|
+
fields=[
|
|
31
|
+
('id', models.UUIDField(default=uuid.uuid4, editable=False, help_text='Unique identifier for this record', primary_key=True, serialize=False)),
|
|
32
|
+
('code', models.CharField(max_length=10)),
|
|
33
|
+
('rate', models.DecimalField(decimal_places=4, max_digits=10)),
|
|
34
|
+
('state', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='currencies', to='currency.currencystate')),
|
|
35
|
+
],
|
|
36
|
+
options={
|
|
37
|
+
'ordering': ['code'],
|
|
38
|
+
'indexes': [models.Index(fields=['code', 'state'], name='currency_cu_code_c68344_idx')],
|
|
39
|
+
},
|
|
40
|
+
),
|
|
41
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from django.db import models
|
|
3
|
+
from django.db import transaction
|
|
4
|
+
import structlog
|
|
5
|
+
from oxutils.models import (
|
|
6
|
+
UUIDPrimaryKeyMixin,
|
|
7
|
+
TimestampMixin,
|
|
8
|
+
)
|
|
9
|
+
from .enums import CurrencySource
|
|
10
|
+
from .utils import load_rates
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
logger = structlog.get_logger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
AVAILABLES_CURRENCIES = [
|
|
18
|
+
"AOA",
|
|
19
|
+
"AUD",
|
|
20
|
+
"BIF",
|
|
21
|
+
"CAD",
|
|
22
|
+
"CHF",
|
|
23
|
+
"CNY",
|
|
24
|
+
"EUR",
|
|
25
|
+
"GBP",
|
|
26
|
+
"JPY",
|
|
27
|
+
"RWF",
|
|
28
|
+
"TZS",
|
|
29
|
+
"UGX",
|
|
30
|
+
"USD",
|
|
31
|
+
"XAF",
|
|
32
|
+
"XDR",
|
|
33
|
+
"ZAR",
|
|
34
|
+
"ZMW"
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class CurrencyStateManager(models.Manager):
|
|
39
|
+
def latest(self):
|
|
40
|
+
return self.get_queryset().prefetch_related("currencies").latest("created_at")
|
|
41
|
+
|
|
42
|
+
class CurrencyState(UUIDPrimaryKeyMixin, TimestampMixin):
|
|
43
|
+
source = models.CharField(max_length=10, choices=CurrencySource.choices)
|
|
44
|
+
objects = CurrencyStateManager()
|
|
45
|
+
|
|
46
|
+
@classmethod
|
|
47
|
+
def sync(cls) -> Optional['CurrencyState']:
|
|
48
|
+
rates, source = load_rates()
|
|
49
|
+
currencies = []
|
|
50
|
+
|
|
51
|
+
if not rates:
|
|
52
|
+
logger.error("currency_state_sync_failed", source=source)
|
|
53
|
+
raise ValueError("No rates found")
|
|
54
|
+
|
|
55
|
+
with transaction.atomic():
|
|
56
|
+
state = cls.objects.create(source=source)
|
|
57
|
+
|
|
58
|
+
for rate in rates:
|
|
59
|
+
currency = Currency(
|
|
60
|
+
code=rate.currency,
|
|
61
|
+
rate=rate.amount,
|
|
62
|
+
state=state
|
|
63
|
+
)
|
|
64
|
+
currencies.append(currency)
|
|
65
|
+
|
|
66
|
+
Currency.objects.bulk_create(currencies)
|
|
67
|
+
|
|
68
|
+
logger.info("currency_state_synced", state=state.id, source=source)
|
|
69
|
+
|
|
70
|
+
return state
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class Currency(UUIDPrimaryKeyMixin):
|
|
74
|
+
code = models.CharField(max_length=10)
|
|
75
|
+
rate = models.DecimalField(max_digits=10, decimal_places=4)
|
|
76
|
+
state = models.ForeignKey(
|
|
77
|
+
CurrencyState,
|
|
78
|
+
on_delete=models.CASCADE,
|
|
79
|
+
related_name="currencies"
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
class Meta:
|
|
83
|
+
ordering = ['code']
|
|
84
|
+
indexes = [
|
|
85
|
+
models.Index(fields=['code', 'state']),
|
|
86
|
+
]
|
|
87
|
+
|
|
88
|
+
def __str__(self):
|
|
89
|
+
return f"{self.code} - {self.rate}"
|
|
90
|
+
|
|
91
|
+
def clean(self):
|
|
92
|
+
if self.code not in AVAILABLES_CURRENCIES:
|
|
93
|
+
raise ValueError(f"Invalid currency code: {self.code}")
|
|
94
|
+
|
|
95
|
+
if self.rate <= 0:
|
|
96
|
+
raise ValueError(f"Invalid currency rate: {self.rate}")
|
|
97
|
+
|
|
98
|
+
def save(self, *args, **kwargs):
|
|
99
|
+
self.clean()
|
|
100
|
+
super().save(*args, **kwargs)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from ninja import Schema
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from uuid import UUID
|
|
4
|
+
from decimal import Decimal
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CurrencySchema(Schema):
|
|
8
|
+
code: str
|
|
9
|
+
rate: Decimal
|
|
10
|
+
|
|
11
|
+
class Config:
|
|
12
|
+
from_attributes = True
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class CurrencyStateSchema(Schema):
|
|
16
|
+
id: UUID
|
|
17
|
+
source: str
|
|
18
|
+
created_at: datetime
|
|
19
|
+
updated_at: datetime
|
|
20
|
+
|
|
21
|
+
class Config:
|
|
22
|
+
from_attributes = True
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class CurrencyStateDetailSchema(Schema):
|
|
26
|
+
id: UUID
|
|
27
|
+
source: str
|
|
28
|
+
created_at: datetime
|
|
29
|
+
updated_at: datetime
|
|
30
|
+
currencies: dict[str, float]
|
|
31
|
+
|
|
32
|
+
class Config:
|
|
33
|
+
from_attributes = True
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class CurrencyRateSchema(Schema):
|
|
37
|
+
code: str
|
|
38
|
+
rate: Decimal
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from django.conf import settings
|
|
3
|
+
from django.db import transaction
|
|
4
|
+
from bcc_rates import BCCBankSource, OXRBankSource, SourceValue
|
|
5
|
+
from oxutils.currency.enums import CurrencySource
|
|
6
|
+
from oxutils.currency.schemas import CurrencyStateDetailSchema
|
|
7
|
+
import structlog
|
|
8
|
+
|
|
9
|
+
logger = structlog.get_logger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def load_rates() -> tuple[list[SourceValue], CurrencySource]:
|
|
13
|
+
max_retries = 3
|
|
14
|
+
retry_count = 0
|
|
15
|
+
|
|
16
|
+
while retry_count < max_retries:
|
|
17
|
+
try:
|
|
18
|
+
bcc_source = BCCBankSource()
|
|
19
|
+
rates = bcc_source.sync(cache=True)
|
|
20
|
+
return rates, CurrencySource.BCC
|
|
21
|
+
except Exception as e:
|
|
22
|
+
retry_count += 1
|
|
23
|
+
if retry_count < max_retries:
|
|
24
|
+
time.sleep(1)
|
|
25
|
+
else:
|
|
26
|
+
if not getattr(settings, 'OXI_BCC_FALLBACK_ON_OXR', False):
|
|
27
|
+
raise Exception(f"Failed to load rates from BCC: {str(e)}")
|
|
28
|
+
break
|
|
29
|
+
|
|
30
|
+
try:
|
|
31
|
+
oxr_source = OXRBankSource()
|
|
32
|
+
rates = oxr_source.sync(cache=True)
|
|
33
|
+
return rates, CurrencySource.OXR
|
|
34
|
+
except Exception as e:
|
|
35
|
+
raise Exception(f"Failed to load rates from both BCC and OXR: {str(e)}")
|
|
36
|
+
|
|
37
|
+
@transaction.atomic
|
|
38
|
+
def update_rates(state: CurrencyStateDetailSchema):
|
|
39
|
+
from oxutils.currency.models import CurrencyState, Currency
|
|
40
|
+
|
|
41
|
+
if CurrencyState.objects.filter(id=state.id).exists():
|
|
42
|
+
logger.info("currency_state_exists", id=state.id)
|
|
43
|
+
return
|
|
44
|
+
|
|
45
|
+
if not len(state.currencies.keys()):
|
|
46
|
+
logger.info("currency_state_no_currencies", id=state.id)
|
|
47
|
+
return
|
|
48
|
+
|
|
49
|
+
_state = CurrencyState.objects.create(
|
|
50
|
+
id=state.id,
|
|
51
|
+
source=state.source,
|
|
52
|
+
created_at=state.created_at,
|
|
53
|
+
updated_at=state.updated_at,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
currencies = []
|
|
57
|
+
|
|
58
|
+
for key, value in state.currencies.items():
|
|
59
|
+
currencies.append(
|
|
60
|
+
Currency(
|
|
61
|
+
code=key,
|
|
62
|
+
rate=value,
|
|
63
|
+
state=_state
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
Currency.objects.bulk_create(currencies)
|
|
68
|
+
|
|
69
|
+
logger.info("currency_state_updated", id=state.id)
|
oxutils/functions.py
CHANGED
|
@@ -6,12 +6,15 @@ from ninja_extra.exceptions import ValidationError
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
def get_absolute_url(url: str, request=None):
|
|
9
|
+
if url.startswith('http'):
|
|
10
|
+
return url
|
|
11
|
+
|
|
9
12
|
if request:
|
|
10
13
|
# Build absolute URL using request
|
|
11
14
|
return request.build_absolute_uri(url)
|
|
12
15
|
else:
|
|
13
|
-
# Fallback: build URL using
|
|
14
|
-
base_url = getattr(settings, '
|
|
16
|
+
# Fallback: build URL using SITE_DOMAIN and domain
|
|
17
|
+
base_url = getattr(settings, 'SITE_DOMAIN', 'http://localhost:8000')
|
|
15
18
|
return urljoin(base_url, url)
|
|
16
19
|
|
|
17
20
|
|
oxutils/logger/receivers.py
CHANGED
|
@@ -2,7 +2,6 @@ from django.contrib.sites.shortcuts import RequestSite
|
|
|
2
2
|
from django.dispatch import receiver
|
|
3
3
|
import structlog
|
|
4
4
|
from django_structlog import signals
|
|
5
|
-
from cid.locals import get_cid
|
|
6
5
|
from oxutils.settings import oxi_settings
|
|
7
6
|
|
|
8
7
|
|
|
@@ -12,7 +11,6 @@ def bind_domain(request, logger, **kwargs):
|
|
|
12
11
|
current_site = RequestSite(request)
|
|
13
12
|
structlog.contextvars.bind_contextvars(
|
|
14
13
|
domain=current_site.domain,
|
|
15
|
-
cid=get_cid(),
|
|
16
14
|
user_id=str(request.user.pk),
|
|
17
15
|
service=oxi_settings.service_name
|
|
18
16
|
)
|
|
File without changes
|
oxutils/oxiliere/apps.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from cacheops import cached_as, cached
|
|
2
|
+
from oxutils.oxiliere.models import Tenant, TenantUser
|
|
3
|
+
from django_tenants.utils import get_tenant_model
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
TenantModel = get_tenant_model()
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@cached_as(TenantModel, timeout=60*15)
|
|
10
|
+
def get_tenant_by_oxi_id(oxi_id: str):
|
|
11
|
+
return TenantModel.objects.get(oxi_id=oxi_id)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@cached_as(TenantModel, timeout=60*15)
|
|
15
|
+
def get_tenant_by_schema_name(schema_name: str) -> Tenant:
|
|
16
|
+
return TenantModel.objects.get(schema_name=schema_name)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@cached_as(TenantUser, timeout=60*15)
|
|
20
|
+
def get_tenant_user(oxi_org_id: str, oxi_user_id: str):
|
|
21
|
+
return TenantUser.objects.get(
|
|
22
|
+
tenant__oxi_id=oxi_org_id,
|
|
23
|
+
user__oxi_id=oxi_user_id
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
@cached(timeout=60*15)
|
|
27
|
+
def get_system_tenant():
|
|
28
|
+
from oxutils.oxiliere.utils import oxid_to_schema_name
|
|
29
|
+
|
|
30
|
+
system_schema_name = oxid_to_schema_name(
|
|
31
|
+
getattr(settings, 'OXI_SYSTEM_TENANT', 'tenant_oxisystem')
|
|
32
|
+
)
|
|
33
|
+
return get_tenant_model().objects.get(schema_name=system_schema_name)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from ninja_extra import (
|
|
2
|
+
api_controller,
|
|
3
|
+
ControllerBase,
|
|
4
|
+
http_post,
|
|
5
|
+
)
|
|
6
|
+
from .permissions import OxiliereServicePermission
|
|
7
|
+
from .schemas import CreateTenantSchema
|
|
8
|
+
from oxutils.mixins.schemas import ResponseSchema
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@api_controller(
|
|
12
|
+
'/setup',
|
|
13
|
+
tags=['Setup'],
|
|
14
|
+
auth=None,
|
|
15
|
+
permissions=[
|
|
16
|
+
OxiliereServicePermission(),
|
|
17
|
+
]
|
|
18
|
+
)
|
|
19
|
+
class SetupController(ControllerBase):
|
|
20
|
+
|
|
21
|
+
@http_post(
|
|
22
|
+
'/init',
|
|
23
|
+
response=ResponseSchema,
|
|
24
|
+
)
|
|
25
|
+
def init(self, payload: CreateTenantSchema):
|
|
26
|
+
try:
|
|
27
|
+
payload.create_tenant()
|
|
28
|
+
except Exception as e:
|
|
29
|
+
return ResponseSchema(
|
|
30
|
+
code='initialization_failed',
|
|
31
|
+
detail=str(e)
|
|
32
|
+
)
|
|
33
|
+
return ResponseSchema(
|
|
34
|
+
code='success',
|
|
35
|
+
detail='Tenant initialized successfully'
|
|
36
|
+
)
|
|
File without changes
|
|
File without changes
|