summaryrefslogtreecommitdiffstats
path: root/tabellio/config/utils.py
blob: f7e59fedb046fdfb43adf175ea43bf0e18b4ad71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-

import time

from zope import component
from plone.registry.interfaces import IRegistry
from interfaces import ITabellioSettings
from plone.memoize import ram

def get_dossiers_path():
    return component.getUtility(IRegistry).forInterface(ITabellioSettings, False).dossiersPath

def get_questions_path():
    return component.getUtility(IRegistry).forInterface(ITabellioSettings, False).questionsPath

def get_documents_path():
    return component.getUtility(IRegistry).forInterface(ITabellioSettings, False).documentsPath

def get_deputies_path():
    return component.getUtility(IRegistry).forInterface(ITabellioSettings, False).deputiesPath

def get_polgroups_path():
    return component.getUtility(IRegistry).forInterface(ITabellioSettings, False).polgroupsPath

def get_sessions():
    settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
    sessions = settings.sessions
    if not sessions:
        return []
    terms = [x.split(':')[1].strip() for x in sessions.splitlines() if x]
    return terms

@ram.cache(lambda *args: time.time() // (5 * 60)) # 5 minutes
def get_topics_dict():
    from plone.i18n.normalizer.fr import normalizer
    settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
    if not settings.topics:
        return {}
    topics = {}
    for line in settings.topics.splitlines():
        if line.count('|') == 1:
            id, term = line.strip().split('|')
        else:
            term = line.strip()
            id = term.encode('ascii', 'replace')
        topics[id] = (term, normalizer.normalize(term).lower())
    return topics

def get_legisl_and_sessions():
    settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
    sessions = settings.sessions
    if not sessions:
        return []
    terms = []
    current_legisl = None
    for term in sessions.splitlines():
        legisl, session = term.strip().split(':')
        if legisl != current_legisl:
            terms.append(legisl)
            current_legisl = legisl
        terms.append('-- %s' % session)
    return terms

def get_list_of_sessions(legisl_or_session):
    settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
    if not legisl_or_session.startswith('--'):
        return [x.split(':')[1].strip() for x in settings.sessions.splitlines() if x.split(':')[0] == legisl_or_session]
    else:
        return [legisl_or_session[3:]]

def get_ordered_sessions():
    settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
    return list(reversed([x.split(':')[1].strip() for x in settings.sessions.splitlines()]))

def get_com_audio_code(com_id):
    settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
    for line in settings.commission_audio_codes.splitlines():
        id, audio_code = line.strip().split(':')
        if id == com_id:
            return audio_code
    return None