def new_ticket(request): ticket_instance = Ticket() message_instance = Message() if request.method == "POST": ticket_form = TicketForm(request.POST, instance=ticket_instance) message_form = MessageForm(request.POST, request.FILES, instance=message_instance) if ticket_form.is_valid() and message_form.is_valid(): new_ticket = ticket_form.save(commit=False) new_ticket.user = request.user new_ticket.save() new_message = message_form.save(commit=False) new_message.user, new_message.ticket = request.user, new_ticket new_message.save() return HttpResponseRedirect(reverse('ticket_details', args=[new_ticket.id])) ticket_form = TicketForm(instance=ticket_instance) message_form = MessageForm(instance=message_instance) return render_to_response('support/new_ticket.html', {'ticket_form':ticket_form, 'message_form':message_form}, context_instance=RequestContext(request))
def answer_ticket(request, ticket_id, template_name='support/answer_ticket.html'): redirect_url = request.META['HTTP_REFERER'] ticket = Ticket.objects.get(id=ticket_id) if request.user.has_perm('is_staff'): pass else: if ticket.user.id is not request.user.id or ticket.status==2: return HttpResponseRedirect(redirect_url) model_instance = Message() if request.method == "POST": form = MessageForm(request.POST, request.FILES, instance=model_instance) if form.is_valid(): new_message = form.save(commit=False) new_message.user = request.user new_message.ticket = ticket new_message.save() return HttpResponseRedirect(reverse('ticket_details', args=[ticket_id])) form = MessageForm(instance=model_instance) return render_to_response(template_name, {'form':form, 'redirect_url':redirect_url}, context_instance=RequestContext(request))