Beispiel #1
0
 async def read_body(self, read):
     """
     Reads a HTTP body from an ASGI connection.
     """
     body = b""
     while True:
         message = await receive()
         if message["type"] == "http.disconnect":
             # Bye bye.
             raise RequestAborted()
         else:
             # See if the message has body, and if it's the end, launch into
             # handling (and a synchronous subthread)
             if "body" in message:
                 body += message["body"]
             if not message.get("more_body", False):
                 break
     # Limit the maximum request data size that will be handled in-memory.
     # TODO: Stream the body to temp disk instead?
     # (we can't provide a file-like with direct reading as we would not be async)
     if (settings.DATA_UPLOAD_MAX_MEMORY_SIZE is not None and
             self._content_length > settings.DATA_UPLOAD_MAX_MEMORY_SIZE):
         raise RequestDataTooBig(
             "Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE.")
     return body
Beispiel #2
0
    def create(self, body):

        if not self.question:
            raise RequestAborted(
                'you can create an answer with questions/<id>/answers/')

        image_ids = body['imageIds']
        images = Image.objects.filter(id__in=image_ids)

        document_ids = body['documentIds']
        documents = Document.objects.filter(id__in=document_ids)

        creation_data = {
            'body': body['body'],
            'user': self.user,
            'question': self.question
        }

        new_answer = Answer(**creation_data)
        new_answer.full_clean()
        new_answer.save()

        reference_files(images, ImageAttach, new_answer)
        reference_files(documents, DocumentAttach, new_answer)

        return new_answer.id
Beispiel #3
0
    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()

        token = account_activation_token.make_token(user)
        activation_url = BASE_URL + ACTIVATE + str(user.id) + '/' + token
        email = email_generator.activation_email(user.email, activation_url)
        response = email.send()

        if response == 0:
            raise RequestAborted("Failed to send account activation email")

        return Response({"registration": "success"},
                        status=status.HTTP_201_CREATED)
Beispiel #4
0
    def get_raw_token(self, header):
        parts = header.split()

        if len(parts) == 0:
            return None

        if parts[0] not in list(h.encode('iso-8859-1') for h in [
                'Bearer',
        ]):
            return None

        if len(parts) != 2:
            raise RequestAborted(
                'Authorization header must contain two space-delimited values')

        return parts[1]
Beispiel #5
0
 async def read_body(self, receive):
     """Reads a HTTP body from an ASGI connection."""
     # Use the tempfile that auto rolls-over to a disk file as it fills up.
     body_file = tempfile.SpooledTemporaryFile(max_size=settings.FILE_UPLOAD_MAX_MEMORY_SIZE, mode='w+b')
     while True:
         message = await receive()
         if message['type'] == 'http.disconnect':
             # Early client disconnect.
             raise RequestAborted()
         # Add a body chunk from the message, if provided.
         if 'body' in message:
             body_file.write(message['body'])
         # Quit out if that's the end.
         if not message.get('more_body', False):
             break
     body_file.seek(0)
     return body_file
Beispiel #6
0
    def create(self, body):

        if not self.reference:
            raise RequestAborted(
                'you can create a comment with questions/<id>/comments/ or answers/<id>/comments'
            )

        creation_data = {
            'body': body['body'],
            'user': self.user,
            'reference_object': self.reference
        }

        new_comment = Comment(**creation_data)
        new_comment.full_clean()
        new_comment.save()

        return new_comment.id