#31 API Improvement
* adds support for returning all shared data * adds documentation
This commit is contained in:
parent
fa6603f218
commit
9da5df2d5b
@ -62,6 +62,11 @@ class AbstractModelAPISerializer:
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if _id is None:
|
||||
# Return all objects
|
||||
del self.lookup["id"]
|
||||
else:
|
||||
# Return certain objects
|
||||
self.lookup["id"] = _id
|
||||
self.lookup["users__in"] = [user]
|
||||
|
||||
@ -73,8 +78,10 @@ class AbstractModelAPISerializer:
|
||||
Returns:
|
||||
serialized_data (dict)
|
||||
"""
|
||||
entry = self.model.objects.get(**self.lookup)
|
||||
serialized_data = self.model_to_geo_json(entry)
|
||||
entries = self.model.objects.filter(**self.lookup)
|
||||
serialized_data = {}
|
||||
for entry in entries:
|
||||
serialized_data[str(entry.id)] = self.model_to_geo_json(entry)
|
||||
return serialized_data
|
||||
|
||||
@abstractmethod
|
||||
|
@ -19,7 +19,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
||||
model = Compensation
|
||||
|
||||
def prepare_lookup(self, id, user):
|
||||
self.lookup["id"] = id
|
||||
super().prepare_lookup(id, user)
|
||||
del self.lookup["users__in"]
|
||||
self.lookup["intervention__users__in"] = [user]
|
||||
|
||||
|
@ -90,6 +90,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||
Returns:
|
||||
obj
|
||||
"""
|
||||
if legal_data is None:
|
||||
return obj
|
||||
obj.legal.registration_date = legal_data["registration_date"]
|
||||
obj.legal.binding_date = legal_data["binding_date"]
|
||||
obj.legal.process_type = self.konova_code_from_json(
|
||||
@ -110,6 +112,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||
Returns:
|
||||
obj
|
||||
"""
|
||||
if responsibility_data is None:
|
||||
return obj
|
||||
obj.responsible.registration_office = self.konova_code_from_json(
|
||||
responsibility_data["registration_office"],
|
||||
CODELIST_REGISTRATION_OFFICE_ID
|
||||
@ -134,6 +138,8 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||
Returns:
|
||||
obj (intervention)
|
||||
"""
|
||||
if payment_data is None:
|
||||
return obj
|
||||
payments = []
|
||||
for entry in payment_data:
|
||||
due_on = entry["due_on"]
|
||||
@ -226,9 +232,9 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||
properties = json_model["properties"]
|
||||
obj.title = properties["title"]
|
||||
obj.modified = update_action
|
||||
self.set_responsibility(obj, properties["responsible"])
|
||||
self.set_legal(obj, properties["legal"])
|
||||
self.set_payments(obj, properties["payments"])
|
||||
self.set_responsibility(obj, properties.get("responsible", None))
|
||||
self.set_legal(obj, properties.get("legal", None))
|
||||
self.set_payments(obj, properties.get("payments", None))
|
||||
obj.geometry.geom = self.create_geometry_from_json(json_model)
|
||||
obj.geometry.modified = update_action
|
||||
|
||||
|
@ -50,6 +50,8 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
|
||||
Returns:
|
||||
serialized_json (dict)
|
||||
"""
|
||||
if konova_code is None:
|
||||
return None
|
||||
return {
|
||||
"atom_id": konova_code.atom_id,
|
||||
"long_name": konova_code.long_name,
|
||||
@ -83,7 +85,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
|
||||
Returns:
|
||||
created_on (timestamp)
|
||||
"""
|
||||
return entry.created.timestamp
|
||||
return entry.created.timestamp if entry.created is not None else None
|
||||
|
||||
def modified_on_to_json(self, entry):
|
||||
""" Serializes the modified_on into json
|
||||
@ -95,7 +97,7 @@ class AbstractModelAPISerializerV1(AbstractModelAPISerializer):
|
||||
modified_on (timestamp)
|
||||
"""
|
||||
modified_on = entry.modified or entry.created
|
||||
modified_on = modified_on.timestamp
|
||||
modified_on = modified_on.timestamp if modified_on is not None else None
|
||||
return modified_on
|
||||
|
||||
|
||||
|
@ -28,21 +28,29 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The entries id
|
||||
id (str): The entries id (optional)
|
||||
|
||||
Returns:
|
||||
|
||||
response (JsonResponse)
|
||||
"""
|
||||
try:
|
||||
if id is None:
|
||||
raise AttributeError("No id provided")
|
||||
self.serializer.prepare_lookup(id, self.user)
|
||||
data = self.serializer.fetch_and_serialize()
|
||||
except Exception as e:
|
||||
return self.return_error_response(e, 500)
|
||||
return JsonResponse(data)
|
||||
|
||||
def post(self, request: HttpRequest, id=None):
|
||||
def post(self, request: HttpRequest):
|
||||
""" Handles the POST request
|
||||
|
||||
Performs creation of new data
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
|
||||
Returns:
|
||||
response (JsonResponse)
|
||||
"""
|
||||
try:
|
||||
body = request.body.decode("utf-8")
|
||||
body = json.loads(body)
|
||||
@ -52,6 +60,17 @@ class AbstractModelAPIViewV1(AbstractModelAPIView):
|
||||
return JsonResponse({"id": created_id})
|
||||
|
||||
def put(self, request: HttpRequest, id=None):
|
||||
""" Handles the PUT request
|
||||
|
||||
Performs updating
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The entries id
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
try:
|
||||
body = request.body.decode("utf-8")
|
||||
body = json.loads(body)
|
||||
|
Loading…
Reference in New Issue
Block a user