Browse Source

add link between django user and accounts table

Gesche 10 months ago
parent
commit
f082f04d01

+ 3
- 1
accounts/models.py View File

@@ -5,6 +5,7 @@
5 5
 #   * Make sure each ForeignKey has `on_delete` set to the desired behavior.
6 6
 #   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
7 7
 # Feel free to rename the models, but don't rename db_table values or field names.
8
+from django.contrib.auth.models import User
8 9
 from django.db import models
9 10
 
10 11
 
@@ -54,9 +55,10 @@ class Accounts(models.Model):
54 55
     lastday = models.PositiveIntegerField(default=0)
55 56
     email = models.CharField(max_length=255)
56 57
     creation = models.IntegerField(default=0)
58
+    linked_django_user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
57 59
 
58 60
     class Meta:
59
-        managed = False
61
+        managed = True
60 62
         db_table = 'accounts'
61 63
 
62 64
 

+ 1
- 1
accounts/templates/base_generic.html View File

@@ -3,7 +3,7 @@
3 3
 <head>
4 4
   {% block title %}<title>Ambitious Server</title>{% endblock %}
5 5
   <meta name="viewport" content="width=device-width, initial-scale=1">
6
-  <!--link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"-->
6
+  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
7 7
   <!-- Add additional CSS in static file -->
8 8
   {% load static %}
9 9
   <link rel="stylesheet" href="{% static 'css/styles.css' %}">

+ 4
- 11
accounts/templates/register_account.html View File

@@ -1,17 +1,10 @@
1
-<!DOCTYPE html>
2
-<html lang="en">
3
-<head>
4
-    <meta charset="UTF-8">
5
-    <title>Register Account</title>
6
-
7
-
8
-</head>
9
-<body>
1
+{% extends "base_generic.html" %}
10 2
 
3
+{% block content %}
4
+<h1>Register</h1>
11 5
     <form method="post">{% csrf_token %}
12 6
     {{ form.as_p }}
13 7
     <input type="submit" value="Save">
14 8
 </form>
15 9
 
16
-</body>
17
-</html>
10
+{% endblock %}

+ 0
- 1
accounts/urls.py View File

@@ -8,5 +8,4 @@ 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'),
12 11
 ]

+ 14
- 8
accounts/views.py View File

@@ -1,3 +1,4 @@
1
+from django.contrib.auth.models import User
1 2
 from django.shortcuts import render, render_to_response
2 3
 from django.views import generic
3 4
 from django.views.generic.edit import CreateView, FormView
@@ -14,17 +15,22 @@ class RegisterAccountView(CreateView):
14 15
 
15 16
     def form_valid(self, form):
16 17
         self.object = form.save(commit=False)
17
-        hashed_password = LoginHelper.hash_password(self.object.password)
18
-        self.object.password = hashed_password
19
-        self.object.save()
20 18
 
19
+        # create django user
20
+        u, created = User.objects.get_or_create(
21
+            username=self.object.name,
22
+            email=self.object.email,
23
+        )
24
+        if created:
25
+            u.set_password(self.object.password)
26
+            u.save()
27
+            # create tibia account with sha1 hashed password
28
+            hashed_password = LoginHelper.hash_password(self.object.password)
29
+            self.object.password = hashed_password
30
+            self.object.linked_django_user = u
31
+            self.object.save()
21 32
         return render_to_response(self.template_name, self.get_context_data())
22 33
 
23
-class LoginView(FormView):
24
-    template_name = 'login.html'
25
-    form_class = LoginForm
26
-    success_url = '/loginarea/'
27
-
28 34
 class IndexView(generic.ListView):
29 35
     template_name = 'index.html'
30 36
     context_object_name = 'best_players'

+ 1
- 1
tibia_website/settings.py View File

@@ -125,5 +125,5 @@ USE_TZ = True
125 125
 STATIC_URL = '/static/'
126 126
 
127 127
 # Redirect to home URL after login (Default redirects to /accounts/profile/)
128
-LOGIN_REDIRECT_URL = '/'
128
+LOGIN_REDIRECT_URL = '/accounts/'
129 129
 

Loading…
Cancel
Save