Refactored and added PrettyTables!

This commit is contained in:
spla 2022-08-02 14:26:30 +02:00
pare 05f097a048
commit 60a5be7b3d
S'han modificat 2 arxius amb 125 adicions i 112 eliminacions

Veure arxiu

@ -8,33 +8,12 @@ import os
import sys
import os.path
import psycopg2
##############################################################################
# ask what user to be edit
##############################################################################
def ask_what_user():
try:
useremail = input("Enter user email address: (press q to quit) ")
if useremail == 'q':
sys.exit()
else:
user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback = get_user(useremail)
return (user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback)
except:
if useremail != 'q':
print("Enter an existent email address!")
sys.exit("Good bye!")
###############################################################################
# get user row
###############################################################################
from prettytable import PrettyTable
def get_user(email):
found_it = False
try:
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
@ -45,6 +24,10 @@ def get_user(email):
row = cur.fetchone()
if row != None:
found_it = True
user_id = row[0]
user_name = row[1]
user_email = row[2]
@ -55,8 +38,21 @@ def get_user(email):
recipient_error = row[7]
user_feedback = row[8]
else:
user_id = ''
user_name = ''
user_email = ''
emailed_at = ''
deleted = False
elapsed_days = '0'
to_be_deleted = False
recipient_error = False
user_feedback = False
cur.close()
return (user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback)
return (found_it, user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback)
except (Exception, psycopg2.DatabaseError) as error:
@ -68,10 +64,6 @@ def get_user(email):
conn.close()
###################################################################################
# write to database mailing status of inactive users
###################################################################################
def update_user(will_be_deleted, recip_error, user_feedback, id):
conn = None
@ -83,8 +75,8 @@ def update_user(will_be_deleted, recip_error, user_feedback, id):
cur = conn.cursor()
cur.execute("UPDATE " + mailing_db_table + " SET to_be_deleted=(%s), recipient_error=(%s), feedback=(%s) where account_id=(%s)", (will_be_deleted, recip_error, user_feedback, id))
print("\n")
print("Updating user " + str(id))
print(f"\nUpdating user {str(id)}")
conn.commit()
@ -100,31 +92,14 @@ def update_user(will_be_deleted, recip_error, user_feedback, id):
conn.close()
##################################################################################
# print user data
##################################################################################
def print_user(user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback):
print("\n")
print("--------------------------------------------------------------------------------------------------------------------------------")
print("| account_id: " + str(user_id) + " | username: " + str(user_name) + " | email: " + str(user_email) + " | emailed at: " + str(emailed_at) + " |")
print("| deleted: " + str(deleted) + " | elapsed days: " + str(elapsed_days) + " | to be deleted: " + str(to_be_deleted) + " | recipient error: " + str(recipient_error) + " | user feedback: " +str(user_feedback) + " |")
print("--------------------------------------------------------------------------------------------------------------------------------")
print("\n")
###############################################################################
# INITIALISATION
###############################################################################
# Returns the parameter from the specified file
def get_parameter( parameter, file_path ):
# Check if secrets file exists
if not os.path.isfile(file_path):
if file_path == "secrets/secrets.txt":
print("File %s not found, exiting. Run setup.py."%file_path)
print(f"File {file_path} not found, exiting. Run setup.py.")
elif file_path == "config.txt":
print("File %s not found, exiting. Run db-setup.py."%file_path)
print(f"File {file_path} not found, exiting. Run db-setup.py.")
sys.exit(0)
# Find parameter in file
@ -134,10 +109,11 @@ def get_parameter( parameter, file_path ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
print("Run setup.py")
print(f"{file_path} Missing parameter {parameter}\nRun setup.py")
sys.exit(0)
def db_config():
# Load configuration from config file
config_filepath = "config.txt"
mastodon_db = get_parameter("mastodon_db", config_filepath)
@ -146,13 +122,35 @@ mailing_db = get_parameter("mailing_db", config_filepath)
mailing_db_user = get_parameter("mailing_db_user", config_filepath)
mailing_db_table = get_parameter("mailing_db_table", config_filepath)
###############################################################################
return (mastodon_db, mastodon_db_user, mailing_db, mailing_db_user, mailing_db_table)
# main
if __name__ == '__main__':
mastodon_db, mastodon_db_user, mailing_db, mailing_db_user, mailing_db_table = db_config()
while True:
user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback = ask_what_user()
useremail = input("Enter user email address: (press q to quit) ")
print_user(user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback)
if useremail == 'q':
sys.exit('Bye.')
else:
found_it, user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback = get_user(useremail)
if found_it:
print_table = PrettyTable()
print_table.field_names = ['id', 'username', 'email', 'emailed_at', 'deleted', 'elapsed_days', 'to_be_deleted', 'recipient_error', 'user_feedback']
print_table.add_row([user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback])
print(f'\n{print_table}\n')
willdeleteit = input("Do you want to mark user to be deleted? (press t for True or f for False) ")
if willdeleteit == 'f':
@ -173,5 +171,19 @@ while True:
user_feed = 'True'
update_user(willdeleteit, recip_error, user_feed, user_id)
user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback = get_user(user_email)
print_user(user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback)
found_it, user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback = get_user(useremail)
print_table = PrettyTable()
print_table.field_names = ['id', 'username', 'email', 'emailed_at', 'deleted', 'elapsed_days', 'to_be_deleted', 'recipient_error', 'user_feedback']
print_table.add_row([user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback])
print(f'\n{print_table}\n')
else:
print(f'email {useremail} not found!')

Veure arxiu

@ -1 +1,2 @@
psycopg2-binary
prettytable