add index and login page (not yet good)
This commit is contained in:
parent
44036e57e9
commit
cd8e918890
|
@ -0,0 +1,20 @@
|
||||||
|
from django import forms
|
||||||
|
|
||||||
|
from accounts.models import Accounts
|
||||||
|
from accounts.utils import LoginHelper
|
||||||
|
|
||||||
|
|
||||||
|
class LoginForm(forms.Form):
|
||||||
|
account_name = forms.CharField()
|
||||||
|
password = forms.CharField(widget=forms.PasswordInput)
|
||||||
|
|
||||||
|
def is_valid(self):
|
||||||
|
if not super(LoginForm, self).is_valid():
|
||||||
|
return False
|
||||||
|
|
||||||
|
account = Accounts.objects.get(name=self.cleaned_data['account_name'])
|
||||||
|
hashed_password = LoginHelper.hash_password(self.cleaned_data['password'])
|
||||||
|
if account.password == hashed_password:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
|
@ -5,18 +5,27 @@
|
||||||
<title>Highscore</title>
|
<title>Highscore</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<h1>Highscore</h1>
|
||||||
|
|
||||||
{% if best_players %}
|
{% if best_players %}
|
||||||
<ul>
|
<table style="width:50%">
|
||||||
{% for player in best_players %}
|
<tr>
|
||||||
<li>{{ player.name }} - {{ player.level }}</li>
|
<th style="text-align: center;">Name</th>
|
||||||
|
<th style="text-align: center;">Level</th>
|
||||||
|
</tr>
|
||||||
|
{% for player in best_players %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ player.name }}</td>
|
||||||
|
<td style="text-align: right; width: 10%">{{ player.level }}</td>
|
||||||
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</table>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>No players registered.</p>
|
<p>No players registered.</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<a href="{% url 'accounts:register' %}">Register now!</a>
|
<a href="{% url 'accounts:register' %}">Register now!</a>
|
||||||
|
<a href="{% url 'accounts:login' %}">Login</a>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Login</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Login</h1>
|
||||||
|
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ form.as_p }}
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -8,4 +8,5 @@ app_name = 'accounts'
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.IndexView.as_view(), name='index'),
|
path('', views.IndexView.as_view(), name='index'),
|
||||||
path('register/', views.RegisterAccountView.as_view(), name='register'),
|
path('register/', views.RegisterAccountView.as_view(), name='register'),
|
||||||
|
path('login/', views.LoginView.as_view(), name='login'),
|
||||||
]
|
]
|
|
@ -0,0 +1,9 @@
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
class LoginHelper(object):
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def hash_password(password):
|
||||||
|
hashed_password = hashlib.sha1(password.encode('utf-8'))\
|
||||||
|
.hexdigest()
|
||||||
|
return hashed_password
|
|
@ -1,8 +1,10 @@
|
||||||
from django.shortcuts import render, render_to_response
|
from django.shortcuts import render, render_to_response
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
from django.views.generic.edit import CreateView
|
from django.views.generic.edit import CreateView, FormView
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
from accounts.forms import LoginForm
|
||||||
from accounts.models import Accounts, Players
|
from accounts.models import Accounts, Players
|
||||||
|
from accounts.utils import LoginHelper
|
||||||
|
|
||||||
|
|
||||||
class RegisterAccountView(CreateView):
|
class RegisterAccountView(CreateView):
|
||||||
|
@ -11,17 +13,21 @@ class RegisterAccountView(CreateView):
|
||||||
template_name = 'register_account.html'
|
template_name = 'register_account.html'
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
import hashlib
|
|
||||||
self.object = form.save(commit=False)
|
self.object = form.save(commit=False)
|
||||||
hashed_password = hashlib.sha1(self.object.password.encode('utf-8')).hexdigest()
|
hashed_password = LoginHelper.hash_password(self.object.password)
|
||||||
self.object.password = hashed_password
|
self.object.password = hashed_password
|
||||||
self.object.save()
|
self.object.save()
|
||||||
|
|
||||||
return render_to_response(self.template_name, self.get_context_data())
|
return render_to_response(self.template_name, self.get_context_data())
|
||||||
|
|
||||||
|
class LoginView(FormView):
|
||||||
|
template_name = 'login.html'
|
||||||
|
form_class = LoginForm
|
||||||
|
success_url = '/loginarea/'
|
||||||
|
|
||||||
class IndexView(generic.ListView):
|
class IndexView(generic.ListView):
|
||||||
template_name = 'index.html'
|
template_name = 'index.html'
|
||||||
context_object_name = 'best_players'
|
context_object_name = 'best_players'
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Players.objects.order_by('-level')[:10]
|
return Players.objects.filter(group_id=1).order_by('-level')[:10]
|
Loading…
Reference in New Issue