Skip to content

Commit ffecb78

Browse files
author
Maria Vatasoiu
committed
fix(form): Fixes #1699
1 parent e0a1b07 commit ffecb78

File tree

9 files changed

+135
-16
lines changed

9 files changed

+135
-16
lines changed

caluma/caluma_core/models.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ def _history_user_getter(historical_instance):
1010

1111
def _history_user_setter(historical_instance, user):
1212
request = getattr(HistoricalRecords.thread, "request", None)
13-
user = None
13+
user = "AnonymousUser"
1414
if request is not None:
15-
user = request.user.username
16-
if request.user.__class__.__name__ == "AnonymousUser":
17-
user = "AnonymousUser"
15+
if hasattr(request, "user"):
16+
user = request.user.username
1817
historical_instance.history_user_id = user
1918

2019

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
from django.urls import reverse
3+
from rest_framework.status import HTTP_201_CREATED
4+
5+
6+
@pytest.mark.parametrize("question__type", ["files"])
7+
def test_minio_callback_view(transactional_db, client, answer, minio_mock, settings):
8+
file = answer.files.first()
9+
data = {
10+
"EventName": "s3:ObjectCreated:Put",
11+
"Key": "caluma-media/218b2504-1736-476e-9975-dc5215ef4f01_test.png",
12+
"Records": [
13+
{
14+
"eventVersion": "2.0",
15+
"eventSource": "minio:s3",
16+
"awsRegion": "",
17+
"eventTime": "2020-07-17T06:38:23.221Z",
18+
"eventName": "s3:ObjectCreated:Put",
19+
"userIdentity": {"principalId": "minio"},
20+
"requestParameters": {
21+
"accessKey": "minio",
22+
"region": "",
23+
"sourceIPAddress": "172.20.0.1",
24+
},
25+
"responseElements": {
26+
"x-amz-request-id": "162276DB8350E531",
27+
"x-minio-deployment-id": "5db7c8da-79cb-4d3a-8d40-189b51ca7aa6",
28+
"x-minio-origin-endpoint": "http://172.20.0.2:9000",
29+
},
30+
"s3": {
31+
"s3SchemaVersion": "1.0",
32+
"configurationId": "Config",
33+
"bucket": {
34+
"name": "caluma-media",
35+
"ownerIdentity": {"principalId": "minio"},
36+
"arn": "arn:aws:s3:::caluma-media",
37+
},
38+
"object": {
39+
"key": "{file_id}_name".format(file_id=file.id),
40+
"size": 299758,
41+
"eTag": "af1421c17294eed533ec99eb82b468fb",
42+
"contentType": "application/pdf",
43+
"userMetadata": {"content-variant": "application/pdf"},
44+
"versionId": "1",
45+
"sequencer": "162276DB83A9F895",
46+
},
47+
},
48+
"source": {
49+
"host": "172.20.0.1",
50+
"port": "",
51+
"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",
52+
},
53+
}
54+
],
55+
}
56+
57+
assert file.is_draft is True
58+
response = client.post(
59+
reverse("minio-callback"), data=data, content_type="application/json"
60+
)
61+
file.refresh_from_db()
62+
assert file.is_draft is False
63+
assert response.status_code == HTTP_201_CREATED

caluma/caluma_core/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
name="graphql",
1212
),
1313
re_path("healthz/?", views.health_check_status, name="healthz"),
14+
re_path("minio-callback/?", views.minio_callback_view, name="minio-callback"),
1415
]

caluma/caluma_core/views.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import json
22

33
from django.conf import settings
4-
from django.http import Http404, JsonResponse
4+
from django.http import Http404, HttpResponse, JsonResponse
5+
from django.views.decorators.http import require_http_methods
6+
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_400_BAD_REQUEST
57
from watchman import settings as watchman_settings, views as watchman_views
68

9+
from caluma.caluma_form.models import File
710
from caluma.caluma_user.models import AnonymousUser
811

912

@@ -44,3 +47,29 @@ def health_check_status(request):
4447
return JsonResponse(json_data, status=response.status_code)
4548

4649
return response
50+
51+
52+
@require_http_methods(["HEAD", "POST"])
53+
def minio_callback_view(request):
54+
status = HTTP_200_OK
55+
if request.method == "HEAD":
56+
return HttpResponse(status=status)
57+
58+
data = json.loads(request.body.decode("utf-8"))
59+
60+
for record in data["Records"]:
61+
bucket_name = record["s3"]["bucket"]["name"]
62+
if not bucket_name == settings.MINIO_STORAGE_MEDIA_BUCKET_NAME:
63+
continue
64+
65+
file_pk = record["s3"]["object"]["key"].split("_")[0]
66+
try:
67+
file = File.objects.get(pk=file_pk)
68+
except File.DoesNotExist:
69+
return HttpResponse(status=HTTP_400_BAD_REQUEST)
70+
71+
file.is_draft = False
72+
file.save()
73+
status = HTTP_201_CREATED
74+
75+
return HttpResponse(status=status)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 3.2.19 on 2023-07-20 12:17
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("caluma_form", "0046_file_answer_reverse_keys"),
9+
]
10+
11+
operations = [
12+
migrations.AddField(
13+
model_name="file",
14+
name="is_draft",
15+
field=models.BooleanField(default=True),
16+
),
17+
migrations.AddField(
18+
model_name="historicalfile",
19+
name="is_draft",
20+
field=models.BooleanField(default=True),
21+
),
22+
]

caluma/caluma_form/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ class File(core_models.UUIDModel):
608608
answer = models.ForeignKey(
609609
Answer, on_delete=models.CASCADE, related_name="files", null=True, default=None
610610
)
611+
is_draft = models.BooleanField(default=True)
611612

612613
@_ignore_missing_file
613614
def _move_blob(self):

caluma/caluma_form/tests/__snapshots__/test_history.ambr

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
dict({
2929
'node': dict({
3030
'__typename': 'HistoricalStringAnswer',
31-
'historyUserId': 'AnonymousUser',
31+
'historyUserId': None,
3232
'value': 'first anon - revision 3',
3333
}),
3434
}),
@@ -48,7 +48,7 @@
4848
dict({
4949
'node': dict({
5050
'__typename': 'HistoricalStringAnswer',
51-
'historyUserId': 'AnonymousUser',
51+
'historyUserId': None,
5252
'value': 'second anon - revision 4',
5353
}),
5454
}),

caluma/caluma_logging/middleware.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ def __call__(self, request):
2222
try:
2323
doc = parser.parse(body["query"])
2424
visitor.visit(doc, vis)
25-
except GraphQLSyntaxError:
25+
except (GraphQLSyntaxError, KeyError):
2626
pass
2727

28-
AccessLog.objects.create(
29-
username=request.user.username,
30-
query=body.get("query"),
31-
variables=body.get("variables"),
32-
status_code=response.status_code,
33-
has_error=response.status_code >= 400,
34-
**vis.values,
35-
)
28+
try:
29+
AccessLog.objects.create(
30+
username=request.user.username,
31+
query=body.get("query"),
32+
variables=body.get("variables"),
33+
status_code=response.status_code,
34+
has_error=response.status_code >= 400,
35+
**vis.values,
36+
)
37+
except AttributeError:
38+
pass
3639

3740
return response
3841

caluma/tests/__snapshots__/test_schema.ambr

+1
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@
11041104
id: ID!
11051105
name: String!
11061106
answer: FilesAnswer
1107+
isDraft: Boolean!
11071108
uploadUrl: String
11081109
downloadUrl: String
11091110
metadata: GenericScalar

0 commit comments

Comments
 (0)