add link between django user and accounts table
This commit is contained in:
parent
5d618dc16e
commit
f082f04d01
|
@ -5,6 +5,7 @@
|
|||
# * 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
|
||||
# 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
|
||||
|
||||
|
||||
|
@ -54,9 +55,10 @@ class Accounts(models.Model):
|
|||
lastday = models.PositiveIntegerField(default=0)
|
||||
email = models.CharField(max_length=255)
|
||||
creation = models.IntegerField(default=0)
|
||||
linked_django_user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
managed = True
|
||||
db_table = 'accounts'
|
||||
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
{% block title %}<title>Ambitious Server</title>{% endblock %}
|
||||
<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 -->
|
||||
{% load static %}
|
||||
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
|
||||
|
|
|
@ -1,17 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Register Account</title>
|
||||
|
||||
|
||||
</head>
|
||||
<body>
|
||||
{% extends "base_generic.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Register</h1>
|
||||
<form method="post">{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<input type="submit" value="Save">
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
{% endblock %}
|
|
@ -8,5 +8,4 @@ app_name = 'accounts'
|
|||
urlpatterns = [
|
||||
path('', views.IndexView.as_view(), name='index'),
|
||||
path('register/', views.RegisterAccountView.as_view(), name='register'),
|
||||
path('login/', views.LoginView.as_view(), name='login'),
|
||||
]
|
|
@ -1,3 +1,4 @@
|
|||
from django.contrib.auth.models import User
|
||||
from django.shortcuts import render, render_to_response
|
||||
from django.views import generic
|
||||
from django.views.generic.edit import CreateView, FormView
|
||||
|
@ -14,17 +15,22 @@ class RegisterAccountView(CreateView):
|
|||
|
||||
def form_valid(self, form):
|
||||
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)
|
||||
self.object.password = hashed_password
|
||||
self.object.linked_django_user = u
|
||||
self.object.save()
|
||||
|
||||
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):
|
||||
template_name = 'index.html'
|
||||
context_object_name = 'best_players'
|
||||
|
|
|
@ -125,5 +125,5 @@ USE_TZ = True
|
|||
STATIC_URL = '/static/'
|
||||
|
||||
# Redirect to home URL after login (Default redirects to /accounts/profile/)
|
||||
LOGIN_REDIRECT_URL = '/'
|
||||
LOGIN_REDIRECT_URL = '/accounts/'
|
||||
|
||||
|
|
Loading…
Reference in New Issue