11from app .home import bp
22from flask_login import login_required , current_user
3- from flask import render_template , jsonify , redirect , url_for
3+ from flask import render_template , jsonify , redirect , url_for , request , current_app
44from app .extensions import db
55from app .models .favoris import FAVORIS
66from app .models .fichier import FICHIER
77from app .models .a_recherche import A_RECHERCHE
88from app .forms .search_form import SearchForm
99from datetime import datetime
1010from app .utils import check_notitications
11+ from app .models .dossier import DOSSIER
12+ from app .utils import Whoosh
13+ from app import socketio
14+ from fasteners import InterProcessLock
1115
1216
1317@bp .route ("/" , methods = ["GET" , "POST" ])
@@ -25,28 +29,57 @@ def home():
2529 Returns:
2630 The rendered home page template.
2731 """
28- favorite_files = get_files_favoris (current_user .id_Utilisateur )
29- researches = get_user_researches (current_user .id_Utilisateur )
32+ form = SearchForm ()
33+
34+ query = ""
35+ whoosh = Whoosh ()
3036 form = SearchForm ()
3137 if form .validate_on_submit ():
3238 add_research (current_user .id_Utilisateur , form .search .data )
33- return redirect (url_for ("search.search" , query = form .search .data ))
39+ query = form .search .data
40+ results = whoosh .search (query )
41+ results = create_rendered_list (results )
42+ favorite_files = get_files_favoris (current_user .id_Utilisateur )
43+ researches = get_user_researches (current_user .id_Utilisateur )
3444 return render_template (
3545 "home/index.html" ,
3646 is_authenticated = True ,
3747 is_admin = current_user .is_admin (),
3848 has_notifications = check_notitications (),
3949 favorite_files = favorite_files ,
4050 researches = researches ,
51+ folders = results ,
52+ query = query ,
4153 form = form ,
4254 )
4355
4456
45- @bp .route ("/favori/<int:id_file>" , methods = ["DELETE" ])
57+ @bp .route ("/favori/<int:id_file>" , methods = ["POST" , " DELETE" ])
4658@login_required
47- def unfavorize (id_file ):
48- unfavorite_file (id_file , current_user .get_id ())
49- return jsonify ({"status" : "ok" })
59+ def favorize (id_file ):
60+ """
61+ Favorize or unfavorize a file for the current user.
62+
63+ Args:
64+ id_file (int): The ID of the file to favorize or unfavorize.
65+
66+ Returns:
67+ dict: A JSON response indicating the status of the operation.
68+ The response will have a 'status' key with the value 'ok'.
69+ """
70+ if request .method == "POST" :
71+ db .session .execute (
72+ FAVORIS .insert ().values (
73+ id_Fichier = id_file , id_Utilisateur = current_user .id_Utilisateur
74+ )
75+ )
76+ else :
77+ db .session .query (FAVORIS ).filter (
78+ FAVORIS .c .id_Fichier == id_file ,
79+ FAVORIS .c .id_Utilisateur == current_user .id_Utilisateur ,
80+ ).delete ()
81+ db .session .commit ()
82+ return jsonify ({"file" : id_file }), 200
5083
5184
5285def get_files_favoris (user_id ):
@@ -111,16 +144,76 @@ def add_research(user_id, search):
111144 db .session .commit ()
112145 db .session .commit ()
113146
147+ #--------------------------------------------------------------------------
114148
115- def unfavorite_file ( file_id , user_id ):
149+ def create_folder_dict ( folder , files ):
116150 """
117- Remove a file from the favorites of a user .
151+ Create a dictionary representation of a folder .
118152
119153 Args:
120- file_id (int): The ID of the file.
121- user_id (int): The ID of the user.
154+ folder (Folder): The folder object.
155+ files (list): A list of file objects.
156+
157+ Returns:
158+ dict: A dictionary representation of the folder, including its name, files, color, id, and subfolders.
122159 """
123- db .session .query (FAVORIS ).filter (
124- FAVORIS .c .id_Fichier == file_id , FAVORIS .c .id_Utilisateur == user_id
125- ).delete ()
126- db .session .commit ()
160+ files_in_folder = [
161+ result for result in files if result ["path" ] == (str (folder .id_Dossier ))
162+ ]
163+ subfolders = recursive_subfolder (folder , files )
164+ return {
165+ "name" : folder .nom_Dossier ,
166+ "files" : files_in_folder ,
167+ "color" : folder .couleur_Dossier ,
168+ "id" : folder .id_Dossier ,
169+ "subfolder" : subfolders
170+ }
171+
172+ def create_rendered_list (results ):
173+ """
174+ Create a rendered list of folders and their associated results.
175+
176+ Args:
177+ results (list): A list of results.
178+
179+ Returns:
180+ list: A list of dictionaries representing folders and their associated results.
181+ """
182+ folders = db .session .query (DOSSIER ).order_by (DOSSIER .priorite_Dossier ).all ()
183+ folders_root = [folder for folder in folders if folder .DOSSIER == [] and is_accessible (folder )]
184+ return [create_folder_dict (folder , results ) for folder in folders_root ]
185+
186+
187+ def recursive_subfolder (folder , files ):
188+ """
189+ Recursively searches for subfolders in the given folder and creates a list of dictionaries
190+ containing information about each subfolder.
191+
192+ Args:
193+ folder (str): The path of the folder to search for subfolders.
194+ files (list): A list of files to include in the dictionaries.
195+
196+ Returns:
197+ list: A list of dictionaries containing information about each subfolder.
198+ """
199+ return [create_folder_dict (subfolder , files ) for subfolder in folder .DOSSIER_ if is_accessible (subfolder )]
200+
201+ def is_accessible (folder ):
202+ """
203+ Check if the current user has access to the given folder.
204+
205+ Args:
206+ folder (Folder): The folder to check.
207+
208+ Returns:
209+ bool: True if the user has access to the folder, False otherwise.
210+ """
211+ return any (current_user .id_Role == role .id_Role for role in folder .ROLE )
212+
213+ @socketio .on ('search_files' , namespace = '/home' )
214+ def search_files (data ):
215+ search_query = data .get ("query" )
216+ with InterProcessLock (f"{ current_app .root_path } /whoosh.lock" ):
217+ search_results = Whoosh ().search (search_query , path = f'{ data .get ("folderId" )} ' )
218+ search_results = [result ["id" ] for result in search_results ]
219+ socketio .emit ("search_results" , {'query' : search_query , 'results' : search_results , 'folderId' : data .get ("folderId" )}, namespace = "/home" )
0 commit comments