123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- import psycopg2
- from config import dbname, user, password, host
- connection = psycopg2.connect(dbname=dbname, user=user, password=password, host=host)
- cursor = connection.cursor()
- # get all users
- def get_all_users():
- with connection:
- cursor.execute(
- """
- SELECT user_id
- FROM users
- """
- )
- try:
- return [i[0] for i in cursor.fetchall()]
- except Exception as e:
- print(e)
- return None
- # state
- def get_current_state(user_id):
- with connection:
- cursor.execute(
- """
- SELECT state
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_state(state, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET state = (%s)
- WHERE user_id = (%s)
- """,
- (state, str(user_id)),
- )
- connection.commit()
- # user_id
- def get_and_set_id(user_id):
- with connection:
- cursor.execute("SELECT 1 FROM users WHERE user_id = (%s)", (str(user_id),))
- a = cursor.fetchone()
- if a is None:
- cursor.execute(
- """
- INSERT INTO users (user_id, klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise, state)
- VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
- """,
- (
- str(user_id),
- 1,
- "None",
- "None",
- "None",
- "None",
- "None",
- "None",
- "None",
- 0,
- ),
- )
- connection.commit()
- # klas
- def get_klas(user_id):
- with connection:
- cursor.execute(
- """
- SELECT klas
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_klas(klas, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET klas = (%s)
- WHERE user_id = (%s)
- """,
- (klas, str(user_id)),
- )
- connection.commit()
- # subject
- def get_subjects(klas):
- with connection:
- cursor.execute(
- """
- SELECT subject, max(id) from gdz
- WHERE klas = (%s)
- GROUP BY subject
- ORDER BY max(id)
- """,
- (klas,),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def set_subject(subject, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET subject = (%s)
- WHERE user_id = (%s)
- """,
- (subject, str(user_id)),
- )
- connection.commit()
- def get_subject(user_id):
- with connection:
- cursor.execute(
- """
- SELECT subject
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- # author
- def get_authors(klas, subject):
- with connection:
- cursor.execute(
- """
- SELECT author, max(id) from gdz
- WHERE klas = (%s) AND subject = (%s)
- GROUP BY author
- ORDER BY max(id)
- """,
- (klas, subject),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def get_author(user_id):
- with connection:
- cursor.execute(
- """
- SELECT author
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_author(author, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET author = (%s)
- WHERE user_id = (%s)
- """,
- (author, str(user_id)),
- )
- connection.commit()
- # types
- def get_types(klas, subject, author):
- with connection:
- cursor.execute(
- """
- SELECT type, max(id) from gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s)
- GROUP BY type
- ORDER BY max(id)
- """,
- (klas, subject, author),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def get_type(user_id):
- with connection:
- cursor.execute(
- """
- SELECT type
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_type(type, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET type = (%s)
- WHERE user_id = (%s)
- """,
- (type, str(user_id)),
- )
- connection.commit()
- # maintopic
- def get_maintopics(klas, subject, author, type):
- with connection:
- cursor.execute(
- """
- SELECT maintopic, max(id) from gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s)
- GROUP BY maintopic
- ORDER BY max(id)
- """,
- (klas, subject, author, type),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def get_maintopic(user_id):
- with connection:
- cursor.execute(
- """
- SELECT maintopic
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_maintopic(maintopic, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET maintopic = (%s)
- WHERE user_id = (%s)
- """,
- (maintopic, str(user_id)),
- )
- connection.commit()
- # subtopic
- def get_subtopics(klas, subject, author, type, maintopic):
- with connection:
- cursor.execute(
- """
- SELECT subtopic, max(id) from gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s)
- GROUP BY subtopic
- ORDER BY max(id)
- """,
- (klas, subject, author, type, maintopic),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def get_subtopic(user_id):
- with connection:
- cursor.execute(
- """
- SELECT subtopic
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_subtopic(subtopic, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET subtopic = (%s)
- WHERE user_id = (%s)
- """,
- (subtopic, str(user_id)),
- )
- connection.commit()
- # subsubtopic
- def get_subsubtopics(klas, subject, author, type, maintopic, subtopic):
- with connection:
- cursor.execute(
- """
- SELECT subsubtopic, max(id) from gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s)
- GROUP BY subsubtopic
- ORDER BY max(id)
- """,
- (klas, subject, author, type, maintopic, subtopic),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def get_subsubtopic(user_id):
- with connection:
- cursor.execute(
- """
- SELECT subsubtopic
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_subsubtopic(subsubtopic, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET subsubtopic = (%s)
- WHERE user_id = (%s)
- """,
- (subsubtopic, str(user_id)),
- )
- connection.commit()
- # exercise
- def get_exercises(klas, subject, author, type, maintopic, subtopic, subsubtopic):
- with connection:
- cursor.execute(
- """
- SELECT exercise, max(id)
- FROM gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s)
- GROUP BY exercise
- ORDER BY max(id)
- """,
- (klas, subject, author, type, maintopic, subtopic, subsubtopic),
- )
- try:
- return cursor.fetchall()
- except Exception as e:
- print(e)
- return None
- def get_exercise(user_id):
- with connection:
- cursor.execute(
- """
- SELECT exercise
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- def set_exercise(exercise, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET exercise = (%s)
- WHERE user_id = (%s)
- """,
- (exercise, str(user_id)),
- )
- connection.commit()
- # solution
- def get_solution(
- klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise
- ):
- with connection:
- cursor.execute(
- """
- SELECT solution_id
- FROM gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s) AND exercise = (%s)
- """,
- (klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise),
- )
- sol_id = cursor.fetchone()[0]
- if not sol_id:
- cursor.execute(
- """
- SELECT exercise_url
- FROM gdz
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s) AND exercise = (%s)
- """,
- (
- klas,
- subject,
- author,
- type,
- maintopic,
- subtopic,
- subsubtopic,
- exercise,
- ),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- else:
- return sol_id
- def set_solution(
- klas, subject, author, type, maintopic, subtopic, subsubtopic, exercise, solution_id
- ):
- with connection:
- cursor.execute(
- """
- UPDATE gdz
- SET solution_id = (%s)
- WHERE klas = (%s) AND subject = (%s) AND author = (%s) AND type = (%s) AND maintopic = (%s) AND subtopic = (%s) AND subsubtopic = (%s) AND exercise = (%s)
- """,
- (
- solution_id,
- klas,
- subject,
- author,
- type,
- maintopic,
- subtopic,
- subsubtopic,
- exercise,
- ),
- )
- connection.commit()
- # save keyboards for 'back' func
- def set_keyboard_and_msg(data, user_id):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET markup = (%s)
- WHERE user_id = (%s)
- """,
- (psycopg2.Binary(data), str(user_id)),
- )
- connection.commit()
- def get_keyboard_and_msg(user_id):
- with connection:
- cursor.execute(
- """
- SELECT markup
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
- # is_blocked user
- def set_is_blocked(user_id, value):
- with connection:
- cursor.execute(
- """
- UPDATE users
- SET is_blocked = (%s)
- WHERE user_id = (%s)
- """,
- (value, str(user_id)),
- )
- connection.commit()
- def get_is_blocked(user_id):
- with connection:
- cursor.execute(
- """
- SELECT is_blocked
- FROM users
- WHERE user_id = (%s)
- """,
- (str(user_id),),
- )
- try:
- return cursor.fetchone()[0]
- except Exception as e:
- print(e)
- return None
|