<reponame>HarinarayananP/Flask-Backend-AirPolutionMonitoring
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""

from flask_login import UserMixin
from sqlalchemy import Binary, Column, Integer, String, DateTime, Boolean, ForeignKey, Float

from app import db, login_manager

from app.base.util import hash_pass


class User(db.Model, UserMixin):
    __tablename__ = 'User'

    id = Column(Integer, primary_key=True)
    username = Column(String(20), unique=True)
    email = Column(String(20), unique=True)
    password = Column(Binary)

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            if property == 'password':
                value = hash_pass(value)  # we need bytes here (not plain str)

            setattr(self, property, value)

    def __repr__(self):
        return str(self.username)


class SensorData(db.Model, UserMixin):
    __tablename__ = 'SensorData'

    id = Column(Integer, primary_key=True)
    sensorname = Column(String(20), ForeignKey("SensorDetails.name"))
    time = Column(DateTime)
    value = Column(Integer)
    sensordetails = db.relationship("SensorDetails", lazy=True)

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            setattr(self, property, value)

    def __repr__(self):
        return str(self.id)


class Settings(db.Model, UserMixin):
    __tablename__ = 'Settings'

    id = Column(Integer, primary_key=True)
    name = Column(String(20))
    value = Column(String(400))

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            setattr(self, property, value)

    def __repr__(self):
        return self.value


class SensorDetails(db.Model, UserMixin):
    __tablename__ = 'SensorDetails'

    id = Column(Integer, primary_key=True)
    name = Column(String(20), unique=True)
    location = Column(String(400))
    unit = Column(String(20))
    warning_level = Column(Integer)
    severe_level = Column(Integer)
    active = Column(Boolean)
    min_value = Column(Integer)
    max_value = Column(Integer)
    color = Column(String(15))
    icon = Column(String(20))

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            setattr(self, property, value)

    def __repr__(self):
        return str(self.id)

@login_manager.user_loader
def user_loader(id):
    return User.query.filter_by(id=id).first()


@login_manager.request_loader
def request_loader(request):
    username = request.form.get('username')
    user = User.query.filter_by(username=username).first()
    return user if user else None

# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""

from flask_login import UserMixin
from sqlalchemy import Binary, Column, Integer, String, DateTime, Boolean, ForeignKey, Float

from app import db, login_manager

from app.base.util import hash_pass


class User(db.Model, UserMixin):
    __tablename__ = 'User'

    id = Column(Integer, primary_key=True)
    username = Column(String(20), unique=True)
    email = Column(String(20), unique=True)
    password = Column(Binary)

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            if property == 'password':
                value = hash_pass(value)  # we need bytes here (not plain str)

            setattr(self, property, value)

    def __repr__(self):
        return str(self.username)


class SensorData(db.Model, UserMixin):
    __tablename__ = 'SensorData'

    id = Column(Integer, primary_key=True)
    sensorname = Column(String(20), ForeignKey("SensorDetails.name"))
    time = Column(DateTime)
    value = Column(Integer)
    sensordetails = db.relationship("SensorDetails", lazy=True)

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            setattr(self, property, value)

    def __repr__(self):
        return str(self.id)


class Settings(db.Model, UserMixin):
    __tablename__ = 'Settings'

    id = Column(Integer, primary_key=True)
    name = Column(String(20))
    value = Column(String(400))

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            setattr(self, property, value)

    def __repr__(self):
        return self.value


class SensorDetails(db.Model, UserMixin):
    __tablename__ = 'SensorDetails'

    id = Column(Integer, primary_key=True)
    name = Column(String(20), unique=True)
    location = Column(String(400))
    unit = Column(String(20))
    warning_level = Column(Integer)
    severe_level = Column(Integer)
    active = Column(Boolean)
    min_value = Column(Integer)
    max_value = Column(Integer)
    color = Column(String(15))
    icon = Column(String(20))

    def __init__(self, **kwargs):
        for property, value in kwargs.items():
            # depending on whether value is an iterable or not, we must
            # unpack it's value (when **kwargs is request.form, some values
            # will be a 1-element list)
            if hasattr(value, '__iter__') and not isinstance(value, str):
                # the ,= unpack of a singleton fails PEP8 (travis flake8 test)
                value = value[0]

            setattr(self, property, value)

    def __repr__(self):
        return str(self.id)

@login_manager.user_loader
def user_loader(id):
    return User.query.filter_by(id=id).first()


@login_manager.request_loader
def request_loader(request):
    username = request.form.get('username')
    user = User.query.filter_by(username=username).first()
    return user if user else None
# -*- encoding: utf-8 -*- Copyright (c) 2019 - present AppSeed.us # depending on whether value is an iterable or not, we must # unpack it's value (when **kwargs is request.form, some values # will be a 1-element list) # the ,= unpack of a singleton fails PEP8 (travis flake8 test) # we need bytes here (not plain str) # depending on whether value is an iterable or not, we must # unpack it's value (when **kwargs is request.form, some values # will be a 1-element list) # the ,= unpack of a singleton fails PEP8 (travis flake8 test) # depending on whether value is an iterable or not, we must # unpack it's value (when **kwargs is request.form, some values # will be a 1-element list) # the ,= unpack of a singleton fails PEP8 (travis flake8 test) # depending on whether value is an iterable or not, we must # unpack it's value (when **kwargs is request.form, some values # will be a 1-element list) # the ,= unpack of a singleton fails PEP8 (travis flake8 test)