Initial commit
This commit is contained in:
0
account/__init__.py
Normal file
0
account/__init__.py
Normal file
5
account/admin.py
Normal file
5
account/admin.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.contrib import admin
|
||||
from .models import *
|
||||
|
||||
|
||||
admin.site.register(SiteUser)
|
6
account/apps.py
Normal file
6
account/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'account'
|
10
account/forms.py
Normal file
10
account/forms.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django import forms
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
from .models import SiteUser
|
||||
|
||||
|
||||
class SiteUserForm(UserCreationForm):
|
||||
class Meta(UserCreationForm.Meta):
|
||||
model = SiteUser
|
||||
fields = ('name', 'surname', 'email', 'phone')
|
||||
error_css_class = 'error'
|
33
account/migrations/0001_initial.py
Normal file
33
account/migrations/0001_initial.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# Generated by Django 4.1.1 on 2022-09-11 21:55
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='SiteUser',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
||||
('surname', models.CharField(max_length=60)),
|
||||
('name', models.CharField(max_length=60)),
|
||||
('email', models.EmailField(max_length=254, unique=True)),
|
||||
('phone', models.CharField(max_length=16, unique=True)),
|
||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
|
||||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
18
account/migrations/0002_siteuser_is_staff.py
Normal file
18
account/migrations/0002_siteuser_is_staff.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.1.1 on 2022-09-11 22:00
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='siteuser',
|
||||
name='is_staff',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
0
account/migrations/__init__.py
Normal file
0
account/migrations/__init__.py
Normal file
57
account/models.py
Normal file
57
account/models.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import PermissionsMixin, AbstractBaseUser, BaseUserManager
|
||||
|
||||
|
||||
class SiteAccountManager(BaseUserManager):
|
||||
def create_user(self, email, name, surname, phone, password):
|
||||
user = self.model(email=email, name=name, surname=surname, phone=phone, password=password)
|
||||
user.set_password(password)
|
||||
user.is_staff = False
|
||||
user.is_superuser = False
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
def create_superuser(self, email, name, surname, phone, password):
|
||||
user = self.create_user(email=email, name=name, surname=surname, phone=phone, password=password)
|
||||
user.is_active = True
|
||||
user.is_staff = True
|
||||
user.is_superuser = True
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
def get_by_natural_key(self, email_):
|
||||
print(email_)
|
||||
return self.get(email=email_)
|
||||
|
||||
|
||||
class SiteUser(AbstractBaseUser, PermissionsMixin):
|
||||
"""
|
||||
Here we are subclassing the Django AbstractBaseUser, which comes with only
|
||||
3 fields:
|
||||
1 - password
|
||||
2 - last_login
|
||||
3 - is_active
|
||||
Note than all fields would be required unless specified otherwise, with
|
||||
`required=False` in the parentheses.
|
||||
The PermissionsMixin is a model that helps you implement permission settings
|
||||
as-is or modified to your requirements.
|
||||
More info: https://goo.gl/YNL2ax
|
||||
"""
|
||||
surname = models.CharField(max_length=60, verbose_name="Фамилия")
|
||||
name = models.CharField(max_length=60, verbose_name="Имя")
|
||||
email = models.EmailField(unique=True, verbose_name="Email")
|
||||
phone = models.CharField(unique=True, max_length=16, verbose_name="Телефон")
|
||||
is_staff = models.BooleanField(default=False, verbose_name="Разрешение на вход в админку")
|
||||
REQUIRED_FIELDS = ['name', 'surname', 'phone']
|
||||
USERNAME_FIELD = 'email'
|
||||
|
||||
objects = SiteAccountManager()
|
||||
|
||||
def get_short_name(self):
|
||||
return self.email
|
||||
|
||||
def natural_key(self):
|
||||
return self.email
|
||||
|
||||
def __str__(self):
|
||||
return self.email
|
3
account/tests.py
Normal file
3
account/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
29
account/urls.py
Normal file
29
account/urls.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""stall URL Configuration
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/3.2/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='account'),
|
||||
# path('account', views.account, name='account'),
|
||||
# path('account_<str:action>', views.account_action, name='account_action'),
|
||||
#
|
||||
# path('catalog/', views.catalog, name='catalog'),
|
||||
# path('catalog/<int:product_id>/', views.product_view, name='product_view'),
|
||||
# path('cart', views.cart, name='cart'),
|
||||
]
|
||||
|
21
account/views.py
Normal file
21
account/views.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
from .forms import SiteUserForm
|
||||
|
||||
|
||||
def index(request):
|
||||
if request.method == 'POST':
|
||||
form = SiteUserForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return HttpResponse("User was created successfully.")
|
||||
else:
|
||||
return HttpResponse("There was an error.")
|
||||
else:
|
||||
form = SiteUserForm()
|
||||
|
||||
return render(request, 'account.html', {'form': form})
|
||||
|
||||
|
||||
# def index(request):
|
||||
# return render(request, 'account.html')
|
Reference in New Issue
Block a user