views.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. from urllib2 import urlopen
  5. from django.http import StreamingHttpResponse
  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 coin.vpn.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. return get_object_or_404(VPNConfiguration, pk=self.kwargs.get("id"),
  24. offersubscription__member=self.request.user)
  25. def generate_password(request, id):
  26. """This generates a random password, saves it in hashed form, and returns
  27. it to the user in cleartext.
  28. """
  29. vpn = get_object_or_404(VPNConfiguration, pk=id,
  30. offersubscription__member=request.user)
  31. # This function has nothing to here, but it's convenient.
  32. password = Member.objects.make_random_password()
  33. vpn.password = password
  34. # This will hash the password automatically
  35. vpn.full_clean()
  36. vpn.save()
  37. return render_to_response('vpn/fragments/password.html', {"vpn": vpn,
  38. "password": password})
  39. class VPNGeneratePasswordView(VPNView):
  40. """This generates a random password, saves it in hashed form, and returns
  41. it to the user in cleartext.
  42. """
  43. def get_context_data(self, **kwargs):
  44. context = super(VPNGeneratePasswordView, self).get_context_data(**kwargs)
  45. # Generate a new random password and save it
  46. password = Member.objects.make_random_password()
  47. self.object.password = password
  48. # This will hash the password automatically
  49. self.object.full_clean()
  50. self.object.save()
  51. context['password'] = password
  52. return context
  53. def get_graph(request, vpn_id, period="daily"):
  54. """ This get the graph for the associated vpn_id and time period
  55. """
  56. vpn = get_object_or_404(VPNConfiguration, pk=vpn_id,
  57. offersubscription__member=request.user)
  58. time_periods = { 'hourly': '-1hour', 'daily': '-24hours', 'weekly': '-8days', 'monthly': '-32days', 'yearly': '-13months', }
  59. if period not in time_periods:
  60. period = 'daily'
  61. graph_url = os.path.join(settings.GRAPHITE_SERVER,
  62. "render/?width=586&height=308&from=%(period)s&" \
  63. "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.downrxbytes%%2C1%%29%%2C%%20%%22Download%%22%%29&" \
  64. "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.uptxbytes%%2C1%%29%%2C%%20%%22Upload%%22%%29&" \
  65. "title=VPN%%20Usage%%20%(login)s" % \
  66. { 'period': time_periods[period], 'login': vpn.login })
  67. return StreamingHttpResponse(urlopen(graph_url), content_type="image/png")