Kaydet (Commit) 981ab284 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

custom user

üst f7d79530
......@@ -123,3 +123,4 @@ STATIC_URL = '/static/'
# Settings
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
AUTH_USER_MODEL = 'social.SocialUser'
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from social.forms import SocialUserCreationForm, SocialUserChangeForm
from social.models import SocialUser
class SocialUserAdmin(UserAdmin):
add_form = SocialUserCreationForm
form = SocialUserChangeForm
model = SocialUser
list_display = ['email', 'username',]
admin.site.register(SocialUser, SocialUserAdmin)
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from social.models import SocialUser
class SocialUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = SocialUser
fields = ('username', 'email')
class SocialUserChangeForm(UserChangeForm):
class Meta:
model = SocialUser
fields = ('username', 'email')
# Generated by Django 2.2.1 on 2019-05-04 15:09
from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0011_update_proxy_permissions'),
]
operations = [
migrations.CreateModel(
name='SocialUser',
fields=[
('id', models.AutoField(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')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('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={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
File mode changed from 100755 to 100644
from django.db import models
from django.conf import settings
from django.contrib.auth.models import AbstractUser
class SocialUser(AbstractUser):
pass
class Post(models.Model):
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
from collections import UserList
from dataclasses import dataclass
from typing import Optional, Sequence
from django.urls import include, path
from social.views import Home, Register
@dataclass(frozen=True, unsafe_hash=True)
class IncludeFilter:
module: str
whitelist: Optional[Sequence] = None
blacklist: Optional[Sequence] = None
def __str__(self):
return self.module
class PatternManager:
"""No list / dict comp, no context manager
no abc, its the simplest way to do a PatternManager """
"""A pattern manager for urlpatterns.
Subclass it with collections.UserList and then
add class variables about your routes.
class XXXPatterns(PatternManager, UserList):
<name> = <path>, <view>
urlpatterns = XXXPatterns()
You dont need to call as_view() for generics. PatternManager
will call them for you.
If you need to include urls from another module define a class variable
called includes (it is reserved for this purpose, you cant use that as a <name>
for normal route).
includes = {<path>: <include>,
<path.n>: <include.n>,
'accounts': 'django.contrib.auth.urls'}
"""
def __init__(self):
self.data = []
patterns = dict(filter(lambda member: not member[0].startswith('_'), vars(self.__class__).items()))
if 'includes' in patterns:
for route, module in patterns.pop('includes').items():
self.data.append(path(route, include(module)))
patterns = dict(
filter(
lambda member: not member[0].startswith("_"),
vars(self.__class__).items(),
)
)
if "includes" in patterns:
for route, module in patterns.pop("includes").items():
included = include(str(module))
if isinstance(module, IncludeFilter):
included = self._filtered_include(module, included)
self.data.append(path(route, included))
for name, route in patterns.items():
route, view = route
if hasattr(view, 'as_view'):
if hasattr(view, "as_view"):
view = view.as_view()
pattern = path(route, view, name=name)
self.data.append(pattern)
def _filtered_include(self, module, included):
urlconf_module, *meta = included
urlpatterns = urlconf_module.urlpatterns
_type = type(urlpatterns)
if module.blacklist:
urlpatterns = filter(
lambda pattern: pattern.name not in module.blacklist,
urlpatterns,
)
if module.whitelist:
urlpatterns = filter(
lambda pattern: pattern.name in module.whitelist,
urlpatterns,
)
urlconf_module.urlpatterns = _type(urlpatterns)
included = urlconf_module, *meta
return included
class SocialPatterns(PatternManager, UserList):
home = '', Home
register = 'accounts/register/', Register
includes = {'accounts': 'django.contrib.auth.urls'}
home = "", Home
register = "accounts/register/", Register
includes = {
"accounts": IncludeFilter(
"django.contrib.auth.urls", whitelist=("login", "logout")
)
}
urlpatterns = SocialPatterns()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment