· 7 years ago · Nov 05, 2018, 03:06 PM
1from django.contrib.auth.models import User
2from django.contrib.auth.backends import ModelBackend
3import psycopg2
4
5
6class PostgresAuthBackend(ModelBackend):
7 def authenticate(self, username=None, password=None, **kwargs):
8 print('Try to login')
9 connection_string = "host = '172.25.1.151' dbname='TMS' port = 5432"
10 try:
11 connection_string += " user='{}' password='{}'".format(username, password)
12 connection = psycopg2.connect(connection_string)
13 if connection:
14 try:
15 user = User.objects.get(username=username)
16 except User.DoesNotExist:
17 # Create a new user. There's no need to set a password
18 # because only the password from settings.py is checked.
19 user = User(username=username, password=password)
20 user.is_staff = True
21 user.is_superuser = True
22 user.save()
23 return user
24 return None
25
26 except psycopg2.Error as e:
27 print("Unable to connect to the PostgreSQL server, check the connection string!")
28 print(e.pgerror)
29 print(e.diag.message_detail)
30 except Exception as error:
31 print("Unknown error", error)
32 finally:
33 if connection is not None:
34 connection.close()
35
36"""
37Django settings for TMS_Services project.
38
39Generated by 'django-admin startproject' using Django 2.1.1.
40
41For more information on this file, see
42https://docs.djangoproject.com/en/2.1/topics/settings/
43
44For the full list of settings and their values, see
45https://docs.djangoproject.com/en/2.1/ref/settings/
46"""
47
48import os
49
50# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
51BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
52
53
54# Quick-start development settings - unsuitable for production
55# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/
56
57# SECURITY WARNING: keep the secret key used in production secret!
58SECRET_KEY = '7q4a(6(xwek=3lejc#tq@&es$__q!(5)3wh9fa#6va*q_8=9*5'
59
60# SECURITY WARNING: don't run with debug turned on in production!
61DEBUG = True
62
63ALLOWED_HOSTS = ['*']
64
65
66# Application definition
67
68INSTALLED_APPS = [
69 'django.contrib.admin',
70 'django.contrib.auth',
71 'django.contrib.contenttypes',
72 'django.contrib.sessions',
73 'django.contrib.messages',
74 'django.contrib.staticfiles',
75 'django_extensions',
76 'rest_framework',
77 'corsheaders',
78 'servicesapp',
79]
80
81MIDDLEWARE = [
82 'django.middleware.security.SecurityMiddleware',
83 'django.contrib.sessions.middleware.SessionMiddleware',
84 'corsheaders.middleware.CorsMiddleware',
85 'django.middleware.common.CommonMiddleware',
86 'django.middleware.csrf.CsrfViewMiddleware',
87 'django.contrib.auth.middleware.AuthenticationMiddleware',
88 'django.contrib.messages.middleware.MessageMiddleware',
89 'django.middleware.clickjacking.XFrameOptionsMiddleware',
90]
91
92ROOT_URLCONF = 'TMS_Services.urls'
93
94TEMPLATES = [
95 {
96 'BACKEND': 'django.template.backends.django.DjangoTemplates',
97 'DIRS': [os.path.join(BASE_DIR, 'templates')]
98 ,
99 'APP_DIRS': True,
100 'OPTIONS': {
101 'context_processors': [
102 'django.template.context_processors.debug',
103 'django.template.context_processors.request',
104 'django.contrib.auth.context_processors.auth',
105 'django.contrib.messages.context_processors.messages',
106 ],
107 },
108 },
109]
110
111WSGI_APPLICATION = 'TMS_Services.wsgi.application'
112
113
114# Database
115# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
116
117DATABASES = {
118 'default': {
119 'ENGINE': 'django.db.backends.postgresql_psycopg2',
120 'NAME': 'TMS',
121 'USER': 'george',
122 'PASSWORD': 'george_1',
123 'HOST': '172.25.1.151',
124 'PORT': '5432',
125 },
126 'sqlite3': {
127 'ENGINE': 'django.db.backends.sqlite3',
128 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
129 }
130}
131
132# Password validation
133# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
134
135AUTH_PASSWORD_VALIDATORS = [
136 {
137 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
138 },
139 {
140 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
141 },
142 {
143 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
144 },
145 {
146 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
147 },
148]
149
150AUTHENTICATION_BACKENDS = (
151 'django.contrib.auth.backends.ModelBackend',
152 'servicesapp.custombackends.PostgresAuthBackend',
153)
154
155# Internationalization
156# https://docs.djangoproject.com/en/2.1/topics/i18n/
157
158LANGUAGE_CODE = 'en-us'
159
160TIME_ZONE = 'UTC'
161
162USE_I18N = True
163
164USE_L10N = True
165
166USE_TZ = True
167
168
169# Static files (CSS, JavaScript, Images)
170# https://docs.djangoproject.com/en/2.1/howto/static-files/
171
172STATIC_URL = '/static/'
173BASE_DIR = os.path.dirname(os.path.abspath(__file__))
174STATIC_ROOT = '/srv/djangoTMS/TMS_Django_Services/static/'
175
176STATICFILES_DIR = os.path.join(BASE_DIR, 'static')
177
178CORS_ORIGIN_ALLOW_ALL = True
179CORS_ALLOW_CREDENTIALS = True