# HG changeset patch # User Filip de Waard # Date 1294370101 -3600 # Node ID 2ec638c44d9445a612fbdb85a0a1674806579b30 # Parent 61391ad037ee6db45900827671f852fa4cf828d7 added in_reply_to to entry creation method diff -r 61391ad037ee6db45900827671f852fa4cf828d7 -r 2ec638c44d9445a612fbdb85a0a1674806579b30 vix/controllers/entries.py --- a/vix/controllers/entries.py Mon Jan 03 20:45:46 2011 +0100 +++ b/vix/controllers/entries.py Fri Jan 07 04:15:01 2011 +0100 @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- """ -vix/controllers/entries.py: Vix Feed controller +vix/controllers/entries.py: Vix Entries controller -Copyright 2009-2010, Net Collective. +Copyright 2009-2011, Net Collective. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -40,18 +40,28 @@ @authenticate() def create(self): """ - Creates a Entry resource. + Creates an Entry resource. Here is a sample request:: POST /entries HTTTP/1.1 Host vix.example.org Content-type: application/json - Slug: Welcome to my website + Slug: Welcome to Taggart Transcontinental Authorization: Basic ZHRhZ2dhcnQ6am9objEyMw== - {"title": "Taggart Transcontinental company weblog", - "subtitle": "From ocean to ocean."} + {"title": "Welcome to the Taggart Transcontinental weblog", + "subtitle": "From ocean to ocean.", + "summary": "This first post introduces the blog.", + "rights": "All rights reserved.", + "content": { + "type": "text/plain", "content": "Here goes the story."}, + "authors": [ + {"name": "Dagny Taggart", "email": "dtaggart@tt.com"}], + "contributors": [ + {"name": "Eddy Willers", "email": "ewillers@tt.com"}], + "categories": [ + {"term": "general", "label": "General"}]} This method requires the 'application/json' HTTP Media Type to be passed through the Content-type header (AtomPub support @@ -96,7 +106,15 @@ abort(400, 'Valid authors value required') else: abort(400, 'Valid authors value required.') - + + if 'in_reply_to' in json: + try: + for irt in json['in_reply_to']: + if u'ref' not in irt: + abort(400, 'Incorrect in_reply_to value.') + except TypeError: + abort(400, 'in_reply_to value should be a list') + draft = False if not 'draft' in json else json['draft'] entry = model.Entry(feeds=json['feeds'], @@ -108,7 +126,8 @@ rights=json.get('rights'), authors=json['authors'], contributors=json.get('contributors'), - categories=json.get('categories') + categories=json.get('categories'), + in_reply_to=json.get('in_reply_to') ) slug_suggestion = request.headers.get('Slug') diff -r 61391ad037ee6db45900827671f852fa4cf828d7 -r 2ec638c44d9445a612fbdb85a0a1674806579b30 vix/tests/functional/test_entries.py --- a/vix/tests/functional/test_entries.py Mon Jan 03 20:45:46 2011 +0100 +++ b/vix/tests/functional/test_entries.py Fri Jan 07 04:15:01 2011 +0100 @@ -1,3 +1,24 @@ +# -*- coding: utf-8 -*- + +""" +vix/tests/functional/test_entries.py: Tests for Entries controller + +Copyright 2009-2011, Net Collective. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +""" + import re import base64 import bcrypt @@ -195,9 +216,10 @@ assert datetime_substr in response.json['published'] assert datetime_substr in response.json['updated'] + first_post_id = u'tag:vix.io,%s:/%s/first' % (date_str_dash, date_str) + self.assertEquals(response.json['slug'], '/%s/first' % (date_str)) - self.assertEquals(response.json['_id'], - u'tag:vix.io,%s:/%s/first' % (date_str_dash, date_str)) + self.assertEquals(response.json['_id'], first_post_id) #try sending a request with invalid JSON input response = self.app.post(url('create_entry'), @@ -283,3 +305,46 @@ #TODO: Add format validation for all values (e.g. check if #values like 'rights' are basestrings, check categories) + + #test in-reply-to functionality + first_entry = model.Entry().load(model.db, first_post_id) + irt = {u'ref': first_entry.id, + u'type': u'text/plain', + u'href': first_entry.get_href()} + + irt_entry = {'feeds': [blog_feed.id], + 'title': 'Reply to first message', + 'subtitle': 'This is my second post!', + 'summary': 'Something I wanted to add to the first post', + 'rights': 'All rights reserved.', + 'content': {'type': 'text/plain', + 'content': 'Hello, my dear reader...'}, + 'authors': authors, + 'in_reply_to': [irt]} + + response = self.app.post(url('create_entry'), + content_type='application/json', + params=json.dumps(irt_entry), + headers=headers, + status=201) + + self.assertEquals(response.json['in_reply_to'], [irt]) + + #see what happens with an incorrectly formatted in-reply-to value + irt_wrong = {u'type': u'text/plain'} + irt_entry['in_reply_to'] = [irt_wrong] + + response = self.app.post(url('create_entry'), + content_type='application/json', + params=json.dumps(irt_entry), + headers=headers, + status=400) + + #try incorrect input for in_reply_to + irt_entry['in_reply_to'] = 1 + + response = self.app.post(url('create_entry'), + content_type='application/json', + params=json.dumps(irt_entry), + headers=headers, + status=400)