2020-05-22 19:55:14 +00:00
|
|
|
import traceback
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from geruecht.exceptions import DatabaseExecption
|
|
|
|
from geruecht.model.creditList import CreditList
|
2020-06-05 20:53:27 +00:00
|
|
|
from geruecht.model.user import User
|
2020-05-22 19:55:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Base:
|
|
|
|
def getCreditListFromUser(self, user, **kwargs):
|
|
|
|
try:
|
2020-06-05 21:26:15 +00:00
|
|
|
if user.uid == 'extern':
|
|
|
|
return []
|
2020-05-22 19:55:14 +00:00
|
|
|
cursor = self.db.connection.cursor()
|
|
|
|
if 'year' in kwargs:
|
2020-06-05 20:53:27 +00:00
|
|
|
sql = "select * from creditList where user_id={} and year_date={}".format(user.id if type(user) is User else user, kwargs['year'])
|
2020-05-22 19:55:14 +00:00
|
|
|
else:
|
2020-06-05 20:53:27 +00:00
|
|
|
sql = "select * from creditList where user_id={}".format(user.id if type(user) is User else user)
|
2020-05-22 19:55:14 +00:00
|
|
|
cursor.execute(sql)
|
|
|
|
data = cursor.fetchall()
|
2020-06-05 20:53:27 +00:00
|
|
|
if len(data) == 0:
|
|
|
|
return self.createCreditList(user_id=user.id, year=datetime.now().year)
|
|
|
|
elif len(data) == 1:
|
2020-05-22 19:55:14 +00:00
|
|
|
return [CreditList(data[0])]
|
|
|
|
else:
|
|
|
|
return [CreditList(value) for value in data]
|
|
|
|
except Exception as err:
|
|
|
|
traceback.print_exc()
|
|
|
|
self.db.connection.rollback()
|
|
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
|
|
|
|
|
|
|
|
def createCreditList(self, user_id, year=datetime.now().year):
|
|
|
|
try:
|
|
|
|
cursor = self.db.connection.cursor()
|
|
|
|
cursor.execute("insert into creditList (year_date, user_id) values ({},{})".format(year, user_id))
|
|
|
|
self.db.connection.commit()
|
2020-06-05 20:53:27 +00:00
|
|
|
return self.getCreditListFromUser(user_id)
|
2020-05-22 19:55:14 +00:00
|
|
|
except Exception as err:
|
|
|
|
traceback.print_exc()
|
|
|
|
self.db.connection.rollback()
|
|
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
|
|
|
|
|
|
|
|
def updateCreditList(self, creditlist):
|
|
|
|
try:
|
|
|
|
cursor = self.db.connection.cursor()
|
|
|
|
cursor.execute("select * from creditList where user_id={} and year_date={}".format(creditlist.user_id, creditlist.year))
|
|
|
|
data = cursor.fetchall()
|
|
|
|
if len(data) == 0:
|
|
|
|
self.createCreditList(creditlist.user_id, creditlist.year)
|
|
|
|
sql = "update creditList set jan_guthaben={}, jan_schulden={},feb_guthaben={}, feb_schulden={}, maer_guthaben={}, maer_schulden={}, apr_guthaben={}, apr_schulden={}, mai_guthaben={}, mai_schulden={}, jun_guthaben={}, jun_schulden={}, jul_guthaben={}, jul_schulden={}, aug_guthaben={}, aug_schulden={},sep_guthaben={}, sep_schulden={},okt_guthaben={}, okt_schulden={}, nov_guthaben={}, nov_schulden={}, dez_guthaben={}, dez_schulden={}, last_schulden={} where year_date={} and user_id={}".format(creditlist.jan_guthaben, creditlist.jan_schulden,
|
|
|
|
creditlist.feb_guthaben, creditlist.feb_schulden,
|
|
|
|
creditlist.maer_guthaben, creditlist.maer_schulden,
|
|
|
|
creditlist.apr_guthaben, creditlist.apr_schulden,
|
|
|
|
creditlist.mai_guthaben, creditlist.mai_schulden,
|
|
|
|
creditlist.jun_guthaben, creditlist.jun_schulden,
|
|
|
|
creditlist.jul_guthaben, creditlist.jul_schulden,
|
|
|
|
creditlist.aug_guthaben, creditlist.aug_schulden,
|
|
|
|
creditlist.sep_guthaben, creditlist.sep_schulden,
|
|
|
|
creditlist.okt_guthaben, creditlist.okt_schulden,
|
|
|
|
creditlist.nov_guthaben, creditlist.nov_schulden,
|
|
|
|
creditlist.dez_guthaben, creditlist.dez_schulden,
|
|
|
|
creditlist.last_schulden, creditlist.year, creditlist.user_id)
|
|
|
|
print(sql)
|
|
|
|
cursor = self.db.connection.cursor()
|
|
|
|
cursor.execute(sql)
|
|
|
|
self.db.connection.commit()
|
|
|
|
except Exception as err:
|
|
|
|
traceback.print_exc()
|
|
|
|
self.db.connection.rollback()
|
|
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|