from gzip import GzipFile
from re import match
-DELAIS = 5 # maximum 5 secondes en cache
+EXPIRE_DELAY = 5 # maximum 5 secondes en cache web
TIME_FORMAT = '%a, %d %b %Y %H:%M:%S GMT' # format date pour HTTP
+#ETABLISSEMENT_FORMAT = r'^(\w+\s-\s.+)\s\(\d+\s-\s(Nord|Sud)\)$'
WCS_ROOT_DIR = '/var/lib/wcs'
WCS_DOMAIN_SUFFIX = '.auf.org'
WCS_CACHE_DIR = '/var/tmp'
+WCS_CACHE_DELAY_DEFAULT = 7*24*60*60 # 1 semaine
+WCS_CACHE_DELAY_FORMS = 5*60 # 5 minutes
#--------------------------------------------------------------------------
# variables globales
# références horaires
current_time = time.time()
mtime = time.gmtime(current_time)
- etime = time.gmtime(current_time + DELAIS)
+ etime = time.gmtime(current_time + EXPIRE_DELAY)
if os.environ.has_key('HTTP_IF_MODIFIED_SINCE'):
try:
itime = time.strptime(os.environ['HTTP_IF_MODIFIED_SINCE'], TIME_FORMAT)
def _make_wcs_cache_name(domain, form, name):
return 'wcs-%s-%s-%s' % (domain, form, name)
-def set_wcs_cache(domain, form, name, data):
+def set_wcs_cache(domain, form, name, data, delay=WCS_CACHE_DELAY_DEFAULT):
os.umask(0022)
cache_filename = _make_wcs_cache_name(domain, form, name)
- f = open(os.path.join(WCS_CACHE_DIR, cache_filename), 'wb')
+ cache_filename = os.path.join(WCS_CACHE_DIR, cache_filename)
+ f = open(cache_filename, 'wb')
f.write(data)
f.close()
+ # la date de modification est utilisée comme date d'expiration
+ atime = time.time()
+ mtime = atime + delay
+ os.utime(cache_filename, (atime, mtime))
def get_wcs_cache(domain, form, name):
data = None
cache_filename = _make_wcs_cache_name(domain, form, name)
cache_filename = os.path.join(WCS_CACHE_DIR, cache_filename)
if os.path.exists(cache_filename):
- f = open(cache_filename, 'rb')
- data = f.read()
- f.close()
+ # la date de modification est utilisée comme date d'expiration
+ if time.time() < os.path.getmtime(cache_filename):
+ data = open(cache_filename, 'rb').read()
+ else:
+ os.unlink(cache_filename)
return data
def clear_wcs_cache(domain, form):
return [x for x in l if os.path.isdir(os.path.join(root, x)) and x.endswith(suffix)]
def get_wcs_forms(domain):
+ """extraction de la liste des formulaires"""
+ data = get_wcs_cache(domain, 'ALL', 'ALL.json')
+ if data is not None:
+ return json.loads(data, encoding='utf-8')
set_wcs_publisher(domain)
from wcs.formdef import FormDef
- return [f.url_name for i,f in FormDef.items()]
+ forms = [f.url_name for i,f in FormDef.items()]
+ data = json.dumps(forms, ensure_ascii=False).encode('utf-8')
+ set_wcs_cache(domain, 'ALL', 'ALL.json', data, WCS_CACHE_DELAY_FORMS)
+ return forms
def get_wcs_form_data(domain, form):
"""extraction des données du formulaire"""
if object.evolution is not None:
for e in object.evolution:
if e.comment is not None:
- who = pub.user_class.get(e.who).display_name
+ try:
+ who = pub.user_class.get(e.who).display_name
+ except:
+ who = 'Inconnu(e)'
e_time = time.strftime('%Y-%m-%d %H:%M:%S', e.time)
comment = '%s -- %s %s' % (e.comment, who, e_time)
result['wcs_comments'].append(comment)
continue
if isinstance(field, StringField) or isinstance(field, TextField) \
or isinstance(field, EmailField) or isinstance(field, ItemField):
+ # nettoyage du nom d'établissement (suppression id et Nord/Sud)
+ #m = match(ETABLISSEMENT_FORMAT, data)
+ #if m is not None:
+ # data = m.groups()[0]
result[field_name] = data
elif isinstance(field, ItemsField) or isinstance(field, TableField):
result[field_name] = data # liste => peux-être joindre sur ';'