| 1 | # Create your views here. |
|---|
| 2 | from django.template import Context, loader |
|---|
| 3 | from django.shortcuts import render_to_response, get_object_or_404 |
|---|
| 4 | from clinicaltrials.tickets.models import Ticket, Followup |
|---|
| 5 | from django.contrib.auth.models import User |
|---|
| 6 | from django.forms.models import inlineformset_factory |
|---|
| 7 | from django.http import HttpResponseRedirect, HttpResponse |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | from django import forms |
|---|
| 11 | from django.utils.translation import ugettext as _ |
|---|
| 12 | |
|---|
| 13 | from django.contrib.auth.decorators import login_required |
|---|
| 14 | import choices |
|---|
| 15 | |
|---|
| 16 | @login_required |
|---|
| 17 | def index(request): |
|---|
| 18 | user_tickets = Ticket.objects.all()[:5] |
|---|
| 19 | user_open_tickets = (i.opened_tickets() for i in Ticket.objects.all()) |
|---|
| 20 | user_close_tickets = (i.closed_tickets() for i in Ticket.objects.all()) |
|---|
| 21 | t = loader.get_template('tickets/user_tickets.html') |
|---|
| 22 | c = Context({ |
|---|
| 23 | 'user_tickets': user_tickets, |
|---|
| 24 | 'user_open_tickets': user_open_tickets, |
|---|
| 25 | 'choices': choices, |
|---|
| 26 | 'user_close_tickets': user_close_tickets, |
|---|
| 27 | 'username': request.user.username, |
|---|
| 28 | }) |
|---|
| 29 | return HttpResponse(t.render(c)) |
|---|
| 30 | |
|---|
| 31 | @login_required |
|---|
| 32 | def waiting_acceptance(request): |
|---|
| 33 | fw_waiting = Followup.objects.filter(status = 'new', ticket__type='review') |
|---|
| 34 | t = loader.get_template('tickets/waiting_acceptance_tickets.html') |
|---|
| 35 | c = Context({ |
|---|
| 36 | 'fw_waiting': fw_waiting, |
|---|
| 37 | 'choices': choices, |
|---|
| 38 | 'username': request.user.username, |
|---|
| 39 | }) |
|---|
| 40 | return HttpResponse(t.render(c)) |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | @login_required |
|---|
| 44 | def reopen_ticket(request, object_id): |
|---|
| 45 | ticket = get_object_or_404(Ticket, id=int(object_id)) |
|---|
| 46 | followup_latest = ticket.followup_set.latest() |
|---|
| 47 | followup_new = Followup(ticket=ticket, status='reopened', description=followup_latest.description, |
|---|
| 48 | subject=followup_latest.subject , reported_by=followup_latest.reported_by ) |
|---|
| 49 | followup_new.save(); |
|---|
| 50 | |
|---|
| 51 | return HttpResponseRedirect(ticket.get_absolute_url()) |
|---|
| 52 | |
|---|
| 53 | @login_required |
|---|
| 54 | def close_ticket(request, object_id): |
|---|
| 55 | ticket = get_object_or_404(Ticket, id=int(object_id)) |
|---|
| 56 | followup_latest = ticket.followup_set.latest() |
|---|
| 57 | followup_new = Followup(ticket=ticket, status='closed', description=followup_latest.description, |
|---|
| 58 | subject=followup_latest.subject , reported_by=followup_latest.reported_by ) |
|---|
| 59 | followup_new.save(); |
|---|
| 60 | |
|---|
| 61 | return HttpResponseRedirect(ticket.get_absolute_url()) |
|---|
| 62 | |
|---|
| 63 | class FollowupParcForm(forms.Form): |
|---|
| 64 | description = forms.CharField(label=_('Description'),widget=forms.Textarea) |
|---|
| 65 | |
|---|
| 66 | @login_required |
|---|
| 67 | def resolve_ticket(request, object_id): |
|---|
| 68 | if request.method == 'POST': # If the forms were submitted... |
|---|
| 69 | form = FollowupParcForm(request.POST) |
|---|
| 70 | if form.is_valid(): |
|---|
| 71 | desc = form.cleaned_data['description'] |
|---|
| 72 | ticket = get_object_or_404(Ticket, id=int(object_id)) |
|---|
| 73 | fw_lt = ticket.followup_set.latest() |
|---|
| 74 | fw_nw = Followup(ticket=ticket, status='resolved', |
|---|
| 75 | description=desc, subject=fw_lt.subject , |
|---|
| 76 | reported_by=fw_lt.reported_by, to_user=fw_lt.to_user, ) |
|---|
| 77 | fw_nw.save() |
|---|
| 78 | |
|---|
| 79 | return HttpResponseRedirect(ticket.get_absolute_url()) |
|---|
| 80 | else: |
|---|
| 81 | # recovering Ticket Data to input form fields |
|---|
| 82 | followup_form = FollowupParcForm() # An unbound form |
|---|
| 83 | return render_to_response('tickets/new_iteration.html', { |
|---|
| 84 | 'iteration_form': followup_form, |
|---|
| 85 | 'ticket_id': object_id, |
|---|
| 86 | 'mode': 'resolve', |
|---|
| 87 | 'username': request.user.username, |
|---|
| 88 | }) |
|---|
| 89 | |
|---|
| 90 | @login_required |
|---|
| 91 | def accept_ticket(request, object_id): |
|---|
| 92 | if request.method == 'POST': # If the forms were submitted... |
|---|
| 93 | form = FollowupParcForm(request.POST) |
|---|
| 94 | if form.is_valid(): |
|---|
| 95 | user = request.user |
|---|
| 96 | desc = form.cleaned_data['description'] |
|---|
| 97 | ticket = get_object_or_404(Ticket, id=int(object_id)) |
|---|
| 98 | fw_lt = ticket.followup_set.latest() |
|---|
| 99 | fw_nw = Followup(ticket=ticket, status='accepted', |
|---|
| 100 | description=desc, subject=fw_lt.subject , |
|---|
| 101 | reported_by=fw_lt.reported_by, to_user=user, ) |
|---|
| 102 | fw_nw.save() |
|---|
| 103 | |
|---|
| 104 | return HttpResponseRedirect(ticket.get_absolute_url()) |
|---|
| 105 | else: |
|---|
| 106 | # recovering Ticket Data to input form fields |
|---|
| 107 | followup_form = FollowupParcForm() # An unbound form |
|---|
| 108 | return render_to_response('tickets/new_iteration.html', { |
|---|
| 109 | 'iteration_form': followup_form, |
|---|
| 110 | 'ticket_id': object_id, |
|---|
| 111 | 'mode': 'accept', |
|---|
| 112 | 'username': request.user.username, |
|---|
| 113 | }) |
|---|
| 114 | |
|---|
| 115 | @login_required |
|---|
| 116 | def new_iteration(request, object_id): |
|---|
| 117 | ticket = get_object_or_404(Ticket, id=int(object_id)) |
|---|
| 118 | |
|---|
| 119 | if request.method == 'POST': # If the forms were submitted... |
|---|
| 120 | form = FollowupParcForm(request.POST) |
|---|
| 121 | if form.is_valid(): |
|---|
| 122 | desc = form.cleaned_data['description'] |
|---|
| 123 | fw_lt = ticket.followup_set.latest() |
|---|
| 124 | fw_nw = Followup(ticket=ticket, status=fw_lt.status, |
|---|
| 125 | description=desc, subject=fw_lt.subject , |
|---|
| 126 | reported_by=fw_lt.reported_by, to_user=fw_lt.to_user, ) |
|---|
| 127 | fw_nw.save() |
|---|
| 128 | |
|---|
| 129 | return HttpResponseRedirect(ticket.get_absolute_url()) |
|---|
| 130 | else: |
|---|
| 131 | # recovering Ticket Data to input form fields |
|---|
| 132 | iteration_form = FollowupParcForm() # An unbound form |
|---|
| 133 | |
|---|
| 134 | import pdb |
|---|
| 135 | pdb.set_trace() |
|---|
| 136 | |
|---|
| 137 | return render_to_response('tickets/new_iteration.html', { |
|---|
| 138 | 'form': iteration_form, |
|---|
| 139 | 'ticket': ticket, |
|---|
| 140 | 'mode': 'newiteration', |
|---|
| 141 | 'username': request.user.username, |
|---|
| 142 | }) |
|---|
| 143 | |
|---|
| 144 | class FollowupParcBForm(forms.Form): |
|---|
| 145 | subject = forms.CharField(label=_('Subject'),required=True,max_length=256) |
|---|
| 146 | description = forms.CharField(label=_('Description'),required=True ,widget=forms.Textarea) |
|---|
| 147 | |
|---|
| 148 | @login_required |
|---|
| 149 | def open_ticket(request,context,type): |
|---|
| 150 | if request.method == 'POST': # If the forms were submitted... |
|---|
| 151 | form = FollowupParcBForm(request.POST) |
|---|
| 152 | if form.is_valid(): |
|---|
| 153 | user = request.user |
|---|
| 154 | ticket = Ticket(context=context,type=type, creator=user, ) |
|---|
| 155 | ticket.save() |
|---|
| 156 | subject = form.cleaned_data['subject'] |
|---|
| 157 | description = form.cleaned_data['description'] |
|---|
| 158 | fw_nw = Followup(ticket=ticket , status='new', |
|---|
| 159 | description=description, subject=subject , |
|---|
| 160 | reported_by=request.user, ) |
|---|
| 161 | fw_nw.save() |
|---|
| 162 | |
|---|
| 163 | return HttpResponseRedirect(ticket.get_absolute_url()) |
|---|
| 164 | else: |
|---|
| 165 | # recovering Ticket Data to input form fields |
|---|
| 166 | open_ticket_form = FollowupParcBForm() # An unbound form |
|---|
| 167 | |
|---|
| 168 | return render_to_response('tickets/open_ticket.html', { |
|---|
| 169 | 'open_ticket_form': open_ticket_form, |
|---|
| 170 | 'context': context, |
|---|
| 171 | 'type': type, |
|---|
| 172 | 'mode': 'open_ticket', |
|---|
| 173 | 'user_name': request.user.pk |
|---|
| 174 | }) |
|---|