def sync(): # Connect to the source repo sourceClient = CmisClient(settings.SOURCE_REPOSITORY_URL, settings.SOURCE_USERNAME, settings.SOURCE_PASSWORD) sourceRepo = sourceClient.defaultRepository dumpRepoHeader(sourceRepo, "SOURCE") # Make sure it supports changes, bail if it does not if sourceRepo.getCapabilities()['Changes'] == None: print "Source repository does not support changes:" + sourceRepo.getCapabilities( )['Changes'] sys.exit(-1) latestChangeToken = sourceRepo.info['latestChangeLogToken'] print "Latest change token: %s" % latestChangeToken # Connect to the target repo targetClient = CmisClient(settings.TARGET_REPOSITORY_URL, settings.TARGET_USERNAME, settings.TARGET_PASSWORD) targetRepo = targetClient.defaultRepository dumpRepoHeader(targetRepo, "TARGET") print " Path: %s" % settings.TARGET_ROOT # Get last token synced from savefile # Using the repository IDs so that you can use this script against # multiple source-target pairs and it will remember where you are syncKey = "%s><%s" % (sourceRepo.id, targetRepo.id) lastChangeSynced = {} changeToken = None if (os.path.exists(SAVE_FILE)): lastChangeSynced = pickle.load(open(SAVE_FILE, "rb")) if lastChangeSynced.has_key(syncKey): print "Last change synced: %s" % lastChangeSynced[syncKey] changeToken = lastChangeSynced[syncKey] else: print "First sync..." else: print "First sync..." if changeToken == latestChangeToken: print "No changes since last sync so no work to do" return # Ask the source repo for changes changes = None if changeToken != None: changes = sourceRepo.getContentChanges(changeLogToken=changeToken) else: changes = sourceRepo.getContentChanges() # Process each change for change in changes: if change.changeType == 'created' or change.changeType == 'updated': processChange(change, sourceRepo, targetRepo) lastChangeSynced[syncKey] = latestChangeToken pickle.dump(lastChangeSynced, open(SAVE_FILE, "wb")) return
def setLastSync(changeToken): sourceClient = CmisClient(settings.SOURCE_REPOSITORY_URL, settings.SOURCE_USERNAME, settings.SOURCE_PASSWORD) sourceRepo = sourceClient.defaultRepository targetClient = CmisClient(settings.TARGET_REPOSITORY_URL, settings.TARGET_USERNAME, settings.TARGET_PASSWORD) targetRepo = targetClient.defaultRepository syncKey = "%s><%s" % (sourceRepo.id, targetRepo.id) lastChangeSynced = {syncKey: changeToken} pickle.dump(lastChangeSynced, open(SAVE_FILE, "wb")) print "Forced last sync to: %s" % changeToken
def _auth(self, cr, uid, ids, context=None): """Test connection with CMIS""" if context is None: context = self.pool['res.users'].context_get(cr, uid) # Get the url, user and password for CMIS # ids = self.search(cr, uid, []) if not ids: raise orm.except_orm( _('Internal Error'), _('Something very wrong happened. _auth() ' 'called without any ids.')) if type(ids) is not list: ids = [ids] res = self.read(cr, uid, ids, ['location', 'username', 'password'], context=context)[0] url = res['location'] user_name = res['username'] user_password = res['password'] client = CmisClient(url, user_name, user_password) try: return client.defaultRepository except cmislib.exceptions.ObjectNotFoundException: raise orm.except_orm(_('CMIS connection Error!'), _("Check your CMIS account configuration.")) except cmislib.exceptions.PermissionDeniedException: raise orm.except_orm(_('CMIS connection Error!'), _("Check your CMIS account configuration.")) except urllib2.URLError: raise orm.except_orm(_('CMIS connection Error!'), _("SERVER is down."))
def testCmisClient(self): """Instantiate a CmisClient object""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) assert cmisClient is not None
def get_cmis_client(self): """ Get an initialized CmisClient using the CMISBrowserBinding """ self.ensure_one() return CmisClient(self.location, self.username, self.password, binding=BrowserBinding())
def testCmisClientBadUrl(self): """Try to instantiate a CmisClient object with a known bad URL""" cmisClient = CmisClient(self.url + 'foobar', self.user, self.pwd, binding=self.binding, **self.ext_args) with pytest.raises(CmisException): cmisClient.getRepositories()
def testGetRepositoryBadId(self): """Try to get a repository with a bad repo ID""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) with pytest.raises(ObjectNotFoundException): cmisClient.getRepository('123FOO')
def testDefaultRepository(self): """Get the default repository by calling the repo's service URL""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repo = cmisClient.getDefaultRepository() assert repo is not None assert repo.getRepositoryId() is not None
def testTypeDefinition(self): """Get the cmis:document type and test a few props of the type.""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repo = cmisClient.getDefaultRepository() docTypeDef = repo.getTypeDefinition('cmis:document') assert 'cmis:document' == docTypeDef.getTypeId() assert docTypeDef.baseId
def get_repository(): try: URL_CMIS = "http://%s:%d/alfresco/cmisatom" % (settings.IP_SERVER, settings.PORT_SERVER) client = CmisClient(URL_CMIS, settings.USERNAME_ALF, settings.PASSWORD_ALF) repo = client.defaultRepository return repo, True except: return False, make_response( jsonify({'error': 'Authentication Failed'}), 503)
def testGetRepository(self): """Get a repository by repository ID""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repo = cmisClient.getDefaultRepository() defaultRepoId = repo.getRepositoryId() defaultRepoName = repo.getRepositoryName() repo = cmisClient.getRepository(defaultRepoId) assert defaultRepoId == repo.getRepositoryId() assert defaultRepoName == repo.getRepositoryName()
def testGetRepositories(self): """Call getRepositories and make sure at least one comes back with an ID and a name """ cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repoInfo = cmisClient.getRepositories() assert len(repoInfo) >= 1 assert 'repositoryId' in repoInfo[0] assert 'repositoryName' in repoInfo[0]
def testTypeProperties(self): """Get the properties for a type.""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repo = cmisClient.getDefaultRepository() docTypeDef = repo.getTypeDefinition('cmis:document') assert 'cmis:document' == docTypeDef.getTypeId() props = docTypeDef.getProperties().values() assert len(props) > 0 for prop in props: if prop.queryable: assert prop.queryName assert prop.propertyType
def testTypeDescendants(self): """Get the descendant types of the repository.""" cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repo = cmisClient.getDefaultRepository() typeDefs = repo.getTypeDescendants() folderDef = None for typeDef in typeDefs: if typeDef.getTypeId() == 'cmis:folder': folderDef = typeDef break assert folderDef assert folderDef.baseId
def testTypeChildren(self): """Get the child types for this repository and make sure cmis:folder is in the list.""" # This test would be more interesting if there was a standard way to # deploy a custom model. Then we could look for custom types. cmisClient = CmisClient(self.url, self.user, self.pwd, binding=self.binding, **self.ext_args) repo = cmisClient.getDefaultRepository() typeDefs = repo.getTypeChildren() folderDef = None for typeDef in typeDefs: if typeDef.getTypeId() == 'cmis:folder': folderDef = typeDef break assert folderDef assert folderDef.baseId
def cmis_env(request): """Initialize a cmis environement with * CmisClient * repo * rootFolder * test folder name * test folder All these attributes are reset after each test method """ _generate_conf(request) param = request.param request.cls._cmisClient = CmisClient(param.url, param.user, param.pwd, binding=param.binding, **param.ext_args) request.cls._repo = request.cls._cmisClient.getDefaultRepository() request.cls._rootFolder = request.cls._repo.getRootFolder() request.cls._folderName = " ".join( ['cmislib', request.cls.__name__, str(time())]) request.cls._testFolder = request.cls._rootFolder.createFolder( request.cls._folderName) productVersion = request.cls._repo.getRepositoryInfo().get( 'productVersion', '9.9.9') request.cls._productVersion = _Version(productVersion) yield request try: request.cls._testFolder.deleteTree() except NotSupportedException: print("Couldn't delete test folder because deleteTree is not " "supported") except RuntimeException: # deleting a folder could fail if the indexation of a new document # is in progress sleep(5) try: request.cls._testFolder.deleteTree() except RuntimeException: print("Couldn't delete test folder")
def submit_document(doc, method=None): repository_disabled = frappe.db.get_single_value("Repository", "disabled") if repository_disabled == 0: mapping_path = frappe.db.get_value("Mapping", {"doc_type": doc.doctype}, "doc_path") if mapping_path is not None: mapping_disabled = frappe.db.get_value("Mapping", {"doc_type": doc.doctype}, "doc_type_disabled") if mapping_disabled == 0: try: repositoryURL = frappe.db.get_single_value("Repository", "url") userName = frappe.db.get_single_value("Repository", "userid") userPass = frappe.db.get_single_value("Repository", "password") #msgprint("RepURL ... "+repositoryURL+" "+userName+" "+userPass, [doc]) client = CmisClient(repositoryURL, userName, userPass) repo = client.defaultRepository mapping_name = frappe.db.get_value("Mapping", {"doc_type": doc.doctype}, "name") mapping_type = frappe.db.get_value("Mapping", {"doc_type": doc.doctype}, "doc_type_alfresco") mapping_user = frappe.db.get_value("User Mapping", {"erpnext_user": frappe.session.user}, "alfresco_user", None, False, False) if mapping_user is None: mapping_user = "******" mapping_properties = frappe.db.get_values("Mapping Item", {"parent": mapping_name}, ["db_field", "alfresco_aspect", "alfresco_property"], as_dict=True) #for map_det in mapping_properties: # print map_det docu = createDoc(doc, repo, mapping_path, mapping_type, mapping_properties) except Exception, e: #print e frappe.throw(_("<p>Cannot connect to Alfresco Repository</p><p><ul><li>Please check the Repository Settings</li><li>Disable the Repository</li><li>Disable the DocType Mapping</li></ul></p>")) # msgprint("Document submitted ... ", [doc]) else: #pass msgprint("Doctype disabled ... ", [doc]) else: pass
import os, pwd uid = pwd.getpwuid(os.getuid())[0] storage = Storage("Jeff's Sample Python App", uid) http = Http( disable_ssl_certificate_validation=True) # Should not have to do this! flow = flow_from_clientsecrets('client_secrets.json', scope='public_api', redirect_uri='http://localhost:8080/') credentials = storage.get() if credentials == None: credentials = run(flow, storage, http=http) storage.put(credentials) print "Your access_token is: %s" % credentials.access_token http = credentials.authorize(http) headers = {'Authorization': 'Bearer ' + credentials.access_token} client = CmisClient('https://api.alfresco.com/cmis/versions/1.0/atom', '', '', headers=headers) repo = client.defaultRepository print "cmislib connected to:" print " Name: %s" % repo.getRepositoryInfo()['repositoryId'] print " Vendor: %s" % repo.getRepositoryInfo()['vendorName'] print " Version: %s" % repo.getRepositoryInfo()['productVersion']
def load_cmis(cmis_foldername, cmis_objtype, cmis_choices): es_host = ES_HOSTS[0] headers = {'Content-Type': 'application/json'} if 'http_auth' in es_host: headers['http_auth'] = es_host['http_auth'] host = es_host['host'] doc_type = "cmis" url = "http://" + host + ":9200/" + "cmis" cmis_dirname = os.path.join(BASE_DIR, 'data', 'cmis') client = CmisClient('http://cmis.alfresco.com/cmisatom', 'admin', 'admin') repositories = client.getRepositories() repo = client.defaultRepository info = repo.info #for k,v in info.items(): # print("key {0}, value: {1}".format(k, v)) caps = repo.capabilities if 'recreate' in cmis_choices: map_cmis(url, headers, client, repo, cmis_objtype, cmis_choices) objtype = repo.getTypeDefinition(cmis_objtype) root = repo.getRootFolder() count = 1 objects = root.getDescendants(depth=3) len(objects.getResults()) for object in objects: doc = {} #repo_obj = repo.getObject(object.id) #for key,val in repo_obj.properties.items(): # print(key, val) doc_props = object.properties #print(object.name) #for prop_name, prop_value in doc_props.items(): # print(prop_name ,prop_value) doc_objtype = doc_props['cmis:objectTypeId'] doc_objtype_str = str(doc_objtype) if doc_objtype_str == cmis_objtype: paths = object.getPaths() doc['paths'] = paths parents = object.getObjectParents() for prop_name, prop in objtype.properties.items(): #field = prop.displayName field = prop.id field_type = prop.getPropertyType() prop_value = doc_props[prop_name] sub_doc = doc # in case of nesting sub_fields = field.split('.') last_sub_field = sub_fields[len(sub_fields) - 1] if len(sub_fields) > 1: for sub_field in sub_fields[:-1]: if sub_field not in sub_doc: if sub_field in nested_fields: sub_doc[sub_field] = [{}] else: sub_doc[sub_field] = {} else: if sub_field in nested_fields: sub_doc[sub_field].append({}) sub_field_value = sub_doc[sub_field] if type(sub_field_value) is dict: sub_doc = sub_doc[sub_field] if type(sub_field_value) is list: sub_doc = sub_doc[sub_field][ len(sub_doc[sub_field]) - 1] if last_sub_field not in properties and last_sub_field in nested_fields: pass field = last_sub_field if field_type == 'list': if field not in doc: sub_doc[field] = [] if prop_value != "": if len(format) > 0: delimiter = format prop_value = str(prop_value) if delimiter == '\\n': items = prop_value.splitlines() else: items = prop_value.split(delimiter) for item in items: item = prop_value(item, decoder) sub_doc[field].append(item) else: sub_doc[field].append(cell) elif field_type == 'nested': if field not in sub_doc: sub_doc[field] = [] if prop_value != '': if answer == '': nested_value = prop_value.split(',') sub_doc[field].append({ 'val': nested_value[0], 'prc': float(nested_value[1]) }) else: sub_doc[field].append({ 'val': answer, 'prc': float(cell) }) elif field_type == 'datetime': sub_doc[field] = prop_value.strftime('%Y-%m-%d') elif field_type == 'text' or field_type == 'string': prop_value = str(prop_value) if field not in doc: sub_doc[field] = prop_value else: sub_doc[field] = sub_doc[field] + format + prop_value else: sub_doc[field] = prop_value # write metadata to ES id = object.properties['cmis:objectId'] id = slugify(str(id)) data = json.dumps(doc) print("load_cmis: write cmis line with id", id) r = requests.put(url + "/" + doc_type + "/" + id, headers=headers, data=data) print("load_cmis: writen cmis line with id", id) # download document doc_basename = doc_props.get('cmis:name', None) doc_mime_type = doc_props.get('cmis:contentStreamMimeType', None) doc_length = doc_props.get('cmis:contentStreamLength', 0) cmis_fullname = os.path.join(cmis_dirname, doc_basename) cmis_file_handler = object.getContentStream() buffer = cmis_file_handler.read() try: with open(cmis_fullname, 'w') as output_file: output_file.write(buffer) except IOError as e: pass cmis_file_handler.close() count = count + 1
weprep = weprep + "<hr>" body = "This is a project to show system load. Using a share amp to expose create update delete behaviors, using a webscript and paper.js with ajax calls. This content is created on the fly using the awesome cmislib (thanks Jeff Potts) to build this content" ftab = "<table width=500 height=30 border=2>" ftab = ftab + "<tr>" ftab = ftab + "<td>Alfresco Hackathon</td><td>marsbard and digcat</td><td>DataVisProject</td><td>1</td>" ftab = ftab + "</tr>" ftab = ftab + "<tr>" ftab = ftab + "<td colspan=4>" + body + "</td>" ftab = ftab + "</tr>" ftab = ftab + "</table>" weprep = weprep + ftab cmurl = 'https://' + hostname + '/alfresco/cmisatom' client = CmisClient(cmurl, 'admin', 'admin') sitename = 'alfresco-hackathon' basepath = '/Sites/' + sitename + '/documentLibrary' rootofwn = basepath + '/' + rootfoldername repo = client.defaultRepository try: rootsite = repo.getObjectByPath(rootofwn) except: rootsite = repo.getObjectByPath(basepath) basefolder = rootsite.createFolder(rootfoldername) try: rootsite = repo.getObjectByPath(rootofwn) except: print "Error: Unable to create folder"
def auth(self): backend_record = self.backend_record self.client = CmisClient(backend_record.location, backend_record.username, backend_record.password)
def open_report_wizard(self, context): print "context .......", context partner_id = context.get('partner_id') uid = context.get('uid') email = self.env['res.partner'].browse(partner_id).email email_uid = self.env['res.partner'].browse(uid).email print "email ....", email_uid configs = self.env['office.alfresco.configuration'].search([ ('is_default', '=', 'True') ])[0] url = configs.url port = configs.port user = configs.user mp = configs.mp try: client = CmisClient( 'http://' + url + ':' + repr(port) + '/alfresco/service/cmis', user, mp) repo = client.defaultRepository except: print 'failed to connect to Alfresco' quit() objet = 'Devis ' + self.reference print "objet .......", objet self.env.cr.execute("DELETE FROM mail_wiz") self.env.cr.execute( "INSERT INTO mail_wiz(id, objet, email, email_from) VALUES(%s,%s,%s,%s)", ('1', objet, email, email_uid)) # *** debut recuperation du rapport --------------------------------------------------------------- document_wiz_rapport = self.env['document_wiz'].create({ 'data': self._rapport_content_jasper(), 'nom_fichier': 'Devis ' + self.reference + '.pdf' }) ir_attachement_rapport = self.env['ir.attachment'].create({ 'datas': self._rapport_content_jasper(), 'datas_fname': 'devis ' + self.reference + '.pdf', 'name': 'Devis ' + self.reference + '.pdf', 'res_model': self._name }) self.env.cr.execute( "INSERT INTO document_wiz_mail_wiz_rel VALUES('1','" + repr(document_wiz_rapport[0].id) + "')") # &&&& Fin recuperation du rapport ----------------------------------------------------------------- # *** debut recuperation des documents ------------------------------------------------------------- obj_courant = self.env['sale.devis'].search([ ('reference', '=', self._get_id_devis(self.env.context)) ]) print "reference obj courant=====", obj_courant.reference, '.....', self.reference, 'self...', self.id products_list = self.devis_lines print "produits===", products_list for product in products_list: product_obj = product.product_id print "product id===", product_obj print "id===", product_obj.id documents_product = self.env[ 'office.document.alfresco.produit'].search([ ('produit_id', '=', product_obj.product_tmpl_id.id) ]) print "document id ======", documents_product for document in documents_product: doc = repo.getObject(document.node) result = doc.getContentStream() fobj = tempfile.NamedTemporaryFile(delete=False) fname = fobj.name fobj.write(result.read()) fobj.close() file_base64 = '' with open(fname, "rb") as file: file_base64 = base64.encodestring(file.read()) document_wiz_piece_jointe = self.env[ 'document_wiz'].create({ 'data': file_base64, 'nom_fichier': document.nom_fichier }) self.env.cr.execute( "INSERT INTO document_wiz_mail_wiz_rel VALUES('1','" + repr(document_wiz_piece_jointe[0].id) + "')") ir_attachement_rapport = self.env['ir.attachment'].create({ 'datas': file_base64, 'datas_fname': document.nom_fichier, 'name': document.nom_fichier, 'res_model': self._name }) # &&&& Fin recuperation des documents ----------------------------------------------------------------- return { 'view_type': 'form', 'view_mode': 'form', 'res_model': 'mail_wiz', 'res_id': int(1), 'view_id ref= report_wizard_form_view1': True, 'type': 'ir.actions.act_window', 'target': 'new', }
def setup(self): self._client = CmisClient(REPOSITORY_URL, USERNAME, PASSWORD) self._repo = self._client.defaultRepository