budget/addincome.py

133 líneas
3,6 KiB
Python
Original Vista normal Històric

2020-12-24 14:13:31 +01:00
from datetime import datetime, timezone, timedelta
import time
import os
import sys
import os.path
import psycopg2
import dateutil
from dateutil.parser import parse
from decimal import *
getcontext().prec = 2
###############################################################################
# get income rows
###############################################################################
def print_incomes():
conn = None
try:
conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute("SELECT datetime, donations, owner FROM incomes")
rows = cur.fetchall()
for row in rows:
date = row[0].date().strftime('%d.%m.%Y')
print(date,' donation: '+ str(row[1]), 'server owner: ' + str(row[2]))
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
###################################################################################
# add incomes to database
###################################################################################
def insert_incomes(incomedate, donationincome, ownerincome):
sql = "INSERT INTO incomes(datetime, donations, owner) VALUES(%s,%s,%s)"
conn = None
try:
conn = psycopg2.connect(database = budget_db, user = budget_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute(sql, (incomedate, donationincome, ownerincome))
print("\n")
print("Updating incomes...")
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
###############################################################################
# INITIALISATION
###############################################################################
# Returns the parameter from the specified file
def get_parameter( parameter, file_path ):
# Check if db_config.txt file exists
if not os.path.isfile(file_path):
if file_path == "config/db_config.txt":
print("File %s not found, exiting. Run db-setup.py."%file_path)
sys.exit(0)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
print("Run setup.py")
sys.exit(0)
# Load configuration from config file
config_filepath = "config/db_config.txt"
budget_db = get_parameter("budget_db", config_filepath) # E.g., budget
budget_db_user = get_parameter("budget_db_user", config_filepath) # E.g., mastodon
###############################################################################
while True:
donationincome = input("Donation income? (q to quit) ")
if donationincome == '':
donationincome = '0.00'
elif donationincome == 'q':
sys.exit("Bye")
donationincome = round(float(donationincome),2)
ownerincome = input("Server owner income? ")
if ownerincome == '':
ownerincome = '0.00'
ownerincome = round(float(ownerincome),2)
incomedate = datetime.strptime(input('Income date in the format dd.mm.yyyy? '), '%d.%m.%Y')
now = datetime.now()
incomedate = incomedate.replace(hour=now.hour, minute=now.minute, second=now.second)
insert_incomes(incomedate, donationincome, ownerincome)
print_incomes()