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
No related branches found
No related tags found
2 merge requests!173Release,!134Weby pro MS
......@@ -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
......@@ -9,7 +9,6 @@ pirates<=0.4
whitenoise
opencv-python
requests
requests-cache
ics
arrow
sentry-sdk
......
......@@ -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
......
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment