welco/welco/sources/phone/models.py

81 lines
2.7 KiB
Python

# welco - multichannel request processing
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
class PhoneCall(models.Model):
class Meta:
verbose_name = _('Phone Call')
creation_timestamp = models.DateTimeField(auto_now_add=True)
last_update_timestamp = models.DateTimeField(auto_now=True)
caller = models.CharField(_('Caller'), max_length=20)
callee = models.CharField(_('Callee'), max_length=20)
start = models.DateTimeField(_('Start'), auto_now_add=True)
stop = models.DateTimeField(_('Stop'), null=True, blank=True)
data = models.TextField(_('Data'), blank=True)
@classmethod
def get_qualification_form_class(cls):
return None
def get_qualification_form(self):
return None
@classmethod
def get_qualification_form_submit_url(cls):
return reverse('qualif-phone-save')
@classmethod
def get_current_calls(cls, user):
return cls.objects.filter(callee__in=PhoneLine.get_callees(user),
stop__isnull=True).order_by('start')
@classmethod
def get_all_callees(cls):
return cls.objects.values_list('callee', flat=True).distinct()
def get_source_context(self, request):
return {
'channel': 'phone',
}
class PhoneLine(models.Model):
callee = models.CharField(_('Callee'), unique=True, max_length=20)
users = models.ManyToManyField('auth.User', verbose_name=_('User'))
@classmethod
def take(cls, callee, user):
'''Take a line number'''
line, created = cls.objects.get_or_create(callee=callee)
line.users.add(user)
@classmethod
def release(cls, callee, user):
'''Release a line number'''
line, created = cls.objects.get_or_create(callee=callee)
line.users.remove(user)
@classmethod
def get_callees(cls, user):
'''Return all line numbers watched by user'''
return PhoneLine.objects.filter(users=user).values_list('callee', flat=True)