added user registration, login, logout

master
Laurent 6 years ago
parent 2454fa1303
commit 11d75e2992
  1. BIN
      db.sqlite3
  2. BIN
      news/__pycache__/forms.cpython-37.pyc
  3. BIN
      news/__pycache__/urls.cpython-37.pyc
  4. BIN
      news/__pycache__/views.cpython-37.pyc
  5. 44
      news/forms.py
  6. 6
      news/templates/base.html
  7. 24
      news/templates/news/createaccount.html
  8. 31
      news/templates/news/submission.html
  9. 33
      news/templates/news/user/register.html
  10. 33
      news/templates/news/user/signin.html
  11. 6
      news/urls.py
  12. 55
      news/views.py
  13. BIN
      pokercc/__pycache__/urls.cpython-37.pyc
  14. 2
      pokercc/urls.py

Binary file not shown.

@ -1,4 +1,7 @@
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django import forms
from django.core.exceptions import ValidationError
from .choices import *
class PostForm(forms.Form):
@ -9,3 +12,44 @@ class PostForm(forms.Form):
# state = forms.IntegerField(label='State', default=1)
style = forms.ChoiceField(label='Style',choices=STYLE_CHOICES, required=True)
image = forms.FileField()
class SigninForm(forms.Form):
username = forms.CharField(label='Enter Username', min_length=4, max_length=150)
password = forms.CharField(label='Enter password', widget=forms.PasswordInput)
class CustomUserCreationForm(forms.Form):
username = forms.CharField(label='Enter Username', min_length=4, max_length=150)
email = forms.EmailField(label='Enter email')
password1 = forms.CharField(label='Enter password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
def clean_username(self):
username = self.cleaned_data['username'].lower()
r = User.objects.filter(username=username)
if r.count():
raise ValidationError("Username already exists")
return username
def clean_email(self):
email = self.cleaned_data['email'].lower()
r = User.objects.filter(email=email)
if r.count():
raise ValidationError("Email already exists")
return email
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise ValidationError("Password don't match")
return password2
def save(self, commit=True):
user = User.objects.create_user(
self.cleaned_data['username'],
self.cleaned_data['email'],
self.cleaned_data['password1']
)
return user

@ -27,8 +27,12 @@
<br/>
{% if user.is_authenticated %}
<a href="{% url 'news:submission' %}">Submit</a>
<br/>
<a href="{% url 'news:logout_view' %}">Log out</a>
{% else %}
<a href="{% url 'news:createaccount' %}">Create account</a>
<a href="{% url 'news:signin' %}">Log in</a>
<br/>
<a href="{% url 'news:register' %}">Create account</a>
{% endif %}
</p>
</div>

@ -1,24 +0,0 @@
{% extends "base.html" %}
{% block title %}My amazing blog{% endblock %}
{% block content %}
<h1>Create account</h1>
<form action="{% url 'news:submit' %}" method="post">
<p>Username</p>
<p>
<input type="text" name="username" value="">
</p>
<p>Password</p>
<p>
<input type="text" name="password1" value="">
<input type="text" name="password2" value="">
</p>
<input type="submit" value="Submit">
</form>
{% endblock content %}

@ -14,37 +14,6 @@
<form action="{% url 'news:submission' %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<!--
<p>
<span>Title</span>
<input type="text" name="title" maxlength="200" required id="id_title" size="100">
</p>
<p>
<span>URL</span>
<input type="text" name="url" maxlength="200" required id="id_url" size="100">
</p>
<p>
<span>Optional text</span>
<textarea name="body" rows="8" cols="80">
</p>
<p>
<span>Image URL</span>
<input type="file" name="image_file" id="image_file"/>
</p>
<p>
<input type="radio" name="state" id="1" value="1" checked="true">
<label for="choice1">Publish</label>
<input type="radio" name="state" id="2" value="2">
<label for="choice2">Draft</label>
<input type="radio" name="state" id="3" value="3">
<label for="choice3">Program</label>
</p>
<br/> -->
<input type="submit" value="Submit">
</form>

@ -0,0 +1,33 @@
{% extends "base.html" %}
{% block title %}
Create User
{% endblock %}
{% block content %}
<div class="login">
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<h1>Create account</h1>
<form method="post" >
{% csrf_token %}
<table>
{{ form.as_table }}
<tr>
<td></td>
<td><input type="submit" name="submit" value="Register" /></td>
</tr>
</table>
</form>
</div>
{% endblock %}

@ -0,0 +1,33 @@
{% extends "base.html" %}
{% block title %}
Sign-in
{% endblock %}
{% block content %}
<div class="login">
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
<h1>Sign in</h1>
<form method="post" >
{% csrf_token %}
<table>
{{ form.as_table }}
<tr>
<td></td>
<td><input type="submit" name="submit" value="Signin" /></td>
</tr>
</table>
</form>
</div>
{% endblock %}

@ -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('<int:post_id>/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'),
]

@ -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():

@ -18,5 +18,5 @@ from django.urls import include, path
urlpatterns = [
path('news/', include('news.urls')),
path('admin/', admin.site.urls)
path('admin/', admin.site.urls),
]

Loading…
Cancel
Save