class City(db.Model): id = db.Column(db.Integer, primary_key=True) state = db.Column(db.String(255), nullable=False) city = db.Column(db.String(255), nullable=False) def __str__(self): return 'City:{}'.format(self.id)
class Iotdevice(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) device_reg_name = db.Column(db.String(255), nullable=False, unique=True) address = db.Column(db.String(255), nullable=False) pubkey = db.Column(db.String(255), nullable=True, unique=True) previous_meter_readings = db.Column(db.Integer, nullable=True) current_meter_readings = db.Column(db.Integer, nullable=True) is_active = db.Column(db.Boolean, nullable=False, default=False) housing_property = db.Column(db.Boolean, nullable=True) property_assigned_status = db.Column(db.Boolean, nullable=False, default=False) device_reg_confirm = db.Column(db.Boolean, nullable=False, default=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) server_id = db.Column(db.Integer, db.ForeignKey('iotserver.id'), nullable=True) property_id = db.relationship('Property', backref='iotdevice', uselist=False, lazy=True) def __str__(self): return 'Device:{}'.format(self.name)
class Tax(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) tax_name = db.Column(db.String(255), nullable=True, unique=True) tax_rate = db.Column(db.String(255), nullable=True) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) def __str__(self): return 'Server:{}'.format(self.tax_name)
class Miscellaneous(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=True, unique=True) amount = db.Column(db.String(255), nullable=True) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) def __str__(self): return 'Miscellaneous:{}'.format(self.name)
class Slablog(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) lower_slab = db.Column(db.String(255), nullable=False) upper_slab = db.Column(db.String(255), nullable=True) housing = db.Column(db.String(255), nullable=False) commercial = db.Column(db.String(255), nullable=False) penalty = db.Column(db.String(255), default=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) def __str__(self): return 'Server:{}'.format(self.id)
class Iotserver(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) server_reg_name = db.Column(db.String(255), nullable=False, unique=True) pincode = db.Column(db.String(255), nullable=False) area = db.Column(db.String(255), nullable=False) device_count = db.Column(db.Integer, nullable=True) pubkey = db.Column(db.String(255), unique=True) is_active = db.Column(db.Boolean, nullable=False, default=False) server_reg_confirm = db.Column(db.Boolean, nullable=False, default=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) remote_ip = db.Column(db.String(255), nullable=True) users = db.relationship("Property", backref="iotserver", lazy='dynamic') devices = db.relationship('Iotdevice', backref='iotserver', lazy='dynamic') api_key = db.Column(db.String(255), nullable=False, unique=True) key = db.Column(db.String(255), nullable=True, unique=True) sent_at = db.Column(db.DateTime, nullable=True) valid_sec = db.Column(db.Integer, nullable=True) def __str__(self): return 'Server:{}'.format(self.name) # Generate API_Key @staticmethod def generate_api_key(): token = token_urlsafe(64) # urlsafe to generate key while Iotserver.query.filter_by(api_key=token).first(): token = token_urlsafe(64) # Incase Key already exists, regenrate return token
class UserToken(db.Model): id = db.Column(db.Integer, primary_key=True) token = db.Column(db.String(), nullable=False) token_type = db.Column(db.String(), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) sent_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) valid_sec = db.Column(db.Integer, nullable=False) db.UniqueConstraint('token_type', 'token', name='user_token_token_type_token_key') @staticmethod def generate_token(token_type, user_id, valid_sec): token = token_urlsafe(64) # urlsafe to generate token while UserToken.query.filter_by(token=token, token_type=token_type).first(): token = token_urlsafe( 64) # incase token already exists, regenrate it user_token = UserToken.query.filter_by(user_id=user_id).first() if user_token is None or user_token == []: new_user_token = UserToken() new_user_token.user_id = user_id new_user_token.token = token new_user_token.token_type = token_type new_user_token.valid_sec = valid_sec db.session.add(new_user_token) db.session.commit() return new_user_token else: user_token.token = token user_token.token_type = token_type user_token.valid_sec = valid_sec db.session.commit() return user_token def is_valid(self): valid_till = self.sent_at + timedelta(seconds=self.valid_sec) return valid_till > datetime.utcnow()
class Supportquery(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) help_type = db.Column(db.String(255), nullable=True) status = db.Column(db.Boolean, nullable=False, default=False) prob_text = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) def __str__(self): return 'Query :{}'.format(self.id)
class Metertransactionlog(db.Model, UserMixin): id = db.Column(db.Integer,primary_key=True) month = db.Column(db.String(255), nullable = False) year = db.Column(db.String(255), nullable = False) date = db.Column(db.String(255), nullable = False) bill_ammount = db.Column(db.Float, nullable=False) bill_paid = db.Column(db.Boolean(255),default=False) unit_cost = db.Column(db.Float, nullable=False) penalty_added = db.Column(db.Float, nullable=False) meter_reading = db.Column(db.Integer, nullable=False) monthly_units = db.Column(db.Integer, nullable=False) bill_data = db.Column(db.Text, nullable = False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) # -- Foreign key property_id = db.Column(db.Integer, db.ForeignKey('property.id'), nullable=True) def __str__(self): return 'Server:{}'.format(self.id)
class Productactivation(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) product_key = db.Column(db.String(255), nullable=False) activated = db.Column(db.Boolean, default=False, nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) def __str__(self): return 'Product: {}'.format(self.product_key) @staticmethod def generate_key(): activation_key = rand_pass(16) print ('Product key :', activation_key) org = Productactivation() org.product_key = activation_key db.session.add(org) db.session.commit() return activation_key
class Admin(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=True) username = db.Column(db.String(255), nullable=True, unique=True) employee_id = db.Column(db.String(255), nullable=True, unique=True) email = db.Column(db.String(255), nullable=True) phone_number = db.Column(db.String(255), nullable=True, unique=True) password = db.Column(db.String(255), nullable=True) email_verified = db.Column(db.Boolean, nullable=True, default=False) is_active = db.Column(db.Boolean, nullable=True, default=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=True) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) role = db.Column(db.String(255), nullable=True, unique=False) tokens = db.relationship('AdminToken', backref='admin', lazy=True) def __str__(self): return 'Admin:{}'.format(self.name) @staticmethod def hash_password(password): return generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password)
class Property(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) flat_no = db.Column(db.String(500), nullable=False) building_name = db.Column(db.String(500), nullable=False) state = db.Column(db.String(500), nullable=False) pincode = db.Column(db.String(255), nullable=False) city = db.Column(db.String(255), nullable=False) street = db.Column(db.String(255), nullable=False) is_active = db.Column(db.Boolean, default=False, nullable=False) bill_gen_status = db.Column(db.Boolean, default=False, nullable=False) tenant_reg_confirm = db.Column(db.Boolean, default=False, nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow) # -- Foreign Keys from other tables owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) tenant_id = db.Column(db.Integer, db.ForeignKey('tenant.id'), nullable=True) server_id = db.Column(db.Integer, db.ForeignKey('iotserver.id'), nullable=True) device_id = db.Column(db.Integer, db.ForeignKey('iotdevice.id'), nullable=True) # -- Reference to the foreign key to other table meter_trans_id = db.relationship('Metertransactionlog', backref='property', lazy=True) bill_req_defaulter_id = db.relationship('Billrequestdefaulters', backref='property', lazy=True) bill_req_payment_defaulter_id = db.relationship( 'Billrequestpaymentdefaulters', backref='property', lazy=True) def __str__(self): return 'Property: {}'.format(self.id)