From a3e2c2f6313d8c47fc99cadfe9370373470369e4 Mon Sep 17 00:00:00 2001 From: starnakin Date: Tue, 24 Oct 2023 16:31:26 +0200 Subject: [PATCH] backend: add: register --- .../accounts/templates/register.html | 8 +++++++ django/trancendence/accounts/urls.py | 2 +- django/trancendence/accounts/views.py | 24 +++++++++++++++++-- django/trancendence/trancendence/settings.py | 4 ++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 django/trancendence/accounts/templates/register.html diff --git a/django/trancendence/accounts/templates/register.html b/django/trancendence/accounts/templates/register.html new file mode 100644 index 0000000..2d0a7d9 --- /dev/null +++ b/django/trancendence/accounts/templates/register.html @@ -0,0 +1,8 @@ + +
+ {% csrf_token %} + + + +
+ \ No newline at end of file diff --git a/django/trancendence/accounts/urls.py b/django/trancendence/accounts/urls.py index 600253b..6729508 100644 --- a/django/trancendence/accounts/urls.py +++ b/django/trancendence/accounts/urls.py @@ -4,6 +4,6 @@ from . import views urlpatterns = [ path("login", views.Login.as_view(), name="login"), - path("create", views.Create.as_view(), name="create"), + path("register", views.Register.as_view(), name="register"), path("delete", views.Delete.as_view(), name="delete"), ] \ No newline at end of file diff --git a/django/trancendence/accounts/views.py b/django/trancendence/accounts/views.py index b5f336d..b65efb9 100644 --- a/django/trancendence/accounts/views.py +++ b/django/trancendence/accounts/views.py @@ -2,11 +2,31 @@ from django.shortcuts import render from django.views import View # Create your views here. +from django.http import HttpResponse +from django.contrib.auth.models import User + class Login(View): pass -class Create(View): - pass +class Register(View): + def get(self, request): + return render(request, "register.html") + + def post(self, request): + password = request.POST.get("password") + if (password == None or len(password) < 3): + return HttpResponse("error: password invalid") + username = request.POST.get("username") + if (username == None or len(username) < 3): + return HttpResponse("error: password invalid") + + if User.objects.filter(username=username).exists(): + return HttpResponse("error: username already used") + + user = User.objects.create_user(username, password=password) + user.save() + + return HttpResponse("ok: user added") class Delete(View): pass \ No newline at end of file diff --git a/django/trancendence/trancendence/settings.py b/django/trancendence/trancendence/settings.py index 1890027..4d306fa 100644 --- a/django/trancendence/trancendence/settings.py +++ b/django/trancendence/trancendence/settings.py @@ -25,12 +25,16 @@ SECRET_KEY = 'django-insecure-18!@88-wm-!skec9^n-85n(f$my^#mh3!#@f=_e@=*arh_yyjj # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True +CSRF_TRUSTED_ORIGINS = ['https://code.chauvet.pro'] + ALLOWED_HOSTS = ["*"] # Application definition INSTALLED_APPS = [ + 'accounts.apps.AccountsConfig', + 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes',