Refactored and added PrettyTables!

master
spla 4 months ago
parent 05f097a048
commit 60a5be7b3d
  1. 228
      edit_status.py
  2. 1
      requirements.txt

@ -8,123 +8,98 @@ import os
import sys
import os.path
import psycopg2
from prettytable import PrettyTable
##############################################################################
# ask what user to be edit
##############################################################################
def ask_what_user():
def get_user(email):
try:
found_it = False
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:
try:
if useremail != 'q':
print("Enter an existent email address!")
sys.exit("Good bye!")
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
###############################################################################
# get user row
###############################################################################
cur = conn.cursor()
def get_user(email):
cur.execute("SELECT account_id, username, email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, feedback FROM " + mailing_db_table + " where email=(%s)", (email,))
try:
row = cur.fetchone()
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
if row != None:
cur = conn.cursor()
found_it = True
cur.execute("SELECT account_id, username, email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, feedback FROM " + mailing_db_table + " where email=(%s)", (email,))
user_id = row[0]
user_name = row[1]
user_email = row[2]
emailed_at = row[3].replace(tzinfo=None)
deleted = row[4]
elapsed_days = row[5]
to_be_deleted = row[6]
recipient_error = row[7]
user_feedback = row[8]
row = cur.fetchone()
else:
user_id = row[0]
user_name = row[1]
user_email = row[2]
emailed_at = row[3].replace(tzinfo=None)
deleted = row[4]
elapsed_days = row[5]
to_be_deleted = row[6]
recipient_error = row[7]
user_feedback = row[8]
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)
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
return (found_it, user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback)
print(error)
except (Exception, psycopg2.DatabaseError) as error:
finally:
print(error)
if conn is not None:
finally:
conn.close()
if conn is not None:
###################################################################################
# write to database mailing status of inactive users
###################################################################################
conn.close()
def update_user(will_be_deleted, recip_error, user_feedback, id):
conn = None
try:
conn = None
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
try:
cur = conn.cursor()
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
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))
cur = conn.cursor()
conn.commit()
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))
cur.close()
print(f"\nUpdating user {str(id)}")
except (Exception, psycopg2.DatabaseError) as error:
conn.commit()
print(error)
cur.close()
finally:
except (Exception, psycopg2.DatabaseError) as error:
if conn is not None:
print(error)
conn.close()
finally:
##################################################################################
# print user data
##################################################################################
if conn is not None:
def print_user(user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback):
conn.close()
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,44 +109,81 @@ 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)
# Load configuration from config file
config_filepath = "config.txt"
mastodon_db = get_parameter("mastodon_db", config_filepath)
mastodon_db_user = get_parameter("mastodon_db_user", config_filepath)
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)
def db_config():
# Load configuration from config file
config_filepath = "config.txt"
mastodon_db = get_parameter("mastodon_db", config_filepath)
mastodon_db_user = get_parameter("mastodon_db_user", config_filepath)
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:
useremail = input("Enter user email address: (press q to quit) ")
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':
willdeleteit = 'False'
elif willdeleteit == 't':
willdeleteit = 'True'
recip_error = input("Was the email refused? (press t for True or f for False) ")
if recip_error == 'f':
recip_error = 'False'
elif recip_error == 't':
recip_error = 'True'
user_feed = input("Have the user replied? (press t for True or f for False) ")
if user_feed == 'f':
user_feed = 'False'
elif user_feed == 't':
user_feed = 'True'
update_user(willdeleteit, recip_error, user_feed, user_id)
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()
while True:
print_table.field_names = ['id', 'username', 'email', 'emailed_at', 'deleted', 'elapsed_days', 'to_be_deleted', 'recipient_error', 'user_feedback']
user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback = ask_what_user()
print_table.add_row([user_id, user_name, user_email, emailed_at, deleted, elapsed_days, to_be_deleted, recipient_error, user_feedback])
print_user(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':
willdeleteit = 'False'
elif willdeleteit == 't':
willdeleteit = 'True'
else:
recip_error = input("Was the email refused? (press t for True or f for False) ")
if recip_error == 'f':
recip_error = 'False'
elif recip_error == 't':
recip_error = 'True'
print(f'email {useremail} not found!')
user_feed = input("Have the user replied? (press t for True or f for False) ")
if user_feed == 'f':
user_feed = 'False'
elif user_feed == 't':
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)

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

Loading…
Cancel
Save