当前位置: 首页>>代码示例>>Python>>正文


Python status.HTTP_201_CREATED属性代码示例

本文整理汇总了Python中rest_framework.status.HTTP_201_CREATED属性的典型用法代码示例。如果您正苦于以下问题:Python status.HTTP_201_CREATED属性的具体用法?Python status.HTTP_201_CREATED怎么用?Python status.HTTP_201_CREATED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在rest_framework.status的用法示例。


在下文中一共展示了status.HTTP_201_CREATED属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_post_puppies

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def get_post_puppies(request):
    # get all puppies
    if request.method == 'GET':
        puppies = Puppy.objects.all()
        serializer = PuppySerializer(puppies, many=True)
        return Response(serializer.data)
    # insert a new record for a puppy
    if request.method == 'POST':
        data = {
            'name': request.data.get('name'),
            'age': int(request.data.get('age')),
            'breed': request.data.get('breed'),
            'color': request.data.get('color')
        }
        serializer = PuppySerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
开发者ID:realpython,项目名称:django-puppy-store,代码行数:21,代码来源:views.py

示例2: test_post

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_post(self):
        # logged-in user w/ perms
        collection_n = Data.objects.count()

        resp = self._post(self.data, self.user1)
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Data.objects.count(), collection_n + 1)

        d = Data.objects.get(pk=resp.data["id"])
        self.assertTrue(now() - d.modified < timedelta(seconds=1))
        self.assertTrue(now() - d.created < timedelta(seconds=1))
        self.assertEqual(d.status, "OK")

        self.assertTrue(now() - d.started < timedelta(seconds=1))
        self.assertTrue(now() - d.finished < timedelta(seconds=1))
        self.assertEqual(d.contributor_id, self.user1.pk) 
开发者ID:genialis,项目名称:resolwe,代码行数:18,代码来源:test_data.py

示例3: test_create_only_entity

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_create_only_entity(self):
        data = {
            "collection": {"id": self.collection.pk},
            "type": "group",
            "category": "clones",
            "partitions": [{"entity": self.entity_3.pk}, {"entity": self.entity_4.pk},],
        }

        resp = self._post(data, user=self.contributor)
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

        self.assertEqual(Relation.objects.count(), 3)

        relation = Relation.objects.last()
        self.assertEqual(relation.collection, self.collection)
        self.assertEqual(relation.relationpartition_set.count(), 2)
        self.assertTrue(
            relation.relationpartition_set.filter(entity=self.entity_3.pk).exists()
        )
        self.assertTrue(
            relation.relationpartition_set.filter(entity=self.entity_4.pk).exists()
        ) 
开发者ID:genialis,项目名称:resolwe,代码行数:24,代码来源:test_relations.py

示例4: post_message

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def post_message(self, request, pk=None):
        """ Pk is the pk of the Thread to which the message belongs. """
        # we get the thread and check for permission
        thread = Thread.objects.get(id=pk)
        self.check_object_permissions(request, thread)
        # we get the body
        body = compat_get_request_data(self.request).get('body')
        # we create the message
        # Message.objects.save() could return an Exception
        try:
            message = Message(sender=request.rest_messaging_participant, thread=thread, body=body)
            message.save()
            serializer = SimpleMessageSerializer(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        except Exception:
            return Response(status=status.HTTP_412_PRECONDITION_FAILED) 
开发者ID:raphaelgyory,项目名称:django-rest-messaging,代码行数:18,代码来源:views.py

示例5: check

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def check(self, request, *args, **kwargs):
        # we get the NotificationCheck instance corresponding to the user or we create it

        try:
            nc = NotificationCheck.objects.get(participant=request.rest_messaging_participant)
            if nc:
                nc.date_check = now()
                nc.save()
            status_code = status.HTTP_200_OK

        except Exception:
            nc = NotificationCheck.objects.create(participant=request.rest_messaging_participant, date_check=now())
            status_code = status.HTTP_201_CREATED

        serializer = self.get_serializer(nc)
        return Response(serializer.data, status=status_code) 
开发者ID:raphaelgyory,项目名称:django-rest-messaging,代码行数:18,代码来源:views.py

示例6: create

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def create(self, request):
        """Create a new product request
        """

        variant_id = request.data.get("variant_id", None)
        if variant_id is not None:
            variant = ProductVariant.objects.get(id=variant_id)
            product_request = ProductRequest(variant=variant)
            product_request.save()
            serializer = self.serializer_class(product_request)
            response = Response(data=serializer.data, status=status.HTTP_201_CREATED)
        else:
            response = Response(
                {"message": "Missing 'variant_id'"},
                status=status.HTTP_400_BAD_REQUEST)

        return response 
开发者ID:JamesRamm,项目名称:longclaw,代码行数:19,代码来源:api.py

示例7: test_read_message

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_read_message(self):
        self.login_user1()
        url = reverse('message-api-list')
        message = {'recipient': 'u2', 'body': 'hello'}
        response = self.client.post(url, message, format='json')
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.logout()
        # Change user
        self.login_user2()
        url = reverse('message-api-detail', kwargs={'pk': 1})
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['body'], 'hello')
        self.logout()
        # Change to another user (not in conversation)
        self.login_user3()
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
        self.logout() 
开发者ID:narrowfail,项目名称:django-channels-chat,代码行数:21,代码来源:test_api.py

示例8: post

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def post(self, request, username=None):
        follower = self.request.user.profile

        try:
            followee = Profile.objects.get(user__username=username)
        except Profile.DoesNotExist:
            raise NotFound('A profile with this username was not found.')

        if follower.pk is followee.pk:
            raise serializers.ValidationError('You can not follow yourself.')

        follower.follow(followee)

        serializer = self.serializer_class(followee, context={
            'request': request
        })

        return Response(serializer.data, status=status.HTTP_201_CREATED) 
开发者ID:tryolabs,项目名称:aws-workshop,代码行数:20,代码来源:views.py

示例9: test_revoke_refresh_token

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_revoke_refresh_token(self):
        self.client.credentials(
            HTTP_AUTHORIZATION='JWT ' + utils.jwt_encode_handler(
                utils.jwt_payload_handler(self.user)))
        response = self.client.post(self.revoke_url)
        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
            (response.status_code, response.content)
        )
        new_rt = self.user.refresh_tokens.get()
        self.assertEqual(
            response.data,
            {'key': new_rt.key,
             'app': new_rt.app,
             'user': self.user.pk,
             'created': new_rt.created.isoformat()[:-6] + 'Z',
             })
        response = self.client.post(self.revoke_url)
        self.assertEqual(
            response.status_code,
            status.HTTP_404_NOT_FOUND,
            (response.status_code, response.content)
        ) 
开发者ID:lock8,项目名称:django-rest-framework-jwt-refresh-token,代码行数:26,代码来源:test_long_refresh_token_views.py

示例10: test_create_donation

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_create_donation(self):
        url = reverse('v1:donation')
        data = {
            'name': 'Komplizierter Vörnämü-ßßß 马大为',
            'iban': 'DE89370400440532013000',
            'bic': 'BYLADEM1SWU',
            'amount': 123.45,
            'message': 'hi there, thank you. Also, some random chars:  ™ • ½ ¼ ¾ ⅓ ⅔ † ‡ µ ¢ £ € « » ♤ ♧ ♥ ♢ ¿ ',
            'email': 'email@example.com',
        }
        response = self.client.post(url, data)
        self.assertTrue(mail.outbox)
        email_internal = str(mail.outbox[0].message())
        direct_debit = str(mail.outbox[0].attachments[0][1])
        self.assertStatus(response, status.HTTP_201_CREATED)
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(response.data['iban'], data['iban'])
        self.assertTrue('Komplizierter Vornamu' in direct_debit)
        self.assertTrue(data['iban'] in email_internal) 
开发者ID:desec-io,项目名称:desec-stack,代码行数:21,代码来源:test_donations.py

示例11: test_bulk_post_my_rr_sets

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_bulk_post_my_rr_sets(self):
        with self.assertPdnsRequests(self.requests_desec_rr_sets_update(name=self.my_empty_domain.name)):
            response = self.client.bulk_post_rr_sets(domain_name=self.my_empty_domain.name, payload=self.data)
            self.assertStatus(response, status.HTTP_201_CREATED)

        response = self.client.get_rr_sets(self.my_empty_domain.name)
        self.assertStatus(response, status.HTTP_200_OK)
        self.assertRRSetsCount(response.data, self.data)

        # Check subname requirement on bulk endpoint (and uniqueness at the same time)
        self.assertResponse(
            self.client.bulk_post_rr_sets(domain_name=self.my_empty_domain.name, payload=self.data_no_subname),
            status.HTTP_400_BAD_REQUEST,
            [
                {'subname': ['This field is required.']},
                {'non_field_errors': ['Another RRset with the same subdomain and type exists for this domain.']}
            ]
        ) 
开发者ID:desec-io,项目名称:desec-stack,代码行数:20,代码来源:test_rrsets_bulk.py

示例12: test_create_domains

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_create_domains(self):
        self.owner.limit_domains = 100
        self.owner.save()
        for name in [
            '0.8.0.0.0.1.c.a.2.4.6.0.c.e.e.d.4.4.0.1.a.0.1.0.8.f.4.0.1.0.a.2.ip6.arpa',
            'very.long.domain.name.' + self.random_domain_name(),
            self.random_domain_name(),
        ]:
            with self.assertPdnsRequests(self.requests_desec_domain_creation(name)):
                response = self.client.post(self.reverse('v1:domain-list'), {'name': name})
                self.assertStatus(response, status.HTTP_201_CREATED)
                self.assertTrue(all(field in response.data for field in
                                    ['created', 'published', 'name', 'keys', 'minimum_ttl', 'touched']))
                self.assertEqual(len(mail.outbox), 0)
                self.assertTrue(isinstance(response.data['keys'], list))

            with self.assertPdnsRequests(self.request_pdns_zone_retrieve_crypto_keys(name)):
                self.assertStatus(
                    self.client.get(self.reverse('v1:domain-detail', name=name), {'name': name}),
                    status.HTTP_200_OK
                )
                response = self.client.get_rr_sets(name, type='NS', subname='')
                self.assertStatus(response, status.HTTP_200_OK)
                self.assertContainsRRSets(response.data, [dict(subname='', records=settings.DEFAULT_NS, type='NS')]) 
开发者ID:desec-io,项目名称:desec-stack,代码行数:26,代码来源:test_domains.py

示例13: test_create_domain_under_public_suffix_with_private_parent

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def test_create_domain_under_public_suffix_with_private_parent(self):
        name = 'amazonaws.com'
        with self.assertPdnsRequests(self.requests_desec_domain_creation(name)[:-1]), PDNSChangeTracker():
            Domain(owner=self.create_user(), name=name).save()
            self.assertTrue(Domain.objects.filter(name=name).exists())

        # If amazonaws.com is owned by another user, we cannot register test.s4.amazonaws.com
        name = 'test.s4.amazonaws.com'
        response = self.client.post(self.reverse('v1:domain-list'), {'name': name})
        self.assertStatus(response, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data['name'][0].code, 'name_unavailable')

        # s3.amazonaws.com is a public suffix. Therefore, test.s3.amazonaws.com can be
        # registered even if the parent zone amazonaws.com is owned by another user
        name = 'test.s3.amazonaws.com'
        psl_cm = self.get_psl_context_manager('s3.amazonaws.com')
        with psl_cm, self.assertPdnsRequests(self.requests_desec_domain_creation(name)):
            response = self.client.post(self.reverse('v1:domain-list'), {'name': name})
            self.assertStatus(response, status.HTTP_201_CREATED) 
开发者ID:desec-io,项目名称:desec-stack,代码行数:21,代码来源:test_domains.py

示例14: post

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def post(self, request, format=None):
        """
        curl -X POST -S \
          -H 'Accept: application/json' \
          -H 'Content-Type: multipart/form-data' \
          -H 'Authorization: Token {header_token_key}' \
          -F "selection={selection_code/url_code}" \
          -F "name={candidate name}" \
          -F "about={about}" \
          -F "photo=@/path/to/your_photo.jpg;type=image/jpg" \
          http://localhost:8000/api/v1/candidates
        """
        serializer = CandidateSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(
                selection=Selection.objects.get(code=request.data.get('selection')),
                photo=request.data.get('photo')
            )
            return Response(data=serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
开发者ID:agusmakmun,项目名称:Some-Examples-of-Simple-Python-Script,代码行数:22,代码来源:views.py

示例15: rest_submission

# 需要导入模块: from rest_framework import status [as 别名]
# 或者: from rest_framework.status import HTTP_201_CREATED [as 别名]
def rest_submission(request):
    if request.method == "POST":
        serializer = QMCDBSetSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response([serializer.errors], status=status.HTTP_400_BAD_REQUEST)
    return HttpResponse(status=400) 
开发者ID:materialsproject,项目名称:MPContribs,代码行数:10,代码来源:views.py


注:本文中的rest_framework.status.HTTP_201_CREATED属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。