PSU Django应用程序的文件上载

psu-upload的Python项目详细描述


上载PSU

可重复使用的Django应用程序,专门针对PSU的定制web应用程序。
提供使用psu_base插件在任何站点上载文件的功能。在

快速入门

依赖性

以下依赖项是必需的,并且必须安装在应用程序中:

安装

pip install psu-upload

配置

  1. 在Django应用程序中配置psu-base
  2. psu-upload添加到您的requirements.txt
  3. settings.py中向已安装的应用程序添加PSU上载和存储: ^{pr2}$ 在
  4. 配置应用程序的顶层urls.py以包含上载视图:
    注意:从版本1.0.0开始,不存在有用的url,但是可以在以后的版本中添加
    urlpatterns=[...path('upload/',include(('psu_upload.urls','psu_upload'),namespace='upload')),]

S3中存储的附加配置

如果不使用Amazon S3,则不需要这些步骤

  1. 将以下设置添加到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
  2. 将以下设置添加到local_settings.py
    AWS_ACCESS_KEY_ID='<Put this in local_settings.py>'AWS_SECRET_ACCESS_KEY='<Put this in local_settings.py>'
  3. 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 = [...]
允许的类型列表可能包括以下任何或全部:

  1. 文件扩展名(['pdf','.txt',…]),它将自动转换为MIME类型
  2. 实际MIME类型(['text/plain'])
  3. 定义的文档类型组(['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
    """

步骤:

  1. 确保表单标记有method="post"enctype="multipart/form-data"
  2. 确保您的文件输入标记有multiple="multiple"
  3. 在您看来:
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
    """
  1. 确保表单标记有method="post"enctype="multipart/form-data"
  2. 在您看来:
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

  1. PyPiTest PyPi上创建帐户
  2. 创建~/.pypirc
    [distutils]
    index-servers=
        pypi
        testpypi
    
    [testpypi]
    repository: https://test.pypi.org/legacy/
    username: mikegostomski
    password: pa$$w0rd
    
    [pypi]
    username: mikegostomski
    password: pa$$w0rd
    
  3. 请现有开发人员将您添加为协作者-test和/或prod
  4. python setup.py sdist bdist_wheel --universal
  5. ^{19}
  6. twine upload dist/*
  7. 在Git中标记发布。别忘了推标签! 示例:
git tag 0.1.2
git push origin 0.1.2 

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何修复使用高停止条件时的StackOverflowerError   java两个非常好的int被除掉,仍然返回0   java将SpringWeb应用程序(Web.xml)迁移到Springboot 1.5.10   java使用CQL查询的结果集检索整行   java Solr 7:当某些请求命中Solr时,QueuedThreadPool线程数很高   在ActionListener中访问另一个类时发生java NullPointerException   异常处理Java重构类似方法的代码   java Hi我需要帮助在我的JSP页面中传输图像   Android中的java工作线程   覆盖字段值的JavaDB模型策略   带有resteasy的java Spring引导“找不到名为requestMappingHandlerMapping的bean的类型”错误   java如何插入(int)和(date)类型?   Java Swing计时器和ActionEvent   java运行一个没有jUnit作为运行选项的类   java通过解析异常来获取方法名及其包含的参数   与枚举匹配的java Get-from列表元素   我的程序中出现java内存不足错误   java在C中创建jobject不起作用   如何在java中测试这个void方法?