Adds account registration form.
This commit is contained in:
parent
9b924fc07e
commit
79d122a1dd
6 changed files with 115 additions and 3 deletions
13
evennia/web/website/forms.py
Normal file
13
evennia/web/website/forms.py
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
from django import forms
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.auth.forms import UserCreationForm, UsernameField
|
||||||
|
from evennia.utils import class_from_module
|
||||||
|
|
||||||
|
class AccountCreationForm(UserCreationForm):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = class_from_module(settings.BASE_ACCOUNT_TYPECLASS)
|
||||||
|
fields = ("username", "email")
|
||||||
|
field_classes = {'username': UsernameField}
|
||||||
|
|
||||||
|
email = forms.EmailField(help_text="A valid email address. Optional; used for password resets.", required=False)
|
||||||
|
|
@ -51,7 +51,7 @@ folder and edit it to add/remove links to the menu.
|
||||||
<a class="nav-link" href="{% url 'login' %}">Log In</a>
|
<a class="nav-link" href="{% url 'login' %}">Log In</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a class="nav-link" href="{% url 'to_be_implemented' %}">Register</a>
|
<a class="nav-link" href="{% url 'register' %}">Register</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ Login
|
||||||
<hr />
|
<hr />
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-6 col-sm-12 text-center small"><a href="{% url 'password_reset' %}">Forgot Password?</a></div>
|
<div class="col-lg-6 col-sm-12 text-center small"><a href="{% url 'password_reset' %}">Forgot Password?</a></div>
|
||||||
<div class="col-lg-6 col-sm-12 text-center small"><a href="{% url 'register' %}">Sign Up</a></div>
|
<div class="col-lg-6 col-sm-12 text-center small"><a href="{% url 'register' %}">Create Account</a></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr />
|
<hr />
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block titleblock %}
|
||||||
|
Register
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
|
||||||
|
{% load addclass %}
|
||||||
|
<div class="container main-content mt-4" id="main-copy">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-5 offset-lg-3 col-sm-12">
|
||||||
|
<div class="card mt-3">
|
||||||
|
<div class="card-body">
|
||||||
|
<h1 class="card-title">Register</h1>
|
||||||
|
<hr />
|
||||||
|
{% if user.is_authenticated %}
|
||||||
|
<div class="alert alert-info" role="alert">You are already registered!</div>
|
||||||
|
{% else %}
|
||||||
|
{% if form.errors %}
|
||||||
|
{% for field in form %}
|
||||||
|
{% for error in field.errors %}
|
||||||
|
<div class="alert alert-danger" role="alert">{{ error }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if not user.is_authenticated %}
|
||||||
|
<form method="post" action="?">
|
||||||
|
{% csrf_token %}
|
||||||
|
|
||||||
|
{% for field in form %}
|
||||||
|
<div class="form-field my-3">
|
||||||
|
{{ field.label_tag }}
|
||||||
|
{{ field | addclass:"form-control" }}
|
||||||
|
{% if field.help_text %}
|
||||||
|
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
<div class="form-group">
|
||||||
|
<input class="form-control btn btn-outline-secondary" type="submit" value="Register" />
|
||||||
|
<input type="hidden" name="next" value="{{ next }}" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -13,7 +13,8 @@ urlpatterns = [
|
||||||
url(r'^tbi/', website_views.to_be_implemented, name='to_be_implemented'),
|
url(r'^tbi/', website_views.to_be_implemented, name='to_be_implemented'),
|
||||||
|
|
||||||
# User Authentication (makes login/logout url names available)
|
# User Authentication (makes login/logout url names available)
|
||||||
url(r'^authenticate/', include('django.contrib.auth.urls')),
|
url(r'^auth/', include('django.contrib.auth.urls')),
|
||||||
|
url(r'^auth/register', website_views.AccountCreationView.as_view(), name="register"),
|
||||||
|
|
||||||
# Django original admin page. Make this URL is always available, whether
|
# Django original admin page. Make this URL is always available, whether
|
||||||
# we've chosen to use Evennia's custom admin or not.
|
# we've chosen to use Evennia's custom admin or not.
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,19 @@ templates on the fly.
|
||||||
"""
|
"""
|
||||||
from django.contrib.admin.sites import site
|
from django.contrib.admin.sites import site
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.contrib import messages
|
||||||
from django.contrib.auth import authenticate
|
from django.contrib.auth import authenticate
|
||||||
from django.contrib.admin.views.decorators import staff_member_required
|
from django.contrib.admin.views.decorators import staff_member_required
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
from django.urls import reverse, reverse_lazy
|
||||||
|
from django.views.generic import View, DetailView, ListView, FormView
|
||||||
|
|
||||||
from evennia import SESSION_HANDLER
|
from evennia import SESSION_HANDLER
|
||||||
from evennia.objects.models import ObjectDB
|
from evennia.objects.models import ObjectDB
|
||||||
from evennia.accounts.models import AccountDB
|
from evennia.accounts.models import AccountDB
|
||||||
from evennia.utils import logger
|
from evennia.utils import logger
|
||||||
|
from evennia.web.website.forms import AccountCreationForm
|
||||||
|
|
||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
|
|
||||||
|
|
@ -134,3 +139,40 @@ def admin_wrapper(request):
|
||||||
Wrapper that allows us to properly use the base Django admin site, if needed.
|
Wrapper that allows us to properly use the base Django admin site, if needed.
|
||||||
"""
|
"""
|
||||||
return staff_member_required(site.index)(request)
|
return staff_member_required(site.index)(request)
|
||||||
|
|
||||||
|
class AccountCreationView(FormView):
|
||||||
|
form_class = AccountCreationForm
|
||||||
|
template_name = 'website/registration/register.html'
|
||||||
|
success_url = reverse_lazy('login')
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
# Check to make sure basics validated
|
||||||
|
valid = super(AccountCreationView, self).form_valid(form)
|
||||||
|
if not valid: return self.form_invalid(form)
|
||||||
|
|
||||||
|
username = form.cleaned_data['username']
|
||||||
|
password = form.cleaned_data['password1']
|
||||||
|
email = form.cleaned_data.get('email', '')
|
||||||
|
|
||||||
|
# Create a fake session object to intercept calls to the terminal
|
||||||
|
from mock import Mock
|
||||||
|
session = self.request
|
||||||
|
session.address = self.request.META.get('REMOTE_ADDR', '')
|
||||||
|
session.msg = Mock()
|
||||||
|
|
||||||
|
# Create account
|
||||||
|
from evennia.commands.default.unloggedin import _create_account
|
||||||
|
permissions = settings.PERMISSION_ACCOUNT_DEFAULT
|
||||||
|
account = _create_account(session, username, password, permissions)
|
||||||
|
|
||||||
|
# If unsuccessful, get messages passed to session.msg
|
||||||
|
if not account:
|
||||||
|
[messages.error(self.request, call) for call in session.msg.call_args_list]
|
||||||
|
return self.form_invalid(form)
|
||||||
|
|
||||||
|
# Append email address if given
|
||||||
|
account.email = email
|
||||||
|
account.save()
|
||||||
|
|
||||||
|
messages.success(self.request, "Your account '%s' was successfully created! You may log in using it now." % account.name)
|
||||||
|
return HttpResponseRedirect(self.success_url)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue