views.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import urllib2
  5. from django.http import StreamingHttpResponse, HttpResponseServerError
  6. from django.shortcuts import render_to_response, get_object_or_404
  7. from django.views.generic.detail import DetailView
  8. from django.views.generic.edit import UpdateView
  9. from django.conf import settings
  10. from django.contrib.messages.views import SuccessMessageMixin
  11. from django.contrib.auth.decorators import login_required
  12. from django.utils.decorators import method_decorator
  13. from coin.members.models import Member
  14. from .models import VPNConfiguration
  15. class VPNView(SuccessMessageMixin, UpdateView):
  16. model = VPNConfiguration
  17. fields = ["ipv4_endpoint", "ipv6_endpoint", "comment"]
  18. success_message = "Configuration enregistrée avec succès !"
  19. @method_decorator(login_required)
  20. def dispatch(self, *args, **kwargs):
  21. return super(VPNView, self).dispatch(*args, **kwargs)
  22. def get_object(self):
  23. if self.request.user.is_superuser:
  24. return get_object_or_404(VPNConfiguration, pk=self.kwargs.get("pk"))
  25. # For normal users, ensure the VPN belongs to them.
  26. return get_object_or_404(
  27. VPNConfiguration,
  28. pk=self.kwargs.get("pk"),
  29. offersubscription__member=self.request.user,
  30. )
  31. class VPNGeneratePasswordView(VPNView):
  32. """This generates a random password, saves it in hashed form, and returns
  33. it to the user in cleartext.
  34. """
  35. def get_context_data(self, **kwargs):
  36. context = super(VPNGeneratePasswordView, self).get_context_data(**kwargs)
  37. # Generate a new random password and save it
  38. password = Member.objects.make_random_password()
  39. self.object.password = password
  40. # This will hash the password automatically
  41. self.object.full_clean()
  42. self.object.save()
  43. context["password"] = password
  44. return context
  45. @login_required
  46. def get_graph(request, vpn_id, period="daily"):
  47. """ This get the graph for the associated vpn_id and time period
  48. """
  49. if request.user.is_superuser:
  50. vpn = get_object_or_404(VPNConfiguration, pk=vpn_id)
  51. else:
  52. # For normal users, ensure the VPN belongs to them
  53. vpn = get_object_or_404(
  54. VPNConfiguration, pk=vpn_id, offersubscription__member=request.user
  55. )
  56. time_periods = {
  57. "hourly": "-1hour",
  58. "daily": "-24hours",
  59. "weekly": "-8days",
  60. "monthly": "-32days",
  61. "yearly": "-13months",
  62. }
  63. if period not in time_periods:
  64. period = "daily"
  65. graph_url = os.path.join(
  66. settings.GRAPHITE_SERVER,
  67. "render/?width=586&height=308&from=%(period)s&"
  68. "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.downrxbytes%%2C1%%29%%2C%%20%%22Download%%22%%29&"
  69. "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.uptxbytes%%2C1%%29%%2C%%20%%22Upload%%22%%29&"
  70. "title=VPN%%20Usage%%20%(login)s"
  71. % {"period": time_periods[period], "login": vpn.login},
  72. )
  73. try:
  74. return StreamingHttpResponse(
  75. urllib2.urlopen(graph_url), content_type="image/png"
  76. )
  77. except urllib2.URLError:
  78. return HttpResponseServerError()