summaryrefslogtreecommitdiff
path: root/archaeological_warehouse/wizards.py
blob: 4bdc150cc7f3eedf823ec5d90bedd325e72fdead (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2016  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# 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/>.

# See the file COPYING for details.

from django.shortcuts import render_to_response
from django.template import RequestContext

from ishtar_common.forms import reverse_lazy
from ishtar_common.wizards import Wizard
from archaeological_finds.wizards import TreatmentWizard

from archaeological_finds.models import Treatment, TreatmentType
import models


class PackagingWizard(TreatmentWizard):
    basket_step = 'base-packaging'

    def save_model(self, dct, m2m, whole_associated_models, form_list,
                   return_object):
        dct = self.get_extra_model(dct, form_list)
        obj = self.get_current_saved_object()
        dct['location'] = dct['container'].location
        items = None
        if 'items' in dct:
            items = dct.pop('items')
        if 'basket' in dct:
            if not items:
                items = dct.pop('basket')
            else:
                dct.pop('basket')
        treatment = Treatment(**dct)
        extra_args_for_new = {"container": dct['container']}
        treatment.save(items=items, user=self.request.user,
                       extra_args_for_new=extra_args_for_new)
        packaging = TreatmentType.objects.get(txt_idx='packaging')
        treatment.treatment_types.add(packaging)
        res = render_to_response('ishtar/wizard/wizard_done.html', {},
                                 context_instance=RequestContext(self.request))
        return return_object and (obj, res) or res


class WarehouseWizard(Wizard):
    model = models.Warehouse
    wizard_done_window = reverse_lazy('show-warehouse')


class WarehouseModificationWizard(Wizard):
    model = models.Warehouse
    modification = True
    wizard_done_window = reverse_lazy('show-warehouse')


class ContainerWizard(Wizard):
    model = models.Container
    wizard_templates = {
        'localisation-container_localisation':
            'ishtar/wizard/wizard_containerlocalisation.html',
    }
    ignore_init_steps = ['localisation']

    def get_form_kwargs(self, step=None):
        kwargs = super(ContainerWizard, self).get_form_kwargs(step)
        if step == 'localisation-' + self.url_name:
            container = None
            container_pk = self.session_get_value(
                'selec-' + self.url_name, 'pk')
            q = models.Container.objects.filter(pk=container_pk)
            if q.count():
                container = q.all()[0]
            kwargs['container'] = container
        return kwargs

    def done(self, form_list, **kwargs):
        """
        Save the localisation
        """
        super(ContainerWizard, self).done(form_list)
        container = None
        for idx, form in enumerate(form_list):
            if not form.is_valid():
                return self.render(form)
            container = self.get_current_object()
            if idx == 2:  # localisation form
                for div_name in form.cleaned_data:
                    try:
                        division = models.WarehouseDivisionLink.objects.get(
                            pk=div_name.split('_')[-1],
                            warehouse=container.location
                        )  # check the localisation match with the container
                    except models.WarehouseDivisionLink.DoesNotExist:
                        return self.render(form)
                    localisation, created = \
                        models.ContainerLocalisation.objects.get_or_create(
                            container=container,
                            division=division
                        )
                    localisation.reference = form.cleaned_data[div_name]
                    localisation.save()
        return render_to_response(
            self.wizard_done_template, {},
            context_instance=RequestContext(self.request))

class ContainerModificationWizard(ContainerWizard):
    modification = True