-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
43 lines (33 loc) · 1.61 KB
/
models.py
File metadata and controls
43 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from datetime import datetime
import bcrypt
db = SQLAlchemy()
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=False)
is_admin = db.Column(db.Boolean, default=False, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
feedbacks = db.relationship('Feedback', backref='user', lazy=True)
def set_password(self, password):
"""Hash và lưu password"""
self.password_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
def check_password(self, password):
"""Kiểm tra password"""
return bcrypt.checkpw(password.encode('utf-8'), self.password_hash.encode('utf-8'))
def __repr__(self):
return f'<User {self.username}>'
class Feedback(db.Model):
__tablename__ = 'feedbacks'
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
sentiment = db.Column(db.String(20), nullable=False)
topic = db.Column(db.String(50), nullable=False)
sentiment_confidence = db.Column(db.Float, nullable=False)
topic_confidence = db.Column(db.Float, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return f'<Feedback {self.id}: {self.sentiment} - {self.topic}>'