def setUp(self): """ Setup tests """ # get the content type for Task model self.task_type = get_allowed_contenttypes().filter( model="task").first() # get the content type for User model self.user_type = get_allowed_contenttypes().filter( model="user").first()
def test_get_allowed_contenttypes(self): """ Test get_allowed_contenttypes """ input_expected = [ { "app_label": "tasking", "model": "task" }, { "app_label": "tasking", "model": "segmentrule" }, ] task_type = ContentType.objects.get(app_label="tasking", model="task") rule_type = ContentType.objects.get(app_label="tasking", model="segmentrule") allowed = get_allowed_contenttypes( allowed_content_types=input_expected) self.assertEqual(2, allowed.count()) self.assertIn(task_type, allowed) self.assertIn(rule_type, allowed)
class ContentTypeFieldSerializer(serializers.ModelSerializer): """ Serializer class that provides a contenty_type field """ target_content_type = serializers.PrimaryKeyRelatedField( many=False, queryset=get_allowed_contenttypes() )
class ContentTypeViewSet(viewsets.ReadOnlyModelViewSet): """ Read Only Viewset for ContentType """ serializer_class = ContentTypeSerializer permission_classes = [IsAuthenticated] queryset = get_allowed_contenttypes() def get_queryset(self): queryset = super(ContentTypeViewSet, self).get_queryset() return queryset.order_by('app_label', 'model')
def test_list_contenttype(self): """ Test that we can get the list of allowed content types """ user = mommy.make("auth.User") view = ContentTypeViewSet.as_view({"get": "list"}) request = self.factory.get("/contenttypes") force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), get_allowed_contenttypes().count()) self.assertTrue( ContentType.objects.filter(pk=response.data[1]["id"]).exists())
def test_list_contenttype(self): """ Test that we can get the list of allowed content types """ user = mommy.make('auth.User') view = ContentTypeViewSet.as_view({'get': 'list'}) request = self.factory.get('/contenttypes') force_authenticate(request, user=user) response = view(request=request) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), get_allowed_contenttypes().count()) self.assertTrue( ContentType.objects.filter( pk=response.data['results'][1]['id']).exists())
def delete_instance(instance: object): """ This method attempts to cleanly delete an Onadata Instance as well as all associated Submissions. The entire transaction is atomic and is only successfully committed to the database when everything PASSES """ # get the instance contenttype instance_type = get_allowed_contenttypes().filter(model='instance').first() # get all related submissions submissions = Submission.objects.filter( # pylint: disable=no-member target_object_id=instance.id, target_content_type=instance_type) # delete submissions in a way that signals are sent for submission in submissions: submission.delete() # finally, delete the instance itself instance.delete()
def test_get_allowed_contenttypes(self): """ Test get_allowed_contenttypes """ input_expected = [{ 'app_label': 'tasking', 'model': 'task' }, { 'app_label': 'tasking', 'model': 'segmentrule' }] task_type = ContentType.objects.get(app_label='tasking', model='task') rule_type = ContentType.objects.get(app_label='tasking', model='segmentrule') allowed = get_allowed_contenttypes( allowed_content_types=input_expected) self.assertEqual(2, allowed.count()) self.assertIn(task_type, allowed) self.assertIn(rule_type, allowed)
def create_submission(ona_instance: object): """ Validates Submission Data and Creates a Submission """ data = ona_instance.json task = ona_instance.get_task() user = ona_instance.user validated_data = { 'task': { 'type': 'Task', 'id': task.id }, 'user': { 'type': 'User', 'id': user.id }, 'submission_time': data['_submission_time'], 'valid': False, 'target_content_type': get_allowed_contenttypes().filter(model='instance').first().id, 'target_id': ona_instance.id } if task.bounty is not None: validated_data['bounty'] = {'type': 'Bounty', 'id': task.bounty.id} # if submission has had a review, update validated_data appropriately if settings.ONA_STATUS_FIELD in data: validated_data['status'] = convert_ona_to_kaznet_submission_status( ona_status=data[settings.ONA_STATUS_FIELD]) validated_data['comments'] = str( data.get(settings.ONA_COMMENTS_FIELD, "")) # indicate that the instance object's review status # has already been synced ona_instance.json["synced_with_ona_data"] = True # if submission hasn't had a review(pending), or no review information of # submission if settings.ONA_STATUS_FIELD not in data or ( validated_data['status'] == Submission.PENDING and not validated_data['comments']): # Validation: order: User - Location - Time status, comment = validate_user(task, user) validated_data['status'] = status validated_data['comments'] = str(comment) # Validate location if submission passed user validation and # submission has location details if validated_data['status'] != Submission.REJECTED and \ all(data[settings.ONA_GEOLOCATION_FIELD]): location, status, comment = validate_location( data[settings.ONA_GEOLOCATION_FIELD], task) if location: validated_data['location'] = { 'type': 'Location', 'id': location.id } validated_data['status'] = status validated_data['comments'] = str(comment) # Validate time if Submission passed location validation if validated_data['status'] != Submission.REJECTED: status, comment = validate_submission_time( task, data['_submission_time']) validated_data['status'] = status validated_data['comments'] = str(comment) # Validate limit if submission passed time validation if validated_data['status'] != Submission.REJECTED: status, comment = validate_submission_limit(task, user) # Auto approve submission if it passes all validations and # auto approval is set if status == Submission.PENDING and \ settings.SUBMISSION_AUTO_APPROVAL: validated_data['status'] = Submission.APPROVED validated_data['comments'] = str(comment) else: validated_data['status'] = status validated_data['comments'] = str(comment) if 'location' not in validated_data: location = get_locations(data.get(settings.ONA_GEOLOCATION_FIELD), task) if location: # get one of the valid locations validated_data['location'] = { 'type': 'Location', 'id': location.first().id } # call sync_submission_review based on validated_data[status] # and the json field task_sync_submission_review.delay(ona_instance.id, validated_data['status'], validated_data['comments']) if validated_data['status'] == Submission.REJECTED: validated_data['valid'] = False else: validated_data['valid'] = True serializer_instance = KaznetSubmissionSerializer(data=validated_data) if serializer_instance.is_valid(): return serializer_instance.save() return None