diff options
author | Filipp Lepalaan <filipp@mac.com> | 2016-05-01 23:54:58 +0300 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2016-05-01 23:54:58 +0300 |
commit | 47cc21441b437fe148c72bd181059728b47bdf9b (patch) | |
tree | cd96c0a704cb31837c6e0b2e9affe998d24acd7b /servo/models/parts.py | |
parent | 65df597329bbb602dd382695c639aab3776123ea (diff) | |
parent | 82e1fe63447f5c4d33d8a3ed7c365e7eab1006c0 (diff) | |
download | Servo-47cc21441b437fe148c72bd181059728b47bdf9b.tar.gz Servo-47cc21441b437fe148c72bd181059728b47bdf9b.tar.bz2 Servo-47cc21441b437fe148c72bd181059728b47bdf9b.zip |
Merge branch 'develop'
Diffstat (limited to 'servo/models/parts.py')
-rw-r--r-- | servo/models/parts.py | 35 |
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'] |