|  | @@ -0,0 +1,210 @@
 | 
	
		
			
				|  |  | +# -*- coding: utf-8 -*-
 | 
	
		
			
				|  |  | +##############################################################################
 | 
	
		
			
				|  |  | +#
 | 
	
		
			
				|  |  | +#    Odoo, Open Source Management Solution
 | 
	
		
			
				|  |  | +#    Copyright (C) 2016 Prime Consulting SA, Cape Verde (<http://prime.cv>).
 | 
	
		
			
				|  |  | +#
 | 
	
		
			
				|  |  | +##############################################################################
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +from odoo import models, api, fields
 | 
	
		
			
				|  |  | +from odoo.exceptions import ValidationError, UserError
 | 
	
		
			
				|  |  | +from odoo.tools.translate import _
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import logging
 | 
	
		
			
				|  |  | +_logger = logging.getLogger(__name__)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import base64
 | 
	
		
			
				|  |  | +from nuxeo.nuxeo import Nuxeo as nx
 | 
	
		
			
				|  |  | +from nuxeo.blob import BufferBlob
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +class Users(models.Model):
 | 
	
		
			
				|  |  | +    _inherit = "res.users"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.model
 | 
	
		
			
				|  |  | +    def create(self, vals):
 | 
	
		
			
				|  |  | +        """Create Nuxeo User on creating new Odoo User
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        user = super(Users, self).create(vals)
 | 
	
		
			
				|  |  | +        #conn = user.get_nuxeo_connection()
 | 
	
		
			
				|  |  | +        #user.nuxeo_user_create(conn)
 | 
	
		
			
				|  |  | +        return user
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def write(self, vals):
 | 
	
		
			
				|  |  | +        for user in self:
 | 
	
		
			
				|  |  | +            user_login = user.login
 | 
	
		
			
				|  |  | +            
 | 
	
		
			
				|  |  | +            res = super(Users, self).write(vals)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            newlogin = vals.get('login', False)
 | 
	
		
			
				|  |  | +            password = vals.get('password', False)
 | 
	
		
			
				|  |  | +            #if newlogin or password:
 | 
	
		
			
				|  |  | +            #        conn = user.get_nuxeo_connection()
 | 
	
		
			
				|  |  | +            #        user.nuxeo_user_update(conn, user_login, newlogin, password)
 | 
	
		
			
				|  |  | +        return res
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def unlink(self):
 | 
	
		
			
				|  |  | +        #for user in self:
 | 
	
		
			
				|  |  | +            #conn = user.get_nuxeo_connection()
 | 
	
		
			
				|  |  | +            #user.nuxeo_user_delete(conn)
 | 
	
		
			
				|  |  | +        return super(Users, self).unlink()
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +#####################################
 | 
	
		
			
				|  |  | +#            Nuxeo API Calls        #
 | 
	
		
			
				|  |  | +#####################################
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def get_nuxeo_connection(self):
 | 
	
		
			
				|  |  | +        """ Nuxeo Server Connection 
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        nuxeo_settings = self.env['nuxeo.settings'].search([('id', '>', 0)], order="create_date asc", limit=1)
 | 
	
		
			
				|  |  | +        for nx_setting in nuxeo_settings:
 | 
	
		
			
				|  |  | +            url = nx_setting.url
 | 
	
		
			
				|  |  | +            login = nx_setting.login
 | 
	
		
			
				|  |  | +            password = nx_setting.password
 | 
	
		
			
				|  |  | +            conn = nx_setting.nuxeo_connection(url, login, password)[2]
 | 
	
		
			
				|  |  | +            return conn
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def nuxeo_user_create(self, conn=False):
 | 
	
		
			
				|  |  | +        """Odoo User - Nuxeo User link
 | 
	
		
			
				|  |  | +        :param user: Odoo User
 | 
	
		
			
				|  |  | +        :var username: login(Odoo) <=> username(Nuxeo)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        if conn:
 | 
	
		
			
				|  |  | +            for user in self:
 | 
	
		
			
				|  |  | +                login = user.login
 | 
	
		
			
				|  |  | +                try:
 | 
	
		
			
				|  |  | +                    nx_user = conn.users().fetch(login)
 | 
	
		
			
				|  |  | +                except Exception: #Odoo User don't exist in Nuxeo : Create Nuxeo User
 | 
	
		
			
				|  |  | +                    username = user.name.split(' ')
 | 
	
		
			
				|  |  | +                    args = {
 | 
	
		
			
				|  |  | +                            'lastName': username[-1], 
 | 
	
		
			
				|  |  | +                            'firstName': username[0], 
 | 
	
		
			
				|  |  | +                            'username': login, 
 | 
	
		
			
				|  |  | +                            'company': user.company_id.name,
 | 
	
		
			
				|  |  | +                            'password': user.password
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                    nx_user = conn.users().create(args)
 | 
	
		
			
				|  |  | +                    _logger.info("User created on Nuxeo Server. Username: %s"%(' '.join([args['firstName'], args['lastName']])))
 | 
	
		
			
				|  |  | +        return True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def nuxeo_user_update(self, conn=False, login=False, newlogin=False, password=False):
 | 
	
		
			
				|  |  | +        """On updating Odoo User's login/password, update login/password for related Nuxeo user
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        if conn:
 | 
	
		
			
				|  |  | +            for user in self:
 | 
	
		
			
				|  |  | +                try:
 | 
	
		
			
				|  |  | +                    nx_user = conn.users().fetch(login)
 | 
	
		
			
				|  |  | +                    if nx_user:
 | 
	
		
			
				|  |  | +                        if newlogin:
 | 
	
		
			
				|  |  | +                            nx_user.properties['username'] = newlogin
 | 
	
		
			
				|  |  | +                            nx_user.save()
 | 
	
		
			
				|  |  | +                        if password:
 | 
	
		
			
				|  |  | +                            nx_user.properties['password'] = password
 | 
	
		
			
				|  |  | +                            nx_user.save()
 | 
	
		
			
				|  |  | +                        _logger.info("User information updated on Nuxeo Server.")
 | 
	
		
			
				|  |  | +                except Exception, e:
 | 
	
		
			
				|  |  | +                    raise ValidationError('Connection to Nuxeo Server Failed! \nError Details:\n%s'%(e))
 | 
	
		
			
				|  |  | +        return True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def nuxeo_user_delete(self, conn=False):
 | 
	
		
			
				|  |  | +        """On Deleteing Odoo User, update related Nuxeo User first name
 | 
	
		
			
				|  |  | +        """
 | 
	
		
			
				|  |  | +        if conn:
 | 
	
		
			
				|  |  | +            for user in self:
 | 
	
		
			
				|  |  | +                login = user.login
 | 
	
		
			
				|  |  | +                try:
 | 
	
		
			
				|  |  | +                    nx_user = conn.users().fetch(login)
 | 
	
		
			
				|  |  | +                    nx_user.properties['firstName'] = str(nx_user.properties['firstName']) + ' (deleted)'
 | 
	
		
			
				|  |  | +                    nx_user.save()
 | 
	
		
			
				|  |  | +                    nx_user.properties['username'] = False
 | 
	
		
			
				|  |  | +                    nx_user.save()
 | 
	
		
			
				|  |  | +                except Exception:
 | 
	
		
			
				|  |  | +                    pass
 | 
	
		
			
				|  |  | +        return True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def nuxeo_filestore_directory_check(self, conn=False):
 | 
	
		
			
				|  |  | +        _logger.info("Checking Filestore directory exists or not.")
 | 
	
		
			
				|  |  | +        dbname = str(self.env.cr.dbname)
 | 
	
		
			
				|  |  | +        upload_dir = 'Filestore_%s'%(dbname)
 | 
	
		
			
				|  |  | +        odooDir = {
 | 
	
		
			
				|  |  | +                    'entity-type': 'document',
 | 
	
		
			
				|  |  | +                    'name': 'Odoo',
 | 
	
		
			
				|  |  | +                    'type':'Folder', 
 | 
	
		
			
				|  |  | +                    'properties': {'dc:title': 'Odoo'}
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +        filestoreDir = {
 | 
	
		
			
				|  |  | +                    'entity-type': 'document',
 | 
	
		
			
				|  |  | +                    'name': upload_dir,
 | 
	
		
			
				|  |  | +                    'type':'Folder', 
 | 
	
		
			
				|  |  | +                    'properties': {'dc:title': upload_dir}
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +        if conn:
 | 
	
		
			
				|  |  | +            try:
 | 
	
		
			
				|  |  | +                conn.repository().fetch('/default-domain/workspaces/Odoo') 
 | 
	
		
			
				|  |  | +                _logger.info('"Odoo" directory already exists in Workspaces.')
 | 
	
		
			
				|  |  | +            except Exception:#create directory "Odoo"
 | 
	
		
			
				|  |  | +                repo = conn.repository(schemas=['dublincore']) 
 | 
	
		
			
				|  |  | +                repo.create('/default-domain/workspaces/', odooDir)
 | 
	
		
			
				|  |  | +                _logger.info('New Directory "Odoo" created in Workspaces.')
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            try:
 | 
	
		
			
				|  |  | +                conn.repository().fetch('/default-domain/workspaces/Odoo/' + upload_dir) 
 | 
	
		
			
				|  |  | +                _logger.info('"%s" directory already exists in Workspaces.'%(upload_dir))
 | 
	
		
			
				|  |  | +            except Exception:#create directory "Filestore_dbname"
 | 
	
		
			
				|  |  | +                repo = conn.repository(schemas=['dublincore']) 
 | 
	
		
			
				|  |  | +                repo.create('/default-domain/workspaces/Odoo/', filestoreDir)
 | 
	
		
			
				|  |  | +                _logger.info('New Directory "%s" created in Workspaces.'%(upload_dir))
 | 
	
		
			
				|  |  | +        return True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def nuxeo_upload_document(self, conn=False, attachment=False):
 | 
	
		
			
				|  |  | +        newDoc = {
 | 
	
		
			
				|  |  | +                    'name': 'Document',
 | 
	
		
			
				|  |  | +                    'type': 'File',
 | 
	
		
			
				|  |  | +                    'properties': {'dc:title': attachment.name}
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        filename = attachment.datas_fname
 | 
	
		
			
				|  |  | +        if not filename:
 | 
	
		
			
				|  |  | +            filename = str(attachment._name) + '.' + str(attachment.id)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        filestoreDir = 'Filestore_' + str(self.env.cr.dbname)
 | 
	
		
			
				|  |  | +        try:
 | 
	
		
			
				|  |  | +            conn.repository().fetch('/default-domain/workspaces/Odoo/' + filestoreDir) 
 | 
	
		
			
				|  |  | +        except Exception, e:
 | 
	
		
			
				|  |  | +            self.nuxeo_filestore_directory_check(conn)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        try:            
 | 
	
		
			
				|  |  | +            repo = conn.repository(schemas=['dublincore']) 
 | 
	
		
			
				|  |  | +            doc = repo.create('/default-domain/workspaces/Odoo/' + filestoreDir, newDoc)
 | 
	
		
			
				|  |  | +            file_content = base64.b64decode(attachment.datas)
 | 
	
		
			
				|  |  | +            blob = BufferBlob(file_content, filename, attachment.mimetype)
 | 
	
		
			
				|  |  | +            blob = conn.batch_upload().upload(blob)
 | 
	
		
			
				|  |  | +            doc.properties["file:content"] = blob
 | 
	
		
			
				|  |  | +            doc.save()
 | 
	
		
			
				|  |  | +            document_id = doc.get_id()
 | 
	
		
			
				|  |  | +            _logger.info("Document Successfully Uploaded to Nuxeo Server. File: %s Document id: %s"%(filename, document_id))
 | 
	
		
			
				|  |  | +            attachment.write({'nuxeo_document_id': document_id, 'type': 'nuxeo'})
 | 
	
		
			
				|  |  | +        except Exception, e:
 | 
	
		
			
				|  |  | +            _logger.warn("Document Upload to Nuxeo server Failed!!!\nError : %s"%(e))
 | 
	
		
			
				|  |  | +        return True
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @api.multi
 | 
	
		
			
				|  |  | +    def nuxeo_fetch_document(self, conn=False, document_id=False):
 | 
	
		
			
				|  |  | +        if conn and document_id:
 | 
	
		
			
				|  |  | +            doc = conn.repository().fetch(document_id)
 | 
	
		
			
				|  |  | +            if doc:
 | 
	
		
			
				|  |  | +                return base64.b64encode(doc.fetch_blob())
 | 
	
		
			
				|  |  | +        return False
 |