Browse Source

add index and login page (not yet good)

Gesche 10 months ago
parent
commit
cd8e918890
6 changed files with 70 additions and 8 deletions
  1. 20
    0
      accounts/forms.py
  2. 13
    4
      accounts/templates/index.html
  3. 17
    0
      accounts/templates/login.html
  4. 1
    0
      accounts/urls.py
  5. 9
    0
      accounts/utils.py
  6. 10
    4
      accounts/views.py

+ 20
- 0
accounts/forms.py View File

@@ -0,0 +1,20 @@
1
+from django import forms
2
+
3
+from accounts.models import Accounts
4
+from accounts.utils import LoginHelper
5
+
6
+
7
+class LoginForm(forms.Form):
8
+    account_name = forms.CharField()
9
+    password = forms.CharField(widget=forms.PasswordInput)
10
+
11
+    def is_valid(self):
12
+        if not super(LoginForm, self).is_valid():
13
+            return False
14
+
15
+        account = Accounts.objects.get(name=self.cleaned_data['account_name'])
16
+        hashed_password = LoginHelper.hash_password(self.cleaned_data['password'])
17
+        if account.password == hashed_password:
18
+            return True
19
+        else:
20
+            return False

+ 13
- 4
accounts/templates/index.html View File

@@ -5,18 +5,27 @@
5 5
     <title>Highscore</title>
6 6
 </head>
7 7
 <body>
8
+<h1>Highscore</h1>
8 9
 
9 10
 {% if best_players %}
10
-    <ul>
11
-    {% for player in best_players %}
12
-        <li>{{ player.name }} - {{ player.level }}</li>
11
+    <table style="width:50%">
12
+    <tr>
13
+        <th style="text-align: center;">Name</th>
14
+        <th style="text-align: center;">Level</th>
15
+    </tr>
16
+        {% for player in best_players %}
17
+            <tr>
18
+                <td>{{ player.name }}</td>
19
+                <td style="text-align: right; width: 10%">{{ player.level }}</td>
20
+            </tr>
13 21
     {% endfor %}
14
-    </ul>
22
+    </table>
15 23
 {% else %}
16 24
     <p>No players registered.</p>
17 25
 {% endif %}
18 26
 
19 27
 <a href="{% url 'accounts:register' %}">Register now!</a>
28
+<a href="{% url 'accounts:login' %}">Login</a>
20 29
 
21 30
 </body>
22 31
 </html>

+ 17
- 0
accounts/templates/login.html View File

@@ -0,0 +1,17 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<head>
4
+    <meta charset="UTF-8">
5
+    <title>Login</title>
6
+</head>
7
+<body>
8
+<h1>Login</h1>
9
+
10
+<form method="post">
11
+  {% csrf_token %}
12
+  {{ form.as_p }}
13
+  <button type="submit">Login</button>
14
+</form>
15
+
16
+</body>
17
+</html>

+ 1
- 0
accounts/urls.py View File

@@ -8,4 +8,5 @@ app_name = 'accounts'
8 8
 urlpatterns = [
9 9
     path('', views.IndexView.as_view(), name='index'),
10 10
     path('register/', views.RegisterAccountView.as_view(), name='register'),
11
+    path('login/', views.LoginView.as_view(), name='login'),
11 12
 ]

+ 9
- 0
accounts/utils.py View File

@@ -0,0 +1,9 @@
1
+import hashlib
2
+
3
+class LoginHelper(object):
4
+
5
+    @staticmethod
6
+    def hash_password(password):
7
+        hashed_password = hashlib.sha1(password.encode('utf-8'))\
8
+            .hexdigest()
9
+        return hashed_password

+ 10
- 4
accounts/views.py View File

@@ -1,8 +1,10 @@
1 1
 from django.shortcuts import render, render_to_response
2 2
 from django.views import generic
3
-from django.views.generic.edit import CreateView
3
+from django.views.generic.edit import CreateView, FormView
4 4
 # Create your views here.
5
+from accounts.forms import LoginForm
5 6
 from accounts.models import Accounts, Players
7
+from accounts.utils import LoginHelper
6 8
 
7 9
 
8 10
 class RegisterAccountView(CreateView):
@@ -11,17 +13,21 @@ class RegisterAccountView(CreateView):
11 13
     template_name = 'register_account.html'
12 14
 
13 15
     def form_valid(self, form):
14
-        import hashlib
15 16
         self.object = form.save(commit=False)
16
-        hashed_password = hashlib.sha1(self.object.password.encode('utf-8')).hexdigest()
17
+        hashed_password = LoginHelper.hash_password(self.object.password)
17 18
         self.object.password = hashed_password
18 19
         self.object.save()
19 20
 
20 21
         return render_to_response(self.template_name, self.get_context_data())
21 22
 
23
+class LoginView(FormView):
24
+    template_name = 'login.html'
25
+    form_class = LoginForm
26
+    success_url = '/loginarea/'
27
+
22 28
 class IndexView(generic.ListView):
23 29
     template_name = 'index.html'
24 30
     context_object_name = 'best_players'
25 31
 
26 32
     def get_queryset(self):
27
-        return Players.objects.order_by('-level')[:10]
33
+        return Players.objects.filter(group_id=1).order_by('-level')[:10]

Loading…
Cancel
Save