-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
47 lines (35 loc) · 1.61 KB
/
models.py
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
44
45
46
47
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy.orm import relationship
from dotenv import load_dotenv
import os
load_dotenv()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
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)
files = relationship('File', backref='uploader', lazy=True)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class File(db.Model):
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(128), nullable=True)
custom_route = db.Column(db.String(120), unique=True, nullable=True)
filepath = db.Column(db.String(120), nullable=False)
info = db.Column(db.String(200), nullable=True)
downloads = db.Column(db.Integer, default=0)
uploaded_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
if __name__ == '__main__':
db.create_all()