How to use djangorestframework - 10 common examples

To help you get started, we’ve selected a few djangorestframework examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github encode / django-rest-framework / examples / blogpost / tests.py View on Github external
def test_fifo_behaviour(self):
        '''It's fine that the Blogposts are capped off at MAX_POSTS. But we want to make sure we see FIFO behaviour.'''
        for post in range(15):
            form_data = {'title': '%s' % post, 'content': 'This is the content of post #%s' % post}
            request = self.factory.post('/blog-post', data=form_data)
            view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
            view(request)
        request = self.factory.get('/blog-post')
        view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
        response = view(request)
        response_posts = json.loads(response.content)
        response_titles = [d['title'] for d in response_posts]
        response_titles.reverse()
        self.assertEquals(response_titles, ['%s' % i for i in range(models.MAX_POSTS - 5, models.MAX_POSTS + 5)])
github encode / django-rest-framework / examples / blogpost / tests.py View on Github external
def test_blogposts_not_exceed_MAX_POSTS(self):
        '''Posting blog-posts should not result in more than MAX_POSTS items stored.'''
        for post in range(models.MAX_POSTS + 5):
            form_data = {'title': 'This is post #%s' % post, 'content': 'This is the content of post #%s' % post}
            request = self.factory.post('/blog-post', data=form_data)
            view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
            view(request)
        self.assertEquals(len(models.BlogPost.objects.all()),models.MAX_POSTS)
github encode / django-rest-framework / examples / blogpost / tests.py View on Github external
def test_get_to_root(self):
        '''Simple get to the *root* url of blogposts'''
        request = self.factory.get('/blog-post')
        view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
        response = view(request)
        self.assertEqual(response.status_code, 200)
github encode / django-rest-framework / examples / blogpost / tests.py View on Github external
def test_fifo_behaviour(self):
        '''It's fine that the Blogposts are capped off at MAX_POSTS. But we want to make sure we see FIFO behaviour.'''
        for post in range(15):
            form_data = {'title': '%s' % post, 'content': 'This is the content of post #%s' % post}
            request = self.factory.post('/blog-post', data=form_data)
            view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
            view(request)
        request = self.factory.get('/blog-post')
        view = ListOrCreateModelView.as_view(resource=urls.BlogPostResource)
        response = view(request)
        response_posts = json.loads(response.content)
        response_titles = [d['title'] for d in response_posts]
        response_titles.reverse()
        self.assertEquals(response_titles, ['%s' % i for i in range(models.MAX_POSTS - 5, models.MAX_POSTS + 5)])
github tranvictor / truongnha / api / views.py View on Github external
    @need_login
    def get(self, request, ids):
        user = request.user
        ids = ids.split('-')
        _smses = sms.objects.filter(id__in=ids, sender=user)
        result = {}
        for s in _smses:
            result[s.id] = '%s-%s' % (s.recent, s.success)
        return Response(status.HTTP_200_OK, content=result)
github devilry / devilry-django / devilry / devilry_student / rest / find_groups.py View on Github external
Uses case-ignore-contains search.


    ## Response
    List of objects with the following attributes:

    - ``id`` (int): Internal Devilry ID of the group. Is never ``null``.
    - ``name`` (string|null): The name of the group.
    - ``assignment`` (object): Information about the assignment.
    - ``period`` (object): Information about the period.
    - ``subject`` (object): Information about the subject.

    The response contains at most most 50 elements, and it is ordered by
    the publishing_time of the assignment (newest first).
    """
    permissions = (IsAuthenticated,)
    resource = FindGroupsResource


    def get_queryset(self):
        querystring = self.request.GET.get('query', '').strip()
        if not querystring:
            return AssignmentGroup.objects.none()

        qry = None
        for word in querystring.split():
            wordqry = Q(Q(name__icontains=word) |
                        # Assignment
                        Q(parentnode__short_name__icontains=word) |
                        Q(parentnode__long_name__icontains=word) |
                        # Period
                        Q(parentnode__parentnode__short_name__icontains=word) |
github encode / django-rest-framework / examples / pygments_api / views.py View on Github external
def get(self, request):
        """
        Return a list of all currently existing snippets.
        """
        unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]
        return [reverse('pygments-instance', request=request, args=[unique_id]) for unique_id in unique_ids]
github haiwen / seahub / api / views.py View on Github external
return api_error(request, '400')

        names =  file_names.split(':')
        names = map(lambda x: unquote(x).decode('utf-8'), names)

        for file_name in names:
            try:
                seafserv_threaded_rpc.del_file(repo_id, parent_dir,
                                               file_name, request.user.username)
            except SearpcError,e:
                return api_error(request, '418', 'SearpcError:' + e.msg)

        return reloaddir_if_neccessary (request, repo_id, parent_dir)


class OpMkdirView(ResponseMixin, View):

    @api_login_required
    def get(self, request, repo_id):
        return api_error(request, '407')

    @api_login_required
    def post(self, request, repo_id):
        resp = check_repo_access_permission(request, get_repo(repo_id))
        if resp:
            return resp

        path = request.GET.get('p')
        if not path or path[0] != '/':
            return api_error(request, '400')

        parent_dir = os.path.dirname(path)
github tranvictor / truongnha / api / views.py View on Github external
"""
        3. def for post Diem danh to server:
          post:
          "classId": "xxx"
          "date":"20-04-2012"
          "list":"studentId-status-20-04-2012%studentId-status-20-04-2012%studentId-status-20-04-2012"

        status la trang thai di hoc: K-Nghi ko phep; P- Nghi co phep; M-Di muon;
        """
        class_id = request.POST['classId']
        day = request.POST['date']
        data = request.POST['list']
        try:
            _class = Class.objects.get(id = int(class_id))
        except ObjectDoesNotExist:
            return Response(status.HTTP_404_NOT_FOUND)
        if (is_teacher(request)
                and request.user.teacher in _class.associated_teacher()):
            date_parts = day.split('-')
            try:
                response = dd(request,
                            class_id,
                            date_parts[0],
                            date_parts[1],
                            date_parts[2],
                            api_called=True,
                            data=data)
                if response.status_code == 200:
                    return Response(status.HTTP_200_OK)
                else:
                    return Response(status.HTTP_400_BAD_REQUEST)
            except Exception as e:
github encode / django-rest-framework / djangorestframework / response.py View on Github external
def _get_406_response(self):
        renderer = self.renderer_classes[0]
        return Response(
            {
                'detail': 'Could not satisfy the client\'s Accept header',
                'available_types': [renderer.media_type
                                    for renderer in self.renderer_classes]
            },
            status=status.HTTP_406_NOT_ACCEPTABLE,
            view=self.view, request=self.request, renderer_classes=[renderer])