views.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. from urllib2 import urlopen
  3. from django.http import StreamingHttpResponse
  4. from django.shortcuts import render_to_response, get_object_or_404
  5. from django.views.generic.detail import DetailView
  6. from django.views.generic.edit import UpdateView
  7. from django.conf import settings
  8. from django.contrib.auth.decorators import login_required
  9. from django.utils.decorators import method_decorator
  10. from coin.members.models import Member
  11. from coin.vpn.models import VPNConfiguration
  12. class VPNView(UpdateView):
  13. model = VPNConfiguration
  14. fields = ['ipv4_endpoint', 'ipv6_endpoint', 'comment']
  15. @method_decorator(login_required)
  16. def dispatch(self, *args, **kwargs):
  17. return super(VPNView, self).dispatch(*args, **kwargs)
  18. def get_object(self):
  19. return get_object_or_404(VPNConfiguration, pk=self.kwargs.get("id"),
  20. offersubscription__member=self.request.user)
  21. def generate_password(request, id):
  22. """This generates a random password, saves it in hashed form, and returns
  23. it to the user in cleartext.
  24. """
  25. vpn = get_object_or_404(VPNConfiguration, pk=id,
  26. offersubscription__member=request.user)
  27. # This function has nothing to here, but it's convenient.
  28. password = Member.objects.make_random_password()
  29. vpn.password = password
  30. # This will hash the password automatically
  31. vpn.full_clean()
  32. vpn.save()
  33. return render_to_response('vpn/password.html', {"vpn": vpn,
  34. "password": password})
  35. class VPNGeneratePasswordView(VPNView):
  36. """This generates a random password, saves it in hashed form, and returns
  37. it to the user in cleartext.
  38. """
  39. def get_context_data(self, **kwargs):
  40. context = super(VPNGeneratePasswordView, self).get_context_data(**kwargs)
  41. # Generate a new random password and save it
  42. password = Member.objects.make_random_password()
  43. self.object.password = password
  44. # This will hash the password automatically
  45. self.object.full_clean()
  46. self.object.save()
  47. context['password'] = password
  48. return context
  49. def get_graph(request, vpn_id, period="daily"):
  50. """ This get the graph for the associated vpn_id and time period
  51. """
  52. vpn = get_object_or_404(VPNConfiguration, pk=vpn_id,
  53. offersubscription__member=request.user)
  54. time_periods = { 'hourly': '-1hour', 'daily': '-24hours', 'weekly': '-8days', 'monthly': '-32days', 'yearly': '-13months', }
  55. if period not in time_periods:
  56. period = 'daily'
  57. graph_url = os.path.join(settings.GRAPHITE_SERVER,
  58. "render/?width=586&height=308&from=%(period)s&" \
  59. "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.downrxbytes%%2C1%%29%%2C%%20%%22Download%%22%%29&" \
  60. "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.uptxbytes%%2C1%%29%%2C%%20%%22Upload%%22%%29&" \
  61. "title=VPN%%20Usage%%20%(login)s" % \
  62. { 'period': time_periods[period], 'login': vpn.login })
  63. return StreamingHttpResponse(urlopen(graph_url), content_type="image/png")