Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adu013 committed Sep 5, 2019
0 parents commit 8aee2d7
Show file tree
Hide file tree
Showing 38 changed files with 371 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env
**/.DS_Store
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Django==2.2.5
pytz==2019.2
sqlparse==0.3.0
Empty file added website/accounts/__init__.py
Empty file.
Binary file added website/accounts/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/admin.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/apps.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/forms.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/models.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/tokens.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/urls.cpython-37.pyc
Binary file not shown.
Binary file added website/accounts/__pycache__/views.cpython-37.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions website/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions website/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AccountsConfig(AppConfig):
name = 'accounts'
11 changes: 11 additions & 0 deletions website/accounts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')

class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
Empty file.
Binary file not shown.
3 changes: 3 additions & 0 deletions website/accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
5 changes: 5 additions & 0 deletions website/accounts/templates/accounts/acc_active_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
17 changes: 17 additions & 0 deletions website/accounts/templates/accounts/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="content-section">
<form class="login-form" method="post">
{% csrf_token %}
<fieldset>
<legend class="border-bottom mb-4">Login</legend>
{{ form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit" name="submit">Login</button>
</div>
</form>
<div class="border-top pt-3">
<small>
Need An Account? <a class="ml-2" href="{% url 'signup' %}">Signup</a>
</small>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="alert alert-info">
your password is reset.
</div>
17 changes: 17 additions & 0 deletions website/accounts/templates/accounts/password-reset-confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="content-section">
<form class="password_reset-form" method="post">
{% csrf_token %}
<fieldset>
<legend class="border-bottom mb-4">Reset Password</legend>
{{ form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit" name="submit">Reset Password</button>
</div>
</form>
<div class="border-top pt-3">
<small>
Need An Account? <a class="ml-2" href="#"
</small>
</div>
</div>
3 changes: 3 additions & 0 deletions website/accounts/templates/accounts/password-reset-done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="alert alert-info">
An email has been sent. Please click on the link to reset your password
</div>
17 changes: 17 additions & 0 deletions website/accounts/templates/accounts/password-reset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="content-section">
<form class="password_reset-form" method="post">
{% csrf_token %}
<fieldset>
<legend class="border-bottom mb-4">Reset Password</legend>
{{ form }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit" name="submit">Reset Password</button>
</div>
</form>
<div class="border-top pt-3">
<small>
Need An Account? <a class="ml-2" href="#"
</small>
</div>
</div>
19 changes: 19 additions & 0 deletions website/accounts/templates/accounts/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="display: none">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Sign up</button>
</form>
{% endblock %}
3 changes: 3 additions & 0 deletions website/accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
13 changes: 13 additions & 0 deletions website/accounts/tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six


class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return(
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.is_active)
)


account_activation_token = TokenGenerator()
35 changes: 35 additions & 0 deletions website/accounts/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.urls import path
from django.conf.urls import url
from django.contrib.auth.views import (
LoginView, PasswordResetView, PasswordResetDoneView,
PasswordResetConfirmView, PasswordResetCompleteView
)
from . import views


urlpatterns = [
url(r'^signup/$', views.signup, name='signup'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate, name='activate'),
path('login/', LoginView.as_view(template_name='accounts/login.html'), name='login'),
path(
'password-reset/',
PasswordResetView.as_view(template_name='accounts/password-reset.html'),
name='password_reset'
),
path(
'password-reset/done/',
PasswordResetDoneView.as_view(template_name='accounts/password-reset-done.html'),
name='password_reset_done'
),
path(
'password-reset-confirm/<uidb64>/<token>/',
PasswordResetConfirmView.as_view(template_name='accounts/password-reset-confirm.html'),
name='password_reset_confirm'
),
path(
'password-reset-complete',
PasswordResetCompleteView.as_view(template_name='accounts/password-reset-complete.html'),
name='password_reset_complete'
),
]
52 changes: 52 additions & 0 deletions website/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .forms import SignUpForm
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from .tokens import account_activation_token
from django.contrib.auth.models import User
from django.core.mail import EmailMessage


def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activate your blog account'
message = render_to_string(
'accounts/acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Please confirm your email address to complete teh registration')
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form':form})


def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
Binary file added website/db.sqlite3
Binary file not shown.
21 changes: 21 additions & 0 deletions website/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file added website/website/__init__.py
Empty file.
Binary file added website/website/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file not shown.
Binary file added website/website/__pycache__/urls.cpython-37.pyc
Binary file not shown.
Binary file added website/website/__pycache__/wsgi.cpython-37.pyc
Binary file not shown.
116 changes: 116 additions & 0 deletions website/website/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@mphmw*9dq_z+lth)k5+z1hen4)&o%0(9dsiy5w(aes@_^1n_='

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts.apps.AccountsConfig',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'website.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'website.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

# Email
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'your_password_or_app_password'
EMAIL_PORT = 587
7 changes: 7 additions & 0 deletions website/website/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('accounts/', include('accounts.urls')),
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions website/website/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for website project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')

application = get_wsgi_application()

0 comments on commit 8aee2d7

Please sign in to comment.