views.py 3.0 KB

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