views.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from django.contrib.auth.models import User
  2. from django.shortcuts import render_to_response, get_object_or_404
  3. from django.views.generic.detail import DetailView
  4. from coin.vpn.models import VPNSubscription
  5. from urllib2 import urlopen
  6. class VPNView(DetailView):
  7. def get_object(self):
  8. return get_object_or_404(VPNSubscription, pk=self.args[0],
  9. administrative_subscription__member__user=self.request.user)
  10. class VPNGeneratePasswordView(VPNView):
  11. """This generates a random password, saves it in hashed form, and returns
  12. it to the user in cleartext.
  13. """
  14. def get_context_data(self, **kwargs):
  15. context = super(VPNGeneratePasswordView, self).get_context_data(**kwargs)
  16. # Generate a new random password and save it
  17. password = User.objects.make_random_password()
  18. self.object.password = password
  19. # This will hash the password automatically
  20. self.object.full_clean()
  21. self.object.save()
  22. context['password'] = password
  23. return context
  24. def get_graph(request, vpn_id, period="daily"):
  25. """ This get the graph for the associated vpn_id and time period
  26. """
  27. vpn = get_object_or_404(VPNSubscription, pk=vpn_id,
  28. administrative_subscription__member__user=request.user)
  29. time_periods = { 'daily': '-24hours', 'weekly': '-8days', 'monthly': '-32days', 'yearly': '-13months', }
  30. if period not in time_periods:
  31. period = 'daily'
  32. graph_url = "http://graphite-dev.illyse.org/render/?width=586&height=308&from=%(period)s&" \
  33. "target=alias%28scaleToSeconds%28vpn1.%(login)s.downrxbytes%2C1%29%2C%20%22Download%22%29&" \
  34. "target=alias%28scaleToSeconds%28vpn1.%(login)s.uptxbytes%2C1%29%2C%20%22Upload%22%29&" \
  35. "title=VPN%20Usage%20%(login)s" % \
  36. { 'period': time_periods[period], 'login': vpn.login }
  37. return HttpResponse(urlopen(graph_url), mimetype="image/png")