-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce multiple file upload app enhancements #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
a1eksb
wants to merge
14
commits into
main
Choose a base branch
from
bmip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
adf14da
[ FEAT ]: Add support for file uploads without owner
a1eksb b6f1ee1
[ FEAT ]: Add keep after deletion option for file deletions
a1eksb f8640d8
[ FEAT ]: Add deleted_on field in the FileUploadSerializer
a1eksb 60a3f50
[ FEAT ]: Add option to configure max_file_size
a1eksb 7344fd6
[ DEP ]: Remove token from download script
a1eksb 9cc1aab
[ DOC ]: Initial documentation
a1eksb 04c1034
[FIX]: Introduce multiple adjustments based on PR comments
a1eksb 00008f6
[FIX]: Add enforcement for user model type
a1eksb 7055890
Merge branch 'main' into bmip
a1eksb f8e0734
Merge branch 'main' into bmip
a1eksb 16b00f7
[FIX]: Remove anonymous user constraint
a1eksb 4989787
[FEAT]: Reintroduce the has_owner flag
a1eksb 69cfc4b
[DOC]: Add documentation on file upload app
a1eksb 12919ea
[FEAT]: Enable users to uplaod files with same name multiple times
a1eksb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| name: Documentation deployment workflow | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - docs/**/* | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| paths: | ||
| - docs/**/* | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
|
|
||
| - name: Install curl | ||
| run: sudo apt-get update && sudo apt-get install -y curl | ||
|
|
||
| - name: Deploying the docs | ||
| run: | | ||
| echo "Deploying the docs" | ||
| curl --request POST ${{ secrets.ACINT_URL }} -H "Content-Type: application/json" -d "{\"action\": \"${{ secrets.ACINT_ACTION }}\", \"token\": \"${{ secrets.ACINT_TOKEN }}\"}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ | |
|
|
||
| class CoreConfig(AppConfig): | ||
| name = "django_fileupload" | ||
| verbose_name = "File Uploads" | ||
26 changes: 26 additions & 0 deletions
26
api/django-fileupload/django_fileupload/migrations/0007_alter_fileuploadbatch_owner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Generated by Django 5.0 on 2024-07-02 13:16 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("django_fileupload", "0006_rename_mime_type_fileupload_detected_mime_type"), | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="fileuploadbatch", | ||
| name="owner", | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.RESTRICT, | ||
| to=settings.AUTH_USER_MODEL, | ||
| ), | ||
| ), | ||
| ] |
18 changes: 18 additions & 0 deletions
18
api/django-fileupload/django_fileupload/migrations/0008_fileupload_deleted_on.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Generated by Django 5.0 on 2024-07-04 19:21 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("django_fileupload", "0007_alter_fileuploadbatch_owner"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="fileupload", | ||
| name="deleted_on", | ||
| field=models.DateTimeField(editable=False, null=True), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| services: | ||
| mkdocs: | ||
| build: | ||
| context: docs | ||
| dockerfile: Dockerfile | ||
| ports: | ||
| - "8000:8000" | ||
| volumes: | ||
| - ./docs/:/docs/:z |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| FROM squidfunk/mkdocs-material | ||
| WORKDIR /docs | ||
| COPY ./mkdocs.yml /docs/mkdocs.yml | ||
| RUN pip install mkdocs-glightbox mkdocs-awesome-pages-plugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| nav: | ||
| - index.md | ||
| - setup.md | ||
| - django_apps | ||
| - vue_components |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| nav: | ||
| - index.md | ||
| - file_upload.md | ||
| - notifications.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| --- | ||
| title: File Upload App | ||
| --- | ||
|
|
||
| ### Features | ||
| The file upload app enables you to easily handle file uploads in your application. Whenever one or more files are uploaded using the file upload app a single batch entry is created and a set of file entries are created in their respective models. | ||
|
|
||
| The batches keep information about the `owner`, `upload date` and `references to the associated files`. The files, on the other hand, keep information about the `deletion date`, `referece to the file upload batch`, `position in the batch`, `file location`, `file type` and a `checksum`. | ||
|
|
||
| Uploading multiple files with the same name will cause the latest file with that name to be retrieved when retrieving files. Deleting the file will only delete the latest version of the file but the previous versions will be kept and will also be returned. Custom implementations are required to enable a different behavior (for example deleting files by name). | ||
|
|
||
| #### Anonymous Owners | ||
| In cases where an owner is not needed you simply extend the FileUploadBatchViewSet and set the `has_owner` property to `False`. This will result in the owner value being set to `None` in the database entries. | ||
|
|
||
| ```python title="views.py" | ||
| from django_fileupload.views import FileUploadBatchViewSet | ||
|
|
||
| class CustomFileUploadBatchViewSet(FileUploadBatchViewSet): | ||
| has_owner = True | ||
| ``` | ||
|
|
||
| !!! note | ||
|
|
||
| In this case you also have to use your newly defined view class in your route definitions. | ||
|
|
||
|
|
||
| #### Max File Size | ||
| There might be cases where you want to provide a max file upload size per upload request. In such cases you can take a similar approach to the one above but instead of setting a flag you can implement the `get_max_file_size` method. | ||
|
|
||
| ```python title="views.py" | ||
| from django_fileupload.views import FileUploadBatchViewSet | ||
|
|
||
| class CustomFileUploadBatchViewSet(FileUploadBatchViewSet): | ||
| def get_max_file_size(self, request): | ||
|
|
||
| # Extract data from the request object or do some other calculations | ||
|
|
||
| if some_condition: | ||
| # The max_file_size is in megabytes | ||
| # The get_max_file_size should return bytes or None | ||
| return max_file_size * 1024 * 1024 | ||
| else: | ||
| return None | ||
| ``` | ||
|
|
||
| !!! warning "Large file protection" | ||
|
|
||
| If a user uploads a very large file, our application may get overwhelmed while checking its size (as it would attempt to load it onto the file system before reading the size). Therefore, we need to introduce additional measures as described below. | ||
|
|
||
| Introduce custom file upload handlers by adding them to your `settings.py` using the following snippet: | ||
|
|
||
| ```python title="settings.py" | ||
| FILE_UPLOAD_HANDLERS = [ | ||
| "django_common.uploadhandlers.HardLimitMemoryFileUploadHandler", | ||
| "django_common.uploadhandlers.HardLimitTemporaryFileUploadHandler", | ||
| ] | ||
| ``` | ||
|
|
||
| Furthermore, you must set the following argument and environment variable in your backend `Dockerfile`: | ||
|
|
||
| ```dockerfile title="api/Dockerfile" | ||
| ARG HARD_UPLOAD_SIZE_LIMIT=524288000 | ||
| ENV DJANGO_HARD_UPLOAD_SIZE_LIMIT $HARD_UPLOAD_SIZE_LIMIT | ||
| ``` | ||
| This will ensure that there is a global django upper size limit that will prevent users from uploading files larger than it. | ||
|
|
||
| #### Keeping Files After Deletion | ||
| To keep files after deletion we must overwrite the method `keep_after_deletion` of the `FileUploadViewSet` class: | ||
|
|
||
| ```python title="views.py" | ||
| from django_fileupload.views import FileUploadViewSet | ||
|
|
||
| class CustomFileUploadViewSet(FileUploadViewSet): | ||
| def keep_after_deletion(self): | ||
| return True | ||
| ``` | ||
| !!! info "Listing only files that are not marked as deleted" | ||
|
|
||
| To retrieve only files that have not been marked as deleted you need to provide custom logic in the list method of the FileUploadViewSet class. | ||
|
|
||
|
|
||
| #### Other Customizations | ||
| Other methods of the `FileUploadBatchViewSet` class and the `FileUploadViewSet` class may be overwriten or extended in order to provide more custom behavior and also for providing additional metadata. | ||
|
|
||
| ### Setup | ||
| 1. Add the following two apps under INSTALLED_APPS in settings.py: | ||
| ```python | ||
| "django_common", | ||
| "django_fileupload", | ||
| ``` | ||
| 2. The following three packages need to be added to the `requirements.txt` of your django project: | ||
| ```text | ||
| -e /nexus-app-stack-contrib/cli/python-utilities | ||
| -e /nexus-app-stack-contrib/api/django-common | ||
| -e /nexus-app-stack-contrib/api/django-fileupload | ||
| ``` | ||
| 3. Make sure to setup the `FileUploadBatchViewSet` and `FileUploadViewSet` class endpoints by importing their routers / urls and defining them in your own routes. | ||
|
|
||
| !!! info "Use Swagger" | ||
|
|
||
| To help you with your debugging process and to enable you to better understand what are the available methods and routes use swagger in your project. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| title: "Django Apps" | ||
| --- | ||
|
|
||
| These are helpful django apps that can be included in your project. Generally the `django_common` app is used by the others and should therfore be always included. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| title: Notifications App | ||
| --- | ||
|
|
||
| ### Features | ||
| Describe all the features as well as the class flags | ||
| ### Setup | ||
| Describe any file upload app specific config (for example what are the dependencies and what apps need to be added where to make it run) | ||
| ### Customizations | ||
| Describe how to customize urls, models etc. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| title: App Stack Contrib | ||
| --- | ||
|
|
||
| # App Stack Contrib 🧰 | ||
| The NEXUS App Stack Contrib project provides a versatile set of django apps, vue components and cli tools that can speed up the development of new projects. Furthermore, it also provides a standardized approach to problems such as file uploads in django. | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.