如何使用django-salesforce获取Salesforce附件文件的内容? [英] How to fetch Salesforce Attachment file content with django-salesforce?

查看:310
本文介绍了如何使用django-salesforce获取Salesforce附件文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Django 1.11应用程序,该应用程序使用django-salesforce与Salesforce进行通信,用作某些Salesforce对象的UI.

I'm developing a Django 1.11 app serving as a UI for some Salesforce Objects using django-salesforce for the communication with Salesforce.

我必须让用户可以选择从与他们的联系人相关的Salesforce附件对象中下载文件.

I have to give users the option to download files from Salesforce Attachment objects related to their Contact.

# Django Attachment model on my_app/models.py

...

class Attachment(models.Model):
    parent = models.ForeignKey(Contact, models.DO_NOTHING,
                               sf_read_only=models.NOT_UPDATEABLE)
    name = models.CharField(max_length=255, verbose_name='File Name')
    content_type = models.CharField(max_length=120, blank=True, null=True)
    body_length = models.IntegerField(sf_read_only=models.READ_ONLY)
    body = models.TextField()

...

在此附件模型上,我可以在body字段中访问文件内容的其余URL,但不能访问实际内容.

On this Attachment model I can access the rest URL for the file content on the body field, but not the actual content.

是否有一种方法可以从附件中获取文件内容,而不必为此专门实现OAuth客户端?

Is there a way of getting file content from the Attachment without having to implement an OAuth client just for this?

推荐答案

A)handle_api_exceptions

from salesforce.backend.driver import handle_api_exceptions
# from salesforce.dbapi.driver import handle_api_exceptions  # can be changed soon to this
from django.db import connections

session = connections['salesforce'].sf_session
rows = Attachment.objects.filter(...)
for row in rows:
    url = session.auth.instance_url + row.body
    blob = handle_api_exceptions(url, session.get).text

B)您可以通过

from salesforce.utils import get_soap_client
import base64

soap = get_soap_client('salesforce')
for ...:
    ret = soap.query("SELECT Body FROM Attachment WHERE Id = '...'")
    blob = base64.b64decode(ret)[0]['Body'])


有用的相关答案是:


Useful related answers are:

  • How to get files from Salesforce using Python
  • Uploading Attachments to Salesforce API via Beatbox, Python

编辑,我希望您的意思是用户=一些联系人,而不是与SFDC对象具有某种形式的付费许可证的用户相关的联系人.出于安全原因,他们应该仅通过您的网站下载,而不是直接下载.任何OAuth都可能无法改善它.

EDIT I expect that you mean User = some Contact, not a Contact necessarily related to SFDC object User with some form of paid license. They should download only through your site, not directly, for security reasons. Any OAuth can not probably improve it.

这篇关于如何使用django-salesforce获取Salesforce附件文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆