-
Notifications
You must be signed in to change notification settings - Fork 20
/
template.py
1105 lines (1056 loc) · 50.9 KB
/
template.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Email templates & preview
"""
#########################################################################
#Power Email is a module for Open ERP which enables it to send mails #
#########################################################################
# ##### # # # #### ### ### # # ## ### # #
# # # # # # # # # # # # # # # # # # #
# #### # # # # ### ### ### # # # # # # #
# # # # # # # # # # # # # #### # # #
# # # # # #### # # ### # # # # ### #### #
# Copyright (C) 2009 Sharoon Thomas #
# #
#This program is free software: you can redistribute it and/or modify #
#it under the terms of the GNU General Public License as published by #
#the Free Software Foundation, either version 3 of the License, or #
# 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 General Public License for more details. #
# #
#You should have received a copy of the GNU General Public License #
#along with this program. If not, see <http://www.gnu.org/licenses/>. #
#########################################################################
import base64
import random
import time
import types
import netsvc
LOGGER = netsvc.Logger()
TEMPLATE_ENGINES = []
from osv import osv, fields
from tools.translate import _
from tools.safe_eval import safe_eval
#Try and check the available templating engines
from mako.template import Template #For backward combatibility
try:
from mako.template import Template as MakoTemplate
from mako import exceptions
TEMPLATE_ENGINES.append(('mako', 'Mako Templates'))
except:
LOGGER.notifyChannel(
_("Power Email"),
netsvc.LOG_ERROR,
_("Mako templates not installed")
)
try:
from django.template import Context, Template as DjangoTemplate
#Workaround for bug:
#http://code.google.com/p/django-tagging/issues/detail?id=110
from django.conf import settings
settings.configure()
#Workaround ends
TEMPLATE_ENGINES.append(('django', 'Django Template'))
except:
LOGGER.notifyChannel(
_("Power Email"),
netsvc.LOG_ERROR,
_("Django templates not installed")
)
import tools
import report
import pooler
def send_on_create(self, cr, uid, vals, context=None):
id = self.old_create(cr, uid, vals, context)
templates = self.pool.get('poweremail.templates').browse(cr, uid, self.template_ids, context)
for template in templates:
# Ensure it's still configured to send on create
if template.send_on_create:
self.pool.get('poweremail.templates').generate_mail(cr, uid, template.id, [id], context)
return id
def send_on_write(self, cr, uid, ids, vals, context=None):
result = self.old_write(cr, uid, ids, vals, context)
templates = self.pool.get('poweremail.templates').browse(cr, uid, self.template_ids, context)
for template in templates:
# Ensure it's still configured to send on write
if template.send_on_write:
self.pool.get('poweremail.templates').generate_mail(cr, uid, template.id, ids, context)
return result
class actions(osv.osv):
_name = 'ir.actions.report.xml'
_inherit = 'ir.actions.report.xml'
def register_all(self, cr):
value = super(actions, self).register_all(cr)
if not 'poweremail.templates' in self.pool.obj_list():
return value
cr.execute("""
SELECT
pt.id,
im.model,
pt.send_on_create,
pt.send_on_write
FROM
poweremail_templates pt,
ir_model im
WHERE
pt.object_name = im.id
""")
for record in cr.fetchall():
id = record[0]
model = record[1]
soc = record[2]
sow = record[3]
obj = self.pool.get(model)
if not obj:
continue
if hasattr(obj, 'old_create'):
obj.create = obj.old_create
del obj.old_create
if hasattr(obj, 'old_write'):
obj.write = obj.old_write
del obj.old_write
if not hasattr(obj, 'template_ids'):
obj.template_ids = []
if soc:
obj.template_ids.append(id)
obj.old_create = obj.create
obj.create = types.MethodType(send_on_create, obj, osv.osv)
if sow:
obj.template_ids.append(id)
obj.old_write = obj.write
obj.write = types.MethodType(send_on_write, obj, osv.osv)
return value
actions()
def get_value(cursor, user, recid, message=None, template=None, context=None):
"""
Evaluates an expression and returns its value
@param cursor: Database Cursor
@param user: ID of current user
@param recid: ID of the target record under evaluation
@param message: The expression to be evaluated
@param template: BrowseRecord object of the current template
@param context: Open ERP Context
@return: Computed message (unicode) or u""
"""
pool = pooler.get_pool(cursor.dbname)
if message is None:
message = {}
#Returns the computed expression
if message:
try:
message = tools.ustr(message)
object = pool.get(template.model_int_name).browse(cursor, user, recid, context)
env = {
'user':pool.get('res.users').browse(cursor, user, user, context),
'db':cursor.dbname
}
if template.template_language == 'mako':
templ = MakoTemplate(message, input_encoding='utf-8')
reply = MakoTemplate(message).render_unicode(object=object,
peobject=object,
env=env,
format_exceptions=True)
elif template.template_language == 'django':
templ = DjangoTemplate(message)
env['object'] = object
env['peobject'] = object
reply = templ.render(Context(env))
return reply or False
except Exception:
return u""
else:
return message
class poweremail_templates(osv.osv):
"Templates for sending Email"
_name = "poweremail.templates"
_description = 'Power Email Templates for Models'
def change_model(self, cursor, user, ids, object_name, context=None):
if object_name:
mod_name = self.pool.get('ir.model').read(
cursor,
user,
object_name,
['model'], context)['model']
else:
mod_name = False
return {
'value':{'model_int_name':mod_name}
}
_columns = {
'name' : fields.char('Name of Template', size=100, required=True),
'object_name':fields.many2one('ir.model', 'Model'),
'model_int_name':fields.char('Model Internal Name', size=200,),
'def_to':fields.char(
'Recepient (To)',
size=250,
help="The default recepient of email. "
"Placeholders can be used here."),
'def_cc':fields.char(
'Default CC',
size=250,
help="The default CC for the email. "
"Placeholders can be used here."),
'def_bcc':fields.char(
'Default BCC',
size=250,
help="The default BCC for the email. "
"Placeholders can be used here."),
'lang':fields.char(
'Language',
size=250,
help="The default language for the email. "
"Placeholders can be used here. "
"eg. ${object.partner_id.lang}"),
'def_subject':fields.char(
'Default Subject',
size=200,
help="The default subject of email. "
"Placeholders can be used here.",
translate=True),
'def_body_text':fields.text(
'Standard Body (Text)',
help="The text version of the mail.",
translate=True),
'def_body_html':fields.text(
'Body (Text-Web Client Only)',
help="The text version of the mail.",
translate=True),
'use_sign':fields.boolean(
'Use Signature',
help="The signature from the User details "
"will be appened to the mail."),
'file_name':fields.char(
'File Name Pattern',
size=200,
help="File name pattern can be specified with placeholders. "
"eg. 2009_SO003.pdf",
translate=True),
'report_template':fields.many2one(
'ir.actions.report.xml',
'Report to send'),
#'report_template':fields.reference('Report to send',[('ir.actions.report.xml','Reports')],size=128),
'allowed_groups':fields.many2many(
'res.groups',
'template_group_rel',
'templ_id', 'group_id',
string="Allowed User Groups",
help="Only users from these groups will be "
"allowed to send mails from this Template."),
'enforce_from_account':fields.many2one(
'poweremail.core_accounts',
string="Enforce From Account",
help="Emails will be sent only from this account.",
domain="[('company','=','yes')]"),
'auto_email':fields.boolean('Auto Email',
help="Selecting Auto Email will create a server "
"action for you which automatically sends mail after a "
"new record is created.\nNote: Auto email can be enabled "
"only after saving template."),
'save_to_drafts':fields.boolean('Save to Drafts',
help="When automatically sending emails generated from"
" this template, save them into the Drafts folder rather"
" than sending them immediately."),
#Referred Stuff - Dont delete even if template is deleted
'attached_wkf':fields.many2one(
'workflow',
'Workflow'),
'attached_activity':fields.many2one(
'workflow.activity',
'Activity'),
#Referred Stuff - Delete these if template are deleted or they will crash the system
'server_action':fields.many2one(
'ir.actions.server',
'Related Server Action',
help="Corresponding server action is here."),
'ref_ir_act_window':fields.many2one(
'ir.actions.act_window',
'Window Action',
readonly=True),
'ref_ir_value':fields.many2one(
'ir.values',
'Wizard Button',
readonly=True),
#Expression Builder fields
#Simple Fields
'model_object_field':fields.many2one(
'ir.model.fields',
string="Field",
help="Select the field from the model you want to use."
"\nIf it is a relationship field you will be able to "
"choose the nested values in the box below.\n(Note: If "
"there are no values make sure you have selected the "
"correct model).",
store=False),
'sub_object':fields.many2one(
'ir.model',
'Sub-model',
help='When a relation field is used this field '
'will show you the type of field you have selected.',
store=False),
'sub_model_object_field':fields.many2one(
'ir.model.fields',
'Sub Field',
help="When you choose relationship fields "
"this field will specify the sub value you can use.",
store=False),
'null_value':fields.char(
'Null Value',
help="This Value is used if the field is empty.",
size=50, store=False),
'copyvalue':fields.char(
'Expression',
size=100,
help="Copy and paste the value in the "
"location you want to use a system value.",
store=False),
#Table Fields
'table_model_object_field':fields.many2one(
'ir.model.fields',
string="Table Field",
help="Select the field from the model you want to use."
"\nOnly one2many & many2many fields can be used for tables.",
store=False),
'table_sub_object':fields.many2one(
'ir.model',
'Table-model',
help="This field shows the model you will "
"be using for your table.", store=False),
'table_required_fields':fields.many2many(
'ir.model.fields',
'fields_table_rel',
'field_id', 'table_id',
string="Required Fields",
help="Select the fields you require in the table.",
store=False),
'table_html':fields.text(
'HTML code',
help="Copy this html code to your HTML message "
"body for displaying the info in your mail.",
store=False),
'send_on_create': fields.boolean(
'Send on Create',
help='Sends an e-mail when a new document is created.'),
'send_on_write': fields.boolean(
'Send on Update',
help='Sends an e-mail when a document is modified.'),
'partner_event': fields.char(
'Partner ID to log Events',
size=250,
help="Partner ID who an email event is logged.\n"
"Placeholders can be used here. eg. ${object.partner_id.id}\n"
"You must install the mail_gateway module to see the mail events "
"in partner form.\nIf you also want to record the link to the "
"object that sends the email, you must to add this object in the "
"'Administration/Low Level Objects/Requests/Accepted Links in "
"Requests' menu (or 'ir.attachment' to record the attachments)."),
'template_language':fields.selection(
TEMPLATE_ENGINES,
'Templating Language',
required=True
),
'single_email': fields.boolean("Single email", help="Check it if you want to send a single email for several records (the optional attachment will be generated as a single file for all these records). If you don't check it, an email with its optional attachment will be send for each record."),
'use_filter':fields.boolean(
'Active Filter',
help="This option allow you to add a custom python filter"
" before sending a mail"),
'filter':fields.text(
'Filter',
help="The python code entered here will be excecuted if the"
"result is True the mail will be send if it false the mail "
"won't be send.\n"
"Example : o.type == 'out_invoice' and o.number and o.number[:3]<>'os_' "),
}
_defaults = {
}
_sql_constraints = [
('name', 'unique (name)', _('The template name must be unique!'))
]
def update_auto_email(self, cr, uid, ids, context=None):
for template in self.browse(cr, uid, ids, context):
if template.auto_email:
if not template.server_action:
# Create server action if necessary
action_id = self.pool.get('ir.actions.server').create(cr, uid, {
'state': 'poweremail',
'poweremail_template': template.id,
'name': template.name,
'condition': 'True',
'model_id': template.object_name.id,
}, context)
self.write(cr, uid, template.id, {
'server_action': action_id,
}, context)
self.pool.get('workflow.activity').write(cr, uid, template.attached_activity.id, {
'action_id': action_id,
}, context)
else:
# Update activity if it was changed
activity_ids = self.pool.get('workflow.activity').search(cr, uid, [('action_id', '=', template.server_action.id)], context=context)
if not template.attached_activity.id in activity_ids:
self.pool.get('workflow.activity').write(cr, uid, activity_ids, {
'action_id': False,
}, context)
if template.attached_activity.id:
self.pool.get('workflow.activity').write(cr, uid, template.attached_activity.id, {
'action_id': template.server_action.id,
}, context)
elif template.server_action:
self.pool.get('ir.actions.server').unlink(cr, uid, template.server_action.id, context)
def update_send_on_store(self, cr, uid, ids, context):
for template in self.browse(cr, uid, ids, context):
obj = self.pool.get(template.object_name.model)
if hasattr(obj, 'old_create'):
obj.create = obj.old_create
del obj.old_create
if hasattr(obj, 'old_write'):
obj.write = obj.old_write
del obj.old_write
if not hasattr(obj, 'template_ids'):
obj.template_ids = []
if template.send_on_create:
obj.template_ids.append(template.id)
obj.old_create = obj.create
obj.create = types.MethodType(send_on_create, obj, osv.osv)
if template.send_on_write:
obj.template_ids.append(template.id)
obj.old_write = obj.write
obj.write = types.MethodType(send_on_write, obj, osv.osv)
def create(self, cr, uid, vals, context=None):
id = super(poweremail_templates, self).create(cr, uid, vals, context)
src_obj = self.pool.get('ir.model').read(cr, uid, vals['object_name'], ['model'], context)['model']
vals['ref_ir_act_window'] = self.pool.get('ir.actions.act_window').create(cr, uid, {
'name': _("%s Mail Form") % vals['name'],
'type': 'ir.actions.act_window',
'res_model': 'poweremail.send.wizard',
'src_model': src_obj,
'view_type': 'form',
'context': "{'src_model':'%s','template_id':'%d','src_rec_id':active_id,'src_rec_ids':active_ids}" % (src_obj, id),
'view_mode':'form,tree',
'view_id': self.pool.get('ir.ui.view').search(cr, uid, [('name', '=', 'poweremail.send.wizard.form')], context=context)[0],
'target': 'new',
'auto_refresh':1
}, context)
vals['ref_ir_value'] = self.pool.get('ir.values').create(cr, uid, {
'name': _('Send Mail (%s)') % vals['name'],
'model': src_obj,
'key2': 'client_action_multi',
'value': "ir.actions.act_window," + str(vals['ref_ir_act_window']),
'object': True,
}, context)
self.write(cr, uid, id, {
'ref_ir_act_window': vals['ref_ir_act_window'],
'ref_ir_value': vals['ref_ir_value'],
}, context)
if vals.get('auto_email'):
self.update_auto_email(cr, uid, [id], context)
if vals.get('send_on_create') or vals.get('send_on_write'):
self.update_send_on_store(cr, uid, [id], context)
#if vals.get('partner_event'):
# self.update_partner_event(cr, uid, [id], context)
return id
def write(self, cr, uid, ids, vals, context=None):
result = super(poweremail_templates, self).write(cr, uid, ids, vals, context)
if 'auto_email' in vals or 'attached_activity' in vals:
self.update_auto_email(cr, uid, ids, context)
if 'send_on_create' in vals or 'send_on_write' in vals:
self.update_send_on_store(cr, uid, ids, context)
#if 'partner_event' in vals:
# self.update_partner_event(cr, uid, ids, context)
return result
def unlink(self, cr, uid, ids, context=None):
for template in self.browse(cr, uid, ids, context):
obj = self.pool.get(template.object_name.model)
if hasattr(obj, 'old_create'):
obj.create = obj.old_create
del obj.old_create
if hasattr(obj, 'old_write'):
obj.write = obj.old_write
del obj.old_write
try:
if template.ref_ir_act_window:
self.pool.get('ir.actions.act_window').unlink(cr, uid, template.ref_ir_act_window.id, context)
if template.ref_ir_value:
self.pool.get('ir.values').unlink(cr, uid, template.ref_ir_value.id, context)
if template.server_action:
self.pool.get('ir.actions.server').unlink(cr, uid, template.server_action.id, context)
except:
raise osv.except_osv(_("Warning"), _("Deletion of Record failed"))
return super(poweremail_templates, self).unlink(cr, uid, ids, context)
def copy(self, cr, uid, id, default=None, context=None):
if default is None:
default = {}
default = default.copy()
old = self.read(cr, uid, id, ['name'], context=context)
new_name = _("Copy of template ") + old.get('name', 'No Name')
check = self.search(cr, uid, [('name', '=', new_name)], context=context)
if check:
new_name = new_name + '_' + random.choice('abcdefghij') + random.choice('lmnopqrs') + random.choice('tuvwzyz')
default.update({'name':new_name})
return super(poweremail_templates, self).copy(cr, uid, id, default, context)
def compute_pl(self,
model_object_field,
sub_model_object_field,
null_value, template_language='mako'):
"""
Returns the expression based on data provided
@param model_object_field: First level field
@param sub_model_object_field: Second level drilled down field (M2O)
@param null_value: What has to be returned if the value is empty
@param template_language: The language used for templating
@return: computed expression
"""
#Configure for MAKO
copy_val = ''
if template_language == 'mako':
if model_object_field:
copy_val = "${object." + model_object_field
if sub_model_object_field:
copy_val += "." + sub_model_object_field
if null_value:
copy_val += " or '" + null_value + "'"
if model_object_field:
copy_val += "}"
elif template_language == 'django':
if model_object_field:
copy_val = "{{object." + model_object_field
if sub_model_object_field:
copy_val += "." + sub_model_object_field
if null_value:
copy_val = copy_val + '|default:"' + null_value + '"'
copy_val = copy_val + "}}"
return copy_val
def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
if not model_object_field:
return {}
result = {}
field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
#Check if field is relational
if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
if res_ids:
result['sub_object'] = res_ids[0]
result['copyvalue'] = self.compute_pl(False,
False,
False,
template_language)
result['sub_model_object_field'] = False
result['null_value'] = False
else:
#Its a simple field... just compute placeholder
result['sub_object'] = False
result['copyvalue'] = self.compute_pl(field_obj.name,
False,
False,
template_language
)
result['sub_model_object_field'] = False
result['null_value'] = False
return {'value':result}
def onchange_sub_model_object_field(self, cr, uid, ids, model_object_field, sub_model_object_field, template_language, context=None):
if not model_object_field or not sub_model_object_field:
return {}
result = {}
field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
if res_ids:
result['sub_object'] = res_ids[0]
result['copyvalue'] = self.compute_pl(field_obj.name,
sub_field_obj.name,
False,
template_language
)
result['sub_model_object_field'] = sub_model_object_field
result['null_value'] = False
else:
#Its a simple field... just compute placeholder
result['sub_object'] = False
result['copyvalue'] = self.compute_pl(field_obj.name,
False,
False,
template_language
)
result['sub_model_object_field'] = False
result['null_value'] = False
return {'value':result}
def onchange_null_value(self, cr, uid, ids, model_object_field, sub_model_object_field, null_value, template_language, context=None):
if not model_object_field and not null_value:
return {}
result = {}
field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
if res_ids:
result['sub_object'] = res_ids[0]
result['copyvalue'] = self.compute_pl(field_obj.name,
sub_field_obj.name,
null_value,
template_language
)
result['sub_model_object_field'] = sub_model_object_field
result['null_value'] = null_value
else:
#Its a simple field... just compute placeholder
result['sub_object'] = False
result['copyvalue'] = self.compute_pl(field_obj.name,
False,
null_value,
template_language
)
result['sub_model_object_field'] = False
result['null_value'] = null_value
return {'value':result}
def onchange_table_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
if not model_object_field:
return {}
result = {}
field_obj = self.pool.get('ir.model.fields').browse(cr, uid, model_object_field, context)
if field_obj.ttype in ['many2one', 'one2many', 'many2many']:
res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
if res_ids:
result['table_sub_object'] = res_ids[0]
else:
#Its a simple field... just compute placeholder
result['sub_object'] = False
return {'value':result}
def onchange_table_required_fields(self, cr, uid, ids, table_model_object_field, table_required_fields, template_language, context=None):
if not table_model_object_field or not table_required_fields:
return {'value':{'table_html': False}}
result = ''
table_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, table_model_object_field, context)
field_obj = self.pool.get('ir.model.fields')
#Generate Html Header
result += "<p>\n<table border='1'>\n<thead>\n<tr>"
for each_rec in table_required_fields[0][2]:
result += "\n<td>"
record = field_obj.browse(cr, uid, each_rec, context)
result += record.field_description
result += "</td>"
result += "\n</tr>\n</thead>\n<tbody>\n"
#Table header is defined, now mako for table
#print "Language:", template_language
if template_language == 'mako':
result += "%for o in object." + table_field_obj.name + ":\n<tr>"
for each_rec in table_required_fields[0][2]:
result += "\n<td>${o."
record = field_obj.browse(cr, uid, each_rec, context)
result += record.name
result += "}</td>"
result += "\n</tr>\n%endfor\n</tbody>\n</table>\n</p>"
elif template_language == 'django':
result += "{% for o in object." + table_field_obj.name + " %}\n<tr>"
for each_rec in table_required_fields[0][2]:
result += "\n<td>{{o."
record = field_obj.browse(cr, uid, each_rec, context)
result += record.name
result += "}}</td>"
result += "\n</tr>\n{% endfor %}\n</tbody>\n</table>\n</p>"
return {'value':{'table_html':result}}
def _generate_partner_events(self,
cursor,
user,
template,
record_id,
mail,
context=None):
"""
Generates partner event if configured
@author: Jordi Esteve
@param cursor: Database Cursor
@param user: ID of User
@param template: Browse record of
template
@param record_id: ID of the target model
for which this mail has
to be generated
@param mail: Browse record of email object
@return: True
"""
name = mail.pem_subject
if isinstance(name, str):
name = unicode(name, 'utf-8')
if len(name) > 64:
name = name[:61] + '...'
model = res_id = False
if template.report_template:
if self.pool.get('res.request.link').search(
cursor,
user,
[('object', '=', template.model_int_name)],
context=context):
model = template.model_int_name
res_id = record_id
elif mail.pem_attachments_ids \
and self.pool.get('res.request.link').search(
cursor,
user,
[('object', '=', 'ir.attachment')],
context=context):
model = 'ir.attachment'
res_id = mail.pem_attachments_ids[0]
event_vals = {
'history': True,
'name': name,
'date': time.strftime('%Y-%m-%d %H:%M:%S'),
'user_id': user,
'email_from': mail.pem_from or None,
'email_to': mail.pem_to or None,
'email_cc': mail.pem_cc or None,
'email_bcc': mail.pem_bcc or None,
'message_id': mail.id,
'description': mail.pem_body_text and mail.pem_body_text or mail.pem_body_html,
'partner_id': get_value(cursor, user, record_id, template.partner_event, template, context),
'model': model,
'res_id': res_id,
}
self.pool.get('mailgate.message').create(cursor,
user,
event_vals,
context)
return True
def _generate_attach_reports(self,
cursor,
user,
template,
record_ids,
mail,
context=None):
"""
Generate report to be attached and attach it
to the email
@param cursor: Database Cursor
@param user: ID of User
@param template: Browse record of
template
@param record_ids: IDs of the target model
for which this mail has
to be generated
@param mail: Browse record of email object
@return: True
"""
lang = get_value(cursor,
user,
record_ids[0],
template.lang,
template,
context)
if lang:
ctx = context.copy()
ctx.update({'lang':lang})
template = self.browse(cursor, user, template.id, context=ctx)
reportname = 'report.' + \
self.pool.get('ir.actions.report.xml').read(
cursor,
user,
template.report_template.id,
['report_name'],
context)['report_name']
service = netsvc.LocalService(reportname)
data = {}
data['model'] = template.model_int_name
(result, format) = service.create(cursor,
user,
record_ids,
data,
context)
attachment_obj = self.pool.get('ir.attachment')
new_att_vals = {
'name': mail.pem_subject + ' (Email Attachment)',
'datas': base64.b64encode(result),
'datas_fname': tools.ustr(
get_value(
cursor,
user,
record_ids[0],
template.file_name,
template,
context
) or 'Report') + "." + format,
'description': mail.pem_subject or "No Description",
'res_model': 'poweremail.mailbox',
'res_id': mail.id
}
attachment_id = attachment_obj.create(cursor,
user,
new_att_vals,
context)
if attachment_id:
self.pool.get('poweremail.mailbox').write(
cursor,
user,
mail.id,
{
'pem_attachments_ids':[
[6, 0, [attachment_id]]
],
'mail_type':'multipart/mixed'
},
context)
return True
def _generate_mailbox_item_from_template(self,
cursor,
user,
template,
record_id,
context=None):
"""
Generates an email from the template for
record record_id of target object
@param cursor: Database Cursor
@param user: ID of User
@param template: Browse record of
template
@param record_id: ID of the target model
for which this mail has
to be generated
@return: ID of created object
"""
if context is None:
context = {}
#If account to send from is in context select it, else use enforced account
if 'account_id' in context.keys():
from_account = self.pool.get('poweremail.core_accounts').read(
cursor,
user,
context.get('account_id'),
['name', 'email_id'],
context
)
else:
from_account = {
'id':template.enforce_from_account.id,
'name':template.enforce_from_account.name,
'email_id':template.enforce_from_account.email_id
}
lang = get_value(cursor,
user,
record_id,
template.lang,
template,
context)
if lang:
ctx = context.copy()
ctx.update({'lang':lang})
template = self.browse(cursor, user, template.id, context=ctx)
mailbox_values = {
'pem_from': tools.ustr(from_account['name']) + \
"<" + tools.ustr(from_account['email_id']) + ">",
'pem_to':get_value(cursor,
user,
record_id,
template.def_to,
template,
context),
'pem_cc':get_value(cursor,
user,
record_id,
template.def_cc,
template,
context),
'pem_bcc':get_value(cursor,
user,
record_id,
template.def_bcc,
template,
context),
'pem_subject':get_value(cursor,
user,
record_id,
template.def_subject,
template,
context),
'pem_body_text':get_value(cursor,
user,
record_id,
template.def_body_text,
template,
context),
'pem_body_html':get_value(cursor,
user,
record_id,
template.def_body_html,
template,
context),
'pem_account_id' :from_account['id'],
#This is a mandatory field when automatic emails are sent
'state':'na',
'folder':'drafts',
'mail_type':'multipart/alternative'
}
#Use signatures if allowed
if template.use_sign:
sign = self.pool.get('res.users').read(cursor,
user,
user,
['signature'],
context)['signature']
if sign:
if mailbox_values['pem_body_text']:
mailbox_values['pem_body_text'] += "\n--\n"+sign
if mailbox_values['pem_body_html']:
mailbox_values['pem_body_html'] += sign
mailbox_id = self.pool.get('poweremail.mailbox').create(
cursor,
user,
mailbox_values,
context)
return mailbox_id
def generate_mail(self,
cursor,
user,
template_id,
record_ids,
context=None):
if context is None:
context = {}
template = self.browse(cursor, user, template_id, context=context)
if not template:
raise Exception("The requested template could not be loaded")
if template.use_filter and template.filter:
filtered_record_ids=[]
for record in self.pool.get(template.object_name.model).browse(cursor, user, record_ids, context=context):
if safe_eval(template.filter,
{'o':record, 'self':self, 'cr':cursor, 'context':context, 'uid': user}):
filtered_record_ids.append(record.id)
record_ids=filtered_record_ids
report_record_ids = record_ids[:]
if template.single_email and len(record_ids) > 1:
# We send a single email for several records
record_ids = record_ids[:1]
for record_id in record_ids:
mailbox_id = self._generate_mailbox_item_from_template(
cursor,
user,
template,
record_id,
context)
mail = self.pool.get('poweremail.mailbox').browse(
cursor,
user,
mailbox_id,
context=context
)
if template.report_template:
if template.single_email and len(report_record_ids) > 1:
# The optional attachment will be generated as a single file for all these records
self._generate_attach_reports(
cursor,
user,
template,
report_record_ids,
mail,
context
)
else:
self._generate_attach_reports(
cursor,
user,
template,
[record_id],
mail,
context
)
# Create a partner event
cursor.execute("SELECT state from ir_module_module where state='installed' and name = 'mail_gateway'")
mail_gateway = cursor.fetchall()