62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
@app.route('/files')
|
|
@login_required
|
|
def files():
|
|
# Get query parameters
|
|
search_query = request.args.get('search', '')
|
|
selected_category = request.args.get('category', 'all')
|
|
page = request.args.get('page', 1, type=int)
|
|
per_page = 10 # Number of documents per page
|
|
|
|
conn = get_db_connection()
|
|
|
|
# Base query
|
|
query = '''
|
|
SELECT * FROM documents
|
|
WHERE (user_id = ? OR visibility = "public"
|
|
OR id IN (SELECT document_id FROM document_shares WHERE shared_with = ?))
|
|
'''
|
|
params = [session['user_id'], session['user_id']]
|
|
|
|
# Add search condition if search query provided
|
|
if search_query:
|
|
query += ' AND (custom_filename LIKE ? OR original_filename LIKE ?)'
|
|
params.extend(['%' + search_query + '%', '%' + search_query + '%'])
|
|
|
|
# Add category filter if specific category selected
|
|
if selected_category != 'all':
|
|
query += ' AND category = ?'
|
|
params.append(selected_category)
|
|
|
|
# Count total matching documents
|
|
count_query = query.replace('SELECT *', 'SELECT COUNT(*)')
|
|
total_docs = conn.execute(count_query, params).fetchone()[0]
|
|
|
|
# Add pagination
|
|
query += ' ORDER BY created_at DESC LIMIT ? OFFSET ?'
|
|
offset = (page - 1) * per_page
|
|
params.extend([per_page, offset])
|
|
|
|
# Execute final query
|
|
documents = conn.execute(query, params).fetchall()
|
|
|
|
# Get available categories for filter dropdown
|
|
categories = ['admin', 'accounting', 'hr', 'marketing', 'legal', 'general', 'other']
|
|
|
|
# Calculate pagination info
|
|
total_pages = (total_docs + per_page - 1) // per_page # Ceiling division
|
|
pagination = {
|
|
'page': page,
|
|
'per_page': per_page,
|
|
'total': total_docs,
|
|
'pages': total_pages
|
|
}
|
|
|
|
conn.close()
|
|
|
|
return render_template('files.html',
|
|
documents=documents,
|
|
categories=categories,
|
|
selected_category=selected_category,
|
|
search_query=search_query,
|
|
pagination=pagination)
|