PSU Django应用程序的文件上载
psu-upload的Python项目详细描述
上载PSU
可重复使用的Django应用程序,专门针对PSU的定制web应用程序。
提供使用psu_base插件在任何站点上载文件的功能。在
快速入门
依赖性
以下依赖项是必需的,并且必须安装在应用程序中:
安装
pip install psu-upload
配置
- 在Django应用程序中配置psu-base
- 将
psu-upload
添加到您的requirements.txt
- 在
settings.py
中向已安装的应用程序添加PSU上载和存储: ^{pr2}$ 在 - 配置应用程序的顶层
urls.py
以包含上载视图:
注意:从版本1.0.0开始,不存在有用的url,但是可以在以后的版本中添加urlpatterns=[...path('upload/',include(('psu_upload.urls','psu_upload'),namespace='upload')),]
在
S3中存储的附加配置
如果不使用Amazon S3,则不需要这些步骤
- 将以下设置添加到
settings.py
:DEFAULT_FILE_STORAGE='storages.backends.s3boto3.S3Boto3Storage'AWS_STORAGE_BUCKET_NAME='<Your App Name>.wdt.pdx.edu'AWS_S3_ENDPOINT_URL="https://s3-us-west-2.amazonaws.com/<Your App Name>.wdt.pdx.edu"AWS_DEFAULT_REGION="us-west-2"AWS_S3_REGION_NAME="us-west-2"AWS_S3_SIGNATURE_VERSION='s3v4'AWS_S3_FILE_OVERWRITE=FalseAWS_DEFAULT_ACL=None
在 - 将以下设置添加到
local_settings.py
:AWS_ACCESS_KEY_ID='<Put this in local_settings.py>'AWS_SECRET_ACCESS_KEY='<Put this in local_settings.py>'
在 - 在
if_aws
条件下,settings.py
添加以下内容:# File Upload SettingsAWS_ACCESS_KEY_ID=os.environ.get('AWS_ACCESS_KEY_ID',None)AWS_SECRET_ACCESS_KEY=os.environ.get('AWS_SECRET_ACCESS_KEY',None)
在
使用
限制接受的文件类型
默认情况下只接受jpg,office文档类型。
您可以在settings.py
中使用ALLOWED_UPLOAD_TYPES = [...]
允许的类型列表可能包括以下任何或全部:
- 文件扩展名(['pdf','.txt',…]),它将自动转换为MIME类型
- 实际MIME类型(['text/plain'])
- 定义的文档类型组(['office','image'])
将根据MIME类型(不仅仅是文件扩展名)自动为您完成验证
从单个输入上载一个或多个文件
使用此函数:
upload_service.upload_files(request, input_name, sequenced_filename=None, parent_directory=None):
"""
Action for saving one or more uploaded file
Parameters:
request - The Django request object
input_name - The name of the file input in your HTML form
sequenced_filename - Gives all files the same name with a sequence number appended
- Retain original filename if not specified
parent_directory - The directory (within your app directory) the files will live in on AWS/S3
"""
步骤:
- 确保表单标记有
method="post"
和enctype="multipart/form-data"
- 确保您的文件输入标记有
multiple="multiple"
- 在您看来:
frompsu_upload.servicesimportupload_servicedefmy_view(request):# Keep original file name (S3 will append junk to it if the name is already taken)uploaded_files=upload_service.upload_files(request,'<file_input_name>')# ... OR ...# Specify a new file name (will append sequence numbers for multiple files)uploaded_files=upload_service.upload_files(request,'<file_input_name>',sequenced_filename='example-',parent_directory='examples')
上载单个文件(不允许多次上载)
使用此函数:
upload_service.upload_file(file_instance, specified_filename=None, parent_directory=None):
"""
Action for saving ONE uploaded file
Parameters:
file_instance - The in-memory file to be saved (from request.FILES)
specified_filename - Rename the file (automatically retains the original extension)
- Retains the original filename if not specified
parent_directory - The directory (within your app directory) the file will live in on AWS/S3
"""
- 确保表单标记有
method="post"
和enctype="multipart/form-data"
- 在您看来:
frompsu_upload.servicesimportupload_servicedefmy_view(request):ifrequest.method=='POST':# Keep original file nameuploaded_file=upload_service.upload_file(request.FILES['<file_input_name>'])# ... OR ...# Specify a new file name (retains the original extension)# Specify a parent directory in S3uploaded_file=upload_service.upload_file(request.FILES['<file_input_name>'],specified_filename='my_new_filename',parent_directory='all_files/my_files/important-files')
读取文件而不将其保存在任何位置
使用以下函数:
# For a single file
upload_service.read_uploaded_file(request, input_name, byte_limit=500000, convert_to_string=True):
"""
Return the contents of an uploaded file without actually saving it anywhere
"""
# For multiple files
upload_service.read_uploaded_files(request, input_name, byte_limit=500000, convert_to_string=True):
"""
Return the contents of uploaded files without actually saving them anywhere
Returns a dict with the file name as the key, and contents as the value
"""
模板标记
打印指向文件的链接:
{% load upload_taglib %}
{%file_link file=file_instance%}
Examples of optional attrs:
{%file_link file=file_instance size=True %} <!-- Show file size -->
{%file_link file=file_instance icon='fa fa-star' %} <!-- specify file icon -->
{%file_link file=file_instance icon=False %} <!-- no file icon -->
有关可用属性,请参阅代码。任何意外的属性(id、style等)都将包含在包装中。
在标记中显示数据库文件
这可能不适用于S3文件,但尚未尝试过
{% load upload_taglib %}
{%database_image file=file_instance %}
Any attrs that can appear in an <img /> tag can also be provided.
对于开发者
必须为每个PyPi版本更新版本号。
版本号在psu_upload/__init__.py
文件更改
在docs/CHANGELOG.txt中记录每次更改
发布到PyPi
- 在PyPi和Test PyPi上创建帐户
- 创建
~/.pypirc
在[distutils] index-servers= pypi testpypi [testpypi] repository: https://test.pypi.org/legacy/ username: mikegostomski password: pa$$w0rd [pypi] username: mikegostomski password: pa$$w0rd
- 请现有开发人员将您添加为协作者-test和/或prod
python setup.py sdist bdist_wheel --universal
- ^{19}
twine upload dist/*
- 在Git中标记发布。别忘了推标签! 示例:
git tag 0.1.2 git push origin 0.1.2
- 项目
标签: