Skip to content

Commit 47a9b5f

Browse files
committed
Complete user endpoint.
2 parents fe9c88e + bf9f481 commit 47a9b5f

33 files changed

+989
-10
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# Django
132+
migrations/

app/.idea/app.iml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/inspectionProfiles/Project_Default.xml

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.idea/workspace.xml

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/app/backends.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.contrib.auth import get_user_model
2+
from django.contrib.auth.backends import ModelBackend
3+
4+
UserModel = get_user_model()
5+
6+
7+
class EmailOrUsernameLogin(ModelBackend):
8+
def authenticate(self, request, username=None, password=None, **kwargs):
9+
if '@' in username:
10+
args = {'email': username}
11+
else:
12+
args = {'username': username}
13+
14+
if username is None or password is None:
15+
return
16+
17+
try:
18+
user = UserModel.objects.get(**args)
19+
except UserModel.DoesNotExist:
20+
# Run the default password hasher once to reduce the timing
21+
# difference between an existing and a nonexistent user (#20760).
22+
UserModel().set_password(password)
23+
else:
24+
if user.check_password(password) and \
25+
self.user_can_authenticate(user):
26+
return user

app/app/settings.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
For the full list of settings and their values, see
1010
https://docs.djangoproject.com/en/3.1/ref/settings/
1111
"""
12-
12+
import os
1313
from pathlib import Path
1414

1515
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1616
BASE_DIR = Path(__file__).resolve().parent.parent
1717

18-
1918
# Quick-start development settings - unsuitable for production
2019
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
2120

@@ -27,7 +26,6 @@
2726

2827
ALLOWED_HOSTS = []
2928

30-
3129
# Application definition
3230

3331
INSTALLED_APPS = [
@@ -37,6 +35,10 @@
3735
'django.contrib.sessions',
3836
'django.contrib.messages',
3937
'django.contrib.staticfiles',
38+
'rest_framework',
39+
'rest_framework.authtoken',
40+
'core',
41+
'user',
4042
]
4143

4244
MIDDLEWARE = [
@@ -69,18 +71,19 @@
6971

7072
WSGI_APPLICATION = 'app.wsgi.application'
7173

72-
7374
# Database
7475
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
7576

7677
DATABASES = {
7778
'default': {
78-
'ENGINE': 'django.db.backends.sqlite3',
79-
'NAME': BASE_DIR / 'db.sqlite3',
79+
'ENGINE': 'django.db.backends.postgresql',
80+
'HOST': os.environ.get('DB_HOST'),
81+
'NAME': os.environ.get('DB_NAME'),
82+
'USER': os.environ.get('DB_USER'),
83+
'PASSWORD': os.environ.get('DB_PASS'),
8084
}
8185
}
8286

83-
8487
# Password validation
8588
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
8689

@@ -99,7 +102,6 @@
99102
},
100103
]
101104

102-
103105
# Internationalization
104106
# https://docs.djangoproject.com/en/3.1/topics/i18n/
105107

@@ -113,8 +115,13 @@
113115

114116
USE_TZ = True
115117

116-
117118
# Static files (CSS, JavaScript, Images)
118119
# https://docs.djangoproject.com/en/3.1/howto/static-files/
119120

120121
STATIC_URL = '/static/'
122+
123+
AUTH_USER_MODEL = 'core.User'
124+
125+
AUTHENTICATION_BACKENDS = [
126+
'app.backends.EmailOrUsernameLogin',
127+
]

app/app/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
1616
from django.contrib import admin
17-
from django.urls import path
17+
from django.urls import path, include
1818

1919
urlpatterns = [
2020
path('admin/', admin.site.urls),
21+
path('api/user/', include('user.urls')),
2122
]

app/core/__init__.py

Whitespace-only changes.

app/core/admin.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from django.contrib import admin
2+
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
3+
from django.utils.translation import gettext as _
4+
5+
from core import models
6+
7+
8+
class UserModel(BaseUserAdmin):
9+
"""User admin class."""
10+
ordering = ['username']
11+
list_display = ['email', 'username',
12+
'first_name', 'last_name', 'gender', 'stage', 'bio']
13+
fieldsets = (
14+
(
15+
_('Personal Information'),
16+
{
17+
'fields': ('first_name', 'last_name', 'stage', 'bio')
18+
}
19+
),
20+
(
21+
_('Contact Information'),
22+
{
23+
'fields': ('username', 'email')
24+
},
25+
),
26+
(
27+
_('Permissions'),
28+
{
29+
'fields': ('is_active', 'is_staff', 'is_superuser')
30+
}
31+
),
32+
(
33+
_('Important Information'),
34+
{
35+
'fields': ('id', 'last_login')
36+
}
37+
)
38+
)
39+
40+
add_fieldsets = (
41+
(
42+
_('Person Information'),
43+
{
44+
'classes': ('wide',),
45+
'fields': ('first_name', 'last_name', 'stage', 'bio')
46+
}
47+
),
48+
(
49+
_('Account Information'),
50+
{
51+
'classes': ('wide',),
52+
'fields': ('username', 'email', 'password1', 'password2')
53+
}
54+
)
55+
)
56+
57+
58+
admin.site.register(models.User)

app/core/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CoreConfig(AppConfig):
5+
name = 'core'

app/core/management/__init__.py

Whitespace-only changes.

app/core/management/commands/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)