/
base.py
111 lines (89 loc) · 4.23 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from django.db import transaction
from pulpcore.app.apps import get_plugin_config
from pulpcore.app.models import CreatedResource
from pulpcore.plugin.models import MasterModel
def general_create_from_temp_file(app_label, serializer_name, temp_file_pk, *args, **kwargs):
"""
Create a model instance from contents stored in a temporary file.
A task which executes this function takes the ownership of a temporary file and deletes it
afterwards. This function calls the function general_create() to create a model instance.
"""
data = kwargs.pop("data", {})
context = kwargs.pop("context", {})
context["pulp_temp_file_pk"] = temp_file_pk
general_create(app_label, serializer_name, data=data, context=context, *args, **kwargs)
def general_create(app_label, serializer_name, *args, **kwargs):
"""
Create a model instance.
Raises:
ValidationError: If the serializer is not valid
"""
data = kwargs.pop("data", None)
context = kwargs.pop("context", {})
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
serializer = serializer_class(data=data, context=context)
serializer.is_valid(raise_exception=True)
instance = serializer.save()
if isinstance(instance, MasterModel):
instance = instance.cast()
resource = CreatedResource(content_object=instance)
resource.save()
def general_update(instance_id, app_label, serializer_name, *args, **kwargs):
"""
Update a model
The model instance is identified using the app_label, id, and serializer name. The serializer is
used to perform validation.
Args:
id (str): the id of the model
app_label (str): the Django app label of the plugin that provides the model
serializer_name (str): name of the serializer class for the model
data (dict): dictionary whose keys represent the fields of the model and their corresponding
values.
partial (bool): When true, only the fields specified in the data dictionary are updated.
When false, any fields missing from the data dictionary are assumed to be None and
their values are updated as such.
Raises:
:class:`rest_framework.exceptions.ValidationError`: When serializer instance can't be saved
due to validation error. This theoretically should never occur since validation is
performed before the task is dispatched.
"""
data = kwargs.pop("data", None)
partial = kwargs.pop("partial", False)
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
instance = serializer_class.Meta.model.objects.get(pk=instance_id)
if isinstance(instance, MasterModel):
instance = instance.cast()
serializer = serializer_class(instance, data=data, partial=partial)
serializer.is_valid(raise_exception=True)
serializer.save()
def general_delete(instance_id, app_label, serializer_name):
"""
Delete a model
The model instance is identified using the app_label, id, and serializer name.
Args:
id (str): the id of the model
app_label (str): the Django app label of the plugin that provides the model
serializer_name (str): name of the serializer class for the model
"""
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
instance = serializer_class.Meta.model.objects.get(pk=instance_id)
if isinstance(instance, MasterModel):
instance = instance.cast()
instance.delete()
def general_multi_delete(instance_ids):
"""
Delete a list of model instances in a transaction
The model instances are identified using the id, app_label, and serializer_name.
Args:
instance_ids (list): List of tupels of id, app_label, serializer_name
"""
instances = []
for instance_id, app_label, serializer_name in instance_ids:
serializer_class = get_plugin_config(app_label).named_serializers[serializer_name]
instance = serializer_class.Meta.model.objects.get(pk=instance_id)
if isinstance(instance, MasterModel):
instance = instance.cast()
instances.append(instance)
with transaction.atomic():
for instance in instances:
instance.delete()