🏷️(backend) accept string as saved document
Saved documents has to be a string now. Before it has to be a json object.
This commit is contained in:
@@ -7,8 +7,6 @@ from timezone_field.rest_framework import TimeZoneSerializerField
|
|||||||
|
|
||||||
from core import models
|
from core import models
|
||||||
|
|
||||||
from .fields import JSONField
|
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
"""Serialize users."""
|
"""Serialize users."""
|
||||||
@@ -136,7 +134,7 @@ class BaseResourceSerializer(serializers.ModelSerializer):
|
|||||||
class DocumentSerializer(BaseResourceSerializer):
|
class DocumentSerializer(BaseResourceSerializer):
|
||||||
"""Serialize documents."""
|
"""Serialize documents."""
|
||||||
|
|
||||||
content = JSONField(required=False)
|
content = serializers.CharField(required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Document
|
model = models.Document
|
||||||
|
|||||||
@@ -344,7 +344,7 @@ class DocumentViewSet(
|
|||||||
|
|
||||||
return drf_response.Response(
|
return drf_response.Response(
|
||||||
{
|
{
|
||||||
"content": json.loads(response["Body"].read()),
|
"content": response["Body"].read().decode("utf-8"),
|
||||||
"last_modified": response["LastModified"],
|
"last_modified": response["LastModified"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class DocumentFactory(factory.django.DjangoModelFactory):
|
|||||||
|
|
||||||
title = factory.Sequence(lambda n: f"document{n}")
|
title = factory.Sequence(lambda n: f"document{n}")
|
||||||
is_public = factory.Faker("boolean")
|
is_public = factory.Faker("boolean")
|
||||||
content = factory.LazyFunction(lambda: {"foo": fake.word()})
|
content = factory.Sequence(lambda n: f"content{n}")
|
||||||
|
|
||||||
@factory.post_generation
|
@factory.post_generation
|
||||||
def users(self, create, extracted, **kwargs):
|
def users(self, create, extracted, **kwargs):
|
||||||
|
|||||||
@@ -325,16 +325,15 @@ class Document(BaseModel):
|
|||||||
except (FileNotFoundError, ClientError):
|
except (FileNotFoundError, ClientError):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self._content = json.loads(response["Body"].read())
|
self._content = response["Body"].read().decode('utf-8')
|
||||||
return self._content
|
return self._content
|
||||||
|
|
||||||
@content.setter
|
@content.setter
|
||||||
def content(self, content):
|
def content(self, content):
|
||||||
"""Cache the content, don't write to object storage yet"""
|
"""Cache the content, don't write to object storage yet"""
|
||||||
if isinstance(content, str):
|
if not isinstance(content, str):
|
||||||
content = json.loads(content)
|
raise ValueError("content should be a string.")
|
||||||
if not isinstance(content, dict):
|
|
||||||
raise ValueError("content should be a json object.")
|
|
||||||
self._content = content
|
self._content = content
|
||||||
|
|
||||||
def get_content_response(self, version_id=""):
|
def get_content_response(self, version_id=""):
|
||||||
@@ -349,7 +348,7 @@ class Document(BaseModel):
|
|||||||
|
|
||||||
if self._content:
|
if self._content:
|
||||||
file_key = self.file_key
|
file_key = self.file_key
|
||||||
bytes_content = json.dumps(self._content).encode("utf-8")
|
bytes_content = self._content.encode("utf-8")
|
||||||
|
|
||||||
if default_storage.exists(file_key):
|
if default_storage.exists(file_key):
|
||||||
response = default_storage.connection.meta.client.head_object(
|
response = default_storage.connection.meta.client.head_object(
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ def test_api_documents_retrieve_anonymous_public():
|
|||||||
"accesses": [],
|
"accesses": [],
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"is_public": True,
|
"is_public": True,
|
||||||
"content": {"foo": document.content["foo"]},
|
"content": document.content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ def test_api_documents_retrieve_authenticated_unrelated_public():
|
|||||||
"accesses": [],
|
"accesses": [],
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"is_public": True,
|
"is_public": True,
|
||||||
"content": {"foo": document.content["foo"]},
|
"content": document.content,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ def test_api_documents_retrieve_authenticated_related_direct():
|
|||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"id": str(document.id),
|
"id": str(document.id),
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"content": {"foo": document.content["foo"]},
|
"content": document.content,
|
||||||
"abilities": document.get_abilities(user),
|
"abilities": document.get_abilities(user),
|
||||||
"is_public": document.is_public,
|
"is_public": document.is_public,
|
||||||
}
|
}
|
||||||
@@ -255,7 +255,7 @@ def test_api_documents_retrieve_authenticated_related_team_members(
|
|||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"id": str(document.id),
|
"id": str(document.id),
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"content": {"foo": document.content["foo"]},
|
"content": document.content,
|
||||||
"abilities": document.get_abilities(user),
|
"abilities": document.get_abilities(user),
|
||||||
"is_public": False,
|
"is_public": False,
|
||||||
}
|
}
|
||||||
@@ -353,7 +353,7 @@ def test_api_documents_retrieve_authenticated_related_team_administrators(
|
|||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"id": str(document.id),
|
"id": str(document.id),
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"content": {"foo": document.content["foo"]},
|
"content": document.content,
|
||||||
"abilities": document.get_abilities(user),
|
"abilities": document.get_abilities(user),
|
||||||
"is_public": False,
|
"is_public": False,
|
||||||
}
|
}
|
||||||
@@ -455,7 +455,7 @@ def test_api_documents_retrieve_authenticated_related_team_owners(
|
|||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
"id": str(document.id),
|
"id": str(document.id),
|
||||||
"title": document.title,
|
"title": document.title,
|
||||||
"content": {"foo": document.content["foo"]},
|
"content": document.content,
|
||||||
"abilities": document.get_abilities(user),
|
"abilities": document.get_abilities(user),
|
||||||
"is_public": False,
|
"is_public": False,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ def test_api_document_versions_list_authenticated_related(via, mock_user_get_tea
|
|||||||
assert len(content["versions"]) == 0
|
assert len(content["versions"]) == 0
|
||||||
|
|
||||||
# Add a new version to the document
|
# Add a new version to the document
|
||||||
document.content = {"foo": "bar"}
|
document.content = "new content"
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
response = client.get(
|
response = client.get(
|
||||||
@@ -243,7 +243,7 @@ def test_api_document_versions_retrieve_authenticated_related(via, mock_user_get
|
|||||||
|
|
||||||
# Create a new version should make it available to the user
|
# Create a new version should make it available to the user
|
||||||
time.sleep(1) # minio stores datetimes with the precision of a second
|
time.sleep(1) # minio stores datetimes with the precision of a second
|
||||||
document.content = {"foo": "bar"}
|
document.content = "new content"
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
version_id = document.get_versions_slice()["versions"][0]["version_id"]
|
version_id = document.get_versions_slice()["versions"][0]["version_id"]
|
||||||
@@ -253,7 +253,7 @@ def test_api_document_versions_retrieve_authenticated_related(via, mock_user_get
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert response.json()["content"] == {"foo": "bar"}
|
assert response.json()["content"] == "new content"
|
||||||
|
|
||||||
|
|
||||||
def test_api_document_versions_create_anonymous():
|
def test_api_document_versions_create_anonymous():
|
||||||
@@ -459,7 +459,7 @@ def test_api_document_versions_delete_member(via, mock_user_get_teams):
|
|||||||
|
|
||||||
# Create a new version should make it available to the user
|
# Create a new version should make it available to the user
|
||||||
time.sleep(1) # minio stores datetimes with the precision of a second
|
time.sleep(1) # minio stores datetimes with the precision of a second
|
||||||
document.content = {"foo": "bar"}
|
document.content = "new content"
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
versions = document.get_versions_slice()["versions"]
|
versions = document.get_versions_slice()["versions"]
|
||||||
@@ -503,7 +503,7 @@ def test_api_document_versions_delete_administrator_or_owner(via, mock_user_get_
|
|||||||
|
|
||||||
# Create a new version should make it available to the user
|
# Create a new version should make it available to the user
|
||||||
time.sleep(1) # minio stores datetimes with the precision of a second
|
time.sleep(1) # minio stores datetimes with the precision of a second
|
||||||
document.content = {"foo": "bar"}
|
document.content = "new content"
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
versions = document.get_versions_slice()["versions"]
|
versions = document.get_versions_slice()["versions"]
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ def test_models_documents_get_versions_slice(settings):
|
|||||||
# Create a document with 7 versions
|
# Create a document with 7 versions
|
||||||
document = factories.DocumentFactory()
|
document = factories.DocumentFactory()
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
document.content = {"foo": f"bar{i:d}"}
|
document.content = f"bar{i:d}"
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
# Add a version not related to the first document
|
# Add a version not related to the first document
|
||||||
@@ -246,7 +246,7 @@ def test_models_documents_version_duplicate():
|
|||||||
assert len(response["Versions"]) == 1
|
assert len(response["Versions"]) == 1
|
||||||
|
|
||||||
# Save modified content
|
# Save modified content
|
||||||
document.content = {"foo": "spam"}
|
document.content = "new content"
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
response = default_storage.connection.meta.client.list_object_versions(
|
response = default_storage.connection.meta.client.list_object_versions(
|
||||||
|
|||||||
Reference in New Issue
Block a user