Skip to content

Instantly share code, notes, and snippets.

@DhruvaDave
Created December 19, 2020 14:04
Show Gist options
  • Save DhruvaDave/7b9e2bd00ae5f0e0e6095082de3c315a to your computer and use it in GitHub Desktop.
Save DhruvaDave/7b9e2bd00ae5f0e0e6095082de3c315a to your computer and use it in GitHub Desktop.
def create_sharefile_link(self):
self.create_user_sharelink()
config_obj = self.env['ir.config_parameter']
parent_id = config_obj.sudo().get_param('sharefile.sharefile_parent_id') # for Odoo
parent_id = "123"
folder_name = "Test"
folder_desc = "Test desc"
hostname, username, password, client_id, client_secret = self.get_config_data()
token = self.authenticate(hostname, client_id, client_secret, username, password)
self.create_folder(hostname, token, parent_id, folder_name, folder_desc)
else:
raise ValidationError(_("Please select customer to generate shareFile link"))
def create_folder(self,hostname, token, parent_id, name, description):
uri_path = '/sf/v3/Items(%s)/Folder'%(parent_id)
folder = {'Name':name, 'Description':description}
headers = self.get_authorization_header(token)
headers['Content-Type'] = 'application/json'
http = httplib.HTTPSConnection(self.get_hostname(token))
http.request('POST', uri_path, json.dumps(folder), headers=headers)
response = http.getresponse()
print(response.status, response.reason)
folder_id = False
if response.status == 200:
new_folder = json.loads(response.read())
print("new_folder",new_folder)
print('Created Folder %s'%(new_folder['Id']))
self.shareFile_link = 'https://%s/f/%s'% (hostname, new_folder['Id'])
self.shareFile_item_id = new_folder['Id']
# Access control to item
accecss_uri_path = '/sf/v3/Items(%s)/AccessControls'%(new_folder['Id'])
http = httplib.HTTPSConnection(self.get_hostname(token))
accecss_data = {
"Principal":{"Email":self.partner_id.email},
"CanUpload":True,
"CanDownload":True,
"CanView":True,
"CanDelete":True,
"CanManagePermissions":True,
"Message":"Message"
}
http.request('POST', accecss_uri_path,json.dumps(accecss_data), headers=headers)
response = http.getresponse()
if response.status == 200:
new_auth = json.loads(response.read())
elif response.status == 409: #conflict folder name
folder_id = self.get_folder_with_query_parameters(token,parent_id,name)
if folder_id:
self.shareFile_link = 'https://%s/f/%s'%(hostname,folder_id)
self.shareFile_item_id = folder_id
else:
raise UserError(_('Something went wrong.'))
else:
raise UserError(_('Operation not allowed.(%s)') % (response.reason))
http.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment