From 7997879bd82a3d16a850edc86890ed6ecce8841e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 12 Jun 2015 20:49:35 +0300 Subject: [PATCH] Account creation --- hc/accounts/forms.py | 8 ++++- hc/accounts/urls.py | 1 + hc/accounts/views.py | 41 ++++++++++++++++++++------ hc/front/urls.py | 1 + hc/front/views.py | 7 ++++- templates/accounts/login.html | 1 - templates/{checks => front}/index.html | 2 +- templates/index.html | 33 +++++++++++++++++++++ 8 files changed, 81 insertions(+), 13 deletions(-) rename templates/{checks => front}/index.html (87%) create mode 100644 templates/index.html diff --git a/hc/accounts/forms.py b/hc/accounts/forms.py index 624e16c0..d4738965 100644 --- a/hc/accounts/forms.py +++ b/hc/accounts/forms.py @@ -1,5 +1,11 @@ from django import forms +class LowercaseEmailField(forms.EmailField): + def clean(self, value): + value = super(LowercaseEmailField, self).clean(value) + return value.lower() + + class EmailForm(forms.Form): - email = forms.EmailField() + email = LowercaseEmailField() diff --git a/hc/accounts/urls.py b/hc/accounts/urls.py index 92acf8bb..2c4d6d0c 100644 --- a/hc/accounts/urls.py +++ b/hc/accounts/urls.py @@ -3,6 +3,7 @@ from django.conf.urls import url from hc.accounts import views urlpatterns = [ + url(r'^create/$', views.create, name="hc-create-account"), url(r'^login/$', views.login, name="hc-login"), url(r'^login_link_sent/$', views.login_link_sent, name="hc-login-link-sent"), url(r'^check_token/([\w-]+)/([\w-]+)/$', views.check_token, name="hc-check-token"), diff --git a/hc/accounts/views.py b/hc/accounts/views.py index 6b5fb849..f54bbc26 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -11,14 +11,41 @@ from django.shortcuts import redirect, render from hc.accounts.forms import EmailForm -def login(request): +def create(request): + assert request.method == "POST" + + form = EmailForm(request.POST) + if form.is_valid(): + email = form.cleaned_data["email"] + + num_existing = User.objects.filter(email=email).count() + if num_existing > 0: + # FIXME be more polite about this + return HttpResponseBadRequest() + + username = str(uuid.uuid4())[:30] + temp_password = str(uuid.uuid4()) + + user = User(username=username, email=email) + user.set_password(temp_password) + user.save() + + user = authenticate(username=username, password=temp_password) + user.set_unusable_password() + user.save() + auth_login(request, user) + return redirect("hc-checks") + + # FIXME do something nicer here + return HttpResponseBadRequest() + + +def login(request): if request.method == 'POST': - # create a form instance and populate it with data from the request: form = EmailForm(request.POST) - # check whether it's valid: if form.is_valid(): - email = form.cleaned_data["email"] + email = form.cleaned_data["email"].lower() user = User.objects.get(email=email) # We don't want to reset passwords of staff users :-) @@ -36,16 +63,12 @@ def login(request): send_mail('Log In', body, 'cuu508@gmail.com', [email], fail_silently=False) - # FIXME send login token here return redirect("hc-login-link-sent") else: form = EmailForm() - ctx = { - "form": form - } - + ctx = {"form": form} return render(request, "accounts/login.html", ctx) diff --git a/hc/front/urls.py b/hc/front/urls.py index 8ea5eff6..f98a9ec3 100644 --- a/hc/front/urls.py +++ b/hc/front/urls.py @@ -3,5 +3,6 @@ from django.conf.urls import url from hc.front import views urlpatterns = [ + url(r'^$', views.index, name="hc-index"), url(r'^checks/$', views.checks, name="hc-checks"), ] diff --git a/hc/front/views.py b/hc/front/views.py index 6be2b4f2..813a8e5d 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -3,6 +3,11 @@ from django.shortcuts import render from hc.api.models import Check + +def index(request): + return render(request, "index.html") + + @login_required def checks(request): @@ -12,4 +17,4 @@ def checks(request): "checks": checks } - return render(request, "checks/index.html", ctx) + return render(request, "front/index.html", ctx) diff --git a/templates/accounts/login.html b/templates/accounts/login.html index fdbb22e0..1de76a1d 100644 --- a/templates/accounts/login.html +++ b/templates/accounts/login.html @@ -15,7 +15,6 @@ {% csrf_token %}
-
@
-

Hello World

+

Hello {{ request.user.email }}

diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 00000000..354a0e3f --- /dev/null +++ b/templates/index.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block content %} +
+
+

Hello World

+ +

Get started here:

+
+ {% csrf_token %} + +
+
+
@
+ +
+
+ +
+ +
+ + +
+
+{% endblock %} \ No newline at end of file
Code