first base of project-changed apps: Herd-livestock-tag-log-elasticsearch-
This commit is contained in:
53
common/tools.py
Normal file
53
common/tools.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import typing
|
||||
|
||||
|
||||
class CustomOperations:
|
||||
"""
|
||||
@for Custom Operations in view sets
|
||||
"""
|
||||
|
||||
def custom_create( # noqa
|
||||
self,
|
||||
user: object = None,
|
||||
request: object = None,
|
||||
view: object = None,
|
||||
data_key: str = None,
|
||||
additional_data: dict = None
|
||||
) -> typing.Any:
|
||||
"""
|
||||
to create custom view objects that
|
||||
defined in VIEW_SET dictionary to
|
||||
just calling this method in other
|
||||
view sets
|
||||
"""
|
||||
view_data = request.data # included needed data for view set # noqa
|
||||
if user:
|
||||
view_data[data_key].update({'user': user.id}) # noqa
|
||||
if additional_data:
|
||||
view_data[data_key].update(additional_data)
|
||||
serializer = view.serializer_class(data=view_data[data_key]) # noqa
|
||||
serializer.is_valid(raise_exception=True)
|
||||
view.perform_create(serializer) # noqa
|
||||
headers = view.get_success_headers(serializer.data) # noqa
|
||||
return serializer.data
|
||||
|
||||
def custom_update( # noqa
|
||||
self,
|
||||
user: object = None,
|
||||
request: object = None,
|
||||
obj_id: object = None,
|
||||
view: object = None,
|
||||
data_key: str = None,
|
||||
additional_data: dict = None
|
||||
) -> typing.Any:
|
||||
view_data = request.data # included needed data for view set # noqa
|
||||
if user:
|
||||
view_data[data_key].update({'user': user.id}) # noqa
|
||||
if additional_data:
|
||||
view_data[data_key].update(additional_data)
|
||||
serializer = view.serializer_class(data=view_data[data_key]) # noqa
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.update(view.queryset.get(id=obj_id), view_data[data_key]) # noqa
|
||||
# view.perform_update(serializer) # noqa
|
||||
headers = view.get_success_headers(serializer.data) # noqa
|
||||
return serializer.data
|
||||
Reference in New Issue
Block a user