models.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # coding: utf-8
  2. from sqlalchemy import Column, DateTime, Integer, Numeric, String
  3. from flask_sqlalchemy import SQLAlchemy
  4. #使用Werkzeug的security加密
  5. from werkzeug.security import generate_password_hash,check_password_hash
  6. #與當前項目相關的模型文件,即所有的實體類在此編寫
  7. from manage import db
  8. class User(db.Model):
  9. __tablename__ = 'user'
  10. sn = db.Column(db.Integer, primary_key=True)
  11. firstname = db.Column(db.String(30), nullable=False)
  12. lastname = db.Column(db.String(30), nullable=False)
  13. mail = db.Column(db.String(50), nullable=False)
  14. phone = db.Column(db.String(20), nullable=False)
  15. username = db.Column(db.String(30), nullable=False, unique=True)
  16. password = db.Column(db.String(40), nullable=False)
  17. status = db.Column(db.Integer, nullable=False, info='0:admin;1:new;2:old')
  18. isActive = db.Column(db.Boolean, nullable=False, default=True)
  19. purchase_info = db.relationship('PurchaseInfo', backref='user', uselist=False)
  20. node_info = db.relationship('NodeInfo', backref='user', uselist=False)
  21. farm_info = db.relationship('FarmInfo', backref='user', uselist=False)
  22. permission = db.relationship('Permissions', backref='user', uselist=False)
  23. node_imgs = db.relationship('NodeImage', backref='user', lazy='dynamic')
  24. node_positions = db.relationship('NodePosition', backref='user', lazy='dynamic')
  25. module_sn_numbers = db.relationship('ModuleSerialNumber', backref='user', lazy='dynamic')
  26. #哈希加密版本
  27. # class User(db.Model):
  28. # __tablename__ = 'user'
  29. #
  30. # sn = db.Column(db.Integer, primary_key=True)
  31. # firstname = db.Column(db.String(30), nullable=False)
  32. # lastname = db.Column(db.String(30), nullable=False)
  33. # mail = db.Column(db.String(50), nullable=False)
  34. # phone = db.Column(db.String(20), nullable=False)
  35. # username = db.Column(db.String(30), nullable=False, unique=True)
  36. # password = db.Column(db.Text, nullable=False)
  37. # status = db.Column(db.Integer, nullable=False, info='0:admin;1:new;2:old')
  38. # isActive = db.Column(db.Boolean, nullable=False, default=True)
  39. #
  40. # purchase_info = db.relationship('PurchaseInfo', backref='user', uselist=False)
  41. #
  42. # node_info = db.relationship('NodeInfo', backref='user', uselist=False)
  43. #
  44. # farm_info = db.relationship('FarmInfo', backref='user', uselist=False)
  45. #
  46. # permission = db.relationship('Permissions', backref='user', uselist=False)
  47. #
  48. # node_imgs = db.relationship('NodeImage', backref='user', lazy='dynamic')
  49. #
  50. # node_positions = db.relationship('NodePosition', backref='user', lazy='dynamic')
  51. #
  52. # module_sn_numbers = db.relationship('ModuleSerialNumber', backref='user', lazy='dynamic')
  53. # def __init__(self, *args, **kwargs):
  54. # username = kwargs.get('username')
  55. # password = kwargs.get('password')
  56. #
  57. # self.username = username
  58. # self.password = generate_password_hash(password)
  59. #
  60. # # 这里的参数是hash过的参数以及原始传入hash
  61. # def check_hash_password(self,raw_password):
  62. # result = check_password_hash(self.password, raw_password)
  63. # return result
  64. # # 得到验证結果  
  65. class PurchaseInfo(db.Model):
  66. __tablename__ = 'purchase_info'
  67. sn = db.Column(db.Integer, primary_key=True)
  68. loc_length = db.Column(db.Text, nullable=False)
  69. loc_width = db.Column(db.Text, nullable=False)
  70. location = db.Column(db.Text, nullable=False)
  71. monitor_num = db.Column(db.Integer, nullable=False)
  72. connect = db.Column(db.Text, nullable=False)
  73. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  74. datetime = db.Column(db.DateTime, nullable=False)
  75. class NodeInfo(db.Model):
  76. __tablename__ = 'node_info'
  77. sn = db.Column(db.Integer, primary_key=True)
  78. node_info = db.Column(db.Text, nullable=False)
  79. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  80. class FarmInfo(db.Model):
  81. __tablename__ = 'farm_info'
  82. sn = db.Column(db.Integer, primary_key=True)
  83. path = db.Column(db.Text, nullable=False, default='0')
  84. location = db.Column(db.Text, nullable=False, default='0')
  85. loc_lat = db.Column(db.Text, nullable=False, default='0')
  86. loc_lng = db.Column(db.Text, nullable=False, default='0')
  87. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  88. datetime = db.Column(db.DateTime, nullable=False)
  89. class NodeImage(db.Model):
  90. __tablename__ = 'node_img'
  91. sn = db.Column(db.Integer, primary_key=True)
  92. node_name = db.Column(db.String(10), nullable=False)
  93. path = db.Column(db.Text, nullable=False, default='0')
  94. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  95. datetime = db.Column(db.DateTime, nullable=False)
  96. class NodePosition(db.Model):
  97. __tablename__ = 'node_position'
  98. sn = db.Column(db.Integer, primary_key=True)
  99. node_name = db.Column(db.String(10), nullable=False)
  100. pos_lat = db.Column(db.Text, nullable=False)
  101. pos_lng = db.Column(db.Text, nullable=False)
  102. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  103. datetime = db.Column(db.DateTime, nullable=False)
  104. class ModuleSerialNumber(db.Model):
  105. __tablename__ = 'module_sn_number'
  106. sn = db.Column(db.Integer, primary_key=True)
  107. module_name = db.Column(db.String(30), nullable=False)
  108. sn_number = db.Column(db.Text, nullable=False)
  109. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  110. datetime = db.Column(db.DateTime, nullable=False)
  111. #權限表
  112. class Permissions(db.Model):
  113. __tablename__ = 'permissions'
  114. sn = db.Column(db.Integer, primary_key=True)
  115. user_id = db.Column(db.Integer, db.ForeignKey('user.sn'))
  116. add_domain = db.Column(db.Integer, nullable=False, default=0)
  117. company_profile = db.Column(db.Integer, nullable=False, default=0)
  118. product_info = db.Column(db.Integer, nullable=False, default=0)
  119. erp = db.Column(db.Integer, nullable=False, default=0)
  120. rma = db.Column(db.Integer, nullable=False, default=0)
  121. download_data = db.Column(db.Integer, nullable=False, default=0)
  122. gps = db.Column(db.Integer, nullable=False, default=0)
  123. sensor = db.Column(db.Integer, nullable=False, default=0)
  124. Actuator = db.Column(db.Integer, nullable=False, default=0)
  125. diary_log = db.Column(db.Integer, nullable=False, default=0)
  126. repair_log = db.Column(db.Integer, nullable=False, default=0)
  127. set_permission = db.Column(db.Integer, nullable=False, default=0)
  128. register = db.Column(db.Integer, nullable=False, default=0)
  129. blacklist = db.Column(db.Integer, nullable=False, default=0)
  130. back_manage = db.Column(db.Integer, nullable=False, default=0)
  131. module_test = db.Column(db.Integer, nullable=False, default=0)
  132. class SensorList(db.Model):
  133. __tablename__ = 'sensor_list'
  134. sn = db.Column(db.Integer, primary_key=True)
  135. ch_name = db.Column(db.String(20), nullable=False)
  136. en_name = db.Column(db.String(20), nullable=False)
  137. version = db.Column(db.String(10), nullable=False)
  138. datetime = db.Column(db.DateTime, nullable=False)
  139. #自定義序列化方法,方便在前端拿數據
  140. def toDict(self):
  141. return {'ch_name':self.ch_name, 'en_name':self.en_name, 'version':self.version}
  142. class ItemList(db.Model):
  143. __tablename__ = 'item_list'
  144. sn = db.Column(db.Integer, primary_key=True)
  145. ip = db.Column(db.Text, nullable=False)
  146. pymysql = db.Column(db.Integer, nullable=False)
  147. eventlet = db.Column(db.Integer, nullable=False)
  148. datetime = db.Column(db.DateTime, nullable=False)
  149. db.create_all()