我有一个 Django 表单,它通过 django-storages 库将文件保存到 s3 并且工作正常。如何生成并返回预签名 URL,以便用户可以在文件上传后临时访问该文件?这是由 django-storages 抽象的还是我必须使用 boto3 api?
我花了几个小时浏览 Django-storages 文档,但是不太清楚如何做到这一点..
表格.py
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
name = models.CharField(max_length=20)
uploaded_at = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
视图.py
def upload_file(request):
if request.method == 'POST':
form = DocumentForm(request.POST)
if form.is_valid():
form.save()
url = ... # generate pre-signed url of uploaded file here
return render(request, 'upload_response.html', url)
Run Code Online (Sandbox Code Playgroud) 如何知道用户是否Enter使用过Python?
例如 :
user = raw_input("type in enter")
if user == "enter":
print "you pressed enter"
else:
print "you haven't pressed enter"
Run Code Online (Sandbox Code Playgroud) 我的 pytest 单元测试不断返回错误 ModuleNotFoundError: No module name billing
。
奇怪的send_invoices
是,当我删除补丁语句时,计费模块中的方法能够被调用。如果是这种情况,为什么mock.patch 无法找到计费模块并修补方法?
billing.py
import pdfkit
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
from projectxapp.models import User
Class Billing:
#creates a pdf of invoice. Takes an invoice dictionary
def create_invoice_pdf(self, invoice, user_id):
#get the html of the invoice
file_path ='/{}-{}.pdf'.format(user_id, invoice['billing_period'])
invoice_html = render_to_string('templates/invoice.html', invoice)
pdf = pdfkit.from_file(invoice_html, file_path)
return file_path, pdf
#sends invoice to customer
def send_invoices(self, invoices):
for user_id, invoice in invoices.items():
#get customers email
email = …
Run Code Online (Sandbox Code Playgroud) 为什么 Pytest 返回集合错误“在 test_billing_month_year:函数不使用参数“日期”,即使使用并定义了日期?
billing_month_year() 函数仅返回当前日期的上个月和上一年。
import datetime as dt
import pytest
from mock import patch
def billing_month_year():
today = dt.datetime.utcnow()
#last month from current date
last_month = today.month - 1 if today.month>1 else 12
#last year from current date
last_month_year = today.year if today.month > last_month else today.year - 1
return last_month, last_month_year
@pytest.mark.parametrize(
'date, expected',
[
#last month in previous year
(dt.datetime(year=2020, month=1, day=21), (12, 2019)),
#last month in current year
(dt.datetime(year=2020, month=2, day=21), (01, 2020)),
]
) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试从 boto3 模块修补 S3get_object
方法,但我不断收到以下错误
AttributeError: <function client at 0x104570200> does not have the attribute 'get_object'
这令人困惑,因为我能够成功修补boto3.client
但不能boto3.client.get_object
,即使 boto3 文档声明它是客户端的方法之一
这是我的代码
import boto3
from mock import patch
@pytest.mark.parametrize(
'response, expected',
[
(200, True),
(400,False)
]
)
@patch('boto3.client.get_object')
def test_get_file(mock, response, expected):
mock.return_values = response
test = get_file('portfolio/test.xls')
assert test == expected
def get_file(self, key):
S3 = boto3.client('s3')
response = S3.get_object(bucket='portfolios', key=key)
if response.status == 200:
return response
return False
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用创建 JSON 模型字段的副本.copy()
,但是我对副本所做的任何更改仍然反映在我不想要的数据库中。我可以迭代模型字段并创建一个新字典,但我认为有一种更Pythonic的方式。
模型.py
Class User(models.model):
settings = models.JSONField(default=dict(color = "Yellow", shape = "Square"))
Run Code Online (Sandbox Code Playgroud)
视图.py
...
user = User.objects.get(id=1)
settings = user.settings
settings_copy = user.settings.copy()
#settings model field is being changed here despite being a copy
settings_copy['color'] = Blue
...
Run Code Online (Sandbox Code Playgroud)