From 0856ea3c55f3d5aec963cecb2eba5a502f7dbb3f Mon Sep 17 00:00:00 2001 From: sipp11 Date: Wed, 25 Jul 2018 02:19:53 +0900 Subject: [PATCH] Initial commit --- .gitignore | 18 +++ .vscode/settings.json | 22 ++++ README.md | 11 ++ ismp/__init__.py | 0 ismp/settings.py | 190 ++++++++++++++++++++++++++++++ ismp/urls.py | 21 ++++ ismp/wsgi.py | 16 +++ manage.py | 15 +++ people/__init__.py | 0 people/admin.py | 3 + people/apps.py | 5 + people/migrations/0001_initial.py | 56 +++++++++ people/migrations/__init__.py | 0 people/models.py | 34 ++++++ people/tests.py | 3 + people/views.py | 3 + pip-dev.txt | 8 ++ pip-prod.txt | 10 ++ public/static/.gitignore | 1 + 19 files changed, 416 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 README.md create mode 100644 ismp/__init__.py create mode 100644 ismp/settings.py create mode 100644 ismp/urls.py create mode 100644 ismp/wsgi.py create mode 100755 manage.py create mode 100644 people/__init__.py create mode 100644 people/admin.py create mode 100644 people/apps.py create mode 100644 people/migrations/0001_initial.py create mode 100644 people/migrations/__init__.py create mode 100644 people/models.py create mode 100644 people/tests.py create mode 100644 people/views.py create mode 100644 pip-dev.txt create mode 100644 pip-prod.txt create mode 100644 public/static/.gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31729bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +*.pyc +.DS_Store + +settings_prod.py +settings_local.py + +bower_components +node_modules +package-lock.json +*.pip + +*.sublime-workspace +*.db +*.pid + +celerybeat-schedule* +__pycache__ +*.sqlite3 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e91f169 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,22 @@ +{ + "python.pythonPath": "~/.virtualenvs/ismp/bin/python", + "python.formatting.provider": "yapf", + "python.linting.pylintArgs": [ + // "--disable=W0312" + "--disable=E0401", + "--disable=E1101" + ], + "search.exclude": { + ".git": true, + ".eslintcache": true, + "__pycache__": true, + "node_modules": true, + "bower_components": true, + "yarn.lock": true + }, + "files.exclude": { + "**/__pycache__": true, + "**/*.pyc": true, + "**/._*": true + }, +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..293679c --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# ISMP + +Started on Jul 25, 2018. Will finish on? Let's see. + +## Dependencies + +* python3 +* PostgreSQL +* django +* restframework + diff --git a/ismp/__init__.py b/ismp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ismp/settings.py b/ismp/settings.py new file mode 100644 index 0000000..52c0844 --- /dev/null +++ b/ismp/settings.py @@ -0,0 +1,190 @@ +import os +import json +import raven +from datetime import timedelta + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'ou1vb^1o^0!9i@h-0d!89+6nu#wxx=u5b+=2@y@vq+ixfrmq(n' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + 'corsheaders', + 'rest_framework', + 'rest_framework_jwt', + 'django_filters', + 'raven.contrib.django.raven_compat', + + 'people', +] + +AUTH_USER_MODEL = 'people.User' + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'ismp.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'ismp.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.0/topics/i18n/ + +LANGUAGE_CODE = 'en-us' +TIME_ZONE = 'Asia/Bangkok' +USE_I18N = True +USE_L10N = True +USE_TZ = True + +CORS_ORIGIN_ALLOW_ALL = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.0/howto/static-files/ + +STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, 'public', 'static') + + + +# django restframework + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework.authentication.SessionAuthentication', + 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', + ), + 'DEFAULT_RENDERER_CLASSES': ( + # 'rest_framework.renderers.YAMLRenderer', + 'rest_framework.renderers.JSONRenderer', + # 'rest_framework.renderers.BrowsableAPIRenderer', + ), + 'DEFAULT_PERMISSION_CLASSES': ( + 'rest_framework.permissions.IsAuthenticatedOrReadOnly', + ), + 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',), + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', + 'PAGE_SIZE': 15, + 'DEFAULT_THROTTLE_CLASSES': ( + 'rest_framework.throttling.AnonRateThrottle', + 'rest_framework.throttling.UserRateThrottle' + ), + 'DEFAULT_THROTTLE_RATES': { + 'anon': '35/minute', + 'user': '500/minute' + }, + 'DEFAULT_PARSER_CLASSES': ( + 'rest_framework.parsers.JSONParser', + # 'rest_framework.parsers.FormParser', + # 'rest_framework.parsers.MultiPartParser', + ) +} + +JWT_AUTH = { + 'JWT_ENCODE_HANDLER': + 'rest_framework_jwt.utils.jwt_encode_handler', + + 'JWT_DECODE_HANDLER': + 'rest_framework_jwt.utils.jwt_decode_handler', + + 'JWT_PAYLOAD_HANDLER': + 'rest_framework_jwt.utils.jwt_payload_handler', + + 'JWT_PAYLOAD_GET_USER_ID_HANDLER': + 'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler', + + 'JWT_RESPONSE_PAYLOAD_HANDLER': + 'rest_framework_jwt.utils.jwt_response_payload_handler', + + 'JWT_SECRET_KEY': SECRET_KEY, + 'JWT_ALGORITHM': 'HS256', + 'JWT_VERIFY': True, + 'JWT_VERIFY_EXPIRATION': True, + 'JWT_LEEWAY': 0, + + 'JWT_EXPIRATION_DELTA': timedelta(days=14), + 'JWT_AUDIENCE': None, + 'JWT_ISSUER': None, + + 'JWT_ALLOW_REFRESH': True, + 'JWT_REFRESH_EXPIRATION_DELTA': timedelta(days=360), + 'JWT_AUTH_HEADER_PREFIX': 'Bearer', # default: 'JWT' +} + + +if os.path.isfile(os.path.join(BASE_DIR, 'settings_local.py')): + from settings_local import * diff --git a/ismp/urls.py b/ismp/urls.py new file mode 100644 index 0000000..b937f42 --- /dev/null +++ b/ismp/urls.py @@ -0,0 +1,21 @@ +"""ismp URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.0/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/ismp/wsgi.py b/ismp/wsgi.py new file mode 100644 index 0000000..0d12b77 --- /dev/null +++ b/ismp/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for ismp project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ismp.settings") + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..d4737bb --- /dev/null +++ b/manage.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ismp.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) diff --git a/people/__init__.py b/people/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/people/admin.py b/people/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/people/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/people/apps.py b/people/apps.py new file mode 100644 index 0000000..3eae75a --- /dev/null +++ b/people/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class PeopleConfig(AppConfig): + name = 'people' diff --git a/people/migrations/0001_initial.py b/people/migrations/0001_initial.py new file mode 100644 index 0000000..456caf8 --- /dev/null +++ b/people/migrations/0001_initial.py @@ -0,0 +1,56 @@ +# Generated by Django 2.0.7 on 2018-07-24 17:15 + +from django.conf import settings +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('auth', '0009_alter_user_last_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + 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='Organization', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=200, verbose_name='Organization Name')), + ('users', models.ManyToManyField(to=settings.AUTH_USER_MODEL)), + ], + options={ + 'verbose_name_plural': 'Organizations', + }, + ), + ] diff --git a/people/migrations/__init__.py b/people/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/people/models.py b/people/models.py new file mode 100644 index 0000000..31c39d8 --- /dev/null +++ b/people/models.py @@ -0,0 +1,34 @@ +from django.db.models import Model, CharField, ManyToManyField +from django.contrib.auth.models import AbstractUser + + +class User(AbstractUser): + """User + + I don't know why I make it people.User though, but I do it anyway, + easier access maybe? + """ + + def __str__(self): + return self.username + + @property + def organization(self): + try: + return self.organization_set.all()[0] + except: + return None + + +class Organization(Model): + """Organization to hold all models together + """ + name = CharField('Organization Name', max_length=200) + users = ManyToManyField('User') + + + class Meta: + verbose_name_plural = "Organizations" + + def __str__(self): + return self.name diff --git a/people/tests.py b/people/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/people/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/people/views.py b/people/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/people/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/pip-dev.txt b/pip-dev.txt new file mode 100644 index 0000000..7f46f50 --- /dev/null +++ b/pip-dev.txt @@ -0,0 +1,8 @@ +django>2 +psycopg2-binary +djangorestframework +djangorestframework-jwt +django-cors-headers +django-extra-fields +django-filter +raven diff --git a/pip-prod.txt b/pip-prod.txt new file mode 100644 index 0000000..3e10610 --- /dev/null +++ b/pip-prod.txt @@ -0,0 +1,10 @@ +django>2 +psycopg2-binary +djangorestframework +djangorestframework-jwt +django-cors-headers +django-extra-fields +django-filter +raven + +gunicorn \ No newline at end of file diff --git a/public/static/.gitignore b/public/static/.gitignore new file mode 100644 index 0000000..72e8ffc --- /dev/null +++ b/public/static/.gitignore @@ -0,0 +1 @@ +*