class EnvironmentDB(db.Document):
    meta = {'collection': 'environment'}

    name = db.StringField(max_length=50, unique=True, required=True)
    temperature = db.FloatField(required=True)
    noise = db.BooleanField(required=True)
    light = db.BooleanField(required=True)
    motion = db.BooleanField(required=False)
    people = db.ListField(db.ReferenceField(PersonDB))
Beispiel #2
0
class MiddlewareDB(db.Document):
    meta = {'collection': 'middleware'}
    temperature = db.FloatField(required=False)
    noise = db.BooleanField(required=False)
    light = db.BooleanField(required=False)
    motion = db.BooleanField(required=False)
    data_from = db.StringField(max_length=50, required=False)
    environment = db.StringField(max_length=50, required=False)
    created_at = db.DateTimeField(default=datetime.utcnow(), required=False)
Beispiel #3
0
class User(db.Document, UserMixin):
    email = db.StringField(max_length=255)
    password = db.StringField(max_length=255)
    active = db.BooleanField(default=True)
    confirmed_at = db.DateTimeField()
    roles = db.ListField(db.ReferenceField(Role), default=[])

    name = db.StringField(max_length=255)
    username = db.StringField(max_length=255)
    howls = ListField(ReferenceField('Howl'))

    def __unicode__(self):
        return self.username
Beispiel #4
0
class TaskModel(db.Document):
    name = db.StringField(required=True)
    description = db.StringField()
    duration = db.IntField()
    startdate = db.DateTimeField()
    status = db.BooleanField(default=True)  #convert to enum

    # shouldnt need this on task model
    def encode_auth_token(self, user_id):
        try:
            payload = {
                'exp':
                datetime.datetime.utcnow() + datetime.timedelta(
                    days=current_app.config.get('TOKEN_EXPIRATION_DAYS'),
                    seconds=current_app.config.get(
                        'TOKEN_EXPIRATION_SECONDS')),
                'iat':
                datetime.datetime.utcnow(),
                'sub': {
                    'id': str(user_id)
                }
            }
            return jwt.encode(payload,
                              current_app.config.get('SECRET_KEY'),
                              algorithm='HS256')
        except Exception as e:
            return e

    @staticmethod
    def decode_auth_token(auth_token):
        try:
            payload = jwt.decode(auth_token,
                                 current_app.config.get('SECRET_KEY'))
            return payload['sub']
        except jwt.ExpiredSignatureError:
            return 'Signature expired. Please log in again.'
        except jwt.InvalidTokenError:
            return 'Invalid token. Please log in again.'
from project import db
from datetime import datetime


class {{device_class}}Status(db.EmbeddedDocument):
    name = db.StringField(max_length=50, unique=True, required=False)
    connect = db.BooleanField(required=False)
    created_at = db.DateTimeField(default=datetime.utcnow(), required=False)


class {{device_class}}Environment(db.EmbeddedDocument):
    {% for sensor in device["device"]["sensors"] %}
    {% if sensor == 'temperature' %}
    {{sensor}} = db.FloatField(required=False)
    {% endif %}
    {% if sensor == 'noise' %}
    {{sensor}} = db.BooleanField(required=False)
    {% endif %}
    {% if sensor == 'light' %}
    {{sensor}} = db.BooleanField(required=False)
    {% endif %}
    {% if sensor == 'motion' %}
    {{sensor}} = db.BooleanField(required=False)
    {% endif %}
    {% endfor %}
    data_from = db.StringField(max_length=50, required=False)
    created_at = db.DateTimeField(default=datetime.utcnow(), required=False)


class {{device_class}}DB(db.Document):
    meta = {'collection': '{{device_name}}'}
Beispiel #6
0
class PersonModel(db.Document):
    meta = {
        'indexes': [{
            'fields': [
                '$username', '$firstname', '$lastname', '$job_title',
                '$department'
            ],
            'default_language':
            'english'
        }]
    }

    username = db.StringField(required=True, unique=True)
    title = db.StringField()
    firstname = db.StringField(required=True)
    lastname = db.StringField(required=True)
    email = db.EmailField()
    gender = db.StringField()
    job_title = db.StringField()
    department = db.StringField()
    avatar = db.URLField()
    employeenumber = db.IntField()
    address = db.EmbeddedDocumentField(Address)
    startdate = db.DateTimeField()
    active = db.BooleanField(default=True)
    password = db.StringField()
    admin = db.BooleanField(default=False, required=True)
    skills = db.ListField(db.ReferenceField(SkillModel), default=[])

    def clean(self):
        self.password = bcrypt.generate_password_hash(
            self.password,
            current_app.config.get('BCRYPT_LOG_ROUNDS')).decode()

    def encode_auth_token(self, user_id):
        try:
            payload = {
                'exp':
                datetime.datetime.utcnow() + datetime.timedelta(
                    days=current_app.config.get('TOKEN_EXPIRATION_DAYS'),
                    seconds=current_app.config.get(
                        'TOKEN_EXPIRATION_SECONDS')),
                'iat':
                datetime.datetime.utcnow(),
                'sub': {
                    'id': str(user_id),
                    'admin': self.admin,
                    'active': self.active
                }
            }
            return jwt.encode(payload,
                              current_app.config.get('SECRET_KEY'),
                              algorithm='HS256')
        except Exception as e:
            return e

    @staticmethod
    def decode_auth_token(auth_token):
        try:
            payload = jwt.decode(auth_token,
                                 current_app.config.get('SECRET_KEY'))
            return payload['sub']
        except jwt.ExpiredSignatureError:
            return 'Signature expired. Please log in again.'
        except jwt.InvalidTokenError:
            return 'Invalid token. Please log in again.'

    @classmethod
    def pre_save(cls, sender, document, **kwargs):
        print(document)