Use local storage for csv
This commit is contained in:
Binary file not shown.
@@ -1,6 +1,7 @@
|
||||
import io
|
||||
import math
|
||||
import os
|
||||
from flask import Blueprint, jsonify, request
|
||||
from flask import Blueprint, jsonify, request, session
|
||||
from werkzeug.utils import secure_filename
|
||||
from src.services.tmdb_service import TMDBService
|
||||
from src.services.image_service import ImageService
|
||||
@@ -12,10 +13,9 @@ api_bp = Blueprint('api', __name__)
|
||||
tmdb_service = TMDBService()
|
||||
image_service = ImageService()
|
||||
|
||||
UPLOAD_FOLDER = './data'
|
||||
ALLOWED_EXTENSIONS = {'csv'}
|
||||
|
||||
csv_file = os.path.join(UPLOAD_FOLDER, 'diary.csv')
|
||||
user_csvs = {}
|
||||
|
||||
@lru_cache(maxsize=1000)
|
||||
def get_poster_url(movie_name, movie_year):
|
||||
@@ -28,27 +28,36 @@ def get_poster_url(movie_name, movie_year):
|
||||
|
||||
@api_bp.route('/movies', methods=['GET'])
|
||||
def get_movies():
|
||||
user_id = session.get('user_id') # Récupérer l'ID de l'utilisateur de la session
|
||||
|
||||
if user_id not in user_csvs:
|
||||
return jsonify({'movies': [], 'message': 'No movies found. Please upload a CSV file.'}), 200
|
||||
|
||||
# Lire le DataFrame à partir du cache
|
||||
csv_content = user_csvs[user_id]
|
||||
|
||||
try:
|
||||
if not os.path.exists(csv_file):
|
||||
return jsonify({'movies': [], 'message': 'No movies found. Please upload a CSV file.'}), 200
|
||||
# Si csv_content est une chaîne, il n'est pas nécessaire de décoder
|
||||
if isinstance(csv_content, bytes):
|
||||
df = pd.read_csv(io.StringIO(csv_content.decode('utf-8')))
|
||||
else:
|
||||
df = pd.read_csv(io.StringIO(csv_content))
|
||||
|
||||
# Reste de votre code pour la pagination et le traitement...
|
||||
page = int(request.args.get('page', 1))
|
||||
limit = int(request.args.get('limit', 10))
|
||||
|
||||
df = pd.read_csv(csv_file)
|
||||
|
||||
|
||||
df = df.iloc[::-1].reset_index(drop=True)
|
||||
|
||||
total_movies = len(df)
|
||||
total_pages = math.ceil(total_movies / limit)
|
||||
|
||||
|
||||
start_index = (page - 1) * limit
|
||||
end_index = start_index + limit
|
||||
|
||||
|
||||
df_page = df.iloc[start_index:end_index]
|
||||
|
||||
|
||||
movies = df_page.to_dict('records')
|
||||
|
||||
|
||||
movies_with_posters = [
|
||||
{
|
||||
'id': start_index + i + 1,
|
||||
@@ -59,7 +68,7 @@ def get_movies():
|
||||
}
|
||||
for i, movie in enumerate(movies)
|
||||
]
|
||||
|
||||
|
||||
return jsonify({
|
||||
'movies': movies_with_posters,
|
||||
'total': total_movies,
|
||||
@@ -88,25 +97,26 @@ def allowed_file(filename):
|
||||
|
||||
@api_bp.route('/upload-csv', methods=['POST'])
|
||||
def upload_csv():
|
||||
user_id = session.get('user_id')
|
||||
|
||||
if 'file' not in request.files:
|
||||
return jsonify({'error': 'No file part'}), 400
|
||||
file = request.files['file']
|
||||
if file.filename == '':
|
||||
return jsonify({'error': 'No selected file'}), 400
|
||||
if file and allowed_file(file.filename):
|
||||
filename = secure_filename(file.filename)
|
||||
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
||||
file.save(file_path)
|
||||
content = file.read()
|
||||
user_csvs[user_id] = content
|
||||
|
||||
try:
|
||||
df = pd.read_csv(file_path)
|
||||
df = pd.read_csv(io.StringIO(content.decode('utf-8')))
|
||||
df = df.rename(columns={
|
||||
'Date': 'Watched Date',
|
||||
'Name': 'Name',
|
||||
'Year': 'Year'
|
||||
})
|
||||
|
||||
df.to_csv(csv_file, index=False)
|
||||
user_csvs[user_id] = df.to_csv(index=False)
|
||||
|
||||
return jsonify({'success': True, 'message': 'File uploaded and processed successfully'}), 200
|
||||
except Exception as e:
|
||||
@@ -115,23 +125,27 @@ def upload_csv():
|
||||
|
||||
@api_bp.route('/check-csv', methods=['GET'])
|
||||
def check_csv():
|
||||
user_id = session.get('user_id')
|
||||
|
||||
try:
|
||||
if os.path.exists(csv_file):
|
||||
if user_id in user_csvs:
|
||||
return jsonify({'fileExists': True}), 200
|
||||
else:
|
||||
return jsonify({'fileExists': False}), 200
|
||||
except Exception as e:
|
||||
print(f"Error in check_csv: {str(e)}")
|
||||
return jsonify({'error': 'An error occurred while checking the CSV file'}), 500
|
||||
|
||||
|
||||
@api_bp.route('/delete-csv', methods=['DELETE'])
|
||||
def delete_csv():
|
||||
user_id = session.get('user_id')
|
||||
|
||||
try:
|
||||
if os.path.exists(csv_file):
|
||||
os.remove(csv_file)
|
||||
if user_id in user_csvs:
|
||||
del user_csvs[user_id]
|
||||
return jsonify({'success': True, 'message': 'CSV file deleted successfully'}), 200
|
||||
else:
|
||||
return jsonify({'error': 'CSV file not found'}), 404
|
||||
except Exception as e:
|
||||
print(f"Error in delete_csv: {str(e)}")
|
||||
return jsonify({'error': 'An error occurred while deleting the CSV file'}), 500
|
||||
return jsonify({'error': 'An error occurred while deleting the CSV file'}), 500
|
||||
|
||||
Reference in New Issue
Block a user