diff --git a/django/finances/models.py b/django/finances/models.py index b9a2201..89cd0ab 100644 --- a/django/finances/models.py +++ b/django/finances/models.py @@ -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, "Connection Error: 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={ diff --git a/django/labellabor/settings.py b/django/labellabor/settings.py index 4104b77..4366510 100644 --- a/django/labellabor/settings.py +++ b/django/labellabor/settings.py @@ -137,6 +137,8 @@ MIDDLEWARE = [ # "django.middleware.cache.FetchFromCacheMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", + "threadlocals.middleware.ThreadLocalMiddleware", + ] diff --git a/django/templates/_base.html b/django/templates/_base.html index fe1e9f7..d3d02e8 100644 --- a/django/templates/_base.html +++ b/django/templates/_base.html @@ -252,12 +252,10 @@ Community - {% comment %}
  • - {% endcomment %} - +
  • diff --git a/django/threadlocals/__init__.py b/django/threadlocals/__init__.py new file mode 100644 index 0000000..413f5a5 --- /dev/null +++ b/django/threadlocals/__init__.py @@ -0,0 +1,6 @@ +# User: Troy Evans +# Date: 1/24/13 +# Time: 8:06 PM +# +# Copyright 2012, Nutrislice Inc. +VERSION = '0.10' \ No newline at end of file diff --git a/django/threadlocals/middleware.py b/django/threadlocals/middleware.py new file mode 100644 index 0000000..4372b6b --- /dev/null +++ b/django/threadlocals/middleware.py @@ -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() diff --git a/django/threadlocals/threadlocals.py b/django/threadlocals/threadlocals.py new file mode 100644 index 0000000..7c17316 --- /dev/null +++ b/django/threadlocals/threadlocals.py @@ -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 +