Много изменений в бд
This commit is contained in:
78
db/db.py
78
db/db.py
@@ -34,16 +34,17 @@ class Database:
|
||||
is_updated = True
|
||||
for act in activities:
|
||||
if is_updated:
|
||||
tags = self.cursor.execute("SELECT * FROM tags WHERE id = ?", (act.tag_id, )).fetchall()
|
||||
vetro_projects = self.cursor.execute("SELECT * FROM vetro_projects WHERE id = ?", (act.vetro_project_id, )).fetchall()
|
||||
tags = self.cursor.execute("SELECT * FROM tag WHERE id = ?", (act.tag_id, )).fetchall()
|
||||
vetro_projects = self.cursor.execute("SELECT * FROM vetro_project WHERE id = ?", (act.vetro_project_id, )).fetchall()
|
||||
if not tags or not vetro_projects:
|
||||
is_updated = False
|
||||
try:
|
||||
self.cursor.execute(
|
||||
"INSERT INTO activities VALUES(?,?,?,?,?,?,?,?,?)",
|
||||
"INSERT INTO activity VALUES(?,?,?,?,?,?,?,?,?,?)",
|
||||
(
|
||||
act.id,
|
||||
act.description,
|
||||
act.author_id,
|
||||
act.time_spent,
|
||||
act.date_start,
|
||||
act.date_end,
|
||||
@@ -55,7 +56,7 @@ class Database:
|
||||
)
|
||||
except sq.IntegrityError:
|
||||
self.cursor.execute(
|
||||
"""UPDATE activities
|
||||
"""UPDATE activity
|
||||
SET description = ?, time_spent = ?, date_start = ?, date_end = ?, is_tracked = ?, vetro_project_id = ?, tag_id = ?, project_id = ?
|
||||
WHERE id = ?""",
|
||||
(
|
||||
@@ -81,15 +82,16 @@ class Database:
|
||||
try:
|
||||
self.cursor.execute(
|
||||
"""INSERT INTO time_entry
|
||||
(date, description, activity_id, time_spent, task_id, activity_type_id)
|
||||
VALUES(?,?,?,?,?,?)""",
|
||||
(date, description, activity_id, time_spent, task_id, activity_type_id, author_id)
|
||||
VALUES(?,?,?,?,?,?,?)""",
|
||||
(
|
||||
act.date_start.date(),
|
||||
act.description,
|
||||
act.id,
|
||||
act.time_spent,
|
||||
act.task_id,
|
||||
ActivityTypes.DEVELOPMENT
|
||||
ActivityTypes.DEVELOPMENT,
|
||||
act.author_id
|
||||
)
|
||||
)
|
||||
self.con.commit()
|
||||
@@ -101,10 +103,10 @@ class Database:
|
||||
"""Сохраняет в базу данных теги Clockify"""
|
||||
for tag in tags:
|
||||
try:
|
||||
self.cursor.execute("INSERT INTO tags VALUES(?, ?)",(tag.id,tag.title))
|
||||
self.cursor.execute("INSERT INTO tag VALUES(?, ?)",(tag.id,tag.title))
|
||||
except sq.IntegrityError:
|
||||
self.cursor.execute(
|
||||
"""UPDATE tags
|
||||
"""UPDATE tag
|
||||
SET title = ?
|
||||
WHERE id = ?""",
|
||||
(tag.title, tag.id)
|
||||
@@ -117,10 +119,10 @@ class Database:
|
||||
"""Сохраняет в базу данных Проекты (ДО) Ветро Clockify"""
|
||||
for vetro_project in vetro_projects:
|
||||
try:
|
||||
self.cursor.execute("INSERT INTO vetro_projects VALUES(?, ?)", (vetro_project.id, vetro_project.title))
|
||||
self.cursor.execute("INSERT INTO vetro_project VALUES(?, ?)", (vetro_project.id, vetro_project.title))
|
||||
except sq.IntegrityError:
|
||||
self.cursor.execute("""
|
||||
UPDATE vetro_projects
|
||||
UPDATE vetro_project
|
||||
SET title = ?
|
||||
WHERE id = ?""",
|
||||
(vetro_project.title, vetro_project.id)
|
||||
@@ -132,13 +134,63 @@ class Database:
|
||||
"""Сохраняет в базу данных Деятельности Redmine"""
|
||||
for activity_type in activity_types:
|
||||
try:
|
||||
self.cursor.execute("INSERT INTO activity_types VALUES(?, ?)", (activity_type.id, activity_type.title))
|
||||
self.cursor.execute("INSERT INTO activity_type VALUES(?, ?)", (activity_type.id, activity_type.title))
|
||||
except sq.IntegrityError:
|
||||
self.cursor.execute("""
|
||||
UPDATE activity_types
|
||||
UPDATE activity_type
|
||||
SET title = ?
|
||||
WHERE id = ?""",
|
||||
(activity_type.title, activity_type.id)
|
||||
)
|
||||
finally:
|
||||
self.con.commit()
|
||||
|
||||
def insert_user(self, **kwargs):
|
||||
"""Сохраняет в базу данных пользователя"""
|
||||
print(kwargs.values())
|
||||
try:
|
||||
self.cursor.execute("""INSERT INTO user(
|
||||
clockify_user_id, clockify_email, clockify_username,
|
||||
redmine_user_id, redmine_email, first_name, last_name, redmine_username)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?)""", [v for k, v in kwargs.items()])
|
||||
except sq.IntegrityError:
|
||||
self.cursor.execute("""
|
||||
UPDATE user
|
||||
SET clockify_email = ?,
|
||||
clockify_username = ?,
|
||||
redmine_email = ?,
|
||||
first_name = ?,
|
||||
last_name = ?,
|
||||
redmine_username = ?
|
||||
WHERE redmine_user_id = ? AND clockify_user_id = ?""",
|
||||
(
|
||||
kwargs["clockify_email"],
|
||||
kwargs["clockify_username"],
|
||||
kwargs["redmine_email"],
|
||||
kwargs["first_name"],
|
||||
kwargs["last_name"],
|
||||
kwargs["redmine_username"],
|
||||
kwargs["redmine_user_id"],
|
||||
kwargs["clockify_user_id"]
|
||||
)
|
||||
)
|
||||
finally:
|
||||
self.con.commit()
|
||||
|
||||
def get_user_by(self, by_field: str, value: str):
|
||||
"""Получает пользователя из бд"""
|
||||
try:
|
||||
user = self.cursor.execute(f"SELECT * FROM user WHERE {by_field} = ?", (value,)).fetchone()
|
||||
return {
|
||||
"id": user[0],
|
||||
"first_name": user[1],
|
||||
"last_name": user[2],
|
||||
"clockify_user_id": user[3],
|
||||
"redmine_user_id": user[4],
|
||||
"clockify_email": user[5],
|
||||
"clockify_username": user[6],
|
||||
"redmine_email": user[7],
|
||||
"redmine_username": user[8]
|
||||
}
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
|
||||
Reference in New Issue
Block a user