Skip to content

Commit

Permalink
Complete user endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhmdHsn313 committed Oct 1, 2020
2 parents fe9c88e + bf9f481 commit 47a9b5f
Show file tree
Hide file tree
Showing 33 changed files with 989 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Django
migrations/
27 changes: 27 additions & 0 deletions app/.idea/app.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions app/.idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/.idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions app/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions app/app/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

UserModel = get_user_model()


class EmailOrUsernameLogin(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
if '@' in username:
args = {'email': username}
else:
args = {'username': username}

if username is None or password is None:
return

try:
user = UserModel.objects.get(**args)
except UserModel.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a nonexistent user (#20760).
UserModel().set_password(password)
else:
if user.check_password(password) and \
self.user_can_authenticate(user):
return user
25 changes: 16 additions & 9 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

import os
from pathlib import Path

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


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

Expand All @@ -27,7 +26,6 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
Expand All @@ -37,6 +35,10 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'core',
'user',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -69,18 +71,19 @@

WSGI_APPLICATION = 'app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
}
}


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

Expand All @@ -99,7 +102,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand All @@ -113,8 +115,13 @@

USE_TZ = True


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

STATIC_URL = '/static/'

AUTH_USER_MODEL = 'core.User'

AUTHENTICATION_BACKENDS = [
'app.backends.EmailOrUsernameLogin',
]
3 changes: 2 additions & 1 deletion app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('api/user/', include('user.urls')),
]
Empty file added app/core/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext as _

from core import models


class UserModel(BaseUserAdmin):
"""User admin class."""
ordering = ['username']
list_display = ['email', 'username',
'first_name', 'last_name', 'gender', 'stage', 'bio']
fieldsets = (
(
_('Personal Information'),
{
'fields': ('first_name', 'last_name', 'stage', 'bio')
}
),
(
_('Contact Information'),
{
'fields': ('username', 'email')
},
),
(
_('Permissions'),
{
'fields': ('is_active', 'is_staff', 'is_superuser')
}
),
(
_('Important Information'),
{
'fields': ('id', 'last_login')
}
)
)

add_fieldsets = (
(
_('Person Information'),
{
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'stage', 'bio')
}
),
(
_('Account Information'),
{
'classes': ('wide',),
'fields': ('username', 'email', 'password1', 'password2')
}
)
)


admin.site.register(models.User)
5 changes: 5 additions & 0 deletions app/core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CoreConfig(AppConfig):
name = 'core'
Empty file added app/core/management/__init__.py
Empty file.
Empty file.
Loading

0 comments on commit 47a9b5f

Please sign in to comment.