Fix gitignore, add random secret_key generator, change static_root dir

parent fcb3f3f9
*.egg-info
*.pot
*.py[co]
*.png
*.logging
*.pdf
__pycache__
MANIFEST
# Cached builds
__pycache__/
.cache/
.idea/
*.pyc
*.pyo
*.pyd
*.so
*.dll
*.egg
*.sqlite3
*.rdb
*.egg-info/
.cache/
.tox/
.sass-cache/
.coverage
# Logs
.cs-debug.log
geckodriver.log
ghostdriver.log
log/
# Build directories
build/
dist/
docs/_build/
docs/locale/
node_modules/
tests/coverage_html/
tests/.coverage
build/
tests/report/
docs/_build/
docker/nginx/collect/
docker/env
htmlcov/
.vagrant/
cookbooks/
Vagrantfile
# .py generated by translations on jinja2 templates
*.jinja2.py
*.html.py
*.txt.py
.coverage
htmlcov/
\ No newline at end of file
# Sensible information
*.logging
security/*
collect/media/*
log/
backup/
......@@ -40,4 +40,5 @@ COPY [ \
COPY src/ /app/src/
ENV PYTHONPATH=src
\ No newline at end of file
ENV PYTHONPATH=src \
SMI_UNB_PRODUCTION=true
\ No newline at end of file
......@@ -4,22 +4,30 @@ from smi_unb.buildings.models import Building
from smi_unb.transductor.models import EnergyTransductor, EnergyMeasurements
class EnergyTransductorSerializer(serializers.ModelSerializer):
class EnergyTransductorSerializer(serializers.HyperlinkedModelSerializer):
def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(EnergyTransductorSerializer, self).__init__(
many=many, *args, **kwargs)
id = serializers.IntegerField()
building = serializers.SerializerMethodField()
model = serializers.SerializerMethodField()
class Meta:
model = EnergyTransductor
fields = (
'id', 'building', 'model', 'active', 'ip_address',
'url', 'id', 'building', 'model', 'active', 'ip_address',
'name', 'serie_number', 'local_description',
'comments', 'creation_date', 'calibration_date',
'broken', 'last_measurement_sent')
def get_building(self, obj):
return obj.building.id
def get_model(self, obj):
return obj.model.id
class BuildingSerializer(serializers.HyperlinkedModelSerializer):
id = serializers.IntegerField()
......@@ -32,21 +40,26 @@ class BuildingSerializer(serializers.HyperlinkedModelSerializer):
'website_address', 'server_ip_address', 'active')
def get_campus(self, obj):
return obj.campus.name
return obj.campus.id
class EnergyMeasurementsSerializer(serializers.ModelSerializer):
class EnergyMeasurementsSerializer(serializers.HyperlinkedModelSerializer):
def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(EnergyMeasurementsSerializer, self).__init__(
many=many, *args, **kwargs)
transductor = serializers.SerializerMethodField()
class Meta:
model = EnergyMeasurements
fields = (
'collection_date', 'transductor',
'url', 'collection_date', 'transductor',
'voltage_a', 'voltage_b', 'voltage_c',
'current_a', 'current_b', 'current_c',
'active_power_a', 'active_power_b', 'active_power_c',
'reactive_power_a', 'reactive_power_b', 'reactive_power_c',
'apparent_power_a', 'apparent_power_b', 'apparent_power_c')
def get_transductor(self, obj):
return obj.transductor.id
......@@ -71,7 +71,7 @@ class SyncUtils(object):
@classmethod
def generate_endpoint_url(
cls, slave_ip, new, api_view_set,
instance_id='', query_params='', port=''
instance_id='', query_params='', port='80'
):
endpoint_url = ''
......
......@@ -369,12 +369,12 @@ class DataCollector(object):
Class responsible to handle all transductor measurements collect.
Attributes:
transductors (Transductor): The existing transductors.
transductors (Transductor): The existing active transductors.
transductor_module (module): The module that contains the
transductor models.
"""
def __init__(self):
self.transductors = Transductor.objects.all()
self.transductors = Transductor.objects.filter(active=True)
self.transductor_module = importlib.import_module(
"smi_unb.transductor.models"
)
......
......@@ -7,17 +7,53 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
import random
import string
from unipath import Path
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
SRC_DIR = os.path.dirname(BASE_DIR)
REPO_DIR = os.path.dirname(SRC_DIR)
choice = random.SystemRandom().choice
# Creating a random SECRET_KEY to project
# SECURITY WARNING: keep the secret key used in production secret!
d = os.path.dirname
base_dir = d(d(d(os.path.abspath(__file__))))
security_path = os.path.join(base_dir, 'security')
secrete_key_path = os.path.join(security_path, 'secret_key.dat')
try:
with open(secrete_key_path, 'r') as F:
SECRET_KEY = F.read()
except FileNotFoundError:
print('Creating a new SECRET_KEY at security/secret_key.dat')
# Create directory, if it does not exist
if not os.path.exists(security_path):
os.mkdir(security_path)
chars = ''.join([string.ascii_letters, string.digits, string.punctuation])
SECRET_KEY = ''.join([choice(chars) for i in range(50)])
with open(secrete_key_path, 'w') as F:
SECRET_KEY = F.write(SECRET_KEY)
SECRET_KEY = 'secret_key'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
if os.environ.get('SMI_UNB_PRODUCTION', False) != 'true':
DEBUG = True
print('Running server in DEBUG mode. Plese do *not* go to production!')
else:
DEBUG = False
print('Running server with DEBUG=False')
if not DEBUG:
ALLOWED_HOSTS = ['localhost']
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
......@@ -87,9 +123,7 @@ TEMPLATES = [
WSGI_APPLICATION = 'smi_unb.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
# Database and Cache
if 'DB_NAME' in os.environ:
# Running the Docker image
DATABASES = {
......@@ -162,7 +196,7 @@ USE_L10N = True
USE_TZ = True
# STATIC_ROOT = os.path.join(COLLECT_DIR, 'static')
STATIC_ROOT = os.path.join(REPO_DIR, 'docker', 'nginx', 'collect', 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment