From a965f4c605c5454ce7d15bbb903df8cc1b5c268d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 11 Jun 2015 23:44:49 +0300 Subject: [PATCH] Login works, stubbed out canary index page --- hc/accounts/urls.py | 6 +++--- hc/accounts/views.py | 18 ++++++++++++++---- hc/front/__init__.py | 0 hc/front/admin.py | 3 +++ hc/front/migrations/__init__.py | 0 hc/front/models.py | 3 +++ hc/front/tests.py | 3 +++ hc/front/urls.py | 7 +++++++ hc/front/views.py | 15 +++++++++++++++ hc/settings.py | 13 +++++++++++-- hc/urls.py | 1 + requirements.txt | 4 +++- templates/bad_link.html | 15 +++++++++++++++ templates/checks/index.html | 23 +++++++++++++++++++++++ 14 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 hc/front/__init__.py create mode 100644 hc/front/admin.py create mode 100644 hc/front/migrations/__init__.py create mode 100644 hc/front/models.py create mode 100644 hc/front/tests.py create mode 100644 hc/front/urls.py create mode 100644 hc/front/views.py create mode 100644 templates/bad_link.html create mode 100644 templates/checks/index.html diff --git a/hc/accounts/urls.py b/hc/accounts/urls.py index 750efa23..92acf8bb 100644 --- a/hc/accounts/urls.py +++ b/hc/accounts/urls.py @@ -3,7 +3,7 @@ from django.conf.urls import url from hc.accounts import views urlpatterns = [ - 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-]+)/$', views.check_token, name="hc-check-token"), + 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 a612656d..f0f2f122 100644 --- a/hc/accounts/views.py +++ b/hc/accounts/views.py @@ -1,6 +1,7 @@ import uuid from django.conf import settings +from django.contrib.auth import authenticate, login as auth_login from django.contrib.auth.models import User from django.core.mail import send_mail from django.core.urlresolvers import reverse @@ -22,11 +23,12 @@ def login(request): user.set_password(token) user.save() - login_link = reverse("hc-check-token", args=[token]) + login_link = reverse("hc-check-token", args=[user.username, token]) login_link = settings.SITE_ROOT + login_link body = "login link: %s" % login_link - send_mail('Log In', body, 'cuu508@gmail.com', [email], fail_silently=False) + send_mail('Log In', body, 'cuu508@gmail.com', [email], + fail_silently=False) # FIXME send login token here return redirect("hc-login-link-sent") @@ -45,5 +47,13 @@ def login_link_sent(request): return render(request, "accounts/login_link_sent.html") -def check_token(request): - return render(request, "accounts/login_link_sent.html") +def check_token(request, username, token): + user = authenticate(username=username, password=token) + if user is not None: + if user.is_active: + user.set_unusable_password() + user.save() + auth_login(request, user) + return redirect("hc-checks") + + return render(request, "bad_link.html") diff --git a/hc/front/__init__.py b/hc/front/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hc/front/admin.py b/hc/front/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/hc/front/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/hc/front/migrations/__init__.py b/hc/front/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hc/front/models.py b/hc/front/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/hc/front/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/hc/front/tests.py b/hc/front/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/hc/front/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/hc/front/urls.py b/hc/front/urls.py new file mode 100644 index 00000000..8ea5eff6 --- /dev/null +++ b/hc/front/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url + +from hc.front import views + +urlpatterns = [ + url(r'^checks/$', views.checks, name="hc-checks"), +] diff --git a/hc/front/views.py b/hc/front/views.py new file mode 100644 index 00000000..13d8a014 --- /dev/null +++ b/hc/front/views.py @@ -0,0 +1,15 @@ +from django.contrib.auth.decorators import login_required +from django.shortcuts import render + +from hc.checks.models import Canary + +@login_required +def checks(request): + + canaries = Canary.objects.filter(user=request.user) + + ctx = { + "canaries": canaries + } + + return render(request, "checks/index.html", ctx) diff --git a/hc/settings.py b/hc/settings.py index ea699019..ee432dd7 100644 --- a/hc/settings.py +++ b/hc/settings.py @@ -10,11 +10,12 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.8/ref/settings/ """ -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +import json import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +hc_config = json.loads(open(os.path.expanduser("~/hc_config.json")).read()) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ @@ -39,7 +40,8 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'hc.accounts', - 'hc.checks' + 'hc.checks', + 'hc.front' ) MIDDLEWARE_CLASSES = ( @@ -107,3 +109,10 @@ USE_TZ = True SITE_ROOT = "http://localhost:8000" STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] + +# AWS +EMAIL_BACKEND = 'django_ses_backend.SESBackend' +AWS_SES_ACCESS_KEY_ID = hc_config["aws_ses_access_key"] +AWS_SES_SECRET_ACCESS_KEY = hc_config["aws_ses_secret_key"] +AWS_SES_REGION_NAME = 'eu-west-1' +AWS_SES_REGION_ENDPOINT = 'email.eu-west-1.amazonaws.com' diff --git a/hc/urls.py b/hc/urls.py index a85dcf3d..2b9afb16 100644 --- a/hc/urls.py +++ b/hc/urls.py @@ -5,4 +5,5 @@ urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^accounts/', include('hc.accounts.urls')), url(r'^', include('hc.checks.urls')), + url(r'^', include('hc.front.urls')), ] diff --git a/requirements.txt b/requirements.txt index 039ff26f..c9a27bf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ Django==1.8.2 -psycopg2==2.6 \ No newline at end of file +django-ses +psycopg2==2.6 +django-ses-backend \ No newline at end of file diff --git a/templates/bad_link.html b/templates/bad_link.html new file mode 100644 index 00000000..ec85c3a9 --- /dev/null +++ b/templates/bad_link.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} +
+
+
+

Server Error

+ +

+ Something bad happened and you should feel bad. +

+
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/checks/index.html b/templates/checks/index.html new file mode 100644 index 00000000..a43f63ae --- /dev/null +++ b/templates/checks/index.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} + +{% block content %} +
+
+

Hello World

+ + + + + + {% for canary in canaries %} + + + + + {% endfor %} + +
CodeLast Ping
{{ canary.code }}{{ canary.last_ping }}
+ +
+
+{% endblock %} \ No newline at end of file