def new_note(request, pk, format=None): """Create a new note for choice.""" choice = get_object_or_404(Choice, pk=pk) if request.user not in [choice.tutee, choice.tutor]: return HttpResponseRedirect(reverse('choices.views.requests')) if request.method == 'POST' and request.POST.get('content'): choice_note = ChoiceNote(choice=choice, content=request.POST.get('content'), user=request.user) choice_note.save() try: channel = Channel.objects.get(choice=choice) # Create notification for this channel notification = Notification() notification.action = 'new' notification.channel = channel notification.choice_note = choice_note notification.model = 'choice_note' notification.user = request.user notification.save() except Channel.DoesNotExist: pass if format and format == '.json': data = { 'choice_note': choice_note.to_json(), } elif request.is_ajax(): d = { 'choice': choice, 'choice_note': choice_note, } choice_note_template = loader.get_template( 'choices/choice_note.html') choice_note_form = loader.get_template( 'choices/choice_note_form.html') context = RequestContext(request, add_csrf(request, d)) data = { 'choice_note_template': choice_note_template.render( context), 'choice_note_form': choice_note_form.render(context) } if (format and format == '.json') or request.is_ajax(): return HttpResponse(json.dumps(data), mimetype='application/json') return HttpResponseRedirect(reverse('choices.views.detail', args=[choice.pk]))