|
@@ -1,47 +1,53 @@
|
|
|
+import os
|
|
|
+from urllib2 import urlopen
|
|
|
+
|
|
|
from django.contrib.auth.models import User
|
|
|
+from django.http import StreamingHttpResponse
|
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
|
from django.views.generic.detail import DetailView
|
|
|
+from django.conf import settings
|
|
|
|
|
|
from coin.vpn.models import VPNSubscription
|
|
|
|
|
|
-from urllib2 import urlopen
|
|
|
-
|
|
|
|
|
|
class VPNView(DetailView):
|
|
|
- def get_object(self):
|
|
|
+
|
|
|
+ def get_object(self):
|
|
|
return get_object_or_404(VPNSubscription, pk=self.args[0],
|
|
|
administrative_subscription__member__user=self.request.user)
|
|
|
|
|
|
|
|
|
-def generate_password(request, vpn_id):
|
|
|
+class VPNGeneratePasswordView(VPNView):
|
|
|
"""This generates a random password, saves it in hashed form, and returns
|
|
|
it to the user in cleartext.
|
|
|
"""
|
|
|
- vpn = get_object_or_404(VPNSubscription, pk=vpn_id,
|
|
|
- administrative_subscription__member__user=request.user)
|
|
|
- # This function has nothing to here, but it's convenient.
|
|
|
- password = User.objects.make_random_password()
|
|
|
- vpn.password = password
|
|
|
- # This will hash the password automatically
|
|
|
- vpn.full_clean()
|
|
|
- vpn.save()
|
|
|
- return render_to_response('vpn/password.html', {"vpn": vpn,
|
|
|
- "password": password})
|
|
|
+
|
|
|
+ def get_context_data(self, **kwargs):
|
|
|
+ context = super(VPNGeneratePasswordView, self).get_context_data(**kwargs)
|
|
|
+ # Generate a new random password and save it
|
|
|
+ password = User.objects.make_random_password()
|
|
|
+ self.object.password = password
|
|
|
+ # This will hash the password automatically
|
|
|
+ self.object.full_clean()
|
|
|
+ self.object.save()
|
|
|
+ context['password'] = password
|
|
|
+ return context
|
|
|
+
|
|
|
|
|
|
def get_graph(request, vpn_id, period="daily"):
|
|
|
""" This get the graph for the associated vpn_id and time period
|
|
|
"""
|
|
|
vpn = get_object_or_404(VPNSubscription, pk=vpn_id,
|
|
|
- administrative_subscription__member__user=request.user)
|
|
|
+ administrative_subscription__member__user=request.user.id)
|
|
|
|
|
|
- time_periods = { 'daily': '-24hours', 'weekly': '-8days', 'monthly': '-32days', 'yearly': '-13months', }
|
|
|
+ time_periods = { 'hourly': '-1hour', 'daily': '-24hours', 'weekly': '-8days', 'monthly': '-32days', 'yearly': '-13months', }
|
|
|
if period not in time_periods:
|
|
|
period = 'daily'
|
|
|
|
|
|
- graph_url = "http://graphite-dev.illyse.org/render/?width=586&height=308&from=%(period)s&" \
|
|
|
- "target=alias%28scaleToSeconds%28vpn1.%(login)s.downrxbytes%2C1%29%2C%20%22Download%22%29&" \
|
|
|
- "target=alias%28scaleToSeconds%28vpn1.%(login)s.uptxbytes%2C1%29%2C%20%22Upload%22%29&" \
|
|
|
- "title=VPN%20Usage%20%(login)s" % \
|
|
|
- { 'period': time_periods[period], 'login': vpn.login }
|
|
|
- return HttpResponse(urlopen(graph_url), mimetype="image/png")
|
|
|
-
|
|
|
+ graph_url = os.path.join(settings.GRAPHITE_SERVER,
|
|
|
+ "render/?width=586&height=308&from=%(period)s&" \
|
|
|
+ "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.downrxbytes%%2C1%%29%%2C%%20%%22Download%%22%%29&" \
|
|
|
+ "target=alias%%28scaleToSeconds%%28vpn1.%(login)s.uptxbytes%%2C1%%29%%2C%%20%%22Upload%%22%%29&" \
|
|
|
+ "title=VPN%%20Usage%%20%(login)s" % \
|
|
|
+ { 'period': time_periods[period], 'login': vpn.login })
|
|
|
+ return StreamingHttpResponse(urlopen(graph_url), content_type="image/png")
|