A simple ticketing application written in Python/Django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # -*- coding: utf-8 -*-
  2. from django.shortcuts import render, render_to_response, redirect
  3. from django.template import RequestContext
  4. from django.contrib.auth.models import User
  5. from django.http import HttpResponseRedirect
  6. from django.utils import timezone
  7. from django.core.mail import send_mail
  8. from .models import Ticket, Attachment, FollowUp
  9. from .forms import UserSettingsForm
  10. from .forms import TicketCreateForm, TicketEditForm, FollowupForm, AttachmentForm
  11. # Logging
  12. import logging
  13. logger = logging.getLogger(__name__)
  14. def inbox_view(request):
  15. users = User.objects.all()
  16. tickets_unassigned = Ticket.objects.all().exclude(assigned_to__in=users)
  17. tickets_assigned = Ticket.objects.filter(assigned_to__in=users)
  18. return render_to_response('main/inbox.html',
  19. {"tickets_assigned": tickets_assigned,
  20. "tickets_unassigned": tickets_unassigned, },
  21. context_instance=RequestContext(request))
  22. def my_tickets_view(request):
  23. tickets = Ticket.objects.filter(assigned_to=request.user).exclude(status__exact="DONE")
  24. tickets_waiting = Ticket.objects.filter(waiting_for=request.user).filter(status__exact="WAITING")
  25. return render_to_response('main/my-tickets.html',
  26. {"tickets": tickets,
  27. "tickets_waiting": tickets_waiting },
  28. context_instance=RequestContext(request))
  29. def all_tickets_view(request):
  30. tickets_open = Ticket.objects.all().exclude(status__exact="DONE")
  31. return render_to_response('main/all-tickets.html',
  32. {"tickets": tickets_open, },
  33. context_instance=RequestContext(request))
  34. def archive_view(request):
  35. tickets_closed = Ticket.objects.filter(status__exact="DONE")
  36. return render_to_response('main/archive.html',
  37. {"tickets": tickets_closed, },
  38. context_instance=RequestContext(request))
  39. def usersettings_update_view(request):
  40. user = request.user
  41. if request.method == 'POST':
  42. # create a form instance and populate it with data from the request:
  43. form_user = UserSettingsForm(request.POST)
  44. # check whether it's valid:
  45. if form_user.is_valid():
  46. # Save User model fields
  47. user.first_name = request.POST['first_name']
  48. user.last_name = request.POST['last_name']
  49. user.save()
  50. # redirect to the index page
  51. return HttpResponseRedirect(request.GET.get('next', '/inbox/'))
  52. # if a GET (or any other method) we'll create a blank form
  53. else:
  54. form_user = UserSettingsForm(instance=user)
  55. return render(request, 'main/settings.html', {'form_user': form_user,})
  56. def ticket_create_view(request):
  57. if request.POST:
  58. form = TicketCreateForm(request.POST)
  59. if form.is_valid():
  60. obj = form.save()
  61. # set owner
  62. obj.owner = request.user
  63. obj.status = "TODO"
  64. obj.save()
  65. return redirect('inbox')
  66. else:
  67. form = TicketCreateForm()
  68. return render(request,
  69. 'main/ticket_edit.html',
  70. {'form': form, })
  71. def ticket_edit_view(request, pk):
  72. data = Ticket.objects.get(id=pk)
  73. if request.POST:
  74. form = TicketEditForm(request.POST, instance=data)
  75. if form.is_valid():
  76. # set field closed_date to now() if status changed to "DONE"
  77. if form.cleaned_data['status'] == "DONE":
  78. data.closed_date = timezone.now()
  79. form.save()
  80. return redirect('inbox')
  81. else:
  82. form = TicketEditForm(instance=data)
  83. return render(request,
  84. 'main/ticket_edit.html',
  85. {'form': form, })
  86. def ticket_detail_view(request, pk):
  87. ticket = Ticket.objects.get(id=pk)
  88. attachments = Attachment.objects.filter(ticket=ticket)
  89. followups = FollowUp.objects.filter(ticket=ticket)
  90. return render(request,
  91. 'main/ticket_detail.html',
  92. {'ticket': ticket,
  93. 'attachments': attachments,
  94. 'followups': followups, })
  95. def followup_create_view(request):
  96. if request.POST:
  97. form = FollowupForm(request.POST)
  98. if form.is_valid():
  99. form.save()
  100. ticket = Ticket.objects.get(id=request.POST['ticket'])
  101. # mail notification to owner of ticket
  102. notification_subject = "[#" + str(ticket.id) + "] New followup"
  103. notification_body = "Hi,\n\na new followup was created for ticket #" \
  104. + str(ticket.id) \
  105. + " (http://localhost:8000/ticket/" \
  106. + str(ticket.id) \
  107. + "/)\n\nTitle: " + form.data['title'] \
  108. + "\n\n" + form.data['text']
  109. send_mail(notification_subject, notification_body, 'test@suenkler.info',
  110. [ticket.owner.email], fail_silently=False)
  111. return redirect('inbox')
  112. else:
  113. form = FollowupForm(initial={'ticket': request.GET.get('ticket'),
  114. 'user': request.user})
  115. return render(request,
  116. 'main/followup_edit.html',
  117. {'form': form, })
  118. def followup_edit_view(request, pk):
  119. data = FollowUp.objects.get(id=pk)
  120. if request.POST:
  121. form = FollowupForm(request.POST, instance=data)
  122. if form.is_valid():
  123. form.save()
  124. return redirect('inbox')
  125. else:
  126. form = FollowupForm(instance=data)
  127. return render(request,
  128. 'main/followup_edit.html',
  129. {'form': form, })
  130. def attachment_create_view(request):
  131. if request.POST:
  132. form = AttachmentForm(request.POST, request.FILES)
  133. if form.is_valid():
  134. attachment = Attachment(
  135. ticket=Ticket.objects.get(id=request.GET['ticket']),
  136. file=request.FILES['file'],
  137. filename=request.FILES['file'].name,
  138. user=request.user
  139. #mime_type=form.file.get_content_type(),
  140. #size=len(form.file),
  141. )
  142. attachment.save()
  143. return redirect('inbox')
  144. else:
  145. form = AttachmentForm()
  146. return render(request,
  147. 'main/attachment_add.html',
  148. {'form': form, })