-
Notifications
You must be signed in to change notification settings - Fork 1
Direct Attachment Uploads #67
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
msc5
wants to merge
3
commits into
main
Choose a base branch
from
msc/direct-upload-2
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
3 commits
Select commit
Hold shift + click to select a range
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
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 @@ | ||
| 3.12 |
Empty file.
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,3 @@ | ||
| from django.contrib import admin | ||
|
|
||
| # Register your models here. |
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,6 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class AppConfig(AppConfig): | ||
| default_auto_field = 'django.db.models.BigAutoField' | ||
| name = 'app' |
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,11 @@ | ||
| SAMPLE_OBJECT_PK_URL_KWARG = "sample_object_id" | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
|
|
||
| # START_FEATURE direct_upload | ||
| {%- endif %} | ||
| ATTACHMENT_PK_URL_KWARG = "attachment_id" | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # END_FEATURE direct_upload | ||
| {%- endif %} | ||
| {%- endif %} |
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,63 @@ | ||
| {%- if cookiecutter.crispy_forms == "enabled" %} | ||
| from crispy_forms.helper import Layout | ||
| from crispy_forms.layout import Fieldset | ||
| from django import forms | ||
| from django.http import HttpRequest | ||
| from app.models import SampleObject | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| from app.models import Attachment | ||
| from common.fields import DirectUploadFileField | ||
| {%- endif %} | ||
| from common.forms import ActionFormMixin, CrispyFormMixin | ||
|
|
||
|
|
||
| class SampleObjectBaseForm(CrispyFormMixin, ActionFormMixin, forms.ModelForm): | ||
| request: HttpRequest | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
|
|
||
| # START_FEATURE direct_upload | ||
| {%- endif %} | ||
| attachments = DirectUploadFileField(queryset=Attachment.objects.filter(deleted_on=None), required=False) | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # END_FEATURE direct_upload | ||
| {%- endif %} | ||
| {%- endif %} | ||
|
|
||
| class Meta: | ||
| model = SampleObject | ||
| exclude = ['created_by'] | ||
|
|
||
| layout = Layout( | ||
| Fieldset( | ||
| "Details", | ||
| "name", | ||
| "description" | ||
| ), | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # START_FEATURE direct_upload | ||
| {%- endif %} | ||
| "attachments" | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # END_FEATURE direct_upload | ||
| {%- endif %} | ||
| {%- endif %} | ||
| ) | ||
|
|
||
| def __init__(self, request: HttpRequest, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
| self.request = request | ||
|
|
||
|
|
||
| class SampleObjectCreateForm(SampleObjectBaseForm): | ||
| action_title = "Create Sample Object" | ||
|
|
||
| def save(self, commit=True): | ||
| self.instance.created_by = self.request.user | ||
| return super().save(commit) | ||
|
|
||
|
|
||
| class SampleObjectEditForm(SampleObjectBaseForm): | ||
| action_title = "Edit {instance}" | ||
| {%- endif %} |
48 changes: 48 additions & 0 deletions
48
{{cookiecutter.project_slug}}/app/migrations/0001_initial.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,48 @@ | ||
| # Generated by Django 5.2 on 2026-03-16 18:26 | ||
|
|
||
| import uuid | ||
| from django.db import migrations, models | ||
| {%- if cookiecutter.django_storages == "enabled" %} | ||
| import common.models | ||
| {%- endif %} | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ] | ||
|
|
||
| operations = [ | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| migrations.CreateModel( | ||
| name='Attachment', | ||
| fields=[ | ||
| ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), | ||
| ('created_on', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_on', models.DateTimeField(auto_now=True)), | ||
| ('name', models.CharField(max_length=512)), | ||
| ('file', models.FileField(max_length=1024, upload_to=common.models.get_upload_prefix)), | ||
| ('upload_completed_on', models.DateTimeField(null=True)), | ||
| ('deleted_on', models.DateTimeField(null=True)), | ||
| ], | ||
| options={ | ||
| 'abstract': False, | ||
| }, | ||
| ), | ||
| {%- endif %} | ||
| migrations.CreateModel( | ||
| name='SampleObject', | ||
| fields=[ | ||
| ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), | ||
| ('created_on', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_on', models.DateTimeField(auto_now=True)), | ||
| ('name', models.CharField(max_length=512, unique=True)), | ||
| ('description', models.TextField(blank=True, default='')), | ||
| ], | ||
| options={ | ||
| 'abstract': False, | ||
| }, | ||
| ), | ||
| ] |
35 changes: 35 additions & 0 deletions
35
{{cookiecutter.project_slug}}/app/migrations/0002_initial.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,35 @@ | ||
| # Generated by Django 5.2 on 2026-03-16 18:26 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ('app', '0001_initial'), | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ] | ||
|
|
||
| operations = [ | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| migrations.AddField( | ||
| model_name='attachment', | ||
| name='user', | ||
| field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='files', to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='sampleobject', | ||
| name='attachments', | ||
| field=models.ManyToManyField(related_name='sample_objects', to='app.attachment'), | ||
| ), | ||
| {%- endif %} | ||
| migrations.AddField( | ||
| model_name='sampleobject', | ||
| name='created_by', | ||
| field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='sample_objects', to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| ] |
Empty file.
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,48 @@ | ||
| from django.db import models | ||
|
|
||
| from common.models import TimestampedModel, User | ||
| {%- if cookiecutter.django_storages == "enabled" %} | ||
| from common.models import UploadFile | ||
| {%- endif %} | ||
|
|
||
|
|
||
| class SampleObject(TimestampedModel): | ||
| created_by = models.ForeignKey(User, related_name="sample_objects", on_delete=models.PROTECT) | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
|
|
||
| # START_FEATURE direct_upload | ||
| {%- endif %} | ||
| attachments = models.ManyToManyField("Attachment", related_name="sample_objects") | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # END_FEATURE direct_upload | ||
| {%- endif %} | ||
| {%- endif %} | ||
|
|
||
| name = models.CharField(max_length=512, unique=True) | ||
| description = models.TextField(default="", blank=True) | ||
|
|
||
| def __str__(self) -> str: | ||
| return f'Sample Object {self.name}' | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
|
|
||
| def get_attachments(self): | ||
| qs = self.attachments.prefetch_related('user') | ||
| return [ | ||
| attachment for attachment in qs | ||
| if not attachment.deleted_on and attachment.upload_completed_on | ||
| ] | ||
| {%- endif %} | ||
|
|
||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
|
|
||
|
|
||
| # START_FEATURE direct_upload | ||
| {%- endif %} | ||
| class Attachment(UploadFile): | ||
| pass | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # END_FEATURE direct_upload | ||
| {%- endif %} | ||
| {%- endif %} |
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,51 @@ | ||
| {%- if cookiecutter.direct_upload == "enabled" %} | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # START_FEATURE direct_upload | ||
| {%- endif %} | ||
| from app.constants import ATTACHMENT_PK_URL_KWARG | ||
| from django.utils.formats import date_format | ||
| from common.serializers import UserSerializer | ||
| from django.urls import reverse | ||
| from rest_framework import serializers | ||
|
|
||
| from app.models import Attachment | ||
|
|
||
|
|
||
| class AttachmentSerializer(serializers.ModelSerializer): | ||
| user = UserSerializer() | ||
| view_url = serializers.SerializerMethodField() | ||
| download_url = serializers.SerializerMethodField() | ||
| delete_url = serializers.SerializerMethodField() | ||
| created_on = serializers.SerializerMethodField() | ||
| upload_completed_on = serializers.SerializerMethodField() | ||
| size = serializers.SerializerMethodField() | ||
| path = serializers.SerializerMethodField() | ||
|
|
||
| class Meta: | ||
| model = Attachment | ||
| exclude = [] | ||
|
|
||
| def get_view_url(self, instance): | ||
| return reverse('attachment_open', kwargs={ATTACHMENT_PK_URL_KWARG: instance.id}) | ||
|
|
||
| def get_download_url(self, instance): | ||
| return reverse('attachment_download', kwargs={ATTACHMENT_PK_URL_KWARG: instance.id}) | ||
|
|
||
| def get_delete_url(self, instance): | ||
| return reverse('attachment_delete', kwargs={ATTACHMENT_PK_URL_KWARG: instance.id}) | ||
|
|
||
| def get_created_on(self, instance): | ||
| return date_format(instance.created_on, format="DATETIME_FORMAT") | ||
|
|
||
| def get_upload_completed_on(self, instance): | ||
| return date_format(instance.upload_completed_on, format="DATETIME_FORMAT") | ||
|
|
||
| def get_size(self, instance): | ||
| return instance.file.size | ||
|
|
||
| def get_path(self, instance): | ||
| return instance.file.name | ||
| {%- if cookiecutter.feature_annotations == "on" %} | ||
| # END_FEATURE direct_upload | ||
| {%- endif %} | ||
| {%- endif %} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also add the vue static directory (which should probably be a subdirectory of a static root) to this, instead of adding the
if vueto each individual vue file. Same for vite config.Could also consider removing the whole
appdirectory ifreference_examplesis disabled (would require associated settings changes etc)