Skip to content
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

feat(form): Implements feature for projectcaluma/caluma#1699 #2035

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
maria-pw marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 4 additions & 2 deletions caluma/caluma_core/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf import settings
from django.urls import re_path

from caluma.caluma_core import views
from caluma.caluma_core import views as core_views
from caluma.caluma_form import views as form_views
from caluma.caluma_user.views import AuthenticationGraphQLView

urlpatterns = [
Expand All @@ -10,5 +11,6 @@
AuthenticationGraphQLView.as_view(graphiql=settings.DEBUG),
name="graphql",
),
re_path("healthz/?", views.health_check_status, name="healthz"),
re_path("healthz/?", core_views.health_check_status, name="healthz"),
re_path("minio-callback/?", form_views.minio_callback_view, name="minio-callback"),
]
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,
maria-pw marked this conversation as resolved.
Show resolved Hide resolved
'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
2 changes: 1 addition & 1 deletion caluma/caluma_form/tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_history(db, question, document, schema_executor, admin_schema_executor)
)
assert history[1].value == "dolor"

assert history[0].history_user == "AnonymousUser"
assert history[0].history_user is None
assert history[0].value == "sit"


Expand Down
34 changes: 34 additions & 0 deletions caluma/caluma_form/views.py
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)
22 changes: 13 additions & 9 deletions caluma/caluma_logging/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ 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
maria-pw marked this conversation as resolved.
Show resolved Hide resolved
# create might fail if the request has no user.

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