diff --git a/templates/base.html b/templates/base.html index 955ac58..dd21dd3 100644 --- a/templates/base.html +++ b/templates/base.html @@ -26,7 +26,7 @@
  • Logout
  • {% else %}
  • Login
  • -
  • Sign up
  • +
  • Sign up
  • {% endif %} diff --git a/users/templates/users/check_confirmation.html b/users/templates/users/check_confirmation.html new file mode 100644 index 0000000..e20614d --- /dev/null +++ b/users/templates/users/check_confirmation.html @@ -0,0 +1,43 @@ +{% extends "base.html" %} + + +{% block title %} New paste {% endblock %} + + +{% block headeraddons %} + +{% endblock %} + + +{% block content %} +
    +
    + +
    + +

    Confirm your registration

    +
    +
    +
    + Oh hey {{confirm_user.username}},
    + thank you for confirming your mail address.
    + You should now be able to login. +
    +
    +
    +
    +

    Latest uploads

    +
      + {% for geometry in latest_geometries %} +
    • + + {{geometry.name}} + - {{geometry.date}} +
    • + {% endfor %} +
    +
    +
    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/users/templates/users/confirmation_email.txt b/users/templates/users/confirmation_email.txt new file mode 100644 index 0000000..0335d10 --- /dev/null +++ b/users/templates/users/confirmation_email.txt @@ -0,0 +1,7 @@ +Hello {{user.username}}, +and thank you for registering an account at {{site_name}}. + +To validate your email address and complete the registration you need to visit this link: +{{validation_link}} + +Have fun ! \ No newline at end of file diff --git a/users/templates/users/send_confirmation.html b/users/templates/users/send_confirmation.html new file mode 100644 index 0000000..cf73e3e --- /dev/null +++ b/users/templates/users/send_confirmation.html @@ -0,0 +1,47 @@ +{% extends "base.html" %} + + +{% block title %} New paste {% endblock %} + + +{% block headeraddons %} + +{% endblock %} + + +{% block content %} +
    +
    + +
    + +

    Confirm your registration

    +
    +
    +
    + Oh hey {{confirm_user.username}},
    + to complete your registration please klick on the link in the email + that has just been send to you. +
    +
    + If you did not receive an email with a validation link, + please click here to send another one. +
    +
    +
    +
    +

    Latest uploads

    +
      + {% for geometry in latest_geometries %} +
    • + + {{geometry.name}} + - {{geometry.date}} +
    • + {% endfor %} +
    +
    +
    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/users/templates/users/signup.html b/users/templates/users/signup.html new file mode 100644 index 0000000..883b8d0 --- /dev/null +++ b/users/templates/users/signup.html @@ -0,0 +1,60 @@ +{% extends "base.html" %} + + +{% block title %} New paste {% endblock %} + + +{% block headeraddons %} + +{% endblock %} + + +{% block content %} +
    +
    + +
    + +

    Sign up

    +
    +
    +
    + {% if user.is_authenticated %} + You are alerady logged in, why in the world would you want to sign up ? + {% else %} +
    + {% csrf_token %} +
    + {% for field in form %} +
    + {{ field.label_tag }} + {{ field }} + {{ field.errors }} +
    + {% endfor %} +
    + + +
    +
    +
    + {% endif %} +
    +
    +
    +
    +

    Latest uploads

    +
      + {% for geometry in latest_geometries %} +
    • + + {{geometry.name}} + - {{geometry.date}} +
    • + {% endfor %} +
    +
    +
    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/users/urls.py b/users/urls.py index 4f5c719..9ed20d0 100644 --- a/users/urls.py +++ b/users/urls.py @@ -3,10 +3,13 @@ from django.core.urlresolvers import reverse_lazy from pastebin.models import Geometry -from views import UserCreate +from views import UserCreate, SendConfirmationView, CheckConfirmationView urlpatterns = patterns('', - url(r'^signup/$', UserCreate.as_view(), name='user_signup'), + url(r'^signup/$', UserCreate.as_view(), name='signup'), + url(r'^confirm/(?P\d+)/$', SendConfirmationView.as_view(), name='send_confirmation'), + url(r'^confirm/(?P\d+)/(?P.+)/$', CheckConfirmationView.as_view(), name='check_confirmation'), + url(r'^login/$', 'django.contrib.auth.views.login', {'extra_context' : {'latest_geometries' : Geometry.get_latest()}, 'template_name' : 'users/login.html'}, name='login'), diff --git a/users/views.py b/users/views.py index 4fd6570..435f7cc 100644 --- a/users/views.py +++ b/users/views.py @@ -1,10 +1,12 @@ from django.views.generic.edit import CreateView +from django.views.generic.base import TemplateView from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.contrib.sites.models import get_current_site from django.contrib.auth.tokens import default_token_generator from django.template import loader from django.utils.http import int_to_base36 +from django.http import Http404 from pastebin.views import LastesGeometriesMixin @@ -13,29 +15,75 @@ from forms import UserCreateForm class UserCreate(CreateView, LastesGeometriesMixin): model = User form_class = UserCreateForm - template_name = 'pastebin/geometry_create.html' - email_template_name = 'users/validate_email.txt' + template_name = 'users/signup.html' - def form_valid(self, form): - res = super(UserCreate, self).form_valid(form) - + def get_success_url(self): + return reverse('send_confirmation', kwargs={'user_id' : self.object.pk}) + + + +class SendConfirmationView(TemplateView, LastesGeometriesMixin): + template_name = 'users/send_confirmation.html' + email_template_name = 'users/confirmation_email.txt' + + def get_context_data(self, **kwargs): + context = super(SendConfirmationView, self).get_context_data(**kwargs) + context['confirm_user'] = User.objects.get(id=kwargs['user_id']) + return context + + def get(self, request, *args, **kwargs): + try: + user = User.objects.get(id=kwargs['user_id']) + except User.DoesNotExist: + raise Http404 + + if user.is_active: + raise Http404 + site_name = get_current_site(self.request).name - uid = int_to_base36(self.object.pk) - token = default_token_generator.make_token(self.object) + uid = int_to_base36(user.pk) + token = default_token_generator.make_token(user) + link = request.build_absolute_uri(reverse('check_confirmation', kwargs={'user_id' : user.pk, 'token' : token})) context = { - 'email': self.object.email, + 'email': user.email, 'site_name': site_name, - 'validation_link': "%s - %s" % (uid, token), - 'user': self.object + 'validation_link': link, + 'user': user } subject = "Validate your registration at %s" % site_name email = loader.render_to_string(self.email_template_name, context) - self.object.email_user(subject,email) + user.email_user(subject,email) - return res - def get_success_url(self): - return reverse('login') + return super(SendConfirmationView,self).get(self, request, *args, **kwargs) + + +class CheckConfirmationView(TemplateView, LastesGeometriesMixin): + template_name = 'users/check_confirmation.html' + + def get_context_data(self, **kwargs): + context = super(CheckConfirmationView, self).get_context_data(**kwargs) + context['confirm_user'] = User.objects.get(id=kwargs['user_id']) + return context + + def get(self, request, *args, **kwargs): + try: + user = User.objects.get(id=kwargs['user_id']) + except User.DoesNotExist: + raise Http404 + + if user.is_active: + raise Http404 + + if not default_token_generator.check_token(user,kwargs['token']): + raise Http404 + + user.is_active = True + user.save() + + print "Acitvating %s" % user.username + + return super(CheckConfirmationView,self).get(self, request, *args, **kwargs) \ No newline at end of file