voicemail_dao.py 2.65 KB
Newer Older
1
# -*- coding: utf-8 -*-
2

3
# Copyright (C) 2012-2014 Avencall
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8 9 10 11 12 13 14 15
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>
17

18
from sqlalchemy.sql.expression import and_
19
from xivo_dao.alchemy.contextmember import ContextMember
20
from xivo_dao.alchemy.voicemail import Voicemail
21
from xivo_dao.helpers.db_utils import flush_session
22
from xivo_dao.helpers.db_manager import daosession
23 24


25 26 27
@daosession
def all(session):
    return session.query(Voicemail).all()
28 29


30 31 32
@daosession
def get(session, voicemail_id):
    return session.query(Voicemail).filter(Voicemail.uniqueid == voicemail_id).first()
33 34 35 36


@daosession
def add(session, voicemail):
37
    with flush_session(session):
38
        session.add(voicemail)
39
        session.flush()
40 41 42 43 44
        contextmember = ContextMember(context=voicemail.context,
                                      type='voicemail',
                                      typeval=str(voicemail.uniqueid),
                                      varname='context')
        session.add(contextmember)
45 46 47 48 49


@daosession
def id_from_mailbox(session, mailbox, context):
    result = session.query(Voicemail.uniqueid).filter(and_(Voicemail.mailbox == mailbox,
Sebastien Duthil's avatar
Sebastien Duthil committed
50
                                                           Voicemail.context == context)).first()
51 52
    if(result is None):
        return None
53 54 55 56 57
    return result[0]


@daosession
def update(session, voicemailid, data):
58
    with flush_session(session):
59
        session.query(Voicemail).filter(Voicemail.uniqueid == voicemailid).update(data)
60

Sebastien Duthil's avatar
Sebastien Duthil committed
61

62 63
@daosession
def delete(session, uniqueid):
64
    with flush_session(session):
65
        impacted_rows = session.query(Voicemail).filter(Voicemail.uniqueid == uniqueid).delete()
Sebastien Duthil's avatar
Sebastien Duthil committed
66 67 68 69
        (session.query(ContextMember)
                .filter(ContextMember.type == 'voicemail')
                .filter(ContextMember.typeval == str(uniqueid))
                .delete())
70
        return impacted_rows
71

Sebastien Duthil's avatar
Sebastien Duthil committed
72

73 74
@daosession
def get_contextmember(session, voicemailid):
Sebastien Duthil's avatar
Sebastien Duthil committed
75 76 77 78
    return (session.query(ContextMember)
                   .filter(ContextMember.type == 'voicemail')
                   .filter(ContextMember.typeval == str(voicemailid))
                   .first())