🔧✉️ Refactor Invoice Emailing Utility

- 🌐 Enhanced web interface for invoice emailing config management with GET/POST at '/data'.
- 📝 Refactored Flask app to specialize as an invoice emailing utility.
- 🔍 Improved read_yaml() to return data and error messages.
- 📤 Enhanced write_yaml() for better YAML file handling.
- 🛠️ Updated handle_data() to efficiently handle '/data' route with GET/POST methods.

Note: Excluded changes to imports and requires.
This commit is contained in:
Isaak Buslovich 2023-12-23 00:12:43 +01:00
parent 59f4ce91fc
commit 2236de57ce
Signed by: Isaak
GPG Key ID: EEC31D6437FBCC63

41
main.py
View File

@ -1,8 +1,8 @@
"""
Flask app for YAML file I/O.
Invoice Emailing Utility.
Provides web interface for reading and updating YAML file content.
Handles HTTP GET (view) and POST (update) at '/data', and serves index page at '/'.
Provides a web interface for managing invoice emailing configurations.
Handles HTTP GET (view) and POST (update) at '/data' for configuration data, and serves the main app page at '/'.
Author: Isaak Buslovich
Date: 2023-12-21
@ -15,16 +15,15 @@ import logging
app = Flask(__name__, static_folder='.', static_url_path='')
logging.basicConfig(level=logging.INFO)
DATA_FILE = 'config.yaml' # Ensure this is the correct path to your YAML file
DATA_FILE = 'config.yaml'
def read_yaml():
"""
Reads and returns content from the YAML file.
Returns:
dict: A dictionary with the key 'email' and 'email_template' containing their respective contents.
None: If an error occurs, along with an error message.
Reads data from YAML file specified by DATA_FILE. Returns a tuple containing the data and error message.
On success: Returns (data_dict, None), where data_dict contains 'email' and 'email_template'.
On file not found: Logs a message and returns ({'email': {}, 'email_template': ''}, None).
On error: Logs the specific error and returns (None, error_message), where error_message explains the issue.
"""
file_path = Path(DATA_FILE)
try:
@ -50,15 +49,17 @@ def read_yaml():
def write_yaml(email_data, email_template_data):
"""
Writes content to the YAML file.
Writes email and template data to the YAML file defined by DATA_FILE.
Args:
email_data (dict): The data for the 'email' section.
email_template_data (str): The data for the 'email_template' section.
email_data (dict): Email configuration settings.
email_template_data (str): Email template content.
Returns:
bool: True if the operation was successful, False otherwise.
None: If successful, or an error message if an error occurs.
tuple: (bool, str) indicating success status and an error message if any.
Raises:
Exception: Logs and returns error details upon failure.
"""
file_path = Path(DATA_FILE)
try:
@ -76,13 +77,11 @@ def write_yaml(email_data, email_template_data):
@app.route('/data', methods=['GET', 'POST'])
def handle_data():
"""
Handles GET and POST requests to '/data'.
GET: Returns the content of the YAML file.
POST: Updates the content of the YAML file.
Returns:
JSON response for GET requests.
JSON response for POST requests indicating success or failure.
Flask route handler function for '/data', accepting both GET and POST methods. - GET method: Reads and outputs
data from the YAML configuration file. In case of errors, it returns a 500 status code with an error message. -
POST method: Gets 'email' and 'email_template' data from the request, writes it to the YAML configuration file.
In case of write errors, it returns a 500 status code with an error message. Otherwise, it returns a message
stating the file was saved successfully with a 200 status code.
"""
if request.method == 'GET':
data, error = read_yaml()