kp
This commit is contained in:
parent
6ca65366d7
commit
4f968e5d1c
3
ofu_app/apps/registration/admin.py
Normal file
3
ofu_app/apps/registration/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
ofu_app/apps/registration/apps.py
Normal file
5
ofu_app/apps/registration/apps.py
Normal file
@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class RegistrationConfig(AppConfig):
|
||||
name = 'apps.registration'
|
||||
11
ofu_app/apps/registration/forms.py
Normal file
11
ofu_app/apps/registration/forms.py
Normal file
@ -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=254, help_text='Required. Inform a valid email address.')
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('username', 'email', 'password1', 'password2', )
|
||||
0
ofu_app/apps/registration/migrations/__init__.py
Normal file
0
ofu_app/apps/registration/migrations/__init__.py
Normal file
17
ofu_app/apps/registration/models.py
Normal file
17
ofu_app/apps/registration/models.py
Normal file
@ -0,0 +1,17 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
email_confirmed = models.BooleanField(default=False)
|
||||
# other fields...
|
||||
|
||||
|
||||
@receiver(post_save, sender=User)
|
||||
def update_user_profile(sender, instance, created, **kwargs):
|
||||
if created:
|
||||
Profile.objects.create(user=instance)
|
||||
instance.profile.save()
|
||||
3
ofu_app/apps/registration/tests.py
Normal file
3
ofu_app/apps/registration/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
11
ofu_app/apps/registration/tokens.py
Normal file
11
ofu_app/apps/registration/tokens.py
Normal file
@ -0,0 +1,11 @@
|
||||
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
||||
from django.utils import six
|
||||
|
||||
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
|
||||
def _make_hash_value(self, user, timestamp):
|
||||
return (
|
||||
six.text_type(user.pk) + six.text_type(timestamp) +
|
||||
six.text_type(user.profile.email_confirmed)
|
||||
)
|
||||
|
||||
account_activation_token = AccountActivationTokenGenerator()
|
||||
9
ofu_app/apps/registration/urls.py
Normal file
9
ofu_app/apps/registration/urls.py
Normal file
@ -0,0 +1,9 @@
|
||||
from django.conf.urls import url
|
||||
from apps.registration import views as core_views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', core_views.signup, name='signup'),
|
||||
url(r'^account_activation_sent/$', core_views.account_activation_sent, name='account_activation_sent'),
|
||||
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
|
||||
core_views.activate, name='activate'),
|
||||
]
|
||||
54
ofu_app/apps/registration/views.py
Normal file
54
ofu_app/apps/registration/views.py
Normal file
@ -0,0 +1,54 @@
|
||||
from django.contrib.sites.shortcuts import get_current_site
|
||||
from django.shortcuts import render, redirect
|
||||
from django.utils.encoding import force_bytes
|
||||
from django.utils.http import urlsafe_base64_encode
|
||||
from django.template.loader import render_to_string
|
||||
from apps.registration.forms import SignUpForm
|
||||
from apps.registration.tokens import account_activation_token
|
||||
from django.contrib.auth import login
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.http import urlsafe_base64_decode
|
||||
|
||||
|
||||
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)
|
||||
subject = 'Activate Your MySite Account'
|
||||
message = render_to_string('registration/account_activation_email.jinja', {
|
||||
'user': user,
|
||||
'domain': current_site.domain,
|
||||
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
|
||||
'token': account_activation_token.make_token(user),
|
||||
})
|
||||
user.email_user(subject, message)
|
||||
return redirect('account_activation_sent')
|
||||
else:
|
||||
form = SignUpForm()
|
||||
return render(request, 'registration/signup.jinja', {'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.profile.email_confirmed = True
|
||||
user.save()
|
||||
login(request, user)
|
||||
return redirect('home')
|
||||
else:
|
||||
return render(request, 'account_activation_invalid.html')
|
||||
|
||||
|
||||
def account_activation_sent(request):
|
||||
return render(request, 'home.jinja', {})
|
||||
0
ofu_app/ofu_app/models.py
Normal file
0
ofu_app/ofu_app/models.py
Normal file
@ -34,6 +34,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'django.contrib.sites',
|
||||
'django_jinja',
|
||||
'apps.food',
|
||||
'apps.events',
|
||||
@ -41,6 +42,8 @@ INSTALLED_APPS = [
|
||||
'rest_framework',
|
||||
]
|
||||
|
||||
SITE_ID = 1
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_PERMISSION_CLASSES': [
|
||||
'rest_framework.permissions.IsAdminUser',
|
||||
@ -58,7 +61,6 @@ REST_FRAMEWORK = {
|
||||
# ]
|
||||
|
||||
ROOT_URLCONF = 'ofu_app.urls'
|
||||
TEMPLATES_DIR = os.path.join(BASE_DIR, 'templates'),
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django_jinja.backend.Jinja2',
|
||||
@ -162,3 +164,8 @@ MEDIA_ROOT = os.path.join(BASE_DIR, "media")
|
||||
MEDIA_URL = '/media/'
|
||||
|
||||
LOGIN_REDIRECT_URL = 'home'
|
||||
|
||||
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
|
||||
|
||||
ACCOUNT_EMAIL_UNIQUE = True
|
||||
ACCOUNT_EMAIL_CONFIRMATION_REQUIRED = True
|
||||
|
||||
@ -23,6 +23,7 @@ urlpatterns = [
|
||||
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
|
||||
url(r'^admin/', admin.site.urls),
|
||||
# url(r'^signup/$', core_views.signup, name='signup'),
|
||||
url(r"^account/", include("apps.registration.urls")),
|
||||
|
||||
url(r'^$', views.home, name="home"),
|
||||
# -- Apps --
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
{% autoescape off %}
|
||||
Hi {{ user.username }},
|
||||
|
||||
Please click on the link below to confirm your registration:
|
||||
|
||||
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
|
||||
{% endautoescape %}
|
||||
23
ofu_app/templates/registration/signup.jinja
Normal file
23
ofu_app/templates/registration/signup.jinja
Normal file
@ -0,0 +1,23 @@
|
||||
{% extends 'base.jinja' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="col-12 text-left p-3 bg-light text-dark">
|
||||
<h2>Sign up</h2>
|
||||
<form method="post" action="{{ url('signup') }}">
|
||||
{% csrf_token %}
|
||||
{% for field in form %}
|
||||
<p>
|
||||
{{ field.label_tag }}<br>
|
||||
{{ field }}
|
||||
{% if field.help_text %}
|
||||
<small style="color: grey">{{ 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>
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user