Register/Login/Logout system works now

This commit is contained in:
Walter 2025-02-07 05:51:40 +01:00
parent 5f149f76b4
commit b0071521dc
14 changed files with 114 additions and 56 deletions

View File

@ -8,7 +8,11 @@
<meta name="keywords" content="flash, games, awesome"/> <meta name="keywords" content="flash, games, awesome"/>
<meta name="language" content="en"/> <meta name="language" content="en"/>
<title>Bloonsworld!</title> {% if title %}
<title>{{ title }}</title>
{% else %}
<title>Bloonsworld!</title>
{% endif %}
<!--[if IE6]> <!--[if IE6]>
<style> <style>
#wrapper { #wrapper {
@ -35,26 +39,13 @@
<div id="adslot1"></div> <div id="adslot1"></div>
</div> </div>
<div id="login_box"> {% if user.is_authenticated %}
<form method="post" action="{% url 'bloonsa_game:login' %}"> <table> {% include 'bloonsa_game/profilebox.html' %}
<tr> {% else %}
<td><label for="login_username">Name: </label></td> {% include 'bloonsa_game/loginbox.html' %}
<td><input type="text" name="login_username" id="login_username" value=""/></td> {% endif %}
</tr>
<tr>
<td><label for="login_password">Password: </label></td>
<td><input type="password" name="login_password" id="login_password" value=""/></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="remember" id="remember" value="1" checked="checked"/>
<label for="remember" id="remember_label">Remember</label>
<input type="submit" name="commit" value="Log in" id="login"/>
</td>
</tr>
</table></form> </div>
<h3><a href="/users/register">Register for free</a>&nbsp;&nbsp;<a href="/users/forgot_password">Forgot Password?</a></h3>
</div> </div>
<div class="skyscraper"> <div class="skyscraper">
<div class="ad ad_160x600"> <div class="ad ad_160x600">
@ -74,13 +65,13 @@
<div class="skyscraper rhs"> <div class="skyscraper rhs">
<ul id="menu" class="light"> <ul id="menu" class="light">
<li><a href="/"><img src="{% static 'bloonsa_game/img/nav_home.gif' %}" alt="Nav_home"/></a></li> <li><a href="{% url 'bloonsa_game:index' %}"><img src="{% static 'bloonsa_game/img/nav_home.gif' %}" alt="Nav_home"/></a></li>
<li><a href="/game"><img src="{% static 'bloonsa_game/img/nav_play.gif' %}" alt="Nav_play"/></a></li> <li><a href="{% url 'bloonsa_game:game' %}"><img src="{% static 'bloonsa_game/img/nav_play.gif' %}" alt="Nav_play"/></a></li>
<li><a href="/level_editor"><img src="{% static 'bloonsa_game/img/nav_build.gif' %}" alt="Nav_build"/></a></li> <li><a href="#"><img src="{% static 'bloonsa_game/img/nav_build.gif' %}" alt="Nav_build"/></a></li>
<li><a href="/level/list"><img src="{% static 'bloonsa_game/img/nav_levels.gif' %}" alt="Nav_levels"/></a></li> <li><a href="#"><img src="{% static 'bloonsa_game/img/nav_levels.gif' %}" alt="Nav_levels"/></a></li>
<li><a href="/user/list"><img src="{% static 'bloonsa_game/img/nav_users.gif' %}" alt="Nav_users"/></a></li> <li><a href="#"><img src="{% static 'bloonsa_game/img/nav_users.gif' %}" alt="Nav_users"/></a></li>
<li><a href="http://www.ninjakiwi.com/"><img src="{% static 'bloonsa_game/img/nav_nk.gif' %}" alt="Nav_nk"/></a></li> <!--<li><a href="#"><img src="{% static 'bloonsa_game/img/nav_nk.gif' %}" alt="Nav_nk"/></a></li>
<li><a href="http://ninjakiwi.com/Games/Tower-Defense/Bloons-Tower-Defense-5-Deluxe.html?predorder"><img src="{% static 'bloonsa_game/img/nav_buy.gif' %}" alt="Nav_buy"/></a></li> <li><a href="#"><img src="{% static 'bloonsa_game/img/nav_buy.gif' %}" alt="Nav_buy"/></a></li>-->
</ul> </ul>
<div id="searchbox"> <div id="searchbox">
<form method="post" action="/level/list"> <form method="post" action="/level/list">

View File

@ -17,7 +17,7 @@
<div class=" wide light"> <div class=" wide light">
<h2 class="centered">Welcome to Bloonsworld!</h2> <h2 class="centered">Welcome to Bloonsworld!</h2>
<p class="centered"></p> <p class="centered"></p>
<a class="homepage_button" href="{% url 'game' %}"> <a class="homepage_button" href="{% url 'bloonsa_game:game' %}">
<img src="{% static 'bloonsa_game/img/top_playnow.gif' %}" <img src="{% static 'bloonsa_game/img/top_playnow.gif' %}"
alt="Top_playnow"/></a> alt="Top_playnow"/></a>
<a class="homepage_button" <a class="homepage_button"

View File

@ -0,0 +1,4 @@
<div id="login_box">
<h2><a href="/users/login">Login</a></h2>
<h2><a href="/users/register">Register</a></h2>
</div>

View File

@ -0,0 +1,4 @@
<div id="profile_box">
<h2>Welcome back {{ user }}!</h2>
<h3><a href="/users/logout">Logout</a></h3>
</div>

View File

@ -9,7 +9,5 @@ urlpatterns = [
path("game", GameView.as_view(), name="game"), path("game", GameView.as_view(), name="game"),
path("game/<int:pk>", GameView.as_view(), name="gameSelect"), path("game/<int:pk>", GameView.as_view(), name="gameSelect"),
path("terms", TermsView.as_view(), name="terms"), path("terms", TermsView.as_view(), name="terms"),
path("login", WIPView.as_view(), name="login"),
path("register", WIPView.as_view(), name="register"),
path("contact", WIPView.as_view(), name="contact"), path("contact", WIPView.as_view(), name="contact"),
] ]

View File

@ -1,3 +1,4 @@
from django.contrib.auth.forms import AuthenticationForm
from django.shortcuts import render from django.shortcuts import render
from django.views.generic import TemplateView from django.views.generic import TemplateView
# Create your views here. # Create your views here.
@ -27,7 +28,7 @@ class GameView(TemplateView):
"levelAuthor": level.author, "levelAuthor": level.author,
}) })
return render(request, "bloonsa_game/game.html", context={}) return render(request, "bloonsa_game/game.html", context={"login_form": AuthenticationForm()})
class WIPView(TemplateView): class WIPView(TemplateView):

View File

@ -1 +1,3 @@
django==5.1.* django==5.1.*
django-crispy-forms
crispy-bulma

View File

@ -38,6 +38,10 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
# Plugins
"crispy_forms",
"crispy_bulma",
# Global # Global
"users", "users",
@ -132,3 +136,6 @@ STATIC_URL = '/static/'
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CRISPY_ALLOWED_TEMPLATE_PACKS = ("bulma",)
CRISPY_TEMPLATE_PACK = 'bulma'

View File

@ -1,33 +1,65 @@
from django.contrib.auth import password_validation
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django import forms from django import forms
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from users.validators import usernameValidator from users.validators import usernameValidator
class UserRegisterForm(UserCreationForm): class UserRegisterForm(UserCreationForm):
field_order = ("username", "password1", "password2") field_order = ("username", "password1", "password2")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-register'
self.helper.form_class = 'label'
self.helper.form_method = 'post'
self.helper.form_action = '/users/register'
self.helper.add_input(Submit('submit', 'Create'))
username = forms.CharField(min_length=3, username = forms.CharField(min_length=3,
max_length=16, max_length=16,
label="Username", label="Username",
required=True, required=True,
validators=[usernameValidator], validators=[usernameValidator,],
help_text="3-16 chars, alphanumeric and _- pls") help_text=_("3-16 chars, alphanumeric and _- pls"))
password1 = forms.CharField(
label="Password",
required=True,
strip=False,
widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}),
#help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
#label="Password2",
required=True,
widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}),
strip=False,
# help_text=_("yes again"),
)
class Meta: class Meta:
model = User model = User
fields = [ fields = [
"username", "username",
"password1",
"password2",
] ]
class UserLoginForm(AuthenticationForm): class UserLoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(UserLoginForm, self).__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'id-login'
self.helper.form_class = 'label'
self.helper.form_method = 'post'
self.helper.form_action = '/users/login'
self.helper.add_input(Submit('submit', 'Login'))
username = forms.CharField(min_length=3, username = forms.CharField(min_length=3,
max_length=16, max_length=16,
label="Username", label="Username",
required=True, required=True)
validators=[usernameValidator])

View File

@ -1 +1,11 @@
{% extends "bloonsa_game/base.html" %} {% extends "bloonsa_game/base.html" %}
{% load crispy_forms_tags %}
{% block title %}
Login
{% endblock title %}
{% block content %}
<h1>Login!</h1>
{% crispy form %}
{% endblock %}

View File

@ -1,4 +1,5 @@
{% extends "bloonsa_game/base.html" %} {% extends "bloonsa_game/base.html" %}
{% load crispy_forms_tags %}
{% block title %} {% block title %}
Register Register
@ -6,9 +7,5 @@
{% block content %} {% block content %}
<h1>Register!</h1> <h1>Register!</h1>
<form class="form-with-validation" action="/users/register" method="post"> {% crispy form %}
{% csrf_token %}
{{ form }}
<button class="form-submit">Create</button>
</form>
{% endblock %} {% endblock %}

View File

@ -1,10 +1,11 @@
from django.urls import path, include from django.urls import path, include
from users.views import LoginView, RegisterView from users.views import LoginView, RegisterView, LogoutView
app_name = "users" app_name = "users"
urlpatterns = [ urlpatterns = [
path("login", LoginView.as_view(), name="login"), path("login", LoginView.as_view(), name="login"),
path("register", RegisterView.as_view(), name="register"), path("register", RegisterView.as_view(), name="register"),
path("logout", LogoutView.as_view(), name="logout")
] ]

View File

@ -9,7 +9,7 @@ def usernameValidator(username):
if User.objects.filter(username__iexact=username).first() is not None: if User.objects.filter(username__iexact=username).first() is not None:
raise ValidationError("Sorry, this username is already in use") raise ValidationError("Sorry, this username is already in use")
if 3 < len(username) < 17: if len(username) < 3 or len(username) > 16:
raise ValidationError("Username must be between 3-16 characters long") raise ValidationError("Username must be between 3-16 characters long")
charset = set(string.ascii_letters + string.digits + "_-") charset = set(string.ascii_letters + string.digits + "_-")

View File

@ -1,34 +1,45 @@
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth import login, logout
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.views.generic import TemplateView from django.views.generic import TemplateView
from users.forms import UserRegisterForm from users.forms import UserRegisterForm, UserLoginForm
class LoginView(TemplateView): class LoginView(TemplateView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
... form = UserLoginForm()
return render(request=request, template_name="users/login.html", context={"form": form})
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
... form = UserLoginForm(data=request.POST)
if not form.is_valid():
return render(request=request, template_name="users/login.html", context={"form": form})
login(request=request, user=form.get_user())
return redirect("bloonsa_game:game")
class RegisterView(TemplateView): class RegisterView(TemplateView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
form = UserCreationForm() form = UserRegisterForm()
return render(request=request, template_name="users/register.html", context={"form": form}) return render(request=request, template_name="users/register.html", context={"form": form})
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
form = UserCreationForm(request.POST) form = UserRegisterForm(request.POST)
if not form.is_valid(): if not form.is_valid():
print("form invalid", form.error_messages, request.POST)
return render(request=request, template_name="users/register.html", context={"form": form}) return render(request=request, template_name="users/register.html", context={"form": form})
# form.save() login(request=request, user=form.save())
return redirect("bloonsa_game:game") return redirect("bloonsa_game:game")
class LogoutView(TemplateView):
def get(self, request, *args, **kwargs):
if request.user.is_authenticated:
logout(request)
return redirect("bloonsa_game:game")
# Create your views here.