Skip to content

Commit 08e198c

Browse files
committed
Initial Commit
0 parents  commit 08e198c

32 files changed

+995
-0
lines changed

.gitignore

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Created by https://www.gitignore.io/api/django
2+
# Edit at https://www.gitignore.io/?templates=django
3+
4+
### Django ###
5+
*.log
6+
*.pot
7+
*.pyc
8+
__pycache__/
9+
local_settings.py
10+
db.sqlite3
11+
db.sqlite3-journal
12+
media
13+
14+
# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/
15+
# in your Git repository. Update and uncomment the following line accordingly.
16+
# <django-project-name>/staticfiles/
17+
18+
### Django.Python Stack ###
19+
# Byte-compiled / optimized / DLL files
20+
*.py[cod]
21+
*$py.class
22+
23+
# C extensions
24+
*.so
25+
26+
# Distribution / packaging
27+
.Python
28+
build/
29+
develop-eggs/
30+
dist/
31+
downloads/
32+
eggs/
33+
.eggs/
34+
lib/
35+
venv/
36+
lib64/
37+
parts/
38+
sdist/
39+
var/
40+
wheels/
41+
pip-wheel-metadata/
42+
share/python-wheels/
43+
*.egg-info/
44+
.installed.cfg
45+
*.egg
46+
MANIFEST
47+
48+
# PyInstaller
49+
# Usually these files are written by a python script from a template
50+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
51+
*.manifest
52+
*.spec
53+
54+
# Installer logs
55+
pip-log.txt
56+
pip-delete-this-directory.txt
57+
58+
# Unit test / coverage reports
59+
htmlcov/
60+
.tox/
61+
.nox/
62+
.coverage
63+
.coverage.*
64+
.cache
65+
nosetests.xml
66+
coverage.xml
67+
*.cover
68+
.hypothesis/
69+
.pytest_cache/
70+
71+
# Translations
72+
*.mo
73+
74+
# Scrapy stuff:
75+
.scrapy
76+
77+
# Sphinx documentation
78+
docs/_build/
79+
80+
# PyBuilder
81+
target/
82+
83+
# pyenv
84+
.python-version
85+
86+
# pipenv
87+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
89+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
90+
# install all needed dependencies.
91+
#Pipfile.lock
92+
93+
# celery beat schedule file
94+
celerybeat-schedule
95+
96+
# SageMath parsed files
97+
*.sage.py
98+
99+
# Spyder project settings
100+
.spyderproject
101+
.spyproject
102+
103+
# Rope project settings
104+
.ropeproject
105+
106+
# Mr Developer
107+
.mr.developer.cfg
108+
.project
109+
.pydevproject
110+
111+
# mkdocs documentation
112+
/site
113+
114+
# mypy
115+
.mypy_cache/
116+
.dmypy.json
117+
dmypy.json
118+
119+
# Pyre type checker
120+
.pyre/
121+
122+
# End of https://www.gitignore.io/api/django

QuizzBizz/__init__.py

Whitespace-only changes.

QuizzBizz/settings.py

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""
2+
Django settings for QuizzBizz project.
3+
4+
Generated by 'django-admin startproject' using Django 2.1.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.1/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.1/ref/settings/
11+
"""
12+
13+
import os
14+
15+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '99&yl(&(ft6ic5fszvm6$4i*mp16b$zeg^e^y^k%l^q)fzy)=m'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
AUTH_USER_MODEL = 'accounts.User'
31+
32+
# Application definition
33+
34+
INSTALLED_APPS = [
35+
'django.contrib.admin',
36+
'django.contrib.auth',
37+
'django.contrib.contenttypes',
38+
'django.contrib.sessions',
39+
'django.contrib.messages',
40+
'django.contrib.staticfiles',
41+
'knox',
42+
'nested_admin',
43+
'accounts',
44+
'quiz'
45+
]
46+
47+
MIDDLEWARE = [
48+
'django.middleware.security.SecurityMiddleware',
49+
'django.contrib.sessions.middleware.SessionMiddleware',
50+
'django.middleware.common.CommonMiddleware',
51+
'django.middleware.csrf.CsrfViewMiddleware',
52+
'django.contrib.auth.middleware.AuthenticationMiddleware',
53+
'django.contrib.messages.middleware.MessageMiddleware',
54+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
55+
]
56+
57+
REST_FRAMEWORK = {
58+
'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',),
59+
'UNICODE_JSON': False
60+
}
61+
62+
ROOT_URLCONF = 'QuizzBizz.urls'
63+
64+
TEMPLATES = [
65+
{
66+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
67+
'DIRS': [],
68+
'APP_DIRS': True,
69+
'OPTIONS': {
70+
'context_processors': [
71+
'django.template.context_processors.debug',
72+
'django.template.context_processors.request',
73+
'django.contrib.auth.context_processors.auth',
74+
'django.contrib.messages.context_processors.messages',
75+
],
76+
},
77+
},
78+
]
79+
80+
WSGI_APPLICATION = 'QuizzBizz.wsgi.application'
81+
82+
83+
# Database
84+
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
85+
86+
DATABASES = {
87+
'default': {
88+
'ENGINE': 'django.db.backends.sqlite3',
89+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
90+
}
91+
}
92+
93+
94+
# Password validation
95+
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
96+
97+
AUTH_PASSWORD_VALIDATORS = [
98+
{
99+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
100+
},
101+
{
102+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
103+
},
104+
{
105+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
106+
},
107+
{
108+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
109+
},
110+
]
111+
112+
113+
# Internationalization
114+
# https://docs.djangoproject.com/en/2.1/topics/i18n/
115+
116+
LANGUAGE_CODE = 'en-us'
117+
118+
TIME_ZONE = 'UTC'
119+
120+
USE_I18N = True
121+
122+
USE_L10N = True
123+
124+
USE_TZ = True
125+
126+
127+
# Static files (CSS, JavaScript, Images)
128+
# https://docs.djangoproject.com/en/2.1/howto/static-files/
129+
130+
STATIC_URL = '/static/'

QuizzBizz/urls.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.contrib import admin
2+
from django.urls import path, include
3+
4+
urlpatterns = [
5+
path('admin/', admin.site.urls),
6+
path('api/', include('quiz.urls')),
7+
path('api/auth/', include('accounts.urls')),
8+
path('nested_admin', include('nested_admin.urls')),
9+
]

QuizzBizz/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for QuizzBizz project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'QuizzBizz.settings')
15+
16+
application = get_wsgi_application()

accounts/__init__.py

Whitespace-only changes.

accounts/admin.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from accounts.forms import UserCreationForm, UserChangeForm
2+
from accounts.models import User
3+
from django.contrib import admin
4+
from django.contrib.auth.admin import UserAdmin
5+
6+
7+
class UserAdmin(UserAdmin):
8+
add_form = UserCreationForm
9+
form = UserChangeForm
10+
model = User
11+
list_display = ('username', 'name', 'email', 'is_active')
12+
list_filter = ('username', 'name', 'email', 'is_active')
13+
fieldsets = (
14+
(None, {'fields': ('name', 'username', 'email', 'password')}),
15+
('Permissions', {'fields': ('is_staff', 'is_active')})
16+
)
17+
search_fields = ('username',)
18+
ordering = ('username',)
19+
20+
21+
admin.site.register(User, UserAdmin)
22+

accounts/api.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from accounts.models import User
2+
from accounts.serializers import LoginSerializer, RegisterSerializer, UserSerializer
3+
from knox.models import AuthToken
4+
from rest_framework import generics, permissions
5+
from rest_framework.response import Response
6+
7+
8+
class LoginAPI(generics.GenericAPIView):
9+
serializer_class = LoginSerializer
10+
11+
def post(self, request, *args, **kwargs):
12+
serializer = self.get_serializer(data=request.data)
13+
serializer.is_valid(raise_exception=True)
14+
user = serializer.validated_data
15+
16+
return Response({
17+
'user': UserSerializer(user).data,
18+
'token': AuthToken.objects.create(user)[1]
19+
})
20+
21+
22+
class RegisterAPI(generics.GenericAPIView):
23+
serializer_class = RegisterSerializer
24+
25+
def post(self, request, *args, **kwargs):
26+
serializer = self.get_serializer(data=request.data)
27+
serializer.is_valid(raise_exception=True)
28+
user = serializer.save()
29+
30+
return Response({
31+
'user': UserSerializer(user).data,
32+
'token': AuthToken.objects.create(user)[1]
33+
})
34+
35+
36+
class UserAPI(generics.RetrieveAPIView):
37+
permission_classes = [
38+
permissions.IsAuthenticated
39+
]
40+
serializer_class = UserSerializer
41+
42+
def get_object(self):
43+
self.request.user

accounts/apps.py

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

accounts/forms.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from accounts.models import User
2+
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
3+
4+
5+
class UserCreationForm(UserCreationForm):
6+
class Meta(UserCreationForm):
7+
model = User
8+
fields = ('username',)
9+
10+
11+
class UserChangeForm(UserChangeForm):
12+
class meta:
13+
model = User
14+
fields = ('email', 'username')

accounts/managers.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.contrib.auth.base_user import BaseUserManager
2+
from django.utils.translation import ugettext_lazy as _
3+
4+
5+
class CustomUserManager(BaseUserManager):
6+
def _create_user(self, name, username, email, password, **extra_fields):
7+
user = self.model(name=name, username=username, email=self.normalize_email(email), **extra_fields)
8+
user.set_password(password)
9+
user.full_clean()
10+
user.save()
11+
return user
12+
13+
def create_user(self, name, username, email, password, **extra_fields):
14+
extra_fields.setdefault('is_staff', False)
15+
extra_fields.setdefault('is_superuser', False)
16+
return self._create_user(name, username, email, password, **extra_fields)
17+
18+
def create_superuser(self, name, username, email, password, **extra_fields):
19+
extra_fields.setdefault('is_staff', True)
20+
extra_fields.setdefault('is_superuser', True)
21+
22+
if extra_fields.get('is_staff') is not True:
23+
raise ValueError('Superuser must have staff priviledges')
24+
return self._create_user(name, username, email, password, **extra_fields)
25+
26+

0 commit comments

Comments
 (0)