add link between django user and accounts table

This commit is contained in:
Gesche 2019-01-23 21:19:50 +01:00
parent 5d618dc16e
commit f082f04d01
6 changed files with 23 additions and 23 deletions

View File

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

View File

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

View File

@ -1,17 +1,10 @@
<!DOCTYPE html> {% extends "base_generic.html" %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register Account</title>
</head>
<body>
{% block content %}
<h1>Register</h1>
<form method="post">{% csrf_token %} <form method="post">{% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<input type="submit" value="Save"> <input type="submit" value="Save">
</form> </form>
</body> {% endblock %}
</html>

View File

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

View File

@ -1,3 +1,4 @@
from django.contrib.auth.models import User
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, FormView from django.views.generic.edit import CreateView, FormView
@ -14,17 +15,22 @@ class RegisterAccountView(CreateView):
def form_valid(self, form): def form_valid(self, form):
self.object = form.save(commit=False) self.object = form.save(commit=False)
# create django user
u, created = User.objects.get_or_create(
username=self.object.name,
email=self.object.email,
)
if created:
u.set_password(self.object.password)
u.save()
# create tibia account with sha1 hashed password
hashed_password = LoginHelper.hash_password(self.object.password) hashed_password = LoginHelper.hash_password(self.object.password)
self.object.password = hashed_password self.object.password = hashed_password
self.object.linked_django_user = u
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'

View File

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