Skip to content

Commit

Permalink
fix(form): Fixes projectcaluma#1699
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Vatasoiu committed Jul 20, 2023
1 parent e0a1b07 commit ffecb78
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 16 deletions.
7 changes: 3 additions & 4 deletions caluma/caluma_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ def _history_user_getter(historical_instance):

def _history_user_setter(historical_instance, user):
request = getattr(HistoricalRecords.thread, "request", None)
user = None
user = "AnonymousUser"
if request is not None:
user = request.user.username
if request.user.__class__.__name__ == "AnonymousUser":
user = "AnonymousUser"
if hasattr(request, "user"):
user = request.user.username
historical_instance.history_user_id = user


Expand Down
63 changes: 63 additions & 0 deletions caluma/caluma_core/tests/test_views.py
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
1 change: 1 addition & 0 deletions caluma/caluma_core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
name="graphql",
),
re_path("healthz/?", views.health_check_status, name="healthz"),
re_path("minio-callback/?", views.minio_callback_view, name="minio-callback"),
]
31 changes: 30 additions & 1 deletion caluma/caluma_core/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import json

from django.conf import settings
from django.http import Http404, JsonResponse
from django.http import Http404, HttpResponse, JsonResponse
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 watchman import settings as watchman_settings, views as watchman_views

from caluma.caluma_form.models import File
from caluma.caluma_user.models import AnonymousUser


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

return response


@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)
22 changes: 22 additions & 0 deletions caluma/caluma_form/migrations/0047_add_draft_flag_to_file.py
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),
),
]
1 change: 1 addition & 0 deletions caluma/caluma_form/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ class File(core_models.UUIDModel):
answer = models.ForeignKey(
Answer, on_delete=models.CASCADE, related_name="files", null=True, default=None
)
is_draft = models.BooleanField(default=True)

@_ignore_missing_file
def _move_blob(self):
Expand Down
4 changes: 2 additions & 2 deletions caluma/caluma_form/tests/__snapshots__/test_history.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
dict({
'node': dict({
'__typename': 'HistoricalStringAnswer',
'historyUserId': 'AnonymousUser',
'historyUserId': None,
'value': 'first anon - revision 3',
}),
}),
Expand All @@ -48,7 +48,7 @@
dict({
'node': dict({
'__typename': 'HistoricalStringAnswer',
'historyUserId': 'AnonymousUser',
'historyUserId': None,
'value': 'second anon - revision 4',
}),
}),
Expand Down
21 changes: 12 additions & 9 deletions caluma/caluma_logging/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ def __call__(self, request):
try:
doc = parser.parse(body["query"])
visitor.visit(doc, vis)
except GraphQLSyntaxError:
except (GraphQLSyntaxError, KeyError):
pass

AccessLog.objects.create(
username=request.user.username,
query=body.get("query"),
variables=body.get("variables"),
status_code=response.status_code,
has_error=response.status_code >= 400,
**vis.values,
)
try:
AccessLog.objects.create(
username=request.user.username,
query=body.get("query"),
variables=body.get("variables"),
status_code=response.status_code,
has_error=response.status_code >= 400,
**vis.values,
)
except AttributeError:
pass

return response

Expand Down
1 change: 1 addition & 0 deletions caluma/tests/__snapshots__/test_schema.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@
id: ID!
name: String!
answer: FilesAnswer
isDraft: Boolean!
uploadUrl: String
downloadUrl: String
metadata: GenericScalar
Expand Down

0 comments on commit ffecb78

Please sign in to comment.