diff --git a/db.sqlite3 b/db.sqlite3
index 7dec1d7..de9576d 100644
Binary files a/db.sqlite3 and b/db.sqlite3 differ
diff --git a/news/__pycache__/forms.cpython-37.pyc b/news/__pycache__/forms.cpython-37.pyc
index 441263e..a957475 100644
Binary files a/news/__pycache__/forms.cpython-37.pyc and b/news/__pycache__/forms.cpython-37.pyc differ
diff --git a/news/__pycache__/urls.cpython-37.pyc b/news/__pycache__/urls.cpython-37.pyc
index e3c3a02..d4a18e7 100644
Binary files a/news/__pycache__/urls.cpython-37.pyc and b/news/__pycache__/urls.cpython-37.pyc differ
diff --git a/news/__pycache__/views.cpython-37.pyc b/news/__pycache__/views.cpython-37.pyc
index 6034061..ff1fc45 100644
Binary files a/news/__pycache__/views.cpython-37.pyc and b/news/__pycache__/views.cpython-37.pyc differ
diff --git a/news/forms.py b/news/forms.py
index 2b54614..17e73b8 100644
--- a/news/forms.py
+++ b/news/forms.py
@@ -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
diff --git a/news/templates/base.html b/news/templates/base.html
index 2ac7bd7..c5fe7ab 100644
--- a/news/templates/base.html
+++ b/news/templates/base.html
@@ -27,8 +27,12 @@
{% 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
-
-
-{% 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 @@
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 %}
+
+
+
+ {% if messages %}
+
+ {% for message in messages %}
+ - {{ message }}
+ {% endfor %}
+
+ {% endif %}
+
+
Create account
+
+
+
+
+{% 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 %}
+
+
+
+ {% if messages %}
+
+ {% for message in messages %}
+ - {{ message }}
+ {% endfor %}
+
+ {% endif %}
+
+
Sign in
+
+
+
+
+{% 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 a60926c..c9a79bf 100644
Binary files a/pokercc/__pycache__/urls.cpython-37.pyc and b/pokercc/__pycache__/urls.cpython-37.pyc differ
diff --git a/pokercc/urls.py b/pokercc/urls.py
index acaed36..e55c93d 100644
--- a/pokercc/urls.py
+++ b/pokercc/urls.py
@@ -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),
]