def update(self, request, post_id=None): if not post_id: resp = rc.BAD_REQUEST resp.write(ugettext('StreamPost ID must be supplied')) return resp try: post_id = int(post_id) post = StreamPost.objects.get(id=post_id) except StreamPost.DoesNotExist: resp = rc.NOT_FOUND resp.write(ugettext('No StreamPost with the specified ID was found')) return resp #Checks that the user is the poster if request.user != post.poster: resp = rc.FORBIDDEN resp.write(ugettext('Only the original poster can update the post')) return resp #Checks if there is a parameter 'model' in the request, created by Backbone.js model = request.POST.get('model', '') if model: post = StreamPostValidationForm(json.loads(model), instance=post) else: post = StreamPostValidationForm(request.POST, instance=post) if post.is_valid(): #Checks that either content or link is non-empty if not post.cleaned_data['content'] and not post.cleaned_data['link']: resp = rc.BAD_REQUEST resp.write(ugettext('Either content or link has to be non-empty')) return resp #If all fields are valid, update the post updated_post = post.save() return updated_post else: resp = rc.BAD_REQUEST resp.write(json.dumps(post.errors)) return resp
def create(self,request, post_id=None): #Checks if there is a parameter 'model' in the request, created by Backbone.js model = request.POST.get('model', '') if model: post = StreamPostValidationForm(json.loads(model)) else: post = StreamPostValidationForm(request.POST) if post.is_valid(): #Checks that either content or link is non-empty if not post.cleaned_data['content'] and not post.cleaned_data['link']: resp = rc.BAD_REQUEST resp.write(ugettext('Either content or link has to be non-empty')) return resp #If all fields are valid, save the post saved_post = post.save(commit=False) saved_post.poster = request.user saved_post.save() post.save_m2m() return saved_post else: resp = rc.BAD_REQUEST resp.write(json.dumps(post.errors)) return resp