1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
from django.contrib.auth import get_user_model |
4
|
|
|
|
5
|
|
|
from modernrpc.core import rpc_method, REQUEST_KEY |
6
|
|
|
|
7
|
|
|
from tcms.management.models import Component |
8
|
|
|
from tcms.rpc.utils import pre_check_product |
9
|
|
|
from tcms.rpc.decorators import permissions_required |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
User = get_user_model() # pylint: disable=invalid-name |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
__all__ = ( |
16
|
|
|
'create', |
17
|
|
|
'update', |
18
|
|
|
'filter', |
19
|
|
|
) |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
@rpc_method(name='Component.filter') |
23
|
|
|
def filter(query): # pylint: disable=redefined-builtin |
24
|
|
|
""" |
25
|
|
|
.. function:: XML-RPC Component.filter(query) |
26
|
|
|
|
27
|
|
|
Search and return the resulting list of components. |
28
|
|
|
|
29
|
|
|
:param query: Field lookups for :class:`tcms.management.models.Component` |
30
|
|
|
:type query: dict |
31
|
|
|
:return: List of serialized :class:`tcms.management.models.Component` objects |
32
|
|
|
:rtype: list(dict) |
33
|
|
|
""" |
34
|
|
|
return Component.to_xmlrpc(query) |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
@permissions_required('management.add_component') |
38
|
|
|
@rpc_method(name='Component.create') |
39
|
|
|
def create(values, **kwargs): |
40
|
|
|
""" |
41
|
|
|
.. function:: XML-RPC Component.create(values) |
42
|
|
|
|
43
|
|
|
Create new component. |
44
|
|
|
|
45
|
|
|
:param values: Field values for :class:`tcms.management.models.Component` |
46
|
|
|
:type values: dict |
47
|
|
|
:return: Serialized :class:`tcms.management.models.Component` object |
48
|
|
|
:rtype: dict |
49
|
|
|
:raises: PermissionDenied if missing *management.add_component* permission |
50
|
|
|
|
51
|
|
|
.. note:: |
52
|
|
|
|
53
|
|
|
If ``initial_owner_id`` or ``initial_qa_owner_id`` are |
54
|
|
|
not specified or don't exist in the database these fields are set to the |
55
|
|
|
user issuing the RPC request! |
56
|
|
|
""" |
57
|
|
|
initial_owner_id = values.get('initial_owner_id', None) |
58
|
|
|
initial_qa_contact_id = values.get('initial_qa_contact_id', None) |
59
|
|
|
product = pre_check_product(values) |
60
|
|
|
|
61
|
|
|
request = kwargs.get(REQUEST_KEY) |
62
|
|
|
if User.objects.filter(pk=initial_owner_id).exists(): |
63
|
|
|
_initial_owner_id = initial_owner_id |
64
|
|
|
else: |
65
|
|
|
_initial_owner_id = request.user.pk |
66
|
|
|
|
67
|
|
|
if User.objects.filter(pk=initial_qa_contact_id).exists(): |
68
|
|
|
_initial_qa_contact_id = initial_qa_contact_id |
69
|
|
|
else: |
70
|
|
|
_initial_qa_contact_id = request.user.pk |
71
|
|
|
|
72
|
|
|
return Component.objects.create( |
73
|
|
|
name=values['name'], |
74
|
|
|
product=product, |
75
|
|
|
initial_owner_id=_initial_owner_id, |
76
|
|
|
initial_qa_contact_id=_initial_qa_contact_id, |
77
|
|
|
).serialize() |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
@permissions_required('management.change_component') |
81
|
|
|
@rpc_method(name='Component.update') |
82
|
|
|
def update(component_id, values): |
83
|
|
|
""" |
84
|
|
|
.. function:: XML-RPC Component.update |
85
|
|
|
|
86
|
|
|
Update component with new values. |
87
|
|
|
|
88
|
|
|
:param component_id: PK of Component to be updated |
89
|
|
|
:type component_id: int |
90
|
|
|
:param values: Fields and values to be updated |
91
|
|
|
:type values: dict |
92
|
|
|
:return: Serialized :class:`tcms.management.models.Component` object |
93
|
|
|
:rtype: dict |
94
|
|
|
:raises: ValueError if ``name`` is missing or empty string |
95
|
|
|
:raises: PermissionDenied if missing *management.change_component* permission |
96
|
|
|
""" |
97
|
|
|
if not isinstance(values, dict) or 'name' not in values: |
98
|
|
|
raise ValueError('Component name is not in values {0}.'.format(values)) |
99
|
|
|
|
100
|
|
|
name = values['name'] |
101
|
|
|
if not isinstance(name, str) or not name: |
102
|
|
|
raise ValueError('Component name {0} is not a string value.'.format(name)) |
103
|
|
|
|
104
|
|
|
component = Component.objects.get(pk=int(component_id)) |
105
|
|
|
component.name = name |
106
|
|
|
if values.get('initial_owner_id') and \ |
107
|
|
|
User.objects.filter(pk=values['initial_owner_id']).exists(): |
108
|
|
|
component.initial_owner_id = values['initial_owner_id'] |
109
|
|
|
if values.get('initial_qa_contact_id') and \ |
110
|
|
|
User.objects.filter(pk=values['initial_qa_contact_id']).exists(): |
111
|
|
|
component.initial_qa_contact_id = values['initial_qa_contact_id'] |
112
|
|
|
component.save() |
113
|
|
|
return component.serialize() |
114
|
|
|
|