-
Notifications
You must be signed in to change notification settings - Fork 27
added file upload storage and versions metadata for FileVersion model #37
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
zainabbaker290
wants to merge
4
commits into
propylon:main
Choose a base branch
from
zainabbaker290:feature/store_any_files
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
4 commits
Select commit
Hold shift + click to select a range
2e51cb8
added file upload storage and versions metadata for FileVersion model
zainabbaker290 9389182
adding support for files at any specified URL
zainabbaker290 dfd7237
reworked fileVersion logic and added db level uniqueness
zainabbaker290 304d1d6
added authentication so documents can only be accessed by owners. Als…
zainabbaker290 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
41 changes: 40 additions & 1 deletion
41
src/propylon_document_manager/file_versions/api/serializers.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 |
|---|---|---|
| @@ -1,8 +1,47 @@ | ||
| import os | ||
|
|
||
| from rest_framework import serializers | ||
|
|
||
| from ..models import FileVersion | ||
|
|
||
|
|
||
| class FileVersionSerializer(serializers.ModelSerializer): | ||
| # Serializer for upload + metadata responses; version/content hash stay server-controlled. | ||
| file = serializers.FileField(write_only=True) | ||
| file_name = serializers.CharField(required=False, allow_blank=True) | ||
| url_path = serializers.CharField(required=False, allow_blank=True, default="") | ||
| file_url = serializers.SerializerMethodField(read_only=True) | ||
|
|
||
| class Meta: | ||
| model = FileVersion | ||
| fields = "__all__" | ||
| fields = ["id", "file", "file_name", "url_path", "content_hash", "version_number", "content_type", "file_size", "file_url", "uploaded_at"] | ||
| read_only_fields = ["id", "content_hash", "version_number", "content_type", "file_size", "file_url", "uploaded_at"] | ||
| validators = [] | ||
|
|
||
| # get_file_url method to return the absolute URL of the uploaded file | ||
| def get_file_url(self, obj): | ||
| request = self.context.get("request") | ||
| if request is not None and obj.file: | ||
| return request.build_absolute_uri(obj.file.url) | ||
| if obj.file: | ||
| return obj.file.url | ||
| return None | ||
|
|
||
| # create method to handle file upload and versioning logic | ||
| def create(self, validated_data): | ||
| # Normalize URL path and infer file_name when caller omits it. | ||
| uploaded_file = validated_data.pop("file") | ||
| owner = validated_data.pop("owner", None) | ||
| url_path = validated_data.pop("url_path", "") or "" | ||
| url_path = url_path.strip("/") | ||
| file_name = validated_data.pop("file_name", None) or os.path.basename(url_path) or uploaded_file.name | ||
| file_version = FileVersion( | ||
| owner=owner, | ||
| url_path=url_path, | ||
| file=uploaded_file, | ||
| file_name=file_name, | ||
| content_type=getattr(uploaded_file, "content_type", None) or "application/octet-stream", | ||
| file_size=uploaded_file.size, | ||
| ) | ||
| file_version.save() | ||
| return file_version |
131 changes: 125 additions & 6 deletions
131
src/propylon_document_manager/file_versions/api/views.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 |
|---|---|---|
| @@ -1,14 +1,133 @@ | ||
| from django.shortcuts import render | ||
| import os | ||
|
|
||
| from rest_framework.mixins import RetrieveModelMixin, ListModelMixin | ||
| from django.http import FileResponse, Http404 | ||
| from rest_framework import status | ||
| from rest_framework.permissions import IsAuthenticated | ||
| from rest_framework.parsers import FormParser, MultiPartParser | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
| from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin | ||
| from rest_framework.viewsets import GenericViewSet | ||
|
|
||
| from ..models import FileVersion | ||
| from .serializers import FileVersionSerializer | ||
|
|
||
| class FileVersionViewSet(RetrieveModelMixin, ListModelMixin, GenericViewSet): | ||
| authentication_classes = [] | ||
| permission_classes = [] | ||
|
|
||
| class FileVersionViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, GenericViewSet): | ||
| # Generic file-version API: authenticated users can only list/retrieve their own records. | ||
| permission_classes = [IsAuthenticated] | ||
| parser_classes = (MultiPartParser, FormParser) | ||
| serializer_class = FileVersionSerializer | ||
| queryset = FileVersion.objects.all() | ||
| queryset = FileVersion.objects.none() | ||
| lookup_field = "id" | ||
|
|
||
| def get_queryset(self): | ||
| return FileVersion.objects.filter(owner=self.request.user).order_by("-uploaded_at", "-version_number") | ||
|
|
||
| def perform_create(self, serializer): | ||
| serializer.save(owner=self.request.user) | ||
|
|
||
|
|
||
| class DocumentView(APIView): | ||
| # URL-addressed upload/retrieval endpoint with revision support. | ||
| permission_classes = [IsAuthenticated] | ||
| parser_classes = (MultiPartParser, FormParser) | ||
|
|
||
| def post(self, request, url_path): | ||
| url_path = url_path.strip("/") | ||
| uploaded_file = request.FILES.get("file") | ||
| if not uploaded_file: | ||
| return Response({"detail": "No file provided."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| file_name = os.path.basename(url_path) or uploaded_file.name | ||
| file_version = FileVersion( | ||
| owner=request.user, | ||
| url_path=url_path, | ||
| file=uploaded_file, | ||
| file_name=file_name, | ||
| content_type=getattr(uploaded_file, "content_type", None) or "application/octet-stream", | ||
| file_size=uploaded_file.size, | ||
| ) | ||
| file_version.save() | ||
|
|
||
| serializer = FileVersionSerializer(file_version, context={"request": request}) | ||
| return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
|
||
| def get(self, request, url_path): | ||
| url_path = url_path.strip("/") | ||
| versions = FileVersion.objects.filter(owner=request.user, url_path=url_path).order_by("version_number") | ||
| if not versions.exists(): | ||
| raise Http404("Document not found.") | ||
|
|
||
| revision = request.query_params.get("revision") | ||
| if revision is not None: | ||
| try: | ||
| revision_index = int(revision) | ||
| except ValueError: | ||
| return Response({"detail": "Invalid revision."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| if revision_index < 0 or revision_index >= versions.count(): | ||
| raise Http404("Revision not found.") | ||
| file_version = versions[revision_index] | ||
| else: | ||
| file_version = versions.last() | ||
|
|
||
| if not file_version.file: | ||
| raise Http404("File not found.") | ||
|
|
||
| return FileResponse( | ||
| file_version.file.open("rb"), | ||
| content_type=file_version.content_type or "application/octet-stream", | ||
| as_attachment=True, | ||
| filename=file_version.file_name, | ||
| ) | ||
|
|
||
|
|
||
| class DocumentMetadataView(APIView): | ||
| # Metadata-only representation for latest or specific revision. | ||
| permission_classes = [IsAuthenticated] | ||
|
|
||
| def get(self, request, url_path): | ||
| url_path = url_path.strip("/") | ||
| versions = FileVersion.objects.filter(owner=request.user, url_path=url_path).order_by("version_number") | ||
| if not versions.exists(): | ||
| raise Http404("Document not found.") | ||
|
|
||
| revision = request.query_params.get("revision") | ||
| if revision is not None: | ||
| try: | ||
| revision_index = int(revision) | ||
| except ValueError: | ||
| return Response({"detail": "Invalid revision."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| if revision_index < 0 or revision_index >= versions.count(): | ||
| raise Http404("Revision not found.") | ||
| file_version = versions[revision_index] | ||
| else: | ||
| file_version = versions.last() | ||
|
|
||
| serializer = FileVersionSerializer(file_version, context={"request": request}) | ||
| return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
|
||
|
|
||
| class CASDocumentView(APIView): | ||
| # Content-addressable retrieval by SHA-256 hash, scoped to the requesting owner. | ||
| permission_classes = [IsAuthenticated] | ||
|
|
||
| def get(self, request, content_hash): | ||
| file_version = ( | ||
| FileVersion.objects.filter(owner=request.user, content_hash=content_hash) | ||
| .order_by("-uploaded_at", "-version_number") | ||
| .first() | ||
| ) | ||
| if file_version is None: | ||
| raise Http404("Document not found.") | ||
| if not file_version.file: | ||
| raise Http404("File not found.") | ||
|
|
||
| return FileResponse( | ||
| file_version.file.open("rb"), | ||
| content_type=file_version.content_type or "application/octet-stream", | ||
| as_attachment=True, | ||
| filename=file_version.file_name, | ||
| ) | ||
42 changes: 42 additions & 0 deletions
42
src/propylon_document_manager/file_versions/migrations/0002_fileversion_storage_fields.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,42 @@ | ||
| import django.utils.timezone | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("file_versions", "0001_initial"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="fileversion", | ||
| name="url_path", | ||
| field=models.CharField(blank=True, default="", db_index=True, max_length=1024), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="fileversion", | ||
| name="content_type", | ||
| field=models.CharField(blank=True, default="", max_length=255), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="fileversion", | ||
| name="file", | ||
| field=models.FileField(blank=True, null=True, upload_to="uploads/%Y/%m/%d"), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="fileversion", | ||
| name="file_size", | ||
| field=models.PositiveIntegerField(default=0), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="fileversion", | ||
| name="uploaded_at", | ||
| field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), | ||
| preserve_default=False, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="fileversion", | ||
| name="version_number", | ||
| field=models.PositiveIntegerField(default=1), | ||
| ), | ||
| ] |
17 changes: 17 additions & 0 deletions
17
src/propylon_document_manager/file_versions/migrations/0003_alter_fileversion_options.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,17 @@ | ||
| # Generated by Django 6.0.6 on 2026-06-30 09:26 | ||
|
|
||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("file_versions", "0002_fileversion_storage_fields"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterModelOptions( | ||
| name="fileversion", | ||
| options={"ordering": ["-uploaded_at", "-version_number"]}, | ||
| ), | ||
| ] |
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.