Skip to content

Commit c794e1c

Browse files
authored
Merge pull request ThePhrontistery#1 from PilarLap/alabarchanges
commit1 alabar models
2 parents db0122e + 37245f5 commit c794e1c

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.poetryPath": "C:\\Users\\plapayes\\AppData\\Roaming\\pypoetry\\venv\\Scripts\\poetry.exe"
3+
}

alabar/__init__.py

Whitespace-only changes.

alabar/data.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

alabar/middleware.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from flask import request, redirect, url_for, session
2+
3+
4+
def authenticate_handler(response):
5+
"Middleware to enforce redirect to login if not authenticated (not the 'static' route)"
6+
7+
if not request.endpoint:
8+
return redirect(url_for('page_not_found'))
9+
10+
if request.endpoint in 'static':
11+
return response
12+
13+
if request.endpoint not in ('login', 'logout') and 'CURRENT_USER' not in session:
14+
return redirect(url_for('login', next=request.url))
15+
16+
return response
17+

alabar/models.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+

alabar/views.py

Whitespace-only changes.

app.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
from flask import Flask, redirect, render_template, request, session, url_for
22
from flask_sqlalchemy import SQLAlchemy
3+
from alabar.models import init_app
34

45
app = Flask(__name__)
56

7+
# The SECRET_KEY is used to encrypt session data in (persistent) cookies.
8+
# >>> import secrets; secrets.token_hex(32)
9+
app.config['SECRET_KEY'] = '642918690903c342d812d16cd33a4de4c8692483462550c9ddcd4303621cc1b2'
10+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///alabar.db'
11+
# app.config['EXPLAIN_TEMPLATE_LOADING'] = True
12+
db = init_app(app)
13+
#app.register_blueprint(crewverve_bp)
14+
#app.register_blueprint(admin_bp)
15+
16+
app.app_context().push()
17+
db.create_all()
18+
619

720
@app.route('/')
821
def index():
9-
return render_template('index.html')
22+
return "Hola mundo"
1023

1124
if __name__ == '__main__':
1225
app.run(debug=True)

0 commit comments

Comments
 (0)