Commit c7d912c6 authored by Etienne Lessard's avatar Etienne Lessard

refactor cti_userstatus_dao a little

parent 2fa68020
......@@ -15,51 +15,52 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import re
from xivo_dao.helpers.db_manager import daosession
from xivo_dao.alchemy.ctipresences import CtiPresences
from xivo_dao.alchemy.ctistatus import CtiStatus
_ACTION_REGEX = re.compile(r'(.*?)\((.*?)\)')
@daosession
def get_config(session):
res = {}
ctipresences = session.query(CtiPresences).all()
for ctipresence in ctipresences:
res[ctipresence.name] = {}
status = get_status_with_presence_id(ctipresence.id)
_build_status_for_presence(status, res[ctipresence.name])
statuses = get_statuses_with_presence_id(ctipresence.id)
res[ctipresence.name] = _build_status_for_presence(statuses)
return res
@daosession
def get_status_with_presence_id(session, presence_id):
def get_statuses_with_presence_id(session, presence_id):
return session.query(CtiStatus).filter(CtiStatus.presence_id == presence_id).all()
def _build_status_for_presence(status, res):
status_dict = _build_status_dict(status)
def _build_status_for_presence(statuses):
res = {}
status_dict = _build_status_dict(statuses)
for status in statuses:
ref = res[status.name] = {}
ref['longname'] = status.display_name
ref['color'] = status.color
for statut in status:
ref = res[statut.name] = {}
ref['longname'] = statut.display_name
ref['color'] = statut.color
status_list = statut.access_status.split(',')
status_list = status.access_status.split(',')
allowed = _build_status_allowed_for_status(status_list, status_dict)
if allowed:
ref['allowed'] = allowed
actions_list = statut.actions.split(',')
actions = _build_actions_for_status(actions_list)
actions_list = status.actions.split(',')
actions = _build_actions(actions_list)
if actions:
ref['actions'] = _build_actions_for_status(actions_list)
ref['actions'] = actions
return res
def _build_status_dict(status):
status_dict = {}
for statut in status:
status_dict[int(statut.id)] = statut.name
return status_dict
def _build_status_dict(statuses):
return dict((int(status.id), status.name) for status in statuses)
def _build_status_allowed_for_status(status_list, status_dict):
......@@ -70,11 +71,10 @@ def _build_status_allowed_for_status(status_list, status_dict):
return allowed
def _build_actions_for_status(actions_list):
action_pattern = r'(.*)\((.*)\)'
def _build_actions(actions_list):
actions = {}
for action in actions_list:
m = re.match(action_pattern, action)
m = _ACTION_REGEX.match(action)
if m:
actions[m.group(1)] = m.group(2)
return actions
......@@ -38,10 +38,10 @@ class TestCtiUserStatusDAO(DAOTestCase):
self._add_status(cti_presence_id_2, 'disconnected', '', '#202020', '')
self._add_status(cti_presence_id_2, 'away', '', '#202020', '')
result_1 = cti_userstatus_dao.get_status_with_presence_id(cti_presence_id_1)
result_1 = cti_userstatus_dao.get_statuses_with_presence_id(cti_presence_id_1)
self.assertEqual(2, len(result_1))
result_2 = cti_userstatus_dao.get_status_with_presence_id(cti_presence_id_2)
result_2 = cti_userstatus_dao.get_statuses_with_presence_id(cti_presence_id_2)
self.assertEqual(3, len(result_2))
def test_get_config(self):
......@@ -60,13 +60,13 @@ class TestCtiUserStatusDAO(DAOTestCase):
"available": {
"longname": "available",
"color": "#08FD20",
# "allowed": ["available"],
"actions": {"enablednd": "false",
"queuepause_all": ""}
},
"disconnected": {
"longname": "disconnected",
"color": "#202020",
"allowed": ["available"],
"actions": {"agentlogoff": ""}
}
}
......@@ -78,9 +78,9 @@ class TestCtiUserStatusDAO(DAOTestCase):
cti_presence_id_2 = self._add_presence('test2')
actions = 'enablednd(false),queuepause_all()'
self._add_status(cti_presence_id_2, 'available', actions, '#08FD20', '')
status_id = self._add_status(cti_presence_id_2, 'available', actions, '#08FD20', '')
actions = 'agentlogoff()'
self._add_status(cti_presence_id_2, 'disconnected', actions, '#202020', '')
self._add_status(cti_presence_id_2, 'disconnected', actions, '#202020', '%s' % status_id)
result = cti_userstatus_dao.get_config()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment