wip
This commit is contained in:
parent
627f805377
commit
02582c6b06
4 changed files with 336 additions and 12 deletions
|
@ -10,6 +10,36 @@ from sqlalchemy import or_
|
|||
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
# Helper function to convert categories to a hierarchical dictionary
|
||||
def build_category_hierarchy(categories):
|
||||
categories_dict = []
|
||||
|
||||
# Start with root categories (parent_id is None)
|
||||
root_categories = [c for c in categories if c.parent_id is None]
|
||||
|
||||
# Helper function to convert category and its children to dict
|
||||
def category_to_dict(category):
|
||||
cat_dict = {
|
||||
'id': category.id,
|
||||
'name': category.name,
|
||||
'icon': category.icon,
|
||||
'is_root': getattr(category, 'is_root', False),
|
||||
'parent_id': category.parent_id,
|
||||
'children': []
|
||||
}
|
||||
|
||||
# Find and add children
|
||||
for child in [c for c in categories if c.parent_id == category.id]:
|
||||
cat_dict['children'].append(category_to_dict(child))
|
||||
|
||||
return cat_dict
|
||||
|
||||
# Convert all root categories and their children
|
||||
for category in root_categories:
|
||||
categories_dict.append(category_to_dict(category))
|
||||
|
||||
return categories_dict
|
||||
|
||||
@main.route('/')
|
||||
@login_required
|
||||
def index():
|
||||
|
@ -46,7 +76,11 @@ def edit_document(doc_id):
|
|||
document = Document.query.filter_by(id=doc_id, user_id=current_user.id).first_or_404()
|
||||
categories = Category.query.filter_by(user_id=current_user.id).all()
|
||||
tags = Tag.query.filter_by(user_id=current_user.id).all()
|
||||
return render_template('document_edit.html', document=document, categories=categories, tags=tags)
|
||||
|
||||
# Convert categories to dictionaries for JSON serialization
|
||||
categories_dict = build_category_hierarchy(categories)
|
||||
|
||||
return render_template('document_edit.html', document=document, categories=categories_dict, tags=tags)
|
||||
|
||||
@main.route('/document/new', methods=['GET'])
|
||||
@login_required
|
||||
|
@ -79,12 +113,10 @@ def new_document():
|
|||
user_id=current_user.id
|
||||
)
|
||||
|
||||
# Make sure all categories include their children for the hierarchical dropdown
|
||||
for category in categories:
|
||||
if hasattr(category, 'to_dict'):
|
||||
setattr(category, '_serialized', category.to_dict())
|
||||
# Convert categories to dictionaries for JSON serialization
|
||||
categories_dict = build_category_hierarchy(categories)
|
||||
|
||||
return render_template('document_edit.html', document=document, categories=categories, tags=tags, preselected_category_id=preselected_category_id)
|
||||
return render_template('document_edit.html', document=document, categories=categories_dict, tags=tags, preselected_category_id=preselected_category_id)
|
||||
|
||||
@main.route('/api/document', methods=['POST'])
|
||||
@login_required
|
||||
|
@ -147,6 +179,22 @@ def delete_document(doc_id):
|
|||
db.session.commit()
|
||||
return jsonify({'success': True})
|
||||
|
||||
@main.route('/api/document/<int:doc_id>', methods=['PATCH'])
|
||||
@login_required
|
||||
def update_document(doc_id):
|
||||
"""Update a document's properties (like category)"""
|
||||
document = Document.query.filter_by(id=doc_id, user_id=current_user.id).first_or_404()
|
||||
data = request.json
|
||||
|
||||
# Update category if provided
|
||||
if 'category_id' in data and data['category_id']:
|
||||
# Verify the category exists and belongs to user
|
||||
category = Category.query.filter_by(id=data['category_id'], user_id=current_user.id).first_or_404()
|
||||
document.category_id = category.id
|
||||
|
||||
db.session.commit()
|
||||
return jsonify(document.to_dict())
|
||||
|
||||
@main.route('/api/category', methods=['POST'])
|
||||
@login_required
|
||||
def save_category():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue