Support PUT and PATCH

master
Nick Sergeant 2012-01-30 22:20:31 -05:00
parent f8ec2ad7e0
commit 29c3d31688
1 changed files with 17 additions and 6 deletions

View File

@ -1,12 +1,12 @@
from taggit.utils import edit_string_for_tags, parse_tags
from tastypie.authentication import ApiKeyAuthentication from tastypie.authentication import ApiKeyAuthentication
from tastypie.authorization import DjangoAuthorization from tastypie.authorization import DjangoAuthorization
from tastypie.validation import FormValidation from tastypie.validation import Validation
from tastypie.resources import ModelResource from tastypie.resources import ModelResource
from django.contrib.auth.models import User from django.contrib.auth.models import User
from tastypie.models import create_api_key from tastypie.models import create_api_key
from tastypie.cache import SimpleCache from tastypie.cache import SimpleCache
from tastypie.fields import ListField from tastypie.fields import ListField
from taggit.utils import parse_tags
from snipts.forms import SniptForm from snipts.forms import SniptForm
from snipts.models import Snipt from snipts.models import Snipt
from taggit.models import Tag from taggit.models import Tag
@ -131,9 +131,9 @@ class PrivateSniptResource(ModelResource):
resource_name = 'snipt' resource_name = 'snipt'
fields = ['title', 'description', 'slug', 'lexer', 'code', 'line_count', fields = ['title', 'description', 'slug', 'lexer', 'code', 'line_count',
'stylized', 'key', 'public', 'created', 'modified',] 'stylized', 'key', 'public', 'created', 'modified',]
validation = FormValidation(form_class=SniptForm) validation = Validation()
include_absolute_url = True include_absolute_url = True
detail_allowed_methods = ['get', 'put', 'delete'] detail_allowed_methods = ['get', 'patch', 'put', 'delete']
list_allowed_methods = ['get', 'post'] list_allowed_methods = ['get', 'post']
authentication = ApiKeyAuthentication() authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization() authorization = DjangoAuthorization()
@ -141,14 +141,24 @@ class PrivateSniptResource(ModelResource):
def dehydrate(self, bundle): def dehydrate(self, bundle):
bundle.data['embed_url'] = bundle.obj.get_embed_url() bundle.data['embed_url'] = bundle.obj.get_embed_url()
bundle.data['tags_list'] = edit_string_for_tags(bundle.obj.tags.all())
return bundle return bundle
def obj_create(self, bundle, request=None, **kwargs): def obj_create(self, bundle, request=None, **kwargs):
bundle.data['tags_list'] = bundle.data['tags'] bundle.data['tags_list'] = bundle.data.get('tags')
bundle.data['tags'] = '' bundle.data['tags'] = ''
return super(PrivateSniptResource, self).obj_create(bundle, request, return super(PrivateSniptResource, self).obj_create(bundle, request,
user=request.user) user=request.user)
def obj_update(self, bundle, request=None, **kwargs):
bundle.data['user'] = request.user
if type(bundle.data['tags']) == unicode:
bundle.data['tags_list'] = bundle.data['tags']
else:
bundle.data['tags_list'] = ''
bundle.data['tags'] = ''
return super(PrivateSniptResource, self).obj_update(bundle, request, **kwargs)
def build_filters(self, filters=None): def build_filters(self, filters=None):
if filters is None: if filters is None:
filters = {} filters = {}
@ -167,4 +177,5 @@ class PrivateSniptResource(ModelResource):
def save_m2m(self, bundle): def save_m2m(self, bundle):
tags = bundle.data.get('tags_list', []) tags = bundle.data.get('tags_list', [])
bundle.obj.tags.set(*parse_tags(tags)) if tags != '':
bundle.obj.tags.set(*parse_tags(tags))