welco/welco/qualif/models.py

81 lines
3.0 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.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
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.utils import get_wcs_formdef_details, push_wcs_formdata, get_wcs_services
class FormdefReference(models.Model):
reference = models.CharField(max_length=250)
@property
def name(self):
return get_wcs_formdef_details(self.reference).get('title')
class FormdataReference(models.Model):
reference = models.CharField(max_length=250)
def formdef(self):
return FormdefReference.objects.get(
reference=self.reference.rsplit(':', 1)[0])
@property
def url(self):
site, formdef, formdata = self.reference.split(':')
wcs_site = get_wcs_services().get(site)
return '%s%s/%s' % (wcs_site.get('url'), formdef, formdata)
class Association(models.Model):
source_type = models.ForeignKey(ContentType)
source_pk = models.PositiveIntegerField()
source = generic.GenericForeignKey('source_type', 'source_pk')
triaged = models.BooleanField(default=False)
comments = models.TextField(blank=True, verbose_name=_('Comments'))
user_id = models.CharField(max_length=50, null=True)
formdefs = models.ManyToManyField(FormdefReference)
formdatas = models.ManyToManyField(FormdataReference)
def push(self, request, context):
# push validated request to wcs
if context:
context = context.copy()
context['user_id'] = self.user_id
for formdef in self.formdefs.all():
formdata_id = push_wcs_formdata(request, formdef.reference, context)
reference = '%s:%s' % (formdef.reference, formdata_id)
formdata_reference = FormdataReference(reference=reference)
formdata_reference.save()
self.formdatas.add(formdata_reference)
self.save()
@receiver(post_save)
def association_triaged(sender, instance, **kwargs):
if not sender is Association:
return
if instance.source.triaged != instance.triaged:
instance.source.triaged = instance.triaged
instance.source.save()