Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Ini config #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ target/
#Ipython Notebook
.ipynb_checkpoints
.idea/
db.sqlite3

# Application generated files
media/

quotes.txt

userlist.txt

alge/config.py
db.sqlite3
streque.ini
95 changes: 53 additions & 42 deletions alge/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,49 @@
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

from configparser import ConfigParser
import os

#Load the config file. See config.py.sample for the structure
import string
import random
import string

try:
import alge.config as config
except ImportError as e:
default_conf = """\
# Remember to set the correct permissions for this file when in production.
# This is done (in Linux) with the command:
# chown *web server user*:*your user group* config.py && chmod 660 config.py

DEBUG = True # False in a production environment
SECRET_KEY = '{secret_key}'
EMAIL_USE_TLS = True
EMAIL_HOST = None # smtp.gmail.com
EMAIL_HOST_USER = None # [email protected]
EMAIL_HOST_PASSWORD = None # Remember to set this
EMAIL_PORT = 587
""".format(secret_key=''.join(random.choice(string.ascii_letters + string.digits) for _ in range(50)))

with open("alge/config.py", "a") as f:
f.write(default_conf)

import alge.config as config
# Load INI config if available. Otherwise, if config.py exists, convert it
# to INI. Otherwise, just create a new one.
config = ConfigParser()
filename = "streque.ini"
had_config = False
try:
with open(filename) as file:
config.read_file(file)
had_config = True
except:
try:
import alge.config as c
config.read_dict({
"main": {
"debug": "yes" if c.DEBUG else "no",
"secret_key": c.SECRET_KEY,
},
"email": {
"use_tls": "yes" if c.EMAIL_USE_TLS else "no",
"host": c.EMAIL_HOST or "",
"user": c.EMAIL_HOST_USER or "",
"password": c.EMAIL_HOST_PASSWORD or "",
"port": str(c.EMAIL_PORT),
},
})
except ImportError:
# Generate a new config file
char = lambda: random.choice(string.ascii_letters + string.digits)
config.read_dict({
"main": {
"secret_key": ''.join(char() for _ in range(50)),
},
})

if not had_config:
with open(filename, "w") as file:
config.write(file)


def filter_non_strings(items):
Expand All @@ -51,15 +67,12 @@ def filter_non_strings(items):
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config.SECRET_KEY
SECRET_KEY = config.get("main", "secret_key")

# SECURITY WARNING: don't run with debug turned on in production!


if 'DEBUG' in dir(config):
DEBUG = config.DEBUG
else:
DEBUG = True
DEBUG = config.getboolean("main", "debug", fallback=False)
if DEBUG:
print("NOTE: Running in DEBUG mode")

ALLOWED_HOSTS = ["localhost", "streque.se", "www.streque.se"]

Expand Down Expand Up @@ -169,23 +182,21 @@ def filter_non_strings(items):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

#Media root (uploaded files)

# Media root (uploaded files)
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'

#Gmail stpm settings

EMAIL_USE_TLS = config.EMAIL_USE_TLS
EMAIL_HOST = config.EMAIL_HOST
EMAIL_HOST_USER = config.EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = config.EMAIL_HOST_PASSWORD
EMAIL_PORT = config.EMAIL_PORT
# E-mail SMTP settings
EMAIL_USE_TLS = config.getboolean("email", "use_tls", fallback=True)
EMAIL_HOST = config.get("email", "host", fallback=None)
EMAIL_HOST_USER = config.get("email", "user", fallback=None)
EMAIL_HOST_PASSWORD = config.get("email", "password", fallback=None)
EMAIL_PORT = config.getint("email", "port", fallback=587)

#url to login page
# url to login page
LOGIN_URL = '/login'

#Custom user model:
# Custom user model:
AUTH_USER_MODEL = 'EmailUser.MyUser'

REST_FRAMEWORK = {
Expand All @@ -203,7 +214,7 @@ def filter_non_strings(items):
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
#Set page size so large numbers of data will be split up over multiple pages
# Set page size so large numbers of data will be split up over multiple pages
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 50,
'MAX_PAGE_SIZE' : 200,
Expand Down