def delete_account(): email, hashed = retrive_data().values() current_user = Trava_Users.query.filter_by(email=email).first() if current_user and current_user.password and current_user.password == hashed: # Select account pictures user_pictures = current_user.picture # Delete account pictures for pic in user_pictures: # print(pic) db.session.delete(pic) # Delete account db.session.delete(current_user) db.session.commit() # Delete account folder pictures from AWS bucket = s3_resource.Bucket(os.getenv('my_bucket_name')) bucket.objects.filter(Prefix=f'{current_user.id}/').delete() return jsonify({ 'message': 'You\'ve syccessfully deleted your Trava account!', 'state': 'success' }), 200 else: return jsonify({'message': 'Authorized!', 'state': 'error'}), 401
def delete_picture(): if request.method == 'POST': to_delete, token = request.get_json().values() # ----- Validating token & get user_id ----- user_id = validate_token(token) if not user_id: return jsonify({ 'message': 'Invalid token!', 'state': 'error' }), 401 # ----- Delete from db and delete from AWS ----- bucket = s3_resource.Bucket(os.getenv('my_bucket_name')) for d in to_delete: record = Trava_Pictures.query.get_or_404(int(d)) db.session.delete(record) bucket.objects.filter(Prefix=f'{user_id}/{d}').delete() db.session.commit() return jsonify({ 'message': 'Picture/s deleted successfully', 'state': 'success' }) return jsonify({'message': 'Page not found!', 'state': 'error'}), 404
def add_picture(): if request.method == 'POST' and request.files: # Fetch request data image_file = request.files['imageFile'] image_text = request.form['imageText'] token = request.form['token'] # ----- Validating token & get user_id ----- user_id = validate_token(token) if not user_id: return jsonify({ 'message': 'Invalid token!', 'state': 'error' }), 401 # ----- Fetch current_user ----- current_user = Trava_Users.query.get(user_id) # ----- Save to db ----- new_picture = Trava_Pictures(user_id=user_id, image_text=image_text, image_url='') db.session.add(new_picture) db.session.commit() image_id = new_picture.id # ----- Upload file to AWS S3 ----- # print('\n',image_file.__dict__) image_format = image_file.filename.split('.')[-1].strip() image_name = f'{current_user.id}/{image_id}.{image_format}' my_bucket_name = os.getenv('my_bucket_name') s3_resource.Bucket(my_bucket_name).put_object(Body=image_file, Key=image_name, ACL='public-read') image_url = f'https://{my_bucket_name}.s3.{os.getenv("AWS_DEFAULT_REGION")}.amazonaws.com/{image_name}' # print(image_url) # ----- update image_url in db ----- new_picture.image_url = image_url db.session.commit() return jsonify({'image': image_url, 'text': image_text}), 200 return jsonify({'message': 'Page not found!', 'state': 'error'}), 404
def save_images(project_id, images_data): # upload images to s3 bucket and save url to db my_bucket_name = os.getenv('my_bucket_name') image_count = 0 # _____________________________ thumbnail = JFW_Images.query.filter_by(thumbnail='thumbnail').filter_by( project_id=project_id).first() if thumbnail: image_count += 1 # _____________________________ for img in images_data: if img.filename == '': break image_name = f'projects_images/id_{project_id}/{img.filename}' s3_resource.Bucket(my_bucket_name).put_object(Body=img, Key=image_name, ACL='public-read') image_url = f'https://foxcode-project-002.s3.eu-central-1.amazonaws.com/projects_images/id_{project_id}/{img.filename}' if not image_count: thumbnail = 'thumbnail' else: thumbnail = '' image_count += 1 new_image = JFW_Images(url=image_url, use='project_image', project_id=project_id, thumbnail=thumbnail) db.session.add(new_image) # _____________________________ # ----------------------------------------------------------------------
def delete_project(id): project = JFW_Projects.query.get(id) if project: # --- delete images from db JFW_Images.query.filter_by(project_id=id).delete() # --- delete project from db db.session.delete(project) db.session.commit() # --- images delete from AWS S3 bucket = s3_resource.Bucket(os.getenv('my_bucket_name')) bucket.objects.filter(Prefix=f'projects_images/id_{id}').delete() flash('Project has been removed successfully!', 'flash flash--warning') return redirect(url_for('my_projects.all_projects'))