Django Channels Rest Framework V3 #model_observer message is None - django

I tried to find anything in the documentation of model observer which could explain under which conditions the model observer is called with message = None, but there was nothing particularly hinting into the right direction. From what I unterstand from the example:
class MyConsumer(GenericAsyncAPIConsumer):
queryset = User.objects.all()
serializer_class = UserSerializer
#model_observer(Comments)
async def comment_activity(
self,
message: CommentSerializer,
observer=None,
subscribing_request_ids=[]
**kwargs
):
await self.send_json(message.data)
#comment_activity.serializer
def comment_activity(self, instance: Comment, action, **kwargs) -> CommentSerializer:
'''This will return the comment serializer'''
return CommentSerializer(instance)
#action()
async def subscribe_to_comment_activity(self, request_id, **kwargs):
await self.comment_activity.subscribe(request_id=request_id)
The message should be the output of the CommentSerializer so the only way a message to become null is, if the serializer would return nothing ?!
To check if this is the case I added logs prints which sadly indicate that the serializer is receiving data and thous there is no reason the message could be None.
Its also occurring only from time to time so any advice about misunderstanding of how the observer works internally will be highly appreciated.
In our implementation:
class StorageConsumer(ListModelMixin, AsyncAPIConsumer):
"""
Async API endpoint that allows Storage updates to be pushed.
"""
async def accept(self, **kwargs):
await super().accept()
await self.model_change_bess.subscribe()
#model_observer(Bess)
async def model_change_bess(self, message, **kwargs):
if message is None:
logger.warning(f"model_change_bess change without a message")
return
message["model"] = kwargs["observer"].model_cls.__name__
await self.send_json(message)
#model_change_bess.serializer
def model_serialize(self, instance, action, **kwargs):
data = BessSerializer(instance).data
# TODO remove once the message is None bug is fixed
logger.info(f"model_serialize bess instance: {instance} data: {data}")
serialized_data = {
"project_id": data["project"],
"simulation_id": data["simulation"],
"data": data,
}
return serialized_data
the corresponding logs in case message is None looks like this:
16/06/2022 13:09:25 [INFO] api_consumers: model_serialize bess instance: Bess object (37227) data: {'id': 37227, 'created': '2022-06-16T13:09:25Z', 'modified': '2022-06-16T13:09:25Z', 'time_stamp': '2022-06-16T13:09:25Z', 'cycles': '0.02541166724274896000000000000000', 'p_bess': '-74.0000', 'soc': '0.45109075850806520000000000000000', 'soh': '0.99514591796314000000000000000000', 'temperature_bess': None, 'time_to_eol': None, 'feedback': True, 'project': 1, 'simulation': 1}
16/06/2022 13:09:25 [WARNING] api_consumers: model_change_bess change without a message

Related

Django api update/create request from test case never fired the related methods in serializer

How i can send the UPDATE or CREATE request from my test case? When i run my test case the create/update methods never fired in serializer..
What i misunderstand in Django philosophy?
Can someone suggest what i have to do?
For example:
#View
class filesPairView (viewsets.ModelViewSet):
serializer_class = filesPairViewSerializer
def create(self, request):
...
return Response(status=status.HTTP_201_CREATED)
#Serializer
class filesPairViewSerializer(serializers.ModelSerializer):
class Meta:
...
def create(self, validated_data):
print ("filesPairViewSerializer CREATE")
def update(self, validated_data):
print ("filesPairViewSerializer UPDATE")
#Test case
class filesPairViewTestCase(APITestCase):
def test_barmi(self):
print("test_barmi")
url = ('http://127.0.0.1:8000/api/filesPairView/')
data ={
#some valid data
}
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
#urls
router.register(r'filesPairView', views.filesPairView )
Do the following:
#Test case
from django.urls import reverse
class filesPairViewTestCase(APITestCase):
def test_barmi(self):
print("test_barmi")
url = reverse('filespairview-list')
data ={
#some valid data
}
response = self.client.post(url, data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
#urls
router.register(r'filesPairView', views.filesPairView, basename='filespairview')
Notice that the trick here is use the basename which is normally in singular and has the following result based on REST:
reverse('filespairview-list') -> /filesPairView/ - GET - List all
reverse('filespairview-list') -> /filesPairView/ - POST - Create
reverse('filespairview-detail') -> /filesPairView/{int:pk}/ - GET - Get one
reverse('filespairview-detail') -> /filesPairView/{int:pk}/ - POST - Update one
reverse('filespairview-detail') -> /filesPairView/{int:pk}/ - PATCH - Update partial

DRF- Post Request Unitest

I have a post method under View Set. I need to write a unit test case for the method. when I pass param its give None. How should I pass both param and data(payload).
views.py
#action(detail=True, methods=['post'])
def complete_task(self, request, *args, **kwargs):
"""
Method for complete the task
input post request : task_id : str, variable_return:boolean, request data: dict
output Response : gives whether task is completed or not
"""
try:
get_task_id = self.request.query_params.get("task_id")
get_process_variables = request.data
print(get_task_id)
print(get_process_variables)
complete_task = CamundaWriteMixins.complete_task(url=CAMUNDA_URL, task_id=get_task_id,
process_variable_data=get_process_variables)
print("compl", complete_task)
return Response({"task_status": str(complete_task)})
except Exception as error:
return Response(error)
test.py
def test_completed_task(self):
self.client = Client()
url = reverse('complete-task')
data = {"variables": {
"dept_status": {"value": "approved", "type": "String"}}
}
response = self.client.post(url, data=data, params={"task_id": "000c29840512"},
headers={'Content-Type': 'application/json'})
print(response.data)
self.assertTrue(response.data)
I have tried above test case method which is getting request data but I got param None.
Thanks in Advance,.
if you just modify your request a bit and add query param as part of your url then i guess you are good to go.
Example:
response = self.client.post(f'{url}?task_id=000c29840512', data=data,
headers={'Content-Type': 'application/json'})
you can refer the official documentation for the example: https://docs.djangoproject.com/en/4.0/topics/testing/tools/

Skip Django Rest Throttling Counter

How can I prevent Django rest throttling count the request when the user request is invalid or the server failed to complete the process?
For example, I need params from the user, but when the user does not give the params, Django rest throttling still counts it.
Is there any solution to skipping the throttling counter when the request is not successful?
Example
class OncePerHourAnonThrottle(AnonRateThrottle):
rate = "1/hour"
class Autoliker(APIView):
throttle_classes = [OncePerHourAnonThrottle]
def get(self, request):
content = {"status": "get"}
return Response(content)
def post(self, request):
post_url = request.POST.get("url", None)
print(post_url)
content = {"status": "post"}
return Response(content)
def throttled(self, request, wait):
raise Throttled(
detail={
"message": "request limit exceeded",
"availableIn": f"{wait} seconds",
"throttleType": "type",
}
)
You can create a decorator to do so.
class OncePerHourAnonThrottle(AnonRateThrottle):
rate = "1/hour"
def allow_request(self, request, view):
"""
This function is copy of SimpleRateThrottle.allow_request
The only difference is, instead of executing self.throttle_success
it directly returns True and doesn't mark this request as success yet.
"""
if self.rate is None:
return True
self.key = self.get_cache_key(request, view)
if self.key is None:
return True
self.history = self.cache.get(self.key, [])
self.now = self.timer()
# Drop any requests from the history which have now passed the
# throttle duration
while self.history and self.history[-1] <= self.now - self.duration:
self.history.pop()
if len(self.history) >= self.num_requests:
return False
return True
def rate_limiter(view_function):
#wraps(view_function)
def inner(view_obj, request, *args, **kwargs):
throttle = OncePerHourAnonThrottle()
allowed = throttle.allow_request(request, None)
if not allowed:
raise exceptions.Throttled(throttle.wait())
try:
response = view_function(view_obj, request, *args, **kwargs)
except Exception as exc:
response = view_obj.handle_exception(exc)
if response.status_code == 200:
# now if everything goes OK, count this request as success
throttle.throttle_success()
return response
return inner
class Autoliker(APIView):
#rate_limiter
def post(requests):
# view logic
pass
This is the basic idea how you can do it, now you can make it a generic decorator or even class based decorator.

Django REST Framework - Testing POST request

I'm currently struggling to make this current unit-test pass:
def test_markNotifications(self):
request_url = f'Notifications/mark_notifications/'
view = NotificationsViewSet.as_view(actions={'post': 'mark_notifications'})
request = self.factory.post(request_url)
request.POST = {'id_notifs': "1"}
force_authenticate(request, user=self.user)
response = view(request)
self.assertEqual(response.status_code, 200)
Here's the associated view:
#action(detail=False, methods=['POST'])
def mark_notifications(self, request, pk=None):
"""
Put Notifications as already read.
"""
id_notifs = request.POST.get("id_notifs")
if not id_notifs:
return Response("Missing parameters.", status=400)
id_notifs = str(id_notifs).split(",")
print(id_notifs)
for id in id_notifs:
notif = Notification.objects.filter(pk=id).first()
if not notif:
return Response("No existant notification with the given id.", status=400)
notif.isRead = True
notif.save()
return Response("Notifications have been marked as read.", status=200)
The problem is that even though I'm passing "id_notifs" through the request in test, I'm getting None when I do id_notifs = request.POST.get("id_notifs").
It seems that the id_notifs I'm passing in the POST request are neither in the body and the form-data. In this context, I have no idea on how to access them.
Looking forward some help, thanks.

Testing custom action on a viewset in Django Rest Framework

I have defined the following custome action for my ViewSet Agenda:
class AgendaViewSet(viewsets.ModelViewSet):
"""
A simple viewset to retrieve all the Agendas
"""
queryset = Agenda.objects.all()
serializer_class = AgendaSerializer
#action(detail=False, methods=['GET'])
def get_user_agenda(self, request, pk=None):
print('here1')
id = request.GET.get("id_user")
if not id:
return Response("No id in the request.", status=400)
id = int(id)
user = User.objects.filter(pk=id)
if not user:
return Response("No existant user with the given id.", status=400)
response = self.queryset.filter(UserRef__in=user)
if not response:
return Response("No existant Agenda.", status=400)
serializer = AgendaSerializer(response, many=True)
return Response(serializer.data)
Here, I'd like to unit-test my custom action named "get_user_agenda".
However, when I'm testing, the debug output("here1") doesn't show up, and it always returns 200 as a status_code.
Here's my test:
def test_GetUserAgenda(self):
request_url = f'Agenda/get_user_agenda/'
view = AgendaViewSet.as_view(actions={'get': 'retrieve'})
request = self.factory.get(request_url, {'id_user': 15})
response = view(request)
self.assertEqual(response.status_code, 400)
Note that:
self.factory = APIRequestFactory()
Am I missing something?
Sincerely,
You will have to use the method name of the custom action and not retrieve so:
view = AgendaViewSet.as_view(actions={'get': 'get_user_agenda'})
You have to specify request url
#action(detail=False, methods=['GET'], url_path='get_user_agenda')
def get_user_agenda(self, request, pk=None):
And in my opinion it would be better to use detail=True, and get pk from url.
For example: 'Agenda/pk_here/get_user_agenda/'