diff --git a/.isort.cfg b/.isort.cfg index 501b4b490f70c8c72d984ffb8e6aaa58f902fed6..6c8f12c9d52da62b5ccaaf135b17f6026ab601b2 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -4,4 +4,4 @@ line_length = 88 multi_line_output = 3 default_section = "THIRDPARTY" include_trailing_comma = true -known_third_party = arrow,django,environ,faker,ics,markdown,modelcluster,nbconvert,pirates,pytest,pytz,requests,requests_cache,sentry_sdk,snapshottest,taggit,traitlets,wagtail,wagtailmetadata +known_third_party = arrow,django,environ,faker,ics,markdown,modelcluster,nbconvert,pirates,pytest,pytz,requests,sentry_sdk,snapshottest,taggit,traitlets,wagtail,wagtailmetadata diff --git a/requirements/base.in b/requirements/base.in index 7819bd18965051dcda6c92beb9fedfc4b467c5dd..37925a5879e4b54bcdf082fc7b21f98ba44cff36 100644 --- a/requirements/base.in +++ b/requirements/base.in @@ -9,7 +9,6 @@ pirates<=0.4 whitenoise opencv-python requests -requests-cache ics arrow sentry-sdk diff --git a/requirements/base.txt b/requirements/base.txt index f87db878bc4015d2a4da735731a88c333962b258..3ca96d79817fc75a8aeae9608e9a16cb4ef9d868 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -69,8 +69,7 @@ python-ldap==3.3.1 # via pirates pytz==2020.4 # via django, django-modelcluster, l18n pyzmq==19.0.2 # via jupyter-client redis==3.5.3 # via django-redis -requests-cache==0.5.2 # via -r base.in -requests==2.24.0 # via -r base.in, mozilla-django-oidc, requests-cache, wagtail +requests==2.24.0 # via -r base.in, mozilla-django-oidc, wagtail sentry-sdk==0.19.2 # via -r base.in six==1.15.0 # via bleach, cryptography, html5lib, ics, josepy, jsonschema, l18n, mozilla-django-oidc, packaging, pyopenssl, python-dateutil soupsieve==2.0.1 # via beautifulsoup4 diff --git a/shared/models.py b/shared/models.py index e7c67d7430128ff8111461a0469dc29230766095..07013d908883ea4bfc0340a8c547e92db95f10ba 100644 --- a/shared/models.py +++ b/shared/models.py @@ -2,7 +2,7 @@ import datetime import random import requests -import requests_cache +from django.core.cache import cache from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from modelcluster.contrib.taggit import ClusterTaggableManager @@ -14,11 +14,6 @@ from wagtail.core.models import Page from wagtail.images.edit_handlers import ImageChooserPanel from wagtailmetadata.models import MetadataPageMixin -# cacheovani requestu na piratska api, ttl 1 hodina -requests_cache.install_cache( - cache_name="shared_models", backend="memory", expire_after=3600 -) - class SharedSubpageMixin: """Must be used in class definition before MetadataPageMixin!""" @@ -116,10 +111,15 @@ class PersonPage(SharedSubpageMixin, MetadataPageMixin, Page): ] def _iapi(self): - """ Vrati data o osobe z piratskeho IAPI jako json. Cached via requests_cache """ - return requests.get( - "https://iapi.pirati.cz/v1/user?username=%s" % self.username - ).json() + """ Vrati data o osobe z piratskeho IAPI jako json. Cached via django cache""" + key = f"profile_{self.username}" + profile = cache.get(key) + if profile is None: + profile = requests.get( + f"https://iapi.pirati.cz/v1/user?username={self.username}" + ).json() + cache.set(key, profile, 3600) + return profile @property def name(self):