def r_getreturn(request, rpc=False, rpcdata={}, successtext=None, nextr=None, postid_m=None): """ Common function for RPCable views to easily return some appropriate data (rpcdata or next-redirect). """ if rpc: # explicit request to return RPC-usable data # do conversion in rpc(). return rpcdata else: if successtext is None and 'msg' in rpcdata: successtext = rpcdata['msg'] if request.is_xmpp(): # simplify it here. return XmppResponse(successtext) # ! TODO: Add a django-mesasge if HTTP? nextr = nextr or request.GET.get('next') if nextr: # know where should return to. return HttpResponseRedirect(nextr) else: # explicitly return if postid_m: # we have a post to return to. # msg isn't going to be used, though. ## XX: success_or_redirect(_redirect_to_posts(...))? return success_or_reverse_redirect('snapboard_post', args=(postid_m,), req=request, msg=successtext) else: # Might supply some more data if we need to return somewhere # else? return HttpResponseServerError( "RPC return: unknown return.") # Nothing else to do here.
def new_thread(request, cat_id=None): """ Start a new discussion. """ warn = None threadform = ThreadForm(request.user, request.POST if request.POST else None, initial={'category': cat_id}) # will be selected if it's there. postform = PostForm(request.POST if request.POST else None) if request.POST: if threadform.is_valid() and postform.is_valid(): cat_id = threadform.cleaned_data.pop('category') category = get_object_or_404(Category, name=cat_id) if not category.can_create_thread(request.user): raise PermissionError("You cannost post in this category") ## To make sure no postless threads remain. #with transaction.commit_on_success(): ## django1.3 required :( @transaction.commit_on_success def make_thread(): nthread = Thread( category=category, #subject=threadform.cleaned_data['subject'], **(threadform.cleaned_data)) nthread.save() # create the post rpost = Post.add_root( user=request.user, thread=nthread, **(postform.cleaned_data)) rpost.save() return nthread, rpost nthread, rpost = make_thread() # redirect to new thread / return success message return success_or_reverse_redirect('snapboard_post', args=(nthread.id_form_m(),), req=request, msg="Thread created.") else: ## Warn the user if there's a problem with supplied category. if cat_id is not None: # A specific category was supplied in URL. try: category = Category.objects.get(name=cat_id) except Category.DoesNotExist: warn = ("Unknown category was supplied." " Select one from the list.") else: if not category.can_create_thread(request.user): warn = ("You cannot post in the supplied category." " You still can select one from the list.") return render_to_response('snapboard/newthread', {'threadform': threadform, 'postform': postform, 'warn': warn}, context_instance=RequestContext(request, processors=extra_processors))