Python rest_framework.decorators.api_view() Examples

The following are 6 code examples of rest_framework.decorators.api_view(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.decorators , or try the search function .
Example #1
Source File: decorators.py    From drf-typed-views with MIT License 6 votes vote down vote up
def typed_api_view(methods):
    def wrap_validate_and_render(view):
        prevalidate(view)

        @api_view(methods)
        @wraps_drf(view)
        def wrapper(*original_args, **original_kwargs):
            original_args = list(original_args)
            request = find_request(original_args)
            transformed = transform_view_params(
                inspect.signature(view).parameters.values(), request, original_kwargs
            )
            return view(*transformed)

        return wrapper

    return wrap_validate_and_render 
Example #2
Source File: test_api_view_serializer_class_getter.py    From django-rest-registration with MIT License 5 votes vote down vote up
def input_post_view():
    return (api_view(['POST']))(dummy_view_func) 
Example #3
Source File: test_api_view_serializer_class_getter.py    From django-rest-registration with MIT License 5 votes vote down vote up
def input_put_view():
    return (api_view(['PUT']))(dummy_view_func) 
Example #4
Source File: test_access_policy.py    From drf-access-policy with MIT License 5 votes vote down vote up
def test_get_invoked_action_from_function_based_view(self):
        @api_view(["GET"])
        def my_view(request):
            return ""

        policy = AccessPolicy()
        view_instance = my_view.cls()

        result = policy._get_invoked_action(view_instance)
        self.assertEqual(result, "my_view") 
Example #5
Source File: views.py    From kobo-predict with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def deploy_general_remaining_sites(request, is_project, pk):
    fxf_id = request.data.get('id')
    fxf_status = request.data.get('is_deployed')
    try:
        if is_project == "1":
            with transaction.atomic():
                fxf = FieldSightXF.objects.get(pk=fxf_id)
                if fxf_status:
                    site_ids=[]
                    for site in fxf.project.sites.filter(is_active=True):
                        child, created = FieldSightXF.objects.get_or_create(is_staged=False, is_scheduled=False, xf=fxf.xf, site=site, fsform_id=fxf_id)
                        child.is_deployed = True
                        child.save()
                        if created:
                            site_ids.append(site.id)
                    send_bulk_message_stages(site_ids)
                else:
                    return Response({'error':"Deploy Form First and deploy to remaining.."}, status=status.HTTP_400_BAD_REQUEST)
            return Response({'msg': 'ok'}, status=status.HTTP_200_OK)
        else:
            return Response({'error':"Site level Deploy to remaining Not permitted."}, status=status.HTTP_400_BAD_REQUEST)
    except Exception as e:
        return Response({'error':e.message}, status=status.HTTP_400_BAD_REQUEST)



# @group_required("Project")
# @api_view([])
# def deploy_general(request, fxf_id):
#     with transaction.atomic():
#         fxf = FieldSightXF.objects.get(pk=fxf_id)
#         FieldSightXF.objects.filter(fsform=fxf, is_scheduled=False, is_staged=False).delete()
#         for site in fxf.project.sites.filter(is_active=True):
#             # cloning from parent
#             child = FieldSightXF(is_staged=False, is_scheduled=False, xf=fxf.xf, site=site, fsform_id=fxf_id,
#                                  is_deployed=True)
#             child.save()
#     messages.info(request, 'General Form {} Deployed to Sites'.format(fxf.xf.title))
#     return HttpResponseRedirect(reverse("forms:project-general", kwargs={'project_id': fxf.project.pk})) 
Example #6
Source File: search.py    From indrz with GNU General Public License v3.0 5 votes vote down vote up
def search_indrz(request, campus_id, search_string, format=None):
    # query_string = ''
    # found_entries = None

    # if using a url like http://www.campus.com/search/?q=sometext
    # use this if statement and indent code block

    # if ('q' in request.GET) and request.GET['q'].strip():
    #     query_string = request.GET['q']

    entry_query = get_query(search_string, ['short_name', 'long_name', 'room_code', 'room_description'])

    # return only first 20 results
    found_entries = BuildingFloorSpace.objects.filter(fk_building__fk_campus=campus_id).filter(entry_query)[:20]

    # buildings_on_campus = BuildingFloorSpace.objects.filter(Q(short_name__icontains=search_string) | Q(room_code__icontains=search_string))
    serializer = BuildingFloorSpaceSerializer(found_entries, many=True)

    if found_entries:

        return Response(serializer.data)

    # elif:
    #     pass
        # return Response({'error': 'sorry nothing found with the text:  ' + search_string})

    else:
        return Response({'error': 'sorry nothing found with the text:  ' + search_string})

# old silly search
# @api_view(['GET'])
# def campus_search(request, campus_id, search_string, format=None):
#     """
#     Search campus spaces in system and pois
#     """
#     if request.method == 'GET':
#
#         buildings_on_campus = BuildingFloorSpace.objects.filter(Q(short_name__icontains=search_string) | Q(room_code__icontains=search_string))
#         serializer = BuildingFloorSpaceSerializer(buildings_on_campus, many=True)
#
#         return Response(serializer.data)