Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.contrib.auth.models import User
- from django.contrib.auth.backends import ModelBackend
- import psycopg2
- class PostgresAuthBackend(ModelBackend):
- def authenticate(self, username=None, password=None, **kwargs):
- print('Try to login')
- connection_string = "host = '172.25.1.151' dbname='TMS' port = 5432"
- try:
- connection_string += " user='{}' password='{}'".format(username, password)
- connection = psycopg2.connect(connection_string)
- if connection:
- try:
- user = User.objects.get(username=username)
- except User.DoesNotExist:
- # Create a new user. There's no need to set a password
- # because only the password from settings.py is checked.
- user = User(username=username, password=password)
- user.is_staff = True
- user.is_superuser = True
- user.save()
- return user
- return None
- except psycopg2.Error as e:
- print("Unable to connect to the PostgreSQL server, check the connection string!")
- print(e.pgerror)
- print(e.diag.message_detail)
- except Exception as error:
- print("Unknown error", error)
- finally:
- if connection is not None:
- connection.close()
- """
- Django settings for TMS_Services project.
- Generated by 'django-admin startproject' using Django 2.1.1.
- For more information on this file, see
- https://docs.djangoproject.com/en/2.1/topics/settings/
- For the full list of settings and their values, see
- https://docs.djangoproject.com/en/2.1/ref/settings/
- """
- import os
- # 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.1/howto/deployment/checklist/
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = '7q4a(6(xwek=3lejc#tq@&es$__q!(5)3wh9fa#6va*q_8=9*5'
- # 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',
- 'django_extensions',
- 'rest_framework',
- 'corsheaders',
- 'servicesapp',
- ]
- 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 = 'TMS_Services.urls'
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [os.path.join(BASE_DIR, 'templates')]
- ,
- '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 = 'TMS_Services.wsgi.application'
- # Database
- # https://docs.djangoproject.com/en/2.1/ref/settings/#databases
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.postgresql_psycopg2',
- 'NAME': 'TMS',
- 'USER': 'george',
- 'PASSWORD': 'george_1',
- 'HOST': '172.25.1.151',
- 'PORT': '5432',
- },
- 'sqlite3': {
- 'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
- }
- }
- # Password validation
- # https://docs.djangoproject.com/en/2.1/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',
- },
- ]
- AUTHENTICATION_BACKENDS = (
- 'django.contrib.auth.backends.ModelBackend',
- 'servicesapp.custombackends.PostgresAuthBackend',
- )
- # Internationalization
- # https://docs.djangoproject.com/en/2.1/topics/i18n/
- LANGUAGE_CODE = 'en-us'
- TIME_ZONE = 'UTC'
- USE_I18N = True
- USE_L10N = True
- USE_TZ = True
- # Static files (CSS, JavaScript, Images)
- # https://docs.djangoproject.com/en/2.1/howto/static-files/
- STATIC_URL = '/static/'
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
- STATIC_ROOT = '/srv/djangoTMS/TMS_Django_Services/static/'
- STATICFILES_DIR = os.path.join(BASE_DIR, 'static')
- CORS_ORIGIN_ALLOW_ALL = True
- CORS_ALLOW_CREDENTIALS = True
Add Comment
Please, Sign In to add comment