From 11d75e2992cb031d12d90d36f759fcf71e45f7dc Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 13 Sep 2019 10:42:05 +0200 Subject: [PATCH] added user registration, login, logout --- db.sqlite3 | Bin 180224 -> 180224 bytes news/__pycache__/forms.cpython-37.pyc | Bin 602 -> 2495 bytes news/__pycache__/urls.cpython-37.pyc | Bin 477 -> 574 bytes news/__pycache__/views.cpython-37.pyc | Bin 2535 -> 3626 bytes news/forms.py | 44 +++++++++++++++++++ news/templates/base.html | 6 ++- news/templates/news/createaccount.html | 24 ----------- news/templates/news/submission.html | 31 ------------- news/templates/news/user/register.html | 33 ++++++++++++++ news/templates/news/user/signin.html | 33 ++++++++++++++ news/urls.py | 6 ++- news/views.py | 55 +++++++++++++++++++----- pokercc/__pycache__/urls.cpython-37.pyc | Bin 958 -> 958 bytes pokercc/urls.py | 2 +- 14 files changed, 164 insertions(+), 70 deletions(-) delete mode 100644 news/templates/news/createaccount.html create mode 100644 news/templates/news/user/register.html create mode 100644 news/templates/news/user/signin.html diff --git a/db.sqlite3 b/db.sqlite3 index 7dec1d70f81b1fc21fb09c24357e3e512f4c10e5..de9576de1cf347dc4eb9c3a3700406dbd5535bf3 100644 GIT binary patch delta 477 zcmZo@;BIK(o*>OwG*QNxv1nt$B6*2f3>^GkjQlV8z4(jx_wz60@8(b7-@t!$vtWS% z|70)y05Sfn4D6hVrMx`M@{Eb4B^mLh#i>P1n>Xl(ByceC9cADT;yb!oQDG+^ZzC5I zFGHgy1BWcHI>+Sr`4aV&ImscpNs(2V>1I-sSpfRhec%9`0G`UKZ&k8SVyQK6#Oj z&iP6C;ZC`}E~ypXIo|GuPWhp>C8@Bw7S zn30j0SyGW^T$r0wnwV*9Xl$9Dk(6RkW|Wz4X=-ekvi(&Z6Dy;%IkKIW2IdA_3_t)h pjsFhN!mIp>DufK$Hi3yn0PFz%M?fvN`K9Gx-Z#Q9ec1*k0RVE+imdN_HBrWyF=}JNB6*1w3>^GkjQlV8z4(jx_w&!<@8(b7-@t!$vtWS% z|70)y05KjOW_ixU(vpn$(&E%2{;Lce96;XY4f-JoY>fOt4E#YrwI2MFgXX6T8Ymc= zSQ!~u8JX)D8XFoJ7*0Ml--?5g|0@F!aRJr6fc>Ak5CRRpi6J&eL z&CJZX7#J8B`R_3B-vL^3iC;yT*_V@$S=%Nsu?PUY@Q8u`5m46+emPlYM^2!lEp-hn Nb&W9WShj&l005+hVMYJ| diff --git a/news/__pycache__/forms.cpython-37.pyc b/news/__pycache__/forms.cpython-37.pyc index 441263ecfe10684136af9535cf9651bff2e8fc56..a957475f9b81f24710481729b92a19cdc2ebe0d2 100644 GIT binary patch literal 2495 zcmb_eOK;mo5Z>iW)YFRNrf|}Snj#OTspO;udMS#ag53rP9Jf{qlt3ZS+_g=G4`)fm zwkls5Uw!S_KK7sVH|(`1|An49vm~R$QG6TKEN8xXtiEqHYXqM9Prv^*wF&tL z7v*CB^9V*NfH1;nNFwS|io6zTk?!h>*Fz&RT~qN!Xhjvb0=&tr&>oN<2&*vrm@u20 zqv|W&t%0P?Cp*MV^2Qbt^j>m!3vrZN3Qb4|4}zyVxY zYGoCB6!polfdVxv;}`L4g_cG zPE*>$VeCbGIFz;FFiO}sM7}W`z8HI9z9TSxvfAJE#9F{ZCd~{@lEzlT4y8U8p{)1S z*=pNuZVrBZvO4Vl`1EmqbyJ!tnjx!eLC6dJAo6zjUqsx1gBAJrZV$td_Cjwgc%1bZ zf7#nho^#>*y_oN(Jr$kq-l2fxA{7`ZI%U%q{`59g^7#R}4==tOAZ7sUd5Dl&%z_|Q zSOws>WoKu|I><|nJugl76TuuU4}i5F zupOSQJGN-UYvKx$MI?BnxC%tt2xlyP;j`zlyw9C_Gpyk|0sQWPxJTt|UYzlCo zWIh}t*trA_3k&Xqx|pE`WGLpT4bW888fm_+u^Ou%6Bi1OHDPYB7F&S1$(Gm^ws=h4 z7P|^yeju;)$7z;C2;iHwSHe^RD?S0k#HTsxfgRbgl^(N=_dPJ2sA)e zlC2T4H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e?W5J+Aj3sx> z7t3fCNRpiu!yuH2zP3CjCUB*322Iq^R)-;V0F78gOe9|mb5H6ae=RSy1b1WZwpvj; zjOTabz_&@qH%WbWc~hnT4ve5*%tRw%GWl+PF_gZ(w(%_aL}!4EZ`Agbz8#6c9#TfY z7DD$q5&yUbldZJ=mxy|iUrwz8F*59QHTUe z1TB;N&Ns+nQY^ePZ{l&XoqQ}je3FN!O z(+$+lxH;`0aSp85i)mY9nlq=#rY(fot-_G$n@w+J5okxW+s(wa-X9r3#JDdzAu%D9 z!@A2(xzhrgezXHmU`JeoWif`3<~E7iA_B4}mJ!hNKWwEeV=9W?hCpqfFA {% if user.is_authenticated %} Submit +
+ Log out {% else %} - Create account + Log in +
+ Create account {% endif %}

diff --git a/news/templates/news/createaccount.html b/news/templates/news/createaccount.html deleted file mode 100644 index e6521a1..0000000 --- a/news/templates/news/createaccount.html +++ /dev/null @@ -1,24 +0,0 @@ -{% extends "base.html" %} - -{% block title %}My amazing blog{% endblock %} - -{% block content %} -

Create account

-
- -

Username

-

- -

- -

Password

-

- - -

- - - -
- -{% endblock content %} diff --git a/news/templates/news/submission.html b/news/templates/news/submission.html index 1e97269..ff7a283 100644 --- a/news/templates/news/submission.html +++ b/news/templates/news/submission.html @@ -14,37 +14,6 @@
{% csrf_token %} {{ form }} - -
diff --git a/news/templates/news/user/register.html b/news/templates/news/user/register.html new file mode 100644 index 0000000..54383f8 --- /dev/null +++ b/news/templates/news/user/register.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block title %} + Create User +{% endblock %} + +{% block content %} + + + +{% endblock %} diff --git a/news/templates/news/user/signin.html b/news/templates/news/user/signin.html new file mode 100644 index 0000000..b98992c --- /dev/null +++ b/news/templates/news/user/signin.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block title %} + Sign-in +{% endblock %} + +{% block content %} + + + +{% endblock %} diff --git a/news/urls.py b/news/urls.py index 8873c3c..b52f0fc 100644 --- a/news/urls.py +++ b/news/urls.py @@ -1,4 +1,4 @@ -from django.urls import path +from django.urls import path, re_path from . import views @@ -10,6 +10,8 @@ urlpatterns = [ # path('submit', views.submit, name='submit'), path('/comment', views.comment, name='comment'), path('submitted', views.submitted, name='submitted'), - path('createaccount', views.createaccount, name='createaccount'), + path('signin', views.signin, name='signin'), + path('logout', views.logout_view, name='logout_view'), + re_path(r'^register/$', views.register, name='register'), ] diff --git a/news/views.py b/news/views.py index 18495e7..d11b1b9 100644 --- a/news/views.py +++ b/news/views.py @@ -4,12 +4,54 @@ from django.template import loader from django.urls import reverse from django.core.paginator import Paginator from django.conf import settings +from django.contrib.auth.forms import UserCreationForm +from django.contrib import messages +from django.contrib.auth import authenticate, login, logout +from django.contrib.auth.decorators import login_required from .models import Post, Comment -from .forms import PostForm +from .forms import PostForm, CustomUserCreationForm, SigninForm from datetime import datetime import logging import uuid +#Users +def register(request): + if request.method == 'POST': + f = CustomUserCreationForm(request.POST) + if f.is_valid(): + f.save() + messages.success(request, 'Account created successfully') + return redirect('register') + else: + f = CustomUserCreationForm() + + return render(request, 'news/user/register.html', {'form': f}) + +def signin(request): + + if request.method == 'POST': + + f = SigninForm(request.POST) + if f.is_valid(): + username = f.cleaned_data['username'] + password = f.cleaned_data['password'] + + user = authenticate(username=username, password=password) + if user is not None: + login(request, user) + return HttpResponseRedirect(reverse('news:index')) + else: + messages.success(request, 'Sign-in failed') + return HttpResponseRedirect(reverse('news:signin')) + else: + f = SigninForm() + return render(request, 'news/user/signin.html', {'form': f}) + +def logout_view(request): + logout(request) + return HttpResponseRedirect(reverse('news:index')) + +#Post def index(request): latest_post_list = Post.objects.filter(state=1).order_by('-date') paginator = Paginator(latest_post_list, 25) @@ -24,9 +66,7 @@ def post(request, post_id): post = get_object_or_404(Post, pk=post_id) return render(request, 'news/post.html', {'post': post}) -def createaccount(request): - return render(request, 'news/createaccount.html', {}) - +@login_required def submission(request): if request.method == 'POST': @@ -52,13 +92,6 @@ def submission(request): return render(request, 'news/submission.html', {'form': form}) - # title = forms.CharField(label='Title', max_length=200) - # body = forms.CharField(label='Optional text', max_length=10000) - # url = forms.CharField(label='URL', max_length=200) - # # date = forms.DateTimeField('date published') - # # state = forms.IntegerField(label='State', default=1) - # image = forms.FileField() - def handle_uploaded_file(filename, f): with open(settings.MEDIA_ROOT + filename, 'wb+') as destination: for chunk in f.chunks(): diff --git a/pokercc/__pycache__/urls.cpython-37.pyc b/pokercc/__pycache__/urls.cpython-37.pyc index a60926cb450306bc7aecb2322b2a9211ac2f7758..c9a79bfeb66eaa51e0fb08fa97eba62adeec5b5e 100644 GIT binary patch delta 22 ccmdnTzK@;PiI