Skip to content

Commit 4696eb9

Browse files
author
Antar Chandra Nath
committed
complate authentication
0 parents  commit 4696eb9

22 files changed

+962
-0
lines changed

.gitignore

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

DjangoAuthApi.postman_collection.json

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"info": {
3+
"_postman_id": "1f741200-fdaf-4067-9cbe-2b2bd7b5b1b7",
4+
"name": "DjangoAuthApi",
5+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
6+
"_exporter_id": "28177439"
7+
},
8+
"item": [
9+
{
10+
"name": "UserRegistration",
11+
"request": {
12+
"method": "GET",
13+
"header": []
14+
},
15+
"response": []
16+
},
17+
{
18+
"name": "UserLogin",
19+
"request": {
20+
"method": "GET",
21+
"header": []
22+
},
23+
"response": []
24+
},
25+
{
26+
"name": "UserProfile",
27+
"request": {
28+
"method": "GET",
29+
"header": []
30+
},
31+
"response": []
32+
},
33+
{
34+
"name": "UserChangePassword",
35+
"request": {
36+
"method": "GET",
37+
"header": []
38+
},
39+
"response": []
40+
},
41+
{
42+
"name": "UserResetPasswordWithActivationLink",
43+
"request": {
44+
"method": "GET",
45+
"header": []
46+
},
47+
"response": []
48+
},
49+
{
50+
"name": "ResetPassword",
51+
"request": {
52+
"method": "GET",
53+
"header": []
54+
},
55+
"response": []
56+
}
57+
]
58+
}

accounts/__init__.py

Whitespace-only changes.

accounts/admin.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from django.contrib import admin
2+
from .models import User
3+
from django.contrib.auth.admin import UserAdmin
4+
5+
# Register your models here.
6+
7+
class UserModelAdmin(UserAdmin):
8+
list_display = ['id', "email", "name", 'tc', "is_admin"]
9+
list_filter = ["is_admin"]
10+
fieldsets = [
11+
('User Credentials', {"fields": ["email", "password"]}),
12+
("Personal info", {"fields": ["name", 'tc']}),
13+
("Permissions", {"fields": ["is_admin"]}),
14+
]
15+
16+
add_fieldsets = [
17+
(
18+
None,
19+
{
20+
"classes": ["wide"],
21+
"fields": ["email", "name", 'tc', "password1", "password2"],
22+
},
23+
),
24+
]
25+
search_fields = ["email"]
26+
ordering = ["email"]
27+
filter_horizontal = []
28+
29+
30+
# Now register the new UserAdmin...
31+
admin.site.register(User, UserModelAdmin)

accounts/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountsConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'accounts'

accounts/manager.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django.db import models
2+
from django.contrib.auth.models import BaseUserManager
3+
4+
class UserManager(BaseUserManager):
5+
def create_user(self, email, name, tc, password=None, password2=None):
6+
if not email:
7+
raise ValueError("Users must have an email address")
8+
9+
user = self.model(
10+
email=self.normalize_email(email),
11+
name=name,
12+
tc=tc,
13+
)
14+
15+
user.set_password(password)
16+
user.save(using=self._db)
17+
return user
18+
19+
def create_superuser(self, email, name, tc, password=None):
20+
user = self.create_user(
21+
email,
22+
password=password,
23+
name=name,
24+
tc=tc,
25+
)
26+
user.is_admin = True
27+
user.is_active = True
28+
user.is_superuser = True
29+
user.save(using=self._db)
30+
return user

accounts/migrations/0001_initial.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Generated by Django 4.2.3 on 2023-07-23 17:44
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='User',
16+
fields=[
17+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('password', models.CharField(max_length=128, verbose_name='password')),
19+
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
20+
('email', models.EmailField(max_length=255, unique=True, verbose_name='Email')),
21+
('name', models.CharField(max_length=255)),
22+
('tc', models.BooleanField()),
23+
('is_active', models.BooleanField(default=True)),
24+
('is_admin', models.BooleanField(default=False)),
25+
('created_at', models.DateTimeField(auto_now_add=True)),
26+
('updated_at', models.DateTimeField(auto_now=True)),
27+
],
28+
options={
29+
'abstract': False,
30+
},
31+
),
32+
]

accounts/migrations/__init__.py

Whitespace-only changes.

accounts/models.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from django.db import models
2+
from django.contrib.auth.models import AbstractBaseUser
3+
from .manager import UserManager
4+
5+
# Create your models here.
6+
class User(AbstractBaseUser):
7+
email = models.EmailField(
8+
verbose_name="Email",
9+
max_length=255,
10+
unique=True,
11+
)
12+
name = models.CharField(max_length=255)
13+
tc = models.BooleanField()
14+
is_active = models.BooleanField(default=True)
15+
is_admin = models.BooleanField(default=False)
16+
created_at = models.DateTimeField(auto_now_add=True)
17+
updated_at = models.DateTimeField(auto_now=True)
18+
19+
objects = UserManager()
20+
21+
USERNAME_FIELD = "email"
22+
REQUIRED_FIELDS = ['name', 'tc']
23+
24+
def __str__(self):
25+
return self.email
26+
27+
def has_perm(self, perm, obj=None):
28+
return self.is_admin
29+
30+
def has_module_perms(self, app_label):
31+
return True
32+
33+
@property
34+
def is_staff(self):
35+
return self.is_admin

accounts/renderers.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rest_framework import renderers
2+
import json
3+
4+
class UserRenderer(renderers.JSONRenderer):
5+
charset = 'utf-8'
6+
def render(self, data, accepted_media_type=None, renderer_context=None):
7+
response = ''
8+
if 'ErrorDetails' in str(data):
9+
response = json.dumps({'errors': data})
10+
else:
11+
response = json.dumps(data)
12+
return response

0 commit comments

Comments
 (0)