コード例 #1
0
ファイル: test_post.py プロジェクト: ericmok/angularblog
	def test_post_serializer(self):
		from blog.rest.serializers import PostSerializer
		data = {
			"title": "Some title",
			"description": "Blah",
			"parent_content_type": "blog",
			"parent_id": 1,
			"content": "Some content here. And here is my second sentence."
		}
		post_serializer = PostSerializer(data=data)
		post_serializer.is_valid()
		self.assertIn('title', post_serializer.data)
コード例 #2
0
ファイル: post_viewset.py プロジェクト: ericmok/angularblog
    def create(self, request):
        """
        Preconditions:
        User is authenticated, either via tokens or cookie sessions
        The request is authenticated via the authentication class. 
        The author *is* the user 
        if request.user != post_serializer.object['author']:
           return Response({'error': 'You are not logged in as the given author'}, status = 401)
        Title is not null
        
        If the parent object is a blog and is RESTRICTED, abort write process, 
        1) unless user is the creator
        2) or user is white listed for that blog
        
        Parent object exists <- This is checked in serializer validation
        Content can be empty or a string containing multiple sentences. 
        
        The post is created before content is validated...?
        There are no validations on sentences...
        
        If there is content, parse it (ie. an essay) into sentences via NLTK punkt.
        Create sentences for each sentence
        """

        # Parse user input
        post_serializer = PostSerializer(data = request.DATA, context={'request': request})

        if not post_serializer.is_valid():
            # The user input was invalid
            return Response(post_serializer.errors, status = 400)

        # Serializer doesn't check for empty content since its IO is dynamic 
        content = request.DATA.get('content', None)
        if not content_is_not_empty(content):
            return Response({"error": "There was no content, refer to template for reference."}, status = 400)
       
       
        # If the post is made on a blog content type and the blog is RESTRICTED, 
        # perform special authorization checks:
        # A restricted blog means only users on the white list of the blog can post to it
        parent_ct_name = post_serializer.data['parent_content_type']

        # The content type is already validated by the is_valid call on the serializer
        blog = get_parent_blog_of_model(post_serializer.data['parent_content_type'], post_serializer.data['parent_id'])
        if blog.is_restricted:
            # When a blog is restricted, the creator is allowed 
            # access to it by default, regardless of whether s/he is on whitelist!
            if blog.creator != request.user:
                wl = WhiteList.objects.filter(blog = blog, user = request.user)
                if len(wl) < 1:
                    return Response({"status": "This blog is restricted to members in the white list."}, status = 401)


        title = post_serializer.data['title']
        author = request.user
        parent_content_type = post_serializer.data['parent_content_type']
        parent_id = post_serializer.data['parent_id']

        # Create post
        creation_result = create_post(title = title, 
                                        author = author, 
                                        parent_content_type = parent_content_type, 
                                        parent_id = parent_id, 
                                        content = content)

        return_json = {}
        
        return_json['post'] = PostSerializer(creation_result['post'], context = {'request': request}).data

        return_json['sentences'] = []
        #sentences = Sentence.objects.filter(edition = creation_result['edition'])
        for sentence in creation_result['sentences']:
            return_json['sentences'].append( serialize_sentence(sentence) )
        return_json['number_sentences'] = len(creation_result['sentences'])

        return_json['number_paragraphs'] = creation_result['number_paragraphs']

        return Response(return_json, status = 201)