views.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from django.views.generic import DetailView
  2. from django.contrib.auth.mixins import LoginRequiredMixin
  3. from django.views.decorators.http import require_GET, require_POST
  4. from django.http import HttpResponseForbidden, HttpResponse
  5. from django.conf import settings
  6. from django.views.decorators.csrf import csrf_exempt
  7. from django.shortcuts import get_object_or_404
  8. from django.db import models
  9. from django.db.models.functions import Concat
  10. from djadhere.utils import get_active_filter
  11. from djadhere.decorators import api_key_required
  12. from .models import Service, Route, IPResource, ServiceAllocation
  13. from .utils.fastpinger import fastpinger_update
  14. from .utils.architecture import export_switch, export_ip
  15. class ServiceDetail(LoginRequiredMixin, DetailView):
  16. def get_queryset(self):
  17. adhesions = self.request.user.profile.adhesions.values_list('pk')
  18. return Service.objects.filter(adhesion__pk__in=adhesions).order_by('service_type')
  19. @csrf_exempt
  20. @require_POST
  21. def fastpinger(request):
  22. if request.POST.get('key', None) != settings.FASTPINGER_KEY:
  23. return HttpResponseForbidden('Invalid key.')
  24. if 'fastpinger' not in request.FILES:
  25. return HttpResponse(status=400) # Bad Request
  26. stats = fastpinger_update(request.FILES['fastpinger'])
  27. return HttpResponse(stats + '\n')
  28. @require_GET
  29. @api_key_required
  30. def routes(request, route=None):
  31. resources = IPResource.objects.filter(category=IPResource.CATEGORY_PUBLIC)
  32. if route:
  33. route = get_object_or_404(Route, name=route)
  34. resources = resources.filter(pk__in=route.get_ip())
  35. resources = resources.annotate(
  36. route=models.Subquery(
  37. ServiceAllocation.objects.filter(
  38. models.Q(resource=models.OuterRef('pk')) & get_active_filter(),
  39. ).values('route__name')[:1],
  40. )
  41. )
  42. resources = resources.annotate(
  43. route=models.Case(
  44. models.When(reserved=True, then=models.Value('reserved')),
  45. models.When(route__isnull=True, then=models.Value('unused')),
  46. default='route',
  47. output_field=models.CharField()
  48. )
  49. )
  50. resources = resources.annotate(
  51. route=Concat('ip', models.Value(' '), 'route', output_field=models.CharField()),
  52. )
  53. resources = resources.annotate(
  54. order=models.Case(
  55. models.When(ip__startswith='91.224', then=1),
  56. models.When(ip__startswith='89.234', then=2),
  57. models.When(ip__startswith='185.119', then=3),
  58. )
  59. )
  60. resources = resources.order_by('order', 'ip')
  61. content = '\n'.join(resources.values_list('route', flat=True)) + '\n'
  62. return HttpResponse(content, content_type='text/plain; charset=utf-8')
  63. @require_GET
  64. @api_key_required
  65. def architecture_switch(request):
  66. return HttpResponse(export_switch(), content_type='text/plain; charset=utf-8')
  67. @require_GET
  68. @api_key_required
  69. def architecture_ip(request):
  70. return HttpResponse(export_ip(), content_type='text/plain; charset=utf-8')