2019-05-10 13:50:57 +02:00
|
|
|
import time
|
2018-11-03 23:40:20 +01:00
|
|
|
from typing import Any, Dict, List, Optional
|
2017-08-24 12:26:55 +02:00
|
|
|
|
2012-07-07 11:14:04 +02:00
|
|
|
from django.db import models
|
|
|
|
|
2019-08-29 14:25:02 +02:00
|
|
|
from . import logging
|
2019-11-04 14:56:01 +01:00
|
|
|
from .autoupdate import AutoupdateElement, inform_changed_data, inform_elements
|
2018-11-05 09:04:41 +01:00
|
|
|
from .rest_api import model_serializer_classes
|
2019-10-18 14:18:49 +02:00
|
|
|
from .utils import convert_camel_case_to_pseudo_snake_case, get_element_id
|
2017-01-18 16:29:13 +01:00
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
2019-05-10 13:50:57 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2019-01-06 16:22:33 +01: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:
|
2019-01-06 16:22:33 +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-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-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
|
2019-01-06 16:22:33 +01:00
|
|
|
return "/".join(
|
|
|
|
(
|
|
|
|
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
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def get_element_id(self) -> str:
|
|
|
|
return get_element_id(self.get_collection_string(), self.get_rest_pk())
|
|
|
|
|
|
|
|
def save(
|
|
|
|
self,
|
|
|
|
skip_autoupdate: bool = False,
|
2020-12-04 06:51:01 +01:00
|
|
|
disable_history: bool = False,
|
2019-10-18 14:18:49 +02:00
|
|
|
*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
|
2018-11-03 23:40:20 +01:00
|
|
|
is not updated. You have to do this manually by calling
|
|
|
|
inform_changed_data().
|
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
|
2019-01-06 16:22:33 +01:00
|
|
|
|
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:
|
2019-10-18 14:18:49 +02:00
|
|
|
inform_changed_data(
|
|
|
|
self.get_root_rest_element(),
|
2020-12-04 06:51:01 +01:00
|
|
|
disable_history=disable_history,
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
2016-09-18 16:00:31 +02:00
|
|
|
return return_value
|
|
|
|
|
2018-08-22 07:59:22 +02:00
|
|
|
def delete(self, skip_autoupdate: bool = False, *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
|
2018-11-03 23:40:20 +01:00
|
|
|
is not updated. You have to do this manually by calling
|
|
|
|
inform_deleted_data().
|
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
|
2019-01-06 16:22:33 +01:00
|
|
|
|
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.
|
2018-08-22 07:59:22 +02:00
|
|
|
inform_changed_data(self.get_root_rest_element())
|
2016-09-30 20:42:58 +02:00
|
|
|
else:
|
2018-08-22 07:59:22 +02:00
|
|
|
inform_deleted_data([(self.get_collection_string(), instance_pk)])
|
2016-09-18 16:00:31 +02:00
|
|
|
return return_value
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
@classmethod
|
2019-11-04 14:56:01 +01:00
|
|
|
def get_elements(cls, ids: Optional[List[int]] = None) -> List[Dict[str, Any]]:
|
2018-07-09 23:22:26 +02:00
|
|
|
"""
|
|
|
|
Returns all elements as full_data.
|
|
|
|
"""
|
2019-11-13 07:46:13 +01:00
|
|
|
do_logging = not bool(ids)
|
|
|
|
|
|
|
|
if do_logging:
|
|
|
|
logger.info(f"Loading {cls.get_collection_string()}")
|
2018-07-09 23:22:26 +02:00
|
|
|
# Get the query to receive all data from the database.
|
|
|
|
try:
|
2019-11-04 14:56:01 +01:00
|
|
|
query = cls.objects.get_prefetched_queryset(ids=ids) # type: ignore
|
2018-07-09 23:22:26 +02:00
|
|
|
except AttributeError:
|
2019-11-04 14:56:01 +01:00
|
|
|
# If the model des not have to method get_prefetched_queryset(), then use
|
2018-07-09 23:22:26 +02:00
|
|
|
# the default queryset from django.
|
|
|
|
query = cls.objects # type: ignore
|
2019-11-04 14:56:01 +01:00
|
|
|
if ids:
|
|
|
|
query = query.filter(pk__in=ids)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
# Build a dict from the instance id to the full_data
|
2019-05-10 13:50:57 +02:00
|
|
|
instances = query.all()
|
|
|
|
full_data = []
|
|
|
|
|
|
|
|
# For logging the progress
|
|
|
|
last_time = time.time()
|
2021-01-20 09:10:23 +01:00
|
|
|
instances_length = len(instances) # this evaluates the query
|
|
|
|
|
2019-05-10 13:50:57 +02:00
|
|
|
for i, instance in enumerate(instances):
|
|
|
|
# Append full data from this instance
|
|
|
|
full_data.append(instance.get_full_data())
|
2019-11-13 07:46:13 +01:00
|
|
|
if do_logging:
|
|
|
|
# log progress every 5 seconds
|
|
|
|
current_time = time.time()
|
|
|
|
if current_time > last_time + 5:
|
|
|
|
last_time = current_time
|
2020-05-28 13:53:01 +02:00
|
|
|
logger.info(f" {i+1}/{instances_length}...")
|
2021-01-20 09:10:23 +01:00
|
|
|
|
2019-05-10 13:50:57 +02:00
|
|
|
return full_data
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2018-11-03 23:40:20 +01:00
|
|
|
def get_full_data(self) -> Dict[str, Any]:
|
|
|
|
"""
|
|
|
|
Returns the full_data of the instance.
|
|
|
|
"""
|
2018-12-17 13:34:17 +01:00
|
|
|
try:
|
|
|
|
serializer_class = model_serializer_classes[type(self)]
|
|
|
|
except KeyError:
|
|
|
|
# Because of the order of imports, it can happen, that the serializer
|
|
|
|
# for a model is not imported yet. Try to guess the name of the
|
|
|
|
# module and import it.
|
|
|
|
module_name = type(self).__module__.rsplit(".", 1)[0] + ".serializers"
|
|
|
|
__import__(module_name)
|
|
|
|
serializer_class = model_serializer_classes[type(self)]
|
2018-11-05 09:04:41 +01:00
|
|
|
return serializer_class(self).data
|
2019-01-19 14:02:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
def SET_NULL_AND_AUTOUPDATE(
|
|
|
|
collector: Any, field: Any, sub_objs: Any, using: Any
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Like models.SET_NULL but also informs the autoupdate system about the
|
2019-01-19 14:32:11 +01:00
|
|
|
instance that was reference.
|
2019-01-19 14:02:13 +01:00
|
|
|
"""
|
2019-03-01 20:51:42 +01:00
|
|
|
instances = []
|
|
|
|
for sub_obj in sub_objs:
|
|
|
|
setattr(sub_obj, field.name, None)
|
|
|
|
instances.append(sub_obj)
|
|
|
|
inform_changed_data(instances)
|
2019-01-19 14:02:13 +01:00
|
|
|
models.SET_NULL(collector, field, sub_objs, using)
|
|
|
|
|
|
|
|
|
2019-06-28 07:24:28 +02:00
|
|
|
def CASCADE_AND_AUTOUPDATE(
|
2019-01-19 14:02:13 +01:00
|
|
|
collector: Any, field: Any, sub_objs: Any, using: Any
|
|
|
|
) -> None:
|
2019-01-19 14:32:11 +01:00
|
|
|
"""
|
2019-02-27 20:16:51 +01:00
|
|
|
Like models.CASCADE but also informs the autoupdate system about the
|
2019-01-19 14:32:11 +01:00
|
|
|
root rest element of the also deleted instance.
|
|
|
|
"""
|
2019-03-01 20:51:42 +01:00
|
|
|
elements = []
|
|
|
|
for sub_obj in sub_objs:
|
|
|
|
root_rest_element = sub_obj.get_root_rest_element()
|
|
|
|
elements.append(
|
2019-11-04 14:56:01 +01:00
|
|
|
AutoupdateElement(
|
2019-01-19 14:02:13 +01:00
|
|
|
collection_string=root_rest_element.get_collection_string(),
|
2019-11-04 14:56:01 +01:00
|
|
|
id=root_rest_element.get_rest_pk(),
|
2019-01-19 14:02:13 +01:00
|
|
|
)
|
2019-03-01 20:51:42 +01:00
|
|
|
)
|
2019-11-04 14:56:01 +01:00
|
|
|
inform_elements(elements)
|
2019-01-19 14:02:13 +01:00
|
|
|
models.CASCADE(collector, field, sub_objs, using)
|