def create(self, request, *args, **kwargs): wrr_serializer = WorkerRequesterRatingSerializer(data=request.data) if wrr_serializer.is_valid(): wrr_serializer.create(origin=request.user.userprofile) return Response({"status": "Rating created"}) else: return Response(wrr_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, pk=None): wrr_serializer = WorkerRequesterRatingSerializer(data=request.data, partial=True) wrr = self.get_object() if wrr_serializer.is_valid(): wrr_serializer.update(wrr, wrr_serializer.validated_data) return Response({"status": "updated rating"}) else: return Response(wrr_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def retrieve_with_data(self, request, *args, **kwargs): task = self.get_object() serializer = TaskSerializer(instance=task, fields=('id', 'task_template', 'module_data', 'status', 'has_comments')) rating = models.WorkerRequesterRating.objects.filter( origin=request.user.userprofile.id, target=task.module.owner.profile.id, origin_type='worker', module=task.module.id) requester_alias = task.module.owner.alias module = task.module.id target = task.module.owner.profile.id if rating.count() != 0: rating_serializer = WorkerRequesterRatingSerializer( instance=rating, many=True, fields=('id', 'weight')) return Response( { 'data': serializer.data, 'rating': rating_serializer.data, 'requester_alias': requester_alias, 'module': module, 'target': target }, status.HTTP_200_OK) else: return Response( { 'data': serializer.data, 'requester_alias': requester_alias, 'module': module, 'target': target }, status.HTTP_200_OK)
def workers_reviews_by_project(self, request, **kwargs): project_id = request.query_params.get('project', -1) data = TaskWorker.objects.raw( ''' SELECT "crowdsourcing_workerrequesterrating"."id" id, "crowdsourcing_task"."project_id" project, 'requester' origin_type, "crowdsourcing_workerrequesterrating"."weight" weight, "crowdsourcing_worker"."profile_id" target, "crowdsourcing_worker"."alias" alias, "crowdsourcing_project"."owner_id" origin, COUNT("crowdsourcing_taskworker"."task_id") AS "task_count" FROM "crowdsourcing_taskworker" INNER JOIN "crowdsourcing_task" ON ("crowdsourcing_taskworker"."task_id" = "crowdsourcing_task"."id") INNER JOIN "crowdsourcing_project" ON ("crowdsourcing_task"."project_id" = "crowdsourcing_project"."id") INNER JOIN "crowdsourcing_worker" ON ("crowdsourcing_taskworker"."worker_id" = "crowdsourcing_worker"."id") INNER JOIN "crowdsourcing_userprofile" ON ("crowdsourcing_worker"."profile_id" = "crowdsourcing_userprofile"."id") LEFT OUTER JOIN "crowdsourcing_workerrequesterrating" ON ("crowdsourcing_userprofile"."id" = "crowdsourcing_workerrequesterrating"."target_id" and crowdsourcing_workerrequesterrating.project_id = crowdsourcing_project.id) WHERE ("crowdsourcing_taskworker"."task_status" IN (2, 3, 4, 5) AND "crowdsourcing_project"."id" = %s) GROUP BY "crowdsourcing_task"."project_id", "crowdsourcing_workerrequesterrating"."weight", "crowdsourcing_worker"."profile_id", "crowdsourcing_worker"."alias", "crowdsourcing_project"."owner_id", "crowdsourcing_workerrequesterrating"."id"; ''', params=[project_id] ) serializer = WorkerRequesterRatingSerializer(data, many=True, context={'request': request}) response_data = serializer.data return Response(data=response_data, status=status.HTTP_200_OK)
def create(self, request, *args, **kwargs): wrr_serializer = WorkerRequesterRatingSerializer(data=request.data) if wrr_serializer.is_valid(): wrr = wrr_serializer.create(origin=request.user.userprofile) wrr_serializer = WorkerRequesterRatingSerializer(instance=wrr) return Response(wrr_serializer.data, status=status.HTTP_201_CREATED) else: return Response(wrr_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): wrr_serializer = WorkerRequesterRatingSerializer(data=request.data, partial=True) wrr = self.get_object() if wrr_serializer.is_valid(): wrr = wrr_serializer.update(wrr, wrr_serializer.validated_data) wrr_serializer = WorkerRequesterRatingSerializer(instance=wrr) return Response(wrr_serializer.data, status=status.HTTP_200_OK) else: return Response(wrr_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def retrieve_with_data(self, request, *args, **kwargs): task = self.get_object() task_worker = TaskWorker.objects.get( id=request.query_params['taskWorkerId']) serializer = TaskSerializer(instance=task, fields=('id', 'task_template', 'project_data', 'status', 'has_comments')) rating = models.WorkerRequesterRating.objects.filter( origin=request.user.userprofile.id, target=task.project.owner.profile.id, origin_type='worker', project=task.project.id) template = serializer.data.get('task_template', []) for item in template['template_items']: # unique ids to send back for additional layer of security if item['type'] == 'iframe': from django.conf import settings from hashids import Hashids hash = Hashids(salt=settings.SECRET_KEY) item['identifier'] = hash.encode(task_worker.id, task.id, item['id']) serializer.data['task_template'] = template requester_alias = task.project.owner.alias project = task.project.id target = task.project.owner.profile.id if rating.count() != 0: rating_serializer = WorkerRequesterRatingSerializer( instance=rating, many=True, fields=('id', 'weight')) return Response( { 'data': serializer.data, 'rating': rating_serializer.data, 'requester_alias': requester_alias, 'project': project, 'target': target }, status.HTTP_200_OK) else: return Response( { 'data': serializer.data, 'requester_alias': requester_alias, 'project': project, 'target': target }, status.HTTP_200_OK)
def requesters_ratings(self, request, **kwargs): data = TaskWorker.objects.raw( ''' SELECT DISTINCT(r.alias) alias, 'worker' origin_type, %(worker_profile)s origin, wrr.id id, r.profile_id target, wrr.weight weight FROM crowdsourcing_taskworker tw INNER JOIN crowdsourcing_task t ON tw.task_id=t.id INNER JOIN crowdsourcing_project p ON t.project_id=p.id INNER JOIN crowdsourcing_requester r ON p.owner_id=r.id INNER JOIN crowdsourcing_userprofile up ON r.profile_id=up.id LEFT OUTER JOIN crowdsourcing_workerrequesterrating wrr ON up.id=wrr.target_id WHERE tw.task_status IN (3,4,5) AND tw.worker_id=%(worker)s; ''', params={'worker_profile': request.user.userprofile.id, 'worker': request.user.userprofile.worker.id} ) serializer = WorkerRequesterRatingSerializer(data, many=True, context={'request': request}) response_data = serializer.data return Response(data=response_data, status=status.HTTP_200_OK)
def workers_reviews_by_module(self, request, **kwargs): if request.query_params.get('fake_module_id', False): submodule = SubModule.objects.get( fake_module_id=request.query_params.get('fake_module_id')) module_id = submodule.origin_module.id module_owner = request.user.userprofile.requester.id taskworkers = submodule.taskworkers data = TaskWorker.objects.raw( ''' SELECT "crowdsourcing_workerrequesterrating"."id" id, "crowdsourcing_task"."module_id" module, 'requester' origin_type, "crowdsourcing_workerrequesterrating"."weight" weight, "crowdsourcing_worker"."profile_id" target, "crowdsourcing_worker"."alias" alias, "crowdsourcing_module"."owner_id" origin, COUNT("crowdsourcing_taskworker"."task_id") AS "task_count" FROM "crowdsourcing_taskworker" INNER JOIN "crowdsourcing_task" ON ("crowdsourcing_taskworker"."task_id" = "crowdsourcing_task"."id") INNER JOIN "crowdsourcing_module" ON ("crowdsourcing_task"."module_id" = "crowdsourcing_module"."id") INNER JOIN "crowdsourcing_worker" ON ("crowdsourcing_taskworker"."worker_id" = "crowdsourcing_worker"."id") INNER JOIN "crowdsourcing_userprofile" ON ("crowdsourcing_worker"."profile_id" = "crowdsourcing_userprofile"."id") LEFT OUTER JOIN "crowdsourcing_workerrequesterrating" ON ("crowdsourcing_userprofile"."id" = "crowdsourcing_workerrequesterrating"."target_id" and crowdsourcing_workerrequesterrating.module_id = crowdsourcing_module.id and "crowdsourcing_workerrequesterrating".origin_id=%s) WHERE ("crowdsourcing_taskworker"."task_status" IN (2, 3, 4, 5) AND "crowdsourcing_module"."id" = %s AND "crowdsourcing_taskworker"."id" =ANY (%s::int[])) GROUP BY "crowdsourcing_task"."module_id", "crowdsourcing_workerrequesterrating"."weight", "crowdsourcing_worker"."profile_id", "crowdsourcing_worker"."alias", "crowdsourcing_module"."owner_id", "crowdsourcing_workerrequesterrating"."id"; ''', params=[module_owner, module_id, taskworkers]) else: module_id = request.query_params.get('module', -1) data = TaskWorker.objects.raw(''' SELECT "crowdsourcing_workerrequesterrating"."id" id, "crowdsourcing_task"."module_id" module, 'requester' origin_type, "crowdsourcing_workerrequesterrating"."weight" weight, "crowdsourcing_worker"."profile_id" target, "crowdsourcing_worker"."alias" alias, "crowdsourcing_module"."owner_id" origin, COUNT("crowdsourcing_taskworker"."task_id") AS "task_count" FROM "crowdsourcing_taskworker" INNER JOIN "crowdsourcing_task" ON ("crowdsourcing_taskworker"."task_id" = "crowdsourcing_task"."id") INNER JOIN "crowdsourcing_module" ON ("crowdsourcing_task"."module_id" = "crowdsourcing_module"."id") INNER JOIN "crowdsourcing_worker" ON ("crowdsourcing_taskworker"."worker_id" = "crowdsourcing_worker"."id") INNER JOIN "crowdsourcing_userprofile" ON ("crowdsourcing_worker"."profile_id" = "crowdsourcing_userprofile"."id") LEFT OUTER JOIN "crowdsourcing_workerrequesterrating" ON ("crowdsourcing_userprofile"."id" = "crowdsourcing_workerrequesterrating"."target_id" and crowdsourcing_workerrequesterrating.module_id = crowdsourcing_module.id) WHERE ("crowdsourcing_taskworker"."task_status" IN (2, 3, 4, 5) AND "crowdsourcing_module"."id" = %s) GROUP BY "crowdsourcing_task"."module_id", "crowdsourcing_workerrequesterrating"."weight", "crowdsourcing_worker"."profile_id", "crowdsourcing_worker"."alias", "crowdsourcing_module"."owner_id", "crowdsourcing_workerrequesterrating"."id"; ''', params=[module_id]) serializer = WorkerRequesterRatingSerializer( data, many=True, context={'request': request}) response_data = serializer.data return Response(data=response_data, status=status.HTTP_200_OK)