-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathattachment.py
143 lines (125 loc) · 5.25 KB
/
attachment.py
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
# This file is part of Coog. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from unidecode import unidecode
from trytond.pool import PoolMeta, Pool
from trytond.model import fields, ModelView
from trytond.pyson import Eval
__all__ = [
'Attachment',
]
class Attachment(metaclass=PoolMeta):
__name__ = 'ir.attachment'
signatures = fields.One2Many('document.signature', 'attachment',
'Signatures')
signature = fields.Function(
fields.Many2One('document.signature', 'Signature'),
'getter_signature')
can_create_new_signature = fields.Function(
fields.Boolean('Can Create New Signature'),
'on_change_with_can_create_new_signature')
can_see_signatures = fields.Function(
fields.Boolean('Can See Signatures'),
'on_change_with_can_see_signatures')
can_force_signature = fields.Function(
fields.Boolean('Can Force Signature'),
'on_change_with_can_force_signature')
@classmethod
def __setup__(cls):
super(Attachment, cls).__setup__()
cls._buttons.update({
'init_new_signature_process': {
'invisible': ~Eval('can_create_new_signature')},
'manually_create_forced_signature': {
'invisible': ~Eval('can_force_signature')}
})
@classmethod
def view_attributes(cls):
return super(Attachment, cls).view_attributes() + [
("/form/notebook/page[@id='e-signature']", 'states', {
'invisible': ~Eval('can_see_signatures', False)}),
]
def getter_signature(self, name):
if len(self.signatures) == 1:
return self.signatures[0].id
elif len([s for s in self.signatures if s.status == 'completed']) > 0:
return [s for s in self.signatures
if s.status == 'completed'][-1].id
else:
pendings = [s for s in self.signatures if s.status not in [
'failed', 'expired', 'canceled', 'completed',
'manually_forced']]
if pendings:
return pendings[-1].id
return self.signatures[-1].id if self.signatures else None
def update_with_signed_document(self, signature):
self.data = signature.get_documents()
self.save()
@classmethod
@ModelView.button
def init_new_signature_process(cls, attachments):
for attachment in attachments:
attachment.create_new_signature(ignore_manual=False)
cls.save(attachments)
def create_new_signature(self, report=None, from_object=None,
config=None, ignore_manual=True):
Signature = Pool().get('document.signature')
signatures = list(getattr(self, 'signatures', []))
report = self.get_struct_for_signature(report)
if report:
signature = Signature.request_transaction(report, self,
from_object or self.origin,
config or self.get_signature_configuration(), ignore_manual)
if signature:
signatures.append(signature)
self.signatures = signatures
@classmethod
@ModelView.button
def manually_create_forced_signature(cls, attachments):
Signature = Pool().get('document.signature')
signatures_to_save = []
for attachment in attachments:
if not attachment.signatures:
signature = Signature(status='issued')
attachment.signature = signature
attachment.signatures = [signature]
signatures_to_save.append(signature)
Signature.save(signatures_to_save)
Signature.manually_force([a.signature for a in attachments])
def get_party(self, report=None):
if report and 'party' in report:
return report['party']
elif self.resource.__name__ == 'party.party':
return self.resource
def get_struct_for_signature(self, report=None):
if not report:
report = {
'report_name': self.name,
'data': self.data,
}
report['report_name'] = unidecode(report['report_name'])
party = self.get_party(report)
if party and party.email:
report['signers'] = [party]
return report
def get_signature_configuration(self):
return None
@fields.depends('signatures')
def on_change_with_can_create_new_signature(self, name=None):
if not self.signatures:
return True
elif self.signature.status == 'completed':
# The process is completed
return False
elif any([s.status in ['issued', 'ready',
'pending_validation'] for s in self.signatures]):
# Pending process
return False
elif self.signature.status in ['expired', 'canceled', 'failed']:
# Process failed, we'll start a new one
return True
@fields.depends('signatures')
def on_change_with_can_force_signature(self, name=None):
return not self.signatures or self.signature.status not in [
'completed', 'manually_forced']
def on_change_with_can_see_signatures(self, name=None):
return True