🔧✉️ 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:
parent
59f4ce91fc
commit
2236de57ce
41
main.py
41
main.py
@ -1,8 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
Flask app for YAML file I/O.
|
Invoice Emailing Utility.
|
||||||
|
|
||||||
Provides web interface for reading and updating YAML file content.
|
Provides a web interface for managing invoice emailing configurations.
|
||||||
Handles HTTP GET (view) and POST (update) at '/data', and serves index page at '/'.
|
Handles HTTP GET (view) and POST (update) at '/data' for configuration data, and serves the main app page at '/'.
|
||||||
|
|
||||||
Author: Isaak Buslovich
|
Author: Isaak Buslovich
|
||||||
Date: 2023-12-21
|
Date: 2023-12-21
|
||||||
@ -15,16 +15,15 @@ import logging
|
|||||||
app = Flask(__name__, static_folder='.', static_url_path='')
|
app = Flask(__name__, static_folder='.', static_url_path='')
|
||||||
logging.basicConfig(level=logging.INFO)
|
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():
|
def read_yaml():
|
||||||
"""
|
"""
|
||||||
Reads and returns content from the YAML file.
|
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'.
|
||||||
Returns:
|
On file not found: Logs a message and returns ({'email': {}, 'email_template': ''}, None).
|
||||||
dict: A dictionary with the key 'email' and 'email_template' containing their respective contents.
|
On error: Logs the specific error and returns (None, error_message), where error_message explains the issue.
|
||||||
None: If an error occurs, along with an error message.
|
|
||||||
"""
|
"""
|
||||||
file_path = Path(DATA_FILE)
|
file_path = Path(DATA_FILE)
|
||||||
try:
|
try:
|
||||||
@ -50,15 +49,17 @@ def read_yaml():
|
|||||||
|
|
||||||
def write_yaml(email_data, email_template_data):
|
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:
|
Args:
|
||||||
email_data (dict): The data for the 'email' section.
|
email_data (dict): Email configuration settings.
|
||||||
email_template_data (str): The data for the 'email_template' section.
|
email_template_data (str): Email template content.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if the operation was successful, False otherwise.
|
tuple: (bool, str) indicating success status and an error message if any.
|
||||||
None: If successful, or an error message if an error occurs.
|
|
||||||
|
Raises:
|
||||||
|
Exception: Logs and returns error details upon failure.
|
||||||
"""
|
"""
|
||||||
file_path = Path(DATA_FILE)
|
file_path = Path(DATA_FILE)
|
||||||
try:
|
try:
|
||||||
@ -76,13 +77,11 @@ def write_yaml(email_data, email_template_data):
|
|||||||
@app.route('/data', methods=['GET', 'POST'])
|
@app.route('/data', methods=['GET', 'POST'])
|
||||||
def handle_data():
|
def handle_data():
|
||||||
"""
|
"""
|
||||||
Handles GET and POST requests to '/data'.
|
Flask route handler function for '/data', accepting both GET and POST methods. - GET method: Reads and outputs
|
||||||
GET: Returns the content of the YAML file.
|
data from the YAML configuration file. In case of errors, it returns a 500 status code with an error message. -
|
||||||
POST: Updates the content of the YAML file.
|
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
|
||||||
Returns:
|
stating the file was saved successfully with a 200 status code.
|
||||||
JSON response for GET requests.
|
|
||||||
JSON response for POST requests indicating success or failure.
|
|
||||||
"""
|
"""
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
data, error = read_yaml()
|
data, error = read_yaml()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user