~openerp-community/openobject-addons/openerp-evernote

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
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2011 Serpent Consulting Services (<http://serpentcs.com>).
#
#    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/>.
#
##############################################################################

from osv import fields, osv
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
import evernote
from evernote.api.client import EvernoteClient
import evernote.edam.type.ttypes as Types
from evernote.edam.notestore import NoteStore
import xml.etree.ElementTree as etree
import time
import re
from datetime import datetime
from  create_evernote_atta import EvernoteUpload


class openerp_evernote(osv.osv):
    
    _name = 'openerp.evernote'
    
    _columns = {
                'name' : fields.char("Evernote", size=256),
                'evernote_id' : fields.many2one("product.product"),
                'url' : fields.text("URL"),
                'description' : fields.text('Description'),
                'date' : fields.date("Date")
                }   
    
    def onchange_url(self, cr, uid, ids, url, evernote_id, context=None):
        if context is None:
            context = {}
        
        if not url:
            raise osv.except_osv(('Warning!'), ('Please enter  url !'))
        if url[0:16] != 'https://www.ever':
            raise osv.except_osv(('Warning!'), ('Please check url !'))
            
        guid = re.findall('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}', url)
    
        if not guid:
            raise osv.except_osv(('Warning!'), ('Please check url !'))
      
        eve_config_obj = self.pool.get('evernote.configuration')
        auth_token = eve_config_obj.browse(cr,uid,1,context=context).auth_token
        
#         auth_token = "S=s411:U=45ed069:E=18fb45c2014:C=14647706903:P=1dd:A=en-w32-xauth-new:V=2:H=f8d727f0a58a18a43a0d5ac8350f1a94"
        try:
            client = EvernoteClient(token=auth_token, sandbox=True)
        except : 
            raise osv.except_osv(('Warning!'), ('Please check token information!'))
        
                
        note = client.get_note_store().getNote(guid[0], True, True, True, True)
#         noteStore = client.get_note_store()
#         getnote = noteStore.getNote(guid[0], True, True, True, True)
        note_created = note.created
        note_date = time.strftime('%Y-%m-%d', time.gmtime(note_created / 1000))
        xml = etree.fromstring(note.content)
        content = xml.text
        title = note.title
        return {'value' : {'name' : title, 'description': content , 'date' : note_date}}

openerp_evernote()


class product_product(osv.osv):

    _inherit = 'product.product'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
    
product_product()

class purchase_order(osv.osv):
   
    _inherit = 'purchase.order'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
    
purchase_order()

class res_partner(osv.osv):
    
    _inherit = 'res.partner'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
   
res_partner()

class bank_statement(osv.osv):
  
    _inherit = 'account.bank.statement'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
    
bank_statement()

class hr_timesheet_sheet(osv.osv):
   
    _inherit = 'hr_timesheet_sheet.sheet'
                
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
   
hr_timesheet_sheet()

class hr_expenses_expenses(osv.osv):
   
    _inherit = 'hr.expense.expense'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
   
hr_expenses_expenses()

class stock_picking_in(osv.osv):
    
    _inherit = 'stock.picking.in'
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
   
stock_picking_in()


class project_project(osv.osv):
   
    _inherit = 'project.project'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
project_project()

class account_bank_statement_line(osv.osv):
    
    _inherit = 'account.bank.statement.line'
    
    _columns = {
                'evernote_ids' : fields.one2many("openerp.evernote", "evernote_id", "Evernote"),
                }
   
account_bank_statement_line()

class project_task(osv.osv):

    _inherit = 'project.task'
    
    _columns = {
                'note_created' : fields.boolean("Created Note" , readonly=1),
    }
   
    def create_note(self, cr, uid, ids, context=None):
        
        eve_config_obj = self.pool.get('evernote.configuration')
        auth_token = eve_config_obj.browse(cr,uid,1,context=context).auth_token
        try:
            client = EvernoteClient(token=auth_token, sandbox=False)
        except : 
            raise osv.except_osv(('Warning!'), ('Please check token information!'))
         
        c1 = EvernoteUpload(auth_token)
        c1.upload_to_notebook('report1.pdf', 'boom')
        note_data = self.browse(cr, uid, ids[0], context=context)
        if not note_data.note_created:
            try:
                note_store = client.get_note_store()
                notebooks = note_store.listNotebooks()
                note = Types.Note()
                note.title = str(note_data.name)
                note_cont = str(note_data.description)
                note.content = '<?xml version="1.0" encoding="UTF-8"?>'
                note.content += '<!DOCTYPE en-note SYSTEM ' \
                    '"http://xml.evernote.com/pub/enml2.dtd">'
                note.content += '<en-note>'
                note.content += str(note_cont)
                note.content += '</en-note>'

                created_note = note_store.createNote(note)
                self.write(cr, uid, ids[0], {'note_created':1}, context=context)
            except:
                raise osv.except_osv(('Warning !'), ('Please check internet connection.!'))
        else:
            raise osv.except_osv(('Error!'), ('Already Created.!'))
        return True
    
    
    def wizard_call(self, cr, uid, ids, context=None):
        self_data = self.browse(cr, uid, ids, context=context)
        if not self_data[0].note_created:
            return {
                'view_type':'form',
                'view_mode':'form',
                'res_model':'create.note.wizard',
                'target' : 'new',
                'type':'ir.actions.act_window',
                'context':context,
            }
        else:
            raise osv.except_osv(('Warning!'), ('Already Created.!'))
            
project_task()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: