|
| 1 | +from typing import Self |
| 2 | +import bcrypt |
| 3 | +from flask_sqlalchemy import SQLAlchemy |
| 4 | + |
| 5 | +db = SQLAlchemy() |
| 6 | + |
| 7 | +def init_app(app): |
| 8 | + db.init_app(app) |
| 9 | + return db |
| 10 | + |
| 11 | +class User(db.Model): |
| 12 | + id_user = db.Column(db.Integer, primary_key=True) |
| 13 | + name_user = db.Column(db.String(255), nullable=False, unique=True) |
| 14 | + email_user = db.Column(db.String(255), nullable=False, unique=True) |
| 15 | + password_hash = db.Column(db.String(128), nullable=False) |
| 16 | + password_salt = db.Column(db.String(128), nullable=False) |
| 17 | + groupss = db.relationship('Group', secondary='user_group', back_populates='users') |
| 18 | + |
| 19 | + def set_password(self, password): |
| 20 | + # Generate a random salt |
| 21 | + salt = bcrypt.gensalt() |
| 22 | + |
| 23 | + # Hash the password using the salt and the bcrypt algorithm |
| 24 | + password_hash = bcrypt.hashpw(password.encode('utf-8'), salt) |
| 25 | + |
| 26 | + # Store the salt and the hashed password in the database |
| 27 | + self.password_salt = salt.decode('utf-8') |
| 28 | + self.password_hash = password_hash.decode('utf-8') |
| 29 | + |
| 30 | + def check_password(self, password): |
| 31 | + # Hash the password using the stored salt and the bcrypt algorithm |
| 32 | + password_hash = bcrypt.hashpw(password.encode('utf-8'), self.password_salt.encode('utf-8')) |
| 33 | + |
| 34 | + # Compare the hashed password to the stored password hash |
| 35 | + return password_hash == self.password_hash.encode('utf-8') |
| 36 | + |
| 37 | + |
| 38 | +class Group(db.Model): |
| 39 | + id_group = db.Column(db.Integer, primary_key=True) |
| 40 | + name_group = db.Column(db.String(255), nullable=False) |
| 41 | + # para hacer 1-n entre proyecto y encuesta |
| 42 | + topics = db.relationship('Topic', backref='group') |
| 43 | + users = db.relationship('User', secondary='user_group', back_populates='groups') |
| 44 | + |
| 45 | +class Topic(db.Model): |
| 46 | + id_topic = db.Column(db.Integer, primary_key=True) |
| 47 | + title_topic = db.Column(db.String(255), nullable=False) |
| 48 | + owner_topic = db.Column(db.Integer, nullable=False) |
| 49 | + type_topic = db.Column(db.String(255), nullable=False) |
| 50 | + start_date = db.Column(db.DateTime, nullable=False) |
| 51 | + end_date = db.Column(db.DateTime, nullable=False) |
| 52 | + status = db.Column(db.Boolean, default=False) |
| 53 | + participation = db.Column(db.Integer, nullable=False) |
| 54 | + answers = db.relationship('Topic_answer', backref='topic') |
| 55 | + tickets = db.relationship('Topic_ticket', backref='topic') |
| 56 | + |
| 57 | +class Topic_answer(db.Model): |
| 58 | + id_topic_answer = db.Column(db.Integer, primary_key=True) |
| 59 | + id_topic = db.Column(db.Integer, nullable=False) |
| 60 | + id_order = db.Column(db.Integer, nullable=False) |
| 61 | + count = db.Column(db.Integer, nullable=False) |
| 62 | + |
| 63 | +class Topic_ticket(db.Model): |
| 64 | + user_id = db.Column(db.Integer, db.ForeignKey('user.id_user'), primary_key=True) |
| 65 | + topic_id = db.Column(db.Integer, db.ForeignKey('topic.id_topic'), primary_key=True) |
| 66 | + completed = db.Column(db.Boolean, default=False) |
| 67 | + |
| 68 | +class Topic_item(db.Model): |
| 69 | + id_topic_item = db.Column(db.Integer, primary_key=True) |
| 70 | + id_topic = db.Column(db.Integer, nullable=False) |
| 71 | + id_order = db.Column(db.Integer, nullable=False) |
| 72 | + text_answers = db.Column(db.String(255), nullable=False) |
| 73 | + |
| 74 | +user_group = db.Table('user_group', |
| 75 | + db.Column('user_id', db.Integer, db.ForeignKey('user.id_user'), primary_key=True), |
| 76 | + db.Column('group_id', db.Integer, db.ForeignKey('group.id_group'), primary_key=True) ) |
| 77 | + |
| 78 | + |
| 79 | +user_topic = db.Table('user_topic', |
| 80 | + db.Column('topic_id', db.Integer, db.ForeignKey('topic.id_topic'), primary_key=True), |
| 81 | + db.Column('user_id', db.Integer, db.ForeignKey('user.id_user'), primary_key=True) ) |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +#class Stats: |
| 86 | + #projects = Project |
| 87 | + #surveys = Survey |
| 88 | + #survey = Survey |
| 89 | + #selected_project: int |
| 90 | + #selected_survey: int |
| 91 | + #survey_has_answers: int |
| 92 | + |
0 commit comments