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

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