forked from projectcaluma/caluma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): Implements feature for issue projectcaluma#1699
- Loading branch information
Maria Vatasoiu
committed
Jul 24, 2023
1 parent
e0a1b07
commit b31ae11
Showing
10 changed files
with
144 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
import pytest | ||
from django.urls import reverse | ||
from rest_framework.status import HTTP_201_CREATED | ||
|
||
|
||
@pytest.mark.parametrize("question__type", ["files"]) | ||
def test_minio_callback_view(transactional_db, client, answer, minio_mock, settings): | ||
file = answer.files.first() | ||
data = { | ||
"EventName": "s3:ObjectCreated:Put", | ||
"Key": "caluma-media/218b2504-1736-476e-9975-dc5215ef4f01_test.png", | ||
"Records": [ | ||
{ | ||
"eventVersion": "2.0", | ||
"eventSource": "minio:s3", | ||
"awsRegion": "", | ||
"eventTime": "2020-07-17T06:38:23.221Z", | ||
"eventName": "s3:ObjectCreated:Put", | ||
"userIdentity": {"principalId": "minio"}, | ||
"requestParameters": { | ||
"accessKey": "minio", | ||
"region": "", | ||
"sourceIPAddress": "172.20.0.1", | ||
}, | ||
"responseElements": { | ||
"x-amz-request-id": "162276DB8350E531", | ||
"x-minio-deployment-id": "5db7c8da-79cb-4d3a-8d40-189b51ca7aa6", | ||
"x-minio-origin-endpoint": "http://172.20.0.2:9000", | ||
}, | ||
"s3": { | ||
"s3SchemaVersion": "1.0", | ||
"configurationId": "Config", | ||
"bucket": { | ||
"name": "caluma-media", | ||
"ownerIdentity": {"principalId": "minio"}, | ||
"arn": "arn:aws:s3:::caluma-media", | ||
}, | ||
"object": { | ||
"key": "{file_id}_name".format(file_id=file.id), | ||
"size": 299758, | ||
"eTag": "af1421c17294eed533ec99eb82b468fb", | ||
"contentType": "application/pdf", | ||
"userMetadata": {"content-variant": "application/pdf"}, | ||
"versionId": "1", | ||
"sequencer": "162276DB83A9F895", | ||
}, | ||
}, | ||
"source": { | ||
"host": "172.20.0.1", | ||
"port": "", | ||
"userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.15.0 Chrome/80.0.3987.163 Safari/537.36", | ||
}, | ||
} | ||
], | ||
} | ||
|
||
assert file.is_draft is True | ||
response = client.post( | ||
reverse("minio-callback"), data=data, content_type="application/json" | ||
) | ||
file.refresh_from_db() | ||
assert file.is_draft is False | ||
assert response.status_code == HTTP_201_CREATED |
This file contains 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
22 changes: 22 additions & 0 deletions
22
caluma/caluma_form/migrations/0047_add_draft_flag_to_file.py
This file contains 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,22 @@ | ||
# Generated by Django 3.2.19 on 2023-07-20 12:17 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("caluma_form", "0046_file_answer_reverse_keys"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="file", | ||
name="is_draft", | ||
field=models.BooleanField(default=True), | ||
), | ||
migrations.AddField( | ||
model_name="historicalfile", | ||
name="is_draft", | ||
field=models.BooleanField(default=True), | ||
), | ||
] |
This file contains 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 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 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 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,34 @@ | ||
import json | ||
|
||
from django.conf import settings | ||
from django.http import HttpResponse | ||
from django.views.decorators.http import require_http_methods | ||
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_400_BAD_REQUEST | ||
|
||
from caluma.caluma_form.models import File | ||
|
||
|
||
@require_http_methods(["HEAD", "POST"]) | ||
def minio_callback_view(request): | ||
status = HTTP_200_OK | ||
if request.method == "HEAD": | ||
return HttpResponse(status=status) | ||
|
||
data = json.loads(request.body.decode("utf-8")) | ||
|
||
for record in data["Records"]: | ||
bucket_name = record["s3"]["bucket"]["name"] | ||
if not bucket_name == settings.MINIO_STORAGE_MEDIA_BUCKET_NAME: | ||
continue | ||
|
||
file_pk = record["s3"]["object"]["key"].split("_")[0] | ||
try: | ||
file = File.objects.get(pk=file_pk) | ||
except File.DoesNotExist: | ||
return HttpResponse(status=HTTP_400_BAD_REQUEST) | ||
|
||
file.is_draft = False | ||
file.save() | ||
status = HTTP_201_CREATED | ||
|
||
return HttpResponse(status=status) |
This file contains 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 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