Skip to content

Commit

Permalink
updates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saxix committed Nov 13, 2024
1 parent bf0e8c4 commit bb0d105
Show file tree
Hide file tree
Showing 43 changed files with 838 additions and 454 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"django-admin-extra-buttons>=1.6.0",
"django-adminactions>=2.3.0",
"django-adminfilters>=2.5.0",
"django-cacheops>=7.1",
"django-celery-beat>=2.6.0",
"django-celery-boost>=0.2.0",
"django-celery-results>=2.5.1",
Expand Down
58 changes: 58 additions & 0 deletions src/country_workspace/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from typing import Any

import django.dispatch
from django.core.cache import cache
from django.db.models.signals import post_save
from django.dispatch import receiver


class CacheManager:
def __init__(self):
self.active = True

def init(self):
pass

def invalidate(self, key):
cache_invalidate.send(CacheManager, key=key)

def get(self, key):
if not self.active:
return None
data = cache.get(key)
cache_get.send(CacheManager, key=key, hit=bool(data))
return data

def set(self, key: str, value: Any, **kwargs):
cache_set.send(CacheManager, key=key)
return cache.set(key, value, **kwargs)

# def _build_key_from_request(self, request):
# if state.tenant and state.program:
# return f"{state.tenant.slug}:{state.program.pk}:
# {slugify(request.path)}:{slugify(str(sorted(request.GET)))}"
# return ""
#
# def store(self, request, value):
# key = self._build_key_from_request(request)
# cache_store.send(CacheManager, key=key, request=request, value=value)
# self.set(key, value)
#
# def retrieve(self, request):
# key = self._build_key_from_request(request)
# if data := cache.get(key):
# return pickle.loads(data)
# return None


cache_manager = CacheManager()

cache_get = django.dispatch.Signal()
cache_set = django.dispatch.Signal()
cache_store = django.dispatch.Signal()
cache_invalidate = django.dispatch.Signal()


@receiver(post_save)
def update_cache(sender, instance, **kwargs):
cache_manager.invalidate(sender)
18 changes: 10 additions & 8 deletions src/country_workspace/config/fragments/debug_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ def show_ddt(request: "HttpRequest") -> bool: # pragma: no-cover
DEBUG_TOOLBAR_PANELS = [
"debug_toolbar.panels.request.RequestPanel",
"debug_toolbar.panels.sql.SQLPanel",
"debug_toolbar.panels.staticfiles.StaticFilesPanel",
# "debug_toolbar.panels.staticfiles.StaticFilesPanel",
"debug_toolbar.panels.templates.TemplatesPanel",
"debug_toolbar.panels.cache.CachePanel",
"country_workspace.utils.ddt.WSCachePanel",
"country_workspace.utils.ddt.WSHopePanel",
# "debug_toolbar.panels.cache.CachePanel",
"debug_toolbar.panels.signals.SignalsPanel",
"debug_toolbar.panels.redirects.RedirectsPanel",
"debug_toolbar.panels.profiling.ProfilingPanel",
"debug_toolbar.panels.history.HistoryPanel",
"debug_toolbar.panels.versions.VersionsPanel",
"debug_toolbar.panels.timer.TimerPanel",
"debug_toolbar.panels.settings.SettingsPanel",
# "debug_toolbar.panels.redirects.RedirectsPanel",
# "debug_toolbar.panels.profiling.ProfilingPanel",
# "debug_toolbar.panels.history.HistoryPanel",
# "debug_toolbar.panels.versions.VersionsPanel",
# "debug_toolbar.panels.timer.TimerPanel",
# "debug_toolbar.panels.settings.SettingsPanel",
"debug_toolbar.panels.headers.HeadersPanel",
]
9 changes: 9 additions & 0 deletions src/country_workspace/contrib/hope/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import hashlib
import re
import time
from http.client import RemoteDisconnected
from json import JSONDecodeError
from typing import TYPE_CHECKING, Any, Generator, Optional, Union
Expand All @@ -8,6 +10,8 @@

from country_workspace.exceptions import RemoteError

from .signals import hope_request_end, hope_request_start

if TYPE_CHECKING:
JsonType = Union[None, int, str, bool, list["JsonType"], dict[str, "JsonType"]]
FlatJsonType = dict[str, Union[str, int, bool]]
Expand Down Expand Up @@ -37,6 +41,9 @@ def get_lookup(self, path: str) -> "FlatJsonType":

def get(self, path: str, params: Optional[dict[str, Any]] = None) -> "Generator[FlatJsonType, None, None]":
url: "str|None" = self.get_url(path)
signature = hashlib.sha256(f"{url}{params}{time.perf_counter_ns()}".encode("utf-8")).hexdigest()
pages = 0
hope_request_start.send(self.__class__, url=url, params=params, signature=signature)
while True:
if not url:
break
Expand All @@ -46,6 +53,7 @@ def get(self, path: str, params: Optional[dict[str, Any]] = None) -> "Generator[
) # nosec
if ret.status_code != 200:
raise RemoteError(f"Error {ret.status_code} fetching {url}")
pages += 1
except RemoteDisconnected:
raise RemoteError(f"Remote Error fetching {url}")

Expand All @@ -62,3 +70,4 @@ def get(self, path: str, params: Optional[dict[str, Any]] = None) -> "Generator[
url = None
except TypeError:
raise RemoteError(f"Malformed JSON fetching {url}")
hope_request_end.send(self.__class__, url=url, params=params, pages=pages, signature=signature)
Loading

0 comments on commit bb0d105

Please sign in to comment.