Features: 1) Enable Elasticsearch DSL autosync and switch to CelerySignalProcessor; 2) Extract common logic into auto_resolver_helper for resolving brands and categories; 3) Add conditional CACHEOPS_ENABLED based on sys arguments;
Fixes: 1) Add missing sys import in caches.py; 2) Ensure CACHEOPS_REDIS is only declared when CACHEOPS_ENABLED is True; Extra: 1) Formatting and cleanup for readability in settings and auto-resolve methods; 2) Rearrange cacheops import in base settings;
This commit is contained in:
parent
8bdb74b80c
commit
87c355474c
4 changed files with 41 additions and 36 deletions
48
core/vendors/__init__.py
vendored
48
core/vendors/__init__.py
vendored
|
|
@ -99,7 +99,20 @@ class AbstractVendor:
|
|||
return value, "string"
|
||||
|
||||
@staticmethod
|
||||
def auto_resolve_category(category_name: str):
|
||||
def auto_resolver_helper(model: type[Brand] | type[Category], resolving_name: str):
|
||||
queryset = model.objects.filter(name=resolving_name)
|
||||
if not queryset.exists():
|
||||
return model.objects.create(name=resolving_name, is_active=False)
|
||||
elif queryset.filter(is_active=True).count() > 1:
|
||||
queryset = queryset.filter(is_active=True)
|
||||
elif queryset.filter(is_active=False).count() > 1:
|
||||
queryset = queryset.filter(is_active=False)
|
||||
chosen = queryset.first()
|
||||
queryset = queryset.exclude(uuid=chosen.uuid)
|
||||
queryset.delete()
|
||||
return chosen
|
||||
|
||||
def auto_resolve_category(self, category_name: str):
|
||||
if category_name:
|
||||
try:
|
||||
uuid = process_query(category_name)["categories"][0]["uuid"]
|
||||
|
|
@ -111,20 +124,12 @@ class AbstractVendor:
|
|||
pass
|
||||
except Category.MultipleObjectsReturned:
|
||||
pass
|
||||
categories = Category.objects.filter(name=category_name)
|
||||
if not categories.exists():
|
||||
return Category.objects.create(name=category_name, is_active=False)
|
||||
elif categories.filter(is_active=True).count() > 1:
|
||||
categories = categories.filter(is_active=True)
|
||||
elif categories.filter(is_active=False).count() > 1:
|
||||
categories = categories.filter(is_active=False)
|
||||
chosen = categories.first()
|
||||
categories.exclude(uuid=chosen.uuid)
|
||||
categories.delete()
|
||||
return chosen
|
||||
except Category.DoesNotExist:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def auto_resolve_brand(brand_name: str):
|
||||
return self.auto_resolver_helper(Category, category_name)
|
||||
|
||||
def auto_resolve_brand(self, brand_name: str):
|
||||
if brand_name:
|
||||
try:
|
||||
uuid = process_query(brand_name)["brands"][0]["uuid"]
|
||||
|
|
@ -136,17 +141,10 @@ class AbstractVendor:
|
|||
pass
|
||||
except Brand.MultipleObjectsReturned:
|
||||
pass
|
||||
brands = Brand.objects.filter(name=brand_name)
|
||||
if not brands.exists():
|
||||
return Brand.objects.create(name=brand_name, is_active=False)
|
||||
elif brands.filter(is_active=True).count() > 1:
|
||||
brands = brands.filter(is_active=True)
|
||||
elif brands.filter(is_active=False).count() > 1:
|
||||
brands = brands.filter(is_active=False)
|
||||
chosen = brands.first()
|
||||
brands.exclude(uuid=chosen.uuid)
|
||||
brands.delete()
|
||||
return chosen
|
||||
except Brand.DoesNotExist:
|
||||
pass
|
||||
|
||||
return self.auto_resolver_helper(Brand, brand_name)
|
||||
|
||||
def resolve_price(self, original_price: int | float, vendor: Vendor = None, category: Category = None) -> float:
|
||||
if not vendor:
|
||||
|
|
|
|||
|
|
@ -70,6 +70,7 @@ INSTALLED_APPS = [
|
|||
"django.contrib.sitemaps",
|
||||
"django.contrib.gis",
|
||||
"django.contrib.humanize",
|
||||
"cacheops",
|
||||
"django_hosts",
|
||||
"django_celery_beat",
|
||||
"django_extensions",
|
||||
|
|
@ -89,7 +90,6 @@ INSTALLED_APPS = [
|
|||
"constance.backends.database",
|
||||
"django_mailbox",
|
||||
"graphene_django",
|
||||
"cacheops",
|
||||
"core",
|
||||
"geo",
|
||||
"payments",
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import sys
|
||||
|
||||
from evibes.settings.base import REDIS_PASSWORD, getenv
|
||||
|
||||
CACHES = {
|
||||
|
|
@ -18,12 +20,17 @@ CACHES = {
|
|||
}
|
||||
}
|
||||
|
||||
CACHEOPS_REDIS = f"redis://:{REDIS_PASSWORD}@redis:6379/0"
|
||||
if any(arg == "celery" for arg in sys.argv):
|
||||
CACHEOPS_ENABLED = False
|
||||
else:
|
||||
CACHEOPS_ENABLED = True
|
||||
|
||||
CACHEOPS = {
|
||||
"vibes_auth.user": {"ops": "get", "timeout": 60*15},
|
||||
"vibes_auth.*": {"ops": {"fetch", "get"}, "timeout": 60*60},
|
||||
"auth.permission": {"ops": "all", "timeout": 60*60},
|
||||
"core.*": {"ops": "all", "timeout": 60*60},
|
||||
"geo.*": {"ops": "all", "timeout": 60*60},
|
||||
}
|
||||
CACHEOPS_REDIS = f"redis://:{REDIS_PASSWORD}@redis:6379/0"
|
||||
|
||||
CACHEOPS = {
|
||||
"vibes_auth.user": {"ops": "get", "timeout": 60 * 15},
|
||||
"vibes_auth.*": {"ops": {"fetch", "get"}, "timeout": 60 * 60},
|
||||
"auth.permission": {"ops": "all", "timeout": 60 * 60},
|
||||
"core.*": {"ops": "all", "timeout": 60 * 60},
|
||||
"geo.*": {"ops": "all", "timeout": 60 * 60},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ ELASTICSEARCH_DSL = {
|
|||
},
|
||||
}
|
||||
|
||||
ELASTICSEARCH_DSL_AUTOSYNC = False
|
||||
ELASTICSEARCH_DSL_AUTOSYNC = True
|
||||
ELASTICSEARCH_DSL_PARALLEL = False
|
||||
ELASTICSEARCH_DSL_SIGNAL_PROCESSOR = (
|
||||
"django_elasticsearch_dsl.signals.RealTimeSignalProcessor"
|
||||
"django_elasticsearch_dsl.signals.CelerySignalProcessor"
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue