summaryrefslogtreecommitdiff
path: root/archaeological_finds/wizards.py
blob: 2352863eb330d8cc35a6d38647b694d6c703ff3f (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012  É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.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _

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

class ItemWizard(Wizard):
    model = models.Item

    def get_current_contextrecord(self, request, storage):
        step = storage.get_current_step()
        if not step:
            return
        if step.endswith('_creation'): # a context record has been selected
            main_form_key = 'selecrecord-' + self.url_name
            try:
                idx = int(self.session_get_value(request, storage,
                                        main_form_key, 'pk'))
                current_cr = models.ContextRecord.objects.get(pk=idx)
                return current_cr
            except(TypeError, ValueError, ObjectDoesNotExist):
                pass
        current_item = self.get_current_object(request, storage)
        if current_item:
            base_finds = current_item.base_finds.all()
            if base_finds:
                return base_finds[0].context_record

    def get_template_context(self, request, storage, form=None):
        """
        Get the operation and context record "reminder" on top of wizard forms
        """
        context = super(ItemWizard, self).get_template_context(request,
                                                            storage, form)
        current_cr = self.get_current_contextrecord(request, storage)
        if not current_cr:
            return context
        operation = current_cr.operation
        items = []
        if hasattr(operation, 'code_patriarche') and operation.code_patriarche:
            items.append(unicode(operation.code_patriarche))
        items.append("-".join((unicode(operation.year),
                               unicode(operation.operation_code))))
        reminder = unicode(_("Current operation: ")) + u" - ".join(items)
        reminder += u"<br/>" + unicode(_("Current context record: "))\
                             + unicode(current_cr.label)
        context['reminder'] = mark_safe(reminder)
        return context

    def get_extra_model(self, dct, request, storage, form_list):
        dct = super(ItemWizard, self).get_extra_model(dct, request, storage,
                                                      form_list)
        dct['order'] = 1
        if 'pk' in dct and type(dct['pk']) == models.ContextRecord:
            dct['base_finds__context_record'] = dct.pop('pk')
        return dct

class ItemModificationWizard(ItemWizard):
    modification = True

class TreatmentWizard(Wizard):
    model = models.Treatment

class ItemSourceWizard(SourceWizard):
    model = models.ItemSource

class ItemSourceDeletionWizard(DeletionWizard):
    model = models.ItemSource
    fields = ['item', 'title', 'source_type', 'authors',]

class TreatmentSourceWizard(SourceWizard):
    model = models.TreamentSource