summaryrefslogtreecommitdiffstats
path: root/welco/qualif/models.py
blob: 8c563f179206ed8d1c0ef50950c880ff5bf4c9c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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.fields import GenericForeignKey
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _

from welco.utils import get_wcs_formdef_details, push_wcs_formdata, get_wcs_services


class Association(models.Model):
    source_type = models.ForeignKey(ContentType)
    source_pk = models.PositiveIntegerField()
    source = GenericForeignKey('source_type', 'source_pk')
    comments = models.TextField(blank=True, verbose_name=_('Comments'))
    formdef_reference = models.CharField(max_length=250, null=True)
    formdata_id = models.CharField(max_length=250, null=True)
    formdata_url_backoffice = models.URLField(null=True)

    def push(self, request):
        # push validated request to wcs
        context = {}
        if self.source.contact_id:
            context['user_id'] = self.source.contact_id
        context['summary_url'] = request.build_absolute_uri(
                reverse('wcs-summary', kwargs={'source_type': self.source_type_id,
                                               'source_pk': self.source_pk}))
        context.update(self.source.get_source_context(request))
        self.formdata_id, self.formdata_url_backoffice = push_wcs_formdata(request, self.formdef_reference, context)
        self.save()

    @property
    def formdef_name(self):
        try:
            return get_wcs_formdef_details(self.formdef_reference).get('title')
        except AttributeError:
            return '?'

    @property
    def formdef_category(self):
        try:
            return get_wcs_formdef_details(self.formdef_reference).get('category')
        except AttributeError:
            return '?'

    @property
    def formdata_url(self):
        site, formdef = self.formdef_reference.split(':')
        wcs_site = get_wcs_services().get(site)
        return '%sbackoffice/management/%s/%s/' % (wcs_site.get('url'), formdef, self.formdata_id)