aboutsummaryrefslogtreecommitdiffstats
path: root/servo
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2016-03-18 13:01:03 +0200
committerFilipp Lepalaan <filipp@mac.com>2016-03-18 13:01:03 +0200
commit5c1943521f35e22f3fce7033d69659bcd6eb6869 (patch)
treef73933bd5fa0060f8303e817041af71759f2b2f1 /servo
parentaa34239d1c85517cdb2bcb1a014a6f90a67e592e (diff)
downloadServo-5c1943521f35e22f3fce7033d69659bcd6eb6869.tar.gz
Servo-5c1943521f35e22f3fce7033d69659bcd6eb6869.tar.bz2
Servo-5c1943521f35e22f3fce7033d69659bcd6eb6869.zip
Added custom comptia cache support
Diffstat (limited to 'servo')
-rw-r--r--servo/models/parts.py35
1 files changed, 27 insertions, 8 deletions
diff --git a/servo/models/parts.py b/servo/models/parts.py
index ab54e79..ecea0ad 100644
--- a/servo/models/parts.py
+++ b/servo/models/parts.py
@@ -6,6 +6,7 @@ import gsxws
from django.db import models
from django.utils import timezone
from django.core.files import File
+from django.core.cache import caches
from django.utils.translation import ugettext_lazy as _
from servo.models import GsxAccount
@@ -18,21 +19,39 @@ def symptom_modifiers():
return gsxws.MODIFIERS
+def get_remote_symptom_codes(group):
+ """
+ Remote lookup for symptom codes
+ """
+ symptoms = {}
+ cache = caches['comptia']
+ # First, try to load from global cache (updated every 24h)
+ data = cache.get('codes') or {}
+
+ if not data:
+ # ... then try to fetch from GSX
+ GsxAccount.fallback()
+ data = gsxws.comptia.fetch()
+ cache.set('codes', data)
+
+ for k, v in data.get(group):
+ symptoms[k] = v
+
+ return symptoms
+
+
def symptom_codes(group):
"""
- Return CompTIA symptom codes for component group
+ Returns CompTIA symptom codes for component group
"""
if group == '':
return
- symptoms = {}
-
try:
- act = GsxAccount.fallback()
- codes = gsxws.comptia.fetch()[group]
- for k, v in codes:
- symptoms[k] = v
- except gsxws.GsxError as e:
+ symptoms = get_remote_symptom_codes(group)
+ except Exception as e:
+ # ... finally fall back to local static data
+ # @FIXME: How do we keep this up to date?
data = yaml.load(open("servo/fixtures/comptia.yaml", "r"))
symptoms = data[group]['symptoms']