Skip to content
Snippets Groups Projects
Commit 98ced150 authored by jarmil's avatar jarmil
Browse files

Cacheovani dotazu na iapi predelano z requests-cache na django cache

parent 927c8ea8
Branches
No related tags found
2 merge requests!173Release,!134Weby pro MS
...@@ -4,4 +4,4 @@ line_length = 88 ...@@ -4,4 +4,4 @@ line_length = 88
multi_line_output = 3 multi_line_output = 3
default_section = "THIRDPARTY" default_section = "THIRDPARTY"
include_trailing_comma = true 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
...@@ -9,7 +9,6 @@ pirates<=0.4 ...@@ -9,7 +9,6 @@ pirates<=0.4
whitenoise whitenoise
opencv-python opencv-python
requests requests
requests-cache
ics ics
arrow arrow
sentry-sdk sentry-sdk
......
...@@ -69,8 +69,7 @@ python-ldap==3.3.1 # via pirates ...@@ -69,8 +69,7 @@ python-ldap==3.3.1 # via pirates
pytz==2020.4 # via django, django-modelcluster, l18n pytz==2020.4 # via django, django-modelcluster, l18n
pyzmq==19.0.2 # via jupyter-client pyzmq==19.0.2 # via jupyter-client
redis==3.5.3 # via django-redis 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, wagtail
requests==2.24.0 # via -r base.in, mozilla-django-oidc, requests-cache, wagtail
sentry-sdk==0.19.2 # via -r base.in 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 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 soupsieve==2.0.1 # via beautifulsoup4
......
...@@ -2,7 +2,7 @@ import datetime ...@@ -2,7 +2,7 @@ import datetime
import random import random
import requests import requests
import requests_cache from django.core.cache import cache
from django.core.validators import MaxValueValidator, MinValueValidator from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models from django.db import models
from modelcluster.contrib.taggit import ClusterTaggableManager from modelcluster.contrib.taggit import ClusterTaggableManager
...@@ -14,11 +14,6 @@ from wagtail.core.models import Page ...@@ -14,11 +14,6 @@ from wagtail.core.models import Page
from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.images.edit_handlers import ImageChooserPanel
from wagtailmetadata.models import MetadataPageMixin 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: class SharedSubpageMixin:
"""Must be used in class definition before MetadataPageMixin!""" """Must be used in class definition before MetadataPageMixin!"""
...@@ -116,10 +111,15 @@ class PersonPage(SharedSubpageMixin, MetadataPageMixin, Page): ...@@ -116,10 +111,15 @@ class PersonPage(SharedSubpageMixin, MetadataPageMixin, Page):
] ]
def _iapi(self): def _iapi(self):
""" Vrati data o osobe z piratskeho IAPI jako json. Cached via requests_cache """ """ Vrati data o osobe z piratskeho IAPI jako json. Cached via django cache"""
return requests.get( key = f"profile_{self.username}"
"https://iapi.pirati.cz/v1/user?username=%s" % self.username profile = cache.get(key)
if profile is None:
profile = requests.get(
f"https://iapi.pirati.cz/v1/user?username={self.username}"
).json() ).json()
cache.set(key, profile, 3600)
return profile
@property @property
def name(self): def name(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment