summaryrefslogtreecommitdiff
path: root/archaeological_context_records/wizards.py
blob: fd48ef3d9b3183424e29d38fcaa0751852a19e80 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2013  É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.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext_lazy as _

from ishtar_common.forms import reverse_lazy
from ishtar_common.wizards import Wizard, DeletionWizard, SourceWizard
import models


class RecordWizard(Wizard):
    model = models.ContextRecord
    edit = False
    wizard_done_window = reverse_lazy('show-contextrecord')
    relations_step_key = 'relations'

    def get_template_names(self):
        templates = super(RecordWizard, self).get_template_names()
        current_step = self.steps.current
        if current_step.startswith(self.relations_step_key):
            templates = ['ishtar/wizard/relations_wizard.html'] + templates
        return templates

    def get_current_operation(self):
        step = self.steps.current
        if not step:
            return
        # manage manualy on creation
        if step.endswith('_creation'):  # an operation has been selected
            main_form_key = 'selec-' + self.url_name
            try:
                idx = int(self.session_get_value(
                    main_form_key, 'operation_id'))
                current_ope = models.Operation.objects.get(pk=idx)
                return current_ope
            except(TypeError, ValueError, ObjectDoesNotExist):
                pass
        else:
            ope_form_key = 'operation-' + self.url_name
            try:
                idx = int(self.session_get_value(
                    ope_form_key, 'operation'))
                current_ope = models.Operation.objects.get(pk=idx)
                return current_ope
            except(TypeError, ValueError, ObjectDoesNotExist):
                pass
        current_cr = self.get_current_object()
        if current_cr:
            return current_cr.operation

    def get_context_data(self, form, **kwargs):
        """
        Get the operation "reminder" on top of wizard forms
        """
        context = super(RecordWizard, self).get_context_data(form)

        operation = self.get_current_operation()
        if not operation or self.steps.current.startswith('selec-'):
            return context
        context['reminders'] = ((_("Operation"), unicode(operation)),)
        return context

    def get_form(self, step=None, data=None, files=None):
        """
        Get associated operation
        """
        if data:
            data = data.copy()
        else:
            data = {}
        if not step:
            step = self.steps.current
            # step = self.determine_step(request, storage)
        form = self.get_form_list()[step]

        # general_form_key = 'general-' + self.url_name
        if step.startswith('general-'):
            if step.endswith('_creation'):  # an operation has been selected
                main_form_key = 'selec-' + self.url_name
                try:
                    idx = int(self.session_get_value(main_form_key,
                                                     'operation_id'))
                    current_obj = models.Operation.objects.get(pk=idx)
                    data['operation'] = current_obj
                except(TypeError, ValueError, ObjectDoesNotExist):
                    pass
            elif step.endswith('_modification'):
                ope_form_key = 'operation-' + self.url_name
                try:
                    idx = int(self.session_get_value(ope_form_key,
                                                     'operation'))
                    current_obj = models.Operation.objects.get(pk=idx)
                    data['operation'] = current_obj
                except(TypeError, ValueError, ObjectDoesNotExist):
                    pass
            else:
                current_object = self.get_current_object()
                data['context_record'] = current_object
        elif step.startswith('relations') and hasattr(form, 'management_form'):
            data['CONTEXT_RECORDS'] = self.get_other_context_records()
        form = super(RecordWizard, self).get_form(step, data, files)
        return form

    def get_other_context_records(self):
        operation = self.get_current_operation()
        if not operation:
            return []
        q = models.ContextRecord.objects.filter(operation=operation)
        obj = self.get_current_object()
        if obj and obj.pk:
            q = q.exclude(pk=obj.pk)
        return [(cr.pk, unicode(cr)) for cr in q.all()]


class RecordModifWizard(RecordWizard):
    modification = True
    model = models.ContextRecord
    filter_owns = {'selec-record_modification': ['pk']}


class RecordDeletionWizard(DeletionWizard):
    model = models.ContextRecord
    fields = ['label', 'parcel', 'description', 'length', 'width', 'thickness',
              'depth', 'location', 'datings', 'units', 'documentations',
              'filling', 'interpretation', 'taq', 'taq_estimated', 'tpq',
              'tpq_estimated']
    filter_owns = {'selec-record_deletion': ['pk']}


class RecordSourceWizard(SourceWizard):
    model = models.ContextRecordSource
    wizard_done_window = reverse_lazy('show-contextrecordsource')


class RecordSourceDeletionWizard(DeletionWizard):
    model = models.ContextRecordSource
    fields = ['context_record', 'title', 'source_type', 'authors', ]