2018-07-09 23:22:26 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
2017-08-24 12:26:55 +02:00
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2012-07-07 11:14:04 +02:00
|
|
|
from django.db import models
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
from .access_permissions import BaseAccessPermissions
|
2017-01-18 16:29:13 +01:00
|
|
|
from .utils import convert_camel_case_to_pseudo_snake_case
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
# Dummy import Collection for mypy, can be fixed with python 3.7
|
|
|
|
from .collection import Collection, CollectionElement # noqa
|
|
|
|
|
|
|
|
|
2012-07-07 11:14:04 +02:00
|
|
|
class MinMaxIntegerField(models.IntegerField):
|
2013-09-25 12:53:44 +02:00
|
|
|
"""
|
|
|
|
IntegerField with options to set a min- and a max-value.
|
|
|
|
"""
|
|
|
|
|
2018-08-08 21:09:22 +02:00
|
|
|
def __init__(self, min_value: int = None, max_value: int = None, *args: Any, **kwargs: Any) -> None:
|
2012-07-07 11:14:04 +02:00
|
|
|
self.min_value, self.max_value = min_value, max_value
|
|
|
|
super(MinMaxIntegerField, self).__init__(*args, **kwargs)
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def formfield(self, **kwargs: Any) -> Any:
|
2012-11-24 14:01:21 +01:00
|
|
|
defaults = {'min_value': self.min_value, 'max_value': self.max_value}
|
2012-07-07 11:14:04 +02:00
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(MinMaxIntegerField, self).formfield(**defaults)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RESTModelMixin:
|
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Mixin for Django models which are used in our REST API.
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
|
|
|
|
2017-08-23 20:51:06 +02:00
|
|
|
access_permissions = None # type: BaseAccessPermissions
|
2016-02-11 11:29:19 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_root_rest_element(self) -> models.Model:
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
|
|
|
Returns the root rest instance.
|
|
|
|
|
|
|
|
Uses self as default.
|
|
|
|
"""
|
|
|
|
return self
|
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
@classmethod
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_access_permissions(cls) -> BaseAccessPermissions:
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns a container to handle access permissions for this model and
|
|
|
|
its corresponding viewset.
|
|
|
|
"""
|
2017-08-24 12:26:55 +02:00
|
|
|
if cls.access_permissions is None:
|
|
|
|
raise ImproperlyConfigured("A RESTModel needs to have an access_permission.")
|
2016-09-17 22:26:23 +02:00
|
|
|
return cls.access_permissions
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
|
|
@classmethod
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_collection_string(cls) -> str:
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Returns the string representing the name of the collection. Returns
|
|
|
|
None if this is not a so called root rest instance.
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
# TODO Check if this is a root rest element class and return None if not.
|
2017-08-24 12:26:55 +02:00
|
|
|
app_label = cls._meta.app_label # type: ignore
|
|
|
|
object_name = cls._meta.object_name # type: ignore
|
2017-01-18 16:29:13 +01:00
|
|
|
return '/'.join(
|
2017-08-24 12:26:55 +02:00
|
|
|
(convert_camel_case_to_pseudo_snake_case(app_label),
|
|
|
|
convert_camel_case_to_pseudo_snake_case(object_name)))
|
2016-01-03 15:33:51 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def get_rest_pk(self) -> int:
|
2016-01-03 15:33:51 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Returns the primary key used in the REST API. By default this is
|
|
|
|
the database pk.
|
2016-01-03 15:33:51 +01:00
|
|
|
"""
|
2017-08-24 12:26:55 +02:00
|
|
|
return self.pk # type: ignore
|
2016-09-18 16:00:31 +02:00
|
|
|
|
2018-08-08 21:09:22 +02:00
|
|
|
def save(self, skip_autoupdate: bool = False, information: Dict[str, str] = None, *args: Any, **kwargs: Any) -> Any:
|
2016-09-18 16:00:31 +02:00
|
|
|
"""
|
2016-09-30 20:42:58 +02:00
|
|
|
Calls Django's save() method and afterwards hits the autoupdate system.
|
2016-09-18 16:00:31 +02:00
|
|
|
|
|
|
|
If skip_autoupdate is set to True, then the autoupdate system is not
|
|
|
|
informed about the model changed. This also means, that the model cache
|
2016-09-30 21:43:22 +02:00
|
|
|
is not updated. You have to do this manually, by creating a collection
|
|
|
|
element from the instance:
|
2016-09-18 16:00:31 +02:00
|
|
|
|
2016-09-30 21:43:22 +02:00
|
|
|
CollectionElement.from_instance(instance)
|
2016-09-30 20:42:58 +02:00
|
|
|
|
|
|
|
The optional argument information can be a dictionary that is given to
|
|
|
|
the autoupdate system.
|
2016-09-18 16:00:31 +02:00
|
|
|
"""
|
2016-09-30 21:43:22 +02:00
|
|
|
# We don't know how to fix this circular import
|
2016-09-18 16:00:31 +02:00
|
|
|
from .autoupdate import inform_changed_data
|
2017-08-24 12:26:55 +02:00
|
|
|
return_value = super().save(*args, **kwargs) # type: ignore
|
2016-09-30 20:42:58 +02:00
|
|
|
if not skip_autoupdate:
|
|
|
|
inform_changed_data(self.get_root_rest_element(), information=information)
|
2016-09-18 16:00:31 +02:00
|
|
|
return return_value
|
|
|
|
|
2018-08-08 21:09:22 +02:00
|
|
|
def delete(self, skip_autoupdate: bool = False, information: Dict[str, str] = None, *args: Any, **kwargs: Any) -> Any:
|
2016-09-18 16:00:31 +02:00
|
|
|
"""
|
2016-09-30 20:42:58 +02:00
|
|
|
Calls Django's delete() method and afterwards hits the autoupdate system.
|
2016-09-18 16:00:31 +02:00
|
|
|
|
2016-09-30 21:43:22 +02:00
|
|
|
If skip_autoupdate is set to True, then the autoupdate system is not
|
|
|
|
informed about the model changed. This also means, that the model cache
|
|
|
|
is not updated. You have to do this manually, by creating a collection
|
|
|
|
element from the instance:
|
|
|
|
|
|
|
|
CollectionElement.from_instance(instance, deleted=True)
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
|
|
CollectionElement.from_values(collection_string, id, deleted=True)
|
|
|
|
|
|
|
|
The optional argument information can be a dictionary that is given to
|
|
|
|
the autoupdate system.
|
2016-09-18 16:00:31 +02:00
|
|
|
"""
|
2016-09-30 21:43:22 +02:00
|
|
|
# We don't know how to fix this circular import
|
2016-09-18 16:00:31 +02:00
|
|
|
from .autoupdate import inform_changed_data, inform_deleted_data
|
2017-08-24 12:26:55 +02:00
|
|
|
instance_pk = self.pk # type: ignore
|
|
|
|
return_value = super().delete(*args, **kwargs) # type: ignore
|
2016-09-30 20:42:58 +02:00
|
|
|
if not skip_autoupdate:
|
|
|
|
if self != self.get_root_rest_element():
|
|
|
|
# The deletion of a included element is a change of the root element.
|
|
|
|
inform_changed_data(self.get_root_rest_element(), information=information)
|
|
|
|
else:
|
2017-09-04 00:25:45 +02:00
|
|
|
inform_deleted_data([(self.get_collection_string(), instance_pk)], information=information)
|
2016-09-18 16:00:31 +02:00
|
|
|
return return_value
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_elements(cls) -> List[Dict[str, Any]]:
|
|
|
|
"""
|
|
|
|
Returns all elements as full_data.
|
|
|
|
"""
|
|
|
|
# Get the query to receive all data from the database.
|
|
|
|
try:
|
|
|
|
query = cls.objects.get_full_queryset() # type: ignore
|
|
|
|
except AttributeError:
|
|
|
|
# If the model des not have to method get_full_queryset(), then use
|
|
|
|
# the default queryset from django.
|
|
|
|
query = cls.objects # type: ignore
|
|
|
|
|
|
|
|
# Build a dict from the instance id to the full_data
|
|
|
|
return [cls.get_access_permissions().get_full_data(instance) for instance in query.all()]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def restrict_elements(
|
|
|
|
cls,
|
|
|
|
user: Optional['CollectionElement'],
|
|
|
|
elements: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
|
|
"""
|
|
|
|
Converts a list of elements from full_data to restricted_data.
|
|
|
|
"""
|
|
|
|
return cls.get_access_permissions().get_restricted_data(elements, user)
|