From 5c1943521f35e22f3fce7033d69659bcd6eb6869 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Fri, 18 Mar 2016 13:01:03 +0200 Subject: Added custom comptia cache support --- servo/models/parts.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'servo') 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'] -- cgit v1.2.3