2015-07-03 11:28:48 +02:00
|
|
|
# welco - multichannel request processing
|
2018-05-19 16:38:57 +02:00
|
|
|
# Copyright (C) 2018 Entr'ouvert
|
2015-07-03 11:28:48 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2016-02-26 12:34:26 +01:00
|
|
|
import re
|
2016-11-12 21:09:32 +01:00
|
|
|
import requests
|
2015-09-19 20:19:28 +02:00
|
|
|
import subprocess
|
|
|
|
|
2016-06-14 15:34:04 +02:00
|
|
|
from django.conf import settings
|
2016-05-16 15:22:39 +02:00
|
|
|
from django.contrib.contenttypes.fields import GenericRelation
|
2015-10-11 17:40:51 +02:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2020-04-02 21:10:49 +02:00
|
|
|
from django.urls import reverse
|
2015-07-03 11:28:48 +02:00
|
|
|
from django.db import models
|
2015-09-19 20:19:28 +02:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2015-07-03 11:28:48 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2015-10-09 15:53:47 +02:00
|
|
|
from welco.qualif.models import Association
|
2015-11-09 18:08:23 +01:00
|
|
|
from welco.utils import get_wcs_data
|
|
|
|
|
2015-10-09 15:53:47 +02:00
|
|
|
|
2015-07-03 11:28:48 +02:00
|
|
|
class Mail(models.Model):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('Mail')
|
2017-10-01 17:04:59 +02:00
|
|
|
ordering = ['post_date', 'creation_timestamp']
|
2015-07-03 11:28:48 +02:00
|
|
|
|
|
|
|
content = models.FileField(_('Content'))
|
2015-08-31 09:57:37 +02:00
|
|
|
post_date = models.DateField(_('Post Date'), null=True)
|
2015-10-10 18:21:37 +02:00
|
|
|
registered_mail_number = models.CharField(_('Registered Mail Number'), null=True, max_length=50)
|
2015-10-11 14:38:41 +02:00
|
|
|
note = models.TextField(_('Note'), null=True)
|
2018-05-19 16:38:57 +02:00
|
|
|
external_id = models.CharField(_('External Id'), null=True, max_length=32)
|
2015-07-03 11:28:48 +02:00
|
|
|
|
2016-10-12 07:32:37 +02:00
|
|
|
reference = models.CharField(_('Reference'), null=True, max_length=30)
|
2016-06-14 15:34:04 +02:00
|
|
|
subject = models.CharField(_('Subject'), null=True, max_length=200)
|
|
|
|
|
2016-04-12 13:32:27 +02:00
|
|
|
scanner_category = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
|
2015-10-09 14:01:59 +02:00
|
|
|
# common to all source types:
|
|
|
|
status = models.CharField(_('Status'), blank=True, max_length=50)
|
|
|
|
contact_id = models.CharField(max_length=50, null=True)
|
2015-10-09 15:53:47 +02:00
|
|
|
associations = GenericRelation(Association, content_type_field='source_type', object_id_field='source_pk')
|
2015-10-09 14:01:59 +02:00
|
|
|
|
2015-07-03 11:28:48 +02:00
|
|
|
creation_timestamp = models.DateTimeField(auto_now_add=True)
|
|
|
|
last_update_timestamp = models.DateTimeField(auto_now=True)
|
2015-08-31 15:45:37 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_qualification_form_class(cls):
|
|
|
|
from .forms import MailQualificationForm
|
2021-01-11 20:10:12 +01:00
|
|
|
|
2015-08-31 15:45:37 +02:00
|
|
|
return MailQualificationForm
|
|
|
|
|
|
|
|
def get_qualification_form(self):
|
2016-06-14 15:34:04 +02:00
|
|
|
data = {
|
2015-08-31 15:45:37 +02:00
|
|
|
'post_date': self.post_date,
|
2016-06-14 15:34:04 +02:00
|
|
|
'registered_mail_number': self.registered_mail_number,
|
|
|
|
}
|
|
|
|
return self.get_qualification_form_class()(data)
|
2015-08-31 15:45:37 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_qualification_form_submit_url(cls):
|
|
|
|
return reverse('qualif-mail-save')
|
2015-09-19 20:19:28 +02:00
|
|
|
|
2015-11-09 18:08:23 +01:00
|
|
|
def contact_name(self):
|
|
|
|
if not self.contact_id:
|
|
|
|
return ''
|
2016-11-12 21:09:32 +01:00
|
|
|
try:
|
|
|
|
user_details = get_wcs_data('api/users/%s/' % self.contact_id)
|
|
|
|
except requests.HTTPError:
|
|
|
|
return _('unknown')
|
2015-11-09 18:08:23 +01:00
|
|
|
return user_details.get('user_display_name')
|
|
|
|
|
2016-02-26 12:34:26 +01:00
|
|
|
def html_note(self):
|
|
|
|
return re.sub(r'[\r?\n]+', '<br><br>', self.note, re.DOTALL)
|
|
|
|
|
2016-03-22 13:25:48 +01:00
|
|
|
def categories(self):
|
|
|
|
categories = {}
|
|
|
|
for association in self.associations.all():
|
|
|
|
if association.formdef_category:
|
|
|
|
categories[association.formdef_category] = True
|
2016-04-12 13:32:49 +02:00
|
|
|
if not categories and self.scanner_category:
|
|
|
|
categories[self.scanner_category] = True
|
2016-03-22 13:25:48 +01:00
|
|
|
if not categories:
|
|
|
|
return '-'
|
|
|
|
return categories.keys()
|
|
|
|
|
2016-03-28 17:13:11 +02:00
|
|
|
def get_source_context(self, request):
|
2016-06-14 15:34:04 +02:00
|
|
|
context = {
|
2016-03-28 17:13:11 +02:00
|
|
|
'channel': 'mail',
|
|
|
|
'post_date': self.post_date and self.post_date.strftime('%Y-%m-%d'),
|
2016-05-16 17:13:38 +02:00
|
|
|
'registered_mail_number': self.registered_mail_number,
|
2018-11-06 19:12:26 +01:00
|
|
|
'external_id': self.external_id,
|
2016-03-28 17:13:11 +02:00
|
|
|
}
|
2016-06-14 15:34:04 +02:00
|
|
|
return context
|
2016-03-28 17:13:11 +02:00
|
|
|
|
2015-09-19 20:19:28 +02:00
|
|
|
|
|
|
|
@receiver(post_save, sender=Mail)
|
|
|
|
def create_thumbnail(sender, instance, created, **kwargs):
|
|
|
|
if not created:
|
|
|
|
return
|
2015-09-21 15:15:21 +02:00
|
|
|
subprocess.call(
|
2021-01-11 20:10:12 +01:00
|
|
|
[
|
2015-09-21 15:15:21 +02:00
|
|
|
'gm',
|
|
|
|
'convert',
|
|
|
|
'-geometry',
|
|
|
|
'200x',
|
2015-09-19 20:19:28 +02:00
|
|
|
instance.content.file.name,
|
|
|
|
instance.content.file.name + '.png',
|
2021-01-11 20:10:12 +01:00
|
|
|
]
|
2015-09-19 20:19:28 +02:00
|
|
|
)
|