welco/welco/sources/mail/models.py

96 lines
3.6 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/>.
import re
import subprocess
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _
from welco.qualif.models import Association
from welco.utils import get_wcs_data
class Mail(models.Model):
class Meta:
verbose_name = _('Mail')
content = models.FileField(_('Content'))
post_date = models.DateField(_('Post Date'), null=True)
registered_mail_number = models.CharField(_('Registered Mail Number'),
null=True, max_length=50)
note = models.TextField(_('Note'), null=True)
# common to all source types:
status = models.CharField(_('Status'), blank=True, max_length=50)
contact_id = models.CharField(max_length=50, null=True)
associations = generic.GenericRelation(Association,
content_type_field='source_type', object_id_field='source_pk')
creation_timestamp = models.DateTimeField(auto_now_add=True)
last_update_timestamp = models.DateTimeField(auto_now=True)
@classmethod
def get_qualification_form_class(cls):
from .forms import MailQualificationForm
return MailQualificationForm
def get_qualification_form(self):
return self.get_qualification_form_class()({
'post_date': self.post_date,
'registered_mail_number': self.registered_mail_number})
@classmethod
def get_qualification_form_submit_url(cls):
return reverse('qualif-mail-save')
# TODO: get_avis() and get_mandatory_avis() are custom to alfortville,
# they shouldn't appear in this file.
def get_avis(self):
from welco.contrib.alfortville.models import Inbox
return Inbox.objects.filter(subtype=Inbox.AVIS,
source_type=ContentType.objects.get_for_model(Mail),
source_pk=self.id).exclude(comments__isnull=True).exclude(comments__exact='')
def get_mandatory_avis(self):
from welco.contrib.alfortville.models import Inbox
return Inbox.objects.filter(subtype=Inbox.MANDATORY_AVIS,
source_type=ContentType.objects.get_for_model(Mail),
source_pk=self.id)
def contact_name(self):
if not self.contact_id:
return ''
user_details = get_wcs_data('api/users/%s/' % self.contact_id)
return user_details.get('user_display_name')
def html_note(self):
return re.sub(r'[\r?\n]+', '<br><br>', self.note, re.DOTALL)
@receiver(post_save, sender=Mail)
def create_thumbnail(sender, instance, created, **kwargs):
if not created:
return
subprocess.call(['gm', 'convert', '-geometry', '200x',
instance.content.file.name, instance.content.file.name + '.png'])