Commits

Raimon Esteve (Zikzakmedia) committed 9eaafef

Active Record

  • Participants
  • Parent commits 8240f01

Comments (0)

Files changed (10)

+Version 2.6.0 - 2012-10-16
+* Active Record
+* Simplify module information with python configuration
+
 Version 2.4.0 - 2012-09-07
 * Initial release
 ==============================
 
 The electronic_mail_wizard module of the Tryton application platform.
-See __tryton__.py
 
 Installing
 ----------
 #This file is part electronic_mail_wizard module for Tryton.
 #The COPYRIGHT file at the top level of this repository contains 
 #the full copyright notices and license terms.
-
+from trytond.pool import Pool
 from .electronic_mail_wizard import *
 from .template import *
 from .action import *
+
+def register():
+    Pool.register(
+        TemplateEmailStart,
+        TemplateEmailResult,
+        module='electronic_mail_wizard', type_='model')
+    Pool.register(
+        GenerateTemplateEmail,
+        # ExampleGenerateTemplateEmail,
+        module='electronic_mail_wizard', type_='wizard')

__tryton__.py

-#This file is part electronic_mail_wizard module for Tryton.
-#The COPYRIGHT file at the top level of this repository contains 
-#the full copyright notices and license terms.
-{
-    'name': 'Electronic Mail Wirzard',
-    'name_ca_ES': 'Assistent Correu electrònic',
-    'name_es_ES': 'Asistente Correo electrónico',
-    'version': '2.4.0',
-    'author': 'Zikzakmedia',
-    'email': 'zikzak@zikzakmedia.com',
-    'website': 'http://www.zikzakmedia.com/',
-    'description': '''Electronic Mail Wirzard''',
-    'description_ca_ES': '''Asistent Correu electrònic''',
-    'description_es_ES': '''Asistente Correo electrónico''',
-    'depends': [
-        'ir',
-        'res',
-        'electronic_mail_template',
-    ],
-    'xml': [
-        'electronic_mail_wizard.xml',
-        'template.xml',
-    ],
-    'translation': [
-        # 'locale/ca_ES.po',
-        # 'locale/es_ES.po',
-    ]
-}
 
 from trytond.model import ModelView, ModelSQL, fields
 
+__all__ = ['ActionWizard']
+
 class ActionWizard(ModelSQL, ModelView):
-    _name = 'ir.action.wizard'
+    __name__ = 'ir.action.wizard'
 
     template = fields.One2Many("electronic.mail.template", 'wizard', 'Template')
 
-ActionWizard()

electronic_mail_wizard.py

 from trytond.transaction import Transaction
 from trytond.pool import Pool
 
+__all__ = ['TemplateEmailStart', 'TemplateEmailResult',
+    'GenerateTemplateEmail', 'ExampleGenerateTemplateEmail']
+
 class TemplateEmailStart(ModelView):
     'Template Email Start'
-    _name = 'electronic.mail.wizard.templateemail.start'
-    _description = __doc__
+    __name__ = 'electronic.mail.wizard.templateemail.start'
 
     from_ = fields.Char('From', readonly=True)
     sender = fields.Char('Sender', required=True)
     model = fields.Many2One(
         'ir.model', 'Model', required=True, select="1")
 
-TemplateEmailStart()
 
 class TemplateEmailResult(ModelView):
     'Template Email Result'
-    _name = 'electronic.mail.wizard.templateemail.result'
-    _description = __doc__
+    __name__ = 'electronic.mail.wizard.templateemail.result'
 
     name = fields.Char('Name', help='Name of Header Field')
 
-TemplateEmailResult()
 
 class GenerateTemplateEmail(Wizard):
     "Generate Email from template"
-    _name = "electronic_mail_wizard.templateemail"
+    __name__ = "electronic_mail_wizard.templateemail"
 
     start = StateView('electronic.mail.wizard.templateemail.start',
         'electronic_mail_wizard.templateemail_start', [
             ])
     send = StateTransition()
 
-    def __init__(self):
-        super(GenerateTemplateEmail, self).__init__()
-        self._error_messages.update({
+    @classmethod
+    def __setup__(cls):
+        super(GenerateTemplateEmail, cls).__setup__()
+        cls._error_messages.update({
             'template_missing': 'You can select a template in this wizard.',
             })
 
         :param values: Dicctionary values
         :return: 'email.message.Message' instance
         '''
-        template_obj = Pool().get('electronic.mail.template')
+        Template = Pool().get('electronic.mail.template')
 
         message = MIMEMultipart('alternative')
         message['date'] = formatdate(localtime=1)
 
         language = Transaction().context.get('language', 'en_US')
         if template.language:
-            language = template_obj.eval(template, template.language, record)
+            language = Template.eval(template, template.language, record)
 
         with Transaction().set_context(language = language):
-            template = template_obj.browse(template.id)
+            template = Template.browse(template.id)
 
-            message['from_'] = template_obj.eval(template, values['from_'], record)
-            message['to'] = template_obj.eval(template, values['to'], record)
-            message['cc'] = template_obj.eval(template, values['cc'], record)
-            message['bcc'] = template_obj.eval(template, values['bcc'], record)
-            message['subject'] = template_obj.eval(template, values['subject'], record)
+            message['from_'] = Template.eval(template, values['from_'], record)
+            message['to'] = Template.eval(template, values['to'], record)
+            message['cc'] = Template.eval(template, values['cc'], record)
+            message['bcc'] = Template.eval(template, values['bcc'], record)
+            message['subject'] = Template.eval(template, values['subject'], record)
 
             # Attach reports
             if template.reports:
-                reports = template_obj.render_reports(
+                reports = Template.render_reports(
                     template, record
                     )
                 for report in reports:
                     ext, data, filename, file_name = report[0:5]
                     if file_name:
-                        filename = template_obj.eval(template, file_name, record)
+                        filename = Template.eval(template, file_name, record)
                     filename = ext and '%s.%s' % (filename, ext) or filename
                     content_type, _ = mimetypes.guess_type(filename)
                     maintype, subtype = (
                     message.attach(attachment)
 
             # HTML & Text Alternate parts
-            plain = template_obj.eval(template, values['plain'], record)
+            plain = Template.eval(template, values['plain'], record)
             if template.signature:
-                user_obj = Pool().get('res.user')
-                user = user_obj.browse(Transaction().user)
+                User = Pool().get('res.user')
+                user = User.browse(Transaction().user)
                 if user.signature:
                     signature = user.signature.encode("ASCII", 'ignore')
                     plain = '%s\n--\n%s' % (plain, signature)
                     header.name,
                     unicode(self.eval(template, header.value, record))
                 )
-
         return message
 
     def render_fields(self, name):
         '''
         default = {}
 
-        wizard_obj = Pool().get('ir.action.wizard')
-        template_obj = Pool().get('electronic.mail.template')
+        Wizard = Pool().get('ir.action.wizard')
+        Template = Pool().get('electronic.mail.template')
         active_ids = Transaction().context.get('active_ids')
 
-        wizards = wizard_obj.search(['wiz_name','=',name])
+        wizards = Wizard.search(['wiz_name','=',name])
         if not len(wizards) > 0:
             return default
-        wizard = wizard_obj.browse(wizards[0])
+        wizard = Wizard.browse(wizards[0])
         if not wizard.template:
             self.raise_user_error('template_missing')
         template = wizard.template[0]
             default['plain'] = template.plain
         else: #show fields with rendered tags
             record = Pool().get(template.model.model).browse(active_ids[0]) 
-            default['to'] = template_obj.eval(template, template.to, record)
-            default['cc'] = template_obj.eval(template, template.cc, record)
-            default['bcc'] = template_obj.eval(template, template.bcc, record)
-            default['subject'] = template_obj.eval(template, template.subject, record)
-            default['plain'] = template_obj.eval(template, template.plain, record)
+            default['to'] = Template.eval(template, template.to, record)
+            default['cc'] = Template.eval(template, template.cc, record)
+            default['bcc'] = Template.eval(template, template.bcc, record)
+            default['subject'] = Template.eval(template, template.subject, record)
+            default['plain'] = Template.eval(template, template.plain, record)
         return default
 
-    def render_and_send(self, session):
-        email_obj = Pool().get('electronic.mail')
-        template_obj = Pool().get('electronic.mail.template')
+    def render_and_send(self):
+        Email = Pool().get('electronic.mail')
+        Template = Pool().get('electronic.mail.template')
 
-        template = session.start.template
-        model = session.start.model
+        template = self.start.template
+        model = self.start.model
 
         for active_id in Transaction().context.get('active_ids'):
             record = Pool().get(model.model).browse(active_id)
             values = {}
-            values['from_'] = session.start.from_
-            values['to'] = session.start.to
-            values['cc'] = session.start.cc
-            values['bcc'] = session.start.bcc
-            values['subject'] = session.start.subject
-            values['plain'] = session.start.plain
+            values['from_'] = self.start.from_
+            values['to'] = self.start.to
+            values['cc'] = self.start.cc
+            values['bcc'] = self.start.bcc
+            values['subject'] = self.start.subject
+            values['plain'] = self.start.plain
             
             email_message = self.render(template, record, values)
-            email_id = email_obj.create_from_email(
+            email_id = Email.create_from_email(
                 email_message, template.mailbox.id)
-            template_obj.send_email(email_id, template)
+            Template.send_email(email_id, template)
 
             Pool().get('electronic.mail.template').add_event(template, record, email_id, email_message) #add event
-
         return True
 
-GenerateTemplateEmail()
 
 class ExampleGenerateTemplateEmail(GenerateTemplateEmail):
     "Example Wizard to Generate Email from template"
-    _name = "electronic_mail_wizard.example"
+    __name__ = "electronic_mail_wizard.example"
 
-    def default_start(self, session, fields):
+    def default_start(self, fields):
         default = self.render_fields(self._name)
         return default
 
-    def transition_send(self, session):
-        self.render_and_send(session)
+    def transition_send(self):
+        self.render_and_send()
         return 'end'
-
-ExampleGenerateTemplateEmail()

electronic_mail_wizard.xml

 The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. -->
 <tryton>
     <data>
-
         <record model="ir.ui.view" id="templateemail_start">
             <field name="model">electronic.mail.wizard.templateemail.start</field>
             <field name="type">form</field>
                 ]]>
             </field>
         </record>
-
     </data>
 </tryton>
 
 from setuptools import setup
 import re
+import os
+import ConfigParser
 
-info = eval(open('__tryton__.py').read())
+config = ConfigParser.ConfigParser()
+config.readfp(open('tryton.cfg'))
+info = dict(config.items('tryton'))
+for key in ('depends', 'extras_depend', 'xml'):
+    if key in info:
+        info[key] = info[key].strip().splitlines()
 major_version, minor_version, _ = info.get('version', '0.0.1').split('.', 2)
 major_version = int(major_version)
 minor_version = int(minor_version)
 
 setup(name='trytond_electronic_mail_wizard',
     version=info.get('version', '0.0.1'),
-    description=info.get('description', ''),
-    author=info.get('author', 'Zikzakmedia'),
-    author_email=info.get('email', 'zikzak@zikzakmedia.com'),
-    url=info.get('website', 'http://www.zikzakmedia.com'),
+    description='Tryton module to add wizard in template mails',
+    author='Zikzakmedia SL',
+    author_email='zikzak@zikzakmedia.com',
+    url='http://www.zikzakmedia.com',
     download_url="https://bitbucket.org/zikzakmedia/trytond-electronic_mail_wizard",
     package_dir={'trytond.modules.electronic_mail_wizard': '.'},
     packages=[
     ],
     package_data={
         'trytond.modules.electronic_mail_wizard': info.get('xml', []) \
-                + info.get('translation', []),
+            + ['tryton.cfg', 'locale/*.po'],
     },
     classifiers=[
         'Development Status :: 5 - Production/Stable',
 #This file is part electronic_mail_wizard module for Tryton.
 #The COPYRIGHT file at the top level of this repository contains 
 #the full copyright notices and license terms.
+from trytond.model import fields
+from trytond.pool import PoolMeta
 
-from trytond.model import ModelView, ModelSQL, fields
+__all__ = ['Template']
+__metaclass__ = PoolMeta
 
-class Template(ModelSQL, ModelView):
-    _name = 'electronic.mail.template'
+class Template:
+    'Email Template'
+    __name__ = 'electronic.mail.template'
 
     wizard = fields.Many2One("ir.action.wizard", 'Wizard')
 
-Template()
+[tryton]
+version=2.6.0
+depends:
+    ir
+    res
+    electronic_mail_template
+xml:
+    electronic_mail_wizard.xml
+    template.xml