views.py 1.0 KB

1234567891011121314151617181920212223242526272829
  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. class VPNView(DetailView):
  6. def get_object(self):
  7. return get_object_or_404(VPNSubscription, pk=self.args[0],
  8. administrative_subscription__member__user=self.request.user)
  9. class VPNGeneratePasswordView(VPNView):
  10. """This generates a random password, saves it in hashed form, and returns
  11. it to the user in cleartext.
  12. """
  13. def get_context_data(self, **kwargs):
  14. context = super(VPNGeneratePasswordView, self).get_context_data(**kwargs)
  15. # Generate a new random password and save it
  16. password = User.objects.make_random_password()
  17. self.object.password = password
  18. # This will hash the password automatically
  19. self.object.full_clean()
  20. self.object.save()
  21. context['password'] = password
  22. return context