mirror of
https://github.com/Labelbase/Labelbase.git
synced 2026-08-13 12:33:23 +02:00
added threadlocals
This commit is contained in:
parent
7a79c66f09
commit
37b2a1b988
6 changed files with 140 additions and 8 deletions
|
|
@ -278,7 +278,7 @@ class HistoricalPrice(models.Model):
|
|||
class Meta:
|
||||
ordering = ['-timestamp']
|
||||
|
||||
@classmethod
|
||||
@classmethod
|
||||
def get_or_create_from_api(cls, user=None, timestamp=-1):
|
||||
if timestamp == -1:
|
||||
current_datetime = datetime.datetime.now()
|
||||
|
|
@ -295,10 +295,13 @@ class HistoricalPrice(models.Model):
|
|||
response = requests.get(url)
|
||||
api_response = response.json()
|
||||
except Exception as ex:
|
||||
#T ODO:
|
||||
#if request:
|
||||
# messages.error(request, "Connection Error: Could not connect to Mempool to retrieve historical price.")
|
||||
logger.error(ex, exc_info=True)
|
||||
try:
|
||||
from threadlocals.threadlocals import get_current_request
|
||||
request = get_current_request()
|
||||
if request:
|
||||
messages.error(request, "<strong>Connection Error:</strong> Could not connect to Mempool to retrieve historical price.")
|
||||
except Exception as ex2:
|
||||
logger.error(ex, exc_info=True)
|
||||
return None, None
|
||||
try:
|
||||
obj, created = cls.objects.get_or_create(timestamp=timestamp, defaults={
|
||||
|
|
|
|||
|
|
@ -137,6 +137,8 @@ MIDDLEWARE = [
|
|||
# "django.middleware.cache.FetchFromCacheMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
"threadlocals.middleware.ThreadLocalMiddleware",
|
||||
|
||||
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -252,12 +252,10 @@
|
|||
<span>Community</span>
|
||||
</h6>
|
||||
|
||||
{% comment %}
|
||||
<li class="nav-item ">
|
||||
<a class="nav-link" href="{% url "donate" %}"><span data-feather="heart" class="align-text-bottom"></span> Donate</a>
|
||||
<li>
|
||||
{% endcomment %}
|
||||
|
||||
|
||||
<li class="nav-item ">
|
||||
<a class="nav-link" href="https://labelbase.space/newsletter/"><span data-feather="mail" class="align-text-bottom"></span> Newsletter</a>
|
||||
<li>
|
||||
|
|
|
|||
6
django/threadlocals/__init__.py
Normal file
6
django/threadlocals/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# User: Troy Evans
|
||||
# Date: 1/24/13
|
||||
# Time: 8:06 PM
|
||||
#
|
||||
# Copyright 2012, Nutrislice Inc.
|
||||
VERSION = '0.10'
|
||||
35
django/threadlocals/middleware.py
Normal file
35
django/threadlocals/middleware.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
threadlocals Middleware, provides a better, faster way to get at request and user.
|
||||
|
||||
:Authors:
|
||||
- Ben Roberts (Nutrislice, Inc.)
|
||||
- Troy Evans (Nutrislice, Inc.)
|
||||
- Herbert Poul http://sct.sphene.net
|
||||
- Bruce Kroeze
|
||||
|
||||
Branched from [http://code.djangoproject.com/wiki/CookBookthreadlocalsAndUser CookBookThreadLocalsAndUser]
|
||||
as modified by [http://sct.sphene.net Sphene Community tools].
|
||||
|
||||
(see license.txt)
|
||||
"""
|
||||
|
||||
from .threadlocals import set_thread_variable, del_thread_variables
|
||||
try:
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
except ImportError:
|
||||
MiddlewareMixin = object
|
||||
|
||||
class ThreadLocalMiddleware(MiddlewareMixin):
|
||||
"""Middleware that puts the request object in thread local storage."""
|
||||
|
||||
def process_request(self, request):
|
||||
set_thread_variable('request', request)
|
||||
# set_current_user(request.user) # not going to store user in TL's for now, since we can get it from the request if we need it, and I read somewhere that accessing reqeust.user can potentially prevent view caching from functioning correctly
|
||||
|
||||
def process_response(self, request, response):
|
||||
del_thread_variables()
|
||||
return response
|
||||
|
||||
def process_exception(self, request, exception):
|
||||
del_thread_variables()
|
||||
88
django/threadlocals/threadlocals.py
Normal file
88
django/threadlocals/threadlocals.py
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
__init__ module for the threadlocals package
|
||||
|
||||
Derived from django-threaded-multihost (see license.txt)
|
||||
"""
|
||||
__docformat__ = "restructuredtext"
|
||||
|
||||
import logging
|
||||
|
||||
log = logging.getLogger('threadlocals.middleware')
|
||||
|
||||
from threading import local
|
||||
|
||||
_threadlocals = local()
|
||||
_threadvariables = set()
|
||||
|
||||
|
||||
def set_thread_variable(key, val):
|
||||
_threadvariables.add(key)
|
||||
setattr(_threadlocals, key, val)
|
||||
|
||||
|
||||
def get_thread_variable(key, default=None):
|
||||
return getattr(_threadlocals, key, default)
|
||||
|
||||
|
||||
def del_thread_variable(key):
|
||||
if hasattr(_threadlocals, key):
|
||||
delattr(_threadlocals, key)
|
||||
|
||||
|
||||
def del_thread_variables():
|
||||
for key in _threadvariables:
|
||||
del_thread_variable(key)
|
||||
|
||||
|
||||
def get_current_request():
|
||||
return get_thread_variable('request', None)
|
||||
|
||||
|
||||
def get_current_session():
|
||||
req = get_current_request()
|
||||
if req is None:
|
||||
return None
|
||||
return req.session
|
||||
|
||||
|
||||
def get_current_user():
|
||||
user = get_thread_variable('user', None)
|
||||
if user is None:
|
||||
req = get_current_request()
|
||||
if req == None or not hasattr(req, 'user'):
|
||||
return None
|
||||
user = req.user
|
||||
return user
|
||||
|
||||
|
||||
def set_current_user(user):
|
||||
set_thread_variable('user', user)
|
||||
|
||||
|
||||
def set_request_variable(key, val, use_threadlocal_if_no_request=True):
|
||||
request = get_current_request()
|
||||
if not request:
|
||||
if not use_threadlocal_if_no_request:
|
||||
raise RuntimeError(
|
||||
"Unable to set request variable. No request available in threadlocals. Is ThreadLocalMiddleware installed?")
|
||||
set_thread_variable(key, val)
|
||||
else:
|
||||
try:
|
||||
var_store = getattr(request, '_variables')
|
||||
except AttributeError:
|
||||
setattr(request, '_variables', {})
|
||||
var_store = getattr(request, '_variables')
|
||||
var_store[key] = val
|
||||
|
||||
|
||||
def get_request_variable(key, default=None, use_threadlocal_if_no_request=True):
|
||||
request = get_current_request()
|
||||
if not request:
|
||||
if not use_threadlocal_if_no_request:
|
||||
raise RuntimeError(
|
||||
"Unable to get request variable. No threadlocal request available. Is ThreadLocalMiddleware installed?")
|
||||
else:
|
||||
return get_thread_variable(key, default)
|
||||
return request._variables.get(key, default) if hasattr(request, '_variables') else default
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue