batman
This commit is contained in:
commit
345a801c40
33 changed files with 5499 additions and 0 deletions
1
app/models/__init__.py
Normal file
1
app/models/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# Models package initialization
|
BIN
app/models/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
app/models/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
app/models/__pycache__/document.cpython-313.pyc
Normal file
BIN
app/models/__pycache__/document.cpython-313.pyc
Normal file
Binary file not shown.
BIN
app/models/__pycache__/user.cpython-313.pyc
Normal file
BIN
app/models/__pycache__/user.cpython-313.pyc
Normal file
Binary file not shown.
84
app/models/document.py
Normal file
84
app/models/document.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
from app import db
|
||||
from datetime import datetime
|
||||
from flask import url_for
|
||||
import json
|
||||
|
||||
# Association table for document-tag many-to-many relationship
|
||||
document_tags = db.Table('document_tags',
|
||||
db.Column('document_id', db.Integer, db.ForeignKey('document.id'), primary_key=True),
|
||||
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)
|
||||
)
|
||||
|
||||
class Document(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(200), nullable=False)
|
||||
content = db.Column(db.Text, nullable=False, default='')
|
||||
created_date = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_date = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
tags = db.relationship('Tag', secondary=document_tags, backref=db.backref('documents', lazy='dynamic'))
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Document {self.title}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'title': self.title,
|
||||
'content': self.content,
|
||||
'created_date': self.created_date.isoformat(),
|
||||
'updated_date': self.updated_date.isoformat(),
|
||||
'category_id': self.category_id,
|
||||
'user_id': self.user_id,
|
||||
'tags': [tag.name for tag in self.tags]
|
||||
}
|
||||
|
||||
class Category(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), nullable=False)
|
||||
icon = db.Column(db.String(100), default='mdi-folder-outline') # Material Design Icons
|
||||
description = db.Column(db.String(200))
|
||||
parent_id = db.Column(db.Integer, db.ForeignKey('category.id'))
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
is_root = db.Column(db.Boolean, default=False)
|
||||
documents = db.relationship('Document', backref='category', lazy='dynamic')
|
||||
children = db.relationship('Category', backref=db.backref('parent', remote_side=[id]), lazy='dynamic')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Category {self.name}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'icon': self.icon,
|
||||
'description': self.description,
|
||||
'parent_id': self.parent_id,
|
||||
'user_id': self.user_id,
|
||||
'is_root': self.is_root,
|
||||
'children': [child.to_dict() for child in self.children],
|
||||
'documents': [doc.id for doc in self.documents]
|
||||
}
|
||||
|
||||
class Tag(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(50), nullable=False)
|
||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||
color = db.Column(db.String(20), default='#50fa7b')
|
||||
|
||||
__table_args__ = (
|
||||
db.UniqueConstraint('name', 'user_id', name='_tag_user_uc'),
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Tag {self.name}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'user_id': self.user_id,
|
||||
'color': self.color,
|
||||
'document_count': self.documents.count()
|
||||
}
|
34
app/models/user.py
Normal file
34
app/models/user.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
from app import db, login_manager
|
||||
from flask_login import UserMixin
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from datetime import datetime
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(64), index=True, unique=True, nullable=False)
|
||||
password_hash = db.Column(db.String(128), nullable=False)
|
||||
created_date = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
theme_color = db.Column(db.String(20), default='#50fa7b') # Default green color
|
||||
documents = db.relationship('Document', backref='author', lazy='dynamic')
|
||||
categories = db.relationship('Category', backref='owner', lazy='dynamic')
|
||||
|
||||
def __repr__(self):
|
||||
return f'<User {self.username}>'
|
||||
|
||||
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)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'username': self.username,
|
||||
'created_date': self.created_date.isoformat(),
|
||||
'theme_color': self.theme_color
|
||||
}
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(id):
|
||||
return User.query.get(int(id))
|
Loading…
Add table
Add a link
Reference in a new issue