all the project copy

This commit is contained in:
2026-07-16 12:27:06 +02:00
parent 926b7de75b
commit 568beef49a
631 changed files with 130346 additions and 0 deletions

39
conn/config.py Normal file
View File

@@ -0,0 +1,39 @@
import os
def _env_int(name, default):
value = os.getenv(name)
if value is None or value == "":
return default
return int(value)
def _env_bool(name, default):
value = os.getenv(name)
if value is None or value == "":
return default
return value.strip().lower() in ("1", "true", "yes", "on")
MYSQL_CONFIG = {
"host": os.getenv("TICKET_MYSQL_HOST", "127.0.0.1"),
"port": _env_int("TICKET_MYSQL_PORT", 3307),
"database": os.getenv("TICKET_MYSQL_DATABASE", "smits"),
"user": os.getenv("TICKET_MYSQL_USER", "root"),
"password": os.getenv("TICKET_MYSQL_PASSWORD", "localpassword"),
"charset": os.getenv("TICKET_MYSQL_CHARSET", "utf8"),
}
POSTGRES_CONFIG = {
"host": os.getenv("TICKET_PG_HOST", "127.0.0.1"),
"port": _env_int("TICKET_PG_PORT", 5433),
"dbname": os.getenv("TICKET_PG_DATABASE", "ticket"),
"user": os.getenv("TICKET_PG_USER", "postgres"),
"password": os.getenv("TICKET_PG_PASSWORD", "localpassword"),
}
TELEGRAM_BOT_TOKEN = os.getenv("TICKET_TELEGRAM_TOKEN", os.getenv("TELEGRAM_BOT_TOKEN", ""))
TELEGRAM_ADMIN_ID = _env_int("TICKET_ADMIN_ID", 145645559)
ENABLE_NOTIFICATIONS = _env_bool("TICKET_ENABLE_NOTIFICATIONS", True)

33
conn/connection.py Normal file
View File

@@ -0,0 +1,33 @@
import psycopg2
import config
def conn():
try:
connection = psycopg2.connect(**config.POSTGRES_CONFIG)
return connection
# cursor = connection.cursor()
# postgres_insert_query = """ INSERT INTO mobile (ID, MODEL, PRICE) VALUES (%s,%s,%s)"""
# record_to_insert = (5, 'One Plus 6', 950)
# cursor.execute(postgres_insert_query, record_to_insert)
# connection.commit()
# count = cursor.rowcount
# print(count, "Record inserted successfully into mobile table")
except (Exception, psycopg2.Error) as error:
print("Failed to insert record into mobile table", error)
return False
# finally:
# # closing database connection.
# if connection:
# connection.cursor().close()
# connection.close()
# print("PostgreSQL connection is closed")

151
conn/filters.py Normal file
View File

@@ -0,0 +1,151 @@
import connection
def getConn():
c = connection.conn()
return c
def closeConnection(connection,cursor):
if connection:
cursor.close()
connection.close()
# print("POstgres connection closed successfully")
# print("Connection was closed before")
# Insert a filter if it does not already exist.
def insertFilter(tid, sort, sort_value ):
if str(sort_value).strip().startswith("_"):
sort_value = "-" + (str(sort_value).strip())[1:]
if getFilterByParam(tid, sort, sort_value):
return False
connection = getConn()
cursor = connection.cursor()
postgres_insert_query = """ INSERT INTO filters (tid, sort, sort_value) VALUES (%s,%s,%s)"""
record_to_insert = (tid, sort, sort_value)
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
if count <1:
closeConnection(connection,cursor)
return False
print(count, "Record inserted successfully into sort table")
closeConnection(connection,cursor)
return True
def exists_area_for_user(tid):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from filters where tid =" + str(tid) + " AND sort = 'A'"
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return cursor.rowcount > 0
def exists_sub_area_for_user(tid):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from filters where tid =" + str(tid) + " AND sort = 'S'"
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return cursor.rowcount > 0
def get_area_from_tid(tid):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select sort_value from filters where sort = 'A' and tid =" + str(tid)
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
# Returns all the filters inserted by a user
def getFilters(tid):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from filters where tid =" + str(tid) + " order by sort"
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
# Returns all the filters inserted by a user
def getFilterByParam(tid,sort, sort_value):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = """select * from filters where tid = %s and sort= %s and sort_value = %s"""
record_to_insert = (tid, sort, sort_value)
cursor.execute(postgreSQL_select_Query,record_to_insert)
publisher_records = cursor.fetchall()
filter_exists = cursor.rowcount > 0
closeConnection(connection,cursor)
return filter_exists
# Returns all the filters inserted by a user
def getAllFilters():
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from filters "
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
for row in publisher_records:
print( row, "\n")
closeConnection(connection,cursor)
return publisher_records
# Delete a single filter
def deleteFilter(telid, id):
connection = getConn()
cursor = connection.cursor()
# Delete first all the ticket
sql_delete_query = """Delete from tickets where fid = %s"""
cursor.execute(sql_delete_query, (id,))
connection.commit()
sql_delete_query = """Delete from filters where tid= %s and fid = %s"""
cursor.execute(sql_delete_query, (telid,id))
connection.commit()
count = cursor.rowcount
print(count, "Record deleted successfully ")
closeConnection(connection,cursor)
return count > 0
#insertFilter(965744443, 'competenza','itech')
#getUsers()
#isUserRegisterd(222222)
#getFilters(965744443)
#deleteFilter(8)

396
conn/mysql5.py Normal file
View File

@@ -0,0 +1,396 @@
import mysql.connector
import config
def getMySqlConnection():
connection = mysql.connector.connect(**config.MYSQL_CONFIG)
return connection
def closeConnection(connection,cursor):
if connection:
cursor.close()
connection.close()
def get_areas():
connection = getMySqlConnection()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from aree"
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
def get_sub_areas(area):
connection = getMySqlConnection()
cursor = connection.cursor()
postgreSQL_select_Query = "select ID,SOTTOAREA,AREA from sottoaree where area = %s"
cursor.execute(postgreSQL_select_Query, (area,))
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
# print(get_sub_areas(2))
def getCompetences(competence_id=999999):
connection = getMySqlConnection()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from competenze order by ID"
if competence_id != 999999:
postgreSQL_select_Query += " WHERE ID =" + str(competence_id)
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
def getProducts(product_id=999999):
connection = getMySqlConnection()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from prodotti order by ID"
if product_id != 999999:
postgreSQL_select_Query += " WHERE ID =" + str(product_id)
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
def getTicket(filter_type="", filter_value=""):
connection = getMySqlConnection()
cursor = connection.cursor()
postgreSQL_select_Query = ""
params = ()
if filter_type == "":
postgreSQL_select_Query = "select * from segnalazioni limit 2"
elif filter_type == "C":
postgreSQL_select_Query = "select s.TICKET,cl.INTESTATARIO,s.OGGETTO,g.GRAVITA,c.COMPETENZA,p.PRODOTTO,s.APERTURA from segnalazioni as s join prodotti as p on s.PRODOTTO=p.ID join competenze as c on s.COMPETENZA=c.ID join gravita as g on s.GRAVITA=g.ID join clienti as cl on cl.ID=s.CLIENTE where s.COMPETENZA=%s AND DATE(s.APERTURA)=CURDATE()"
params = (filter_value,)
elif filter_type == "P":
postgreSQL_select_Query = "select s.TICKET,cl.INTESTATARIO,s.OGGETTO,g.GRAVITA,c.COMPETENZA,p.PRODOTTO,s.APERTURA from segnalazioni as s join prodotti as p on s.PRODOTTO=p.ID join gravita as g on s.GRAVITA=g.ID join competenze as c on s.COMPETENZA=c.ID join clienti as cl on cl.ID=s.CLIENTE where s.PRODOTTO=%s AND DATE(s.APERTURA)=CURDATE()"
params = (filter_value,)
elif filter_type == "A":
postgreSQL_select_Query = "select s.TICKET,cl.INTESTATARIO,s.OGGETTO,g.GRAVITA,c.COMPETENZA,p.PRODOTTO,s.APERTURA from segnalazioni as s join prodotti as p on s.PRODOTTO=p.ID join gravita as g on s.GRAVITA=g.ID join competenze as c on s.COMPETENZA=c.ID join clienti as cl on cl.ID=s.CLIENTE where s.AREA=%s AND DATE(s.APERTURA)=CURDATE()"
params = (filter_value,)
elif filter_type == "S":
postgreSQL_select_Query = "select s.TICKET,cl.INTESTATARIO,s.OGGETTO,g.GRAVITA,c.COMPETENZA,p.PRODOTTO,s.APERTURA from segnalazioni as s join prodotti as p on s.PRODOTTO=p.ID join gravita as g on s.GRAVITA=g.ID join competenze as c on s.COMPETENZA=c.ID join clienti as cl on cl.ID=s.CLIENTE where s.SOTTOAREA=%s AND DATE(s.APERTURA)=CURDATE()"
params = (filter_value,)
else:
closeConnection(connection, cursor)
return []
cursor.execute(postgreSQL_select_Query, params)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
def getTicketsByFilterSet(filter_rows):
filter_values = {
"C": [],
"P": [],
"A": [],
"S": [],
}
for row in filter_rows:
filter_type = str(row[2])
if filter_type not in filter_values:
continue
try:
filter_value = int(row[3])
except (TypeError, ValueError):
continue
if filter_value not in filter_values[filter_type]:
filter_values[filter_type].append(filter_value)
if not any(filter_values.values()):
return []
connection = getMySqlConnection()
cursor = connection.cursor()
select_query = """
select
s.TICKET,
cl.INTESTATARIO,
s.OGGETTO,
g.GRAVITA,
c.COMPETENZA,
p.PRODOTTO,
s.APERTURA
from segnalazioni as s
join prodotti as p on s.PRODOTTO=p.ID
join gravita as g on s.GRAVITA=g.ID
join competenze as c on s.COMPETENZA=c.ID
join clienti as cl on cl.ID=s.CLIENTE
"""
column_map = {
"C": "s.COMPETENZA",
"P": "s.PRODOTTO",
"A": "s.AREA",
"S": "s.SOTTOAREA",
}
where_parts = ["DATE(s.APERTURA)=CURDATE()"]
params = []
for filter_type, values in filter_values.items():
if not values:
continue
placeholders = ",".join(["%s"] * len(values))
where_parts.append(f"{column_map[filter_type]} IN ({placeholders})")
params.extend(values)
select_query += " where " + " AND ".join(where_parts)
cursor.execute(select_query, tuple(params))
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
def getProductOrCompetence(type_,value_):
connection = getMySqlConnection()
cursor = connection.cursor()
postgreSQL_select_product = "select prodotto from prodotti WHERE ID="+value_
postgreSQL_select_competence = "select COMPETENZA from competenze WHERE ID="+value_
postgreSQL_select_area = "select AREA from aree a where ID="+value_
postgreSQL_select_sub_area = "select SOTTOAREA from sottoaree where ID="+value_
if type_ == "P":
cursor.execute(postgreSQL_select_product)
elif type_ == 'C':
cursor.execute(postgreSQL_select_competence)
elif type_ == 'A':
cursor.execute(postgreSQL_select_area)
elif type_ == 'S':
cursor.execute(postgreSQL_select_sub_area)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
# print(publisher_records)
return publisher_records
def getCompetenceOrProdotto(ticket):
connection = getMySqlConnection()
cursor = connection.cursor()
select_problem = "SELECT COMPETENZA, PRODOTTO, PROBLEMA, AREA, SOTTOAREA FROM segnalazioni where TICKET = %s"
cursor.execute(select_problem, (ticket,))
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
if not publisher_records:
return "", "", "", "", ""
return str(publisher_records[0][0]) ,str(publisher_records[0][1]) ,str(publisher_records[0][2]) , str(publisher_records[0][3]) ,str(publisher_records[0][4])
# def getProblemaBYCompetenzaOrProgramma(competence, prodotto):
# connection = getMySqlConnection()
# cursor = connection.cursor()
# # From DB the competence is not null
# if prodotto != None and competence != None:
# select_problem = "SELECT PROBLEMA FROM segnalazioni where COMPETENZA = " + str(competence) + " AND PRODOTTO = " + str(prodotto)+ ""
# elif prodotto == None:
# select_problem = "SELECT PROBLEMA FROM segnalazioni where COMPETENZA = " + str(competence) + ""
# cursor.execute(select_problem)
# publisher_records = cursor.fetchall()
# closeConnection(connection,cursor)
# return publisher_records
def getProblemaBYCompetenzaOrProgramma_manul(competence, prodotto, area, sottoarea):
connection = getMySqlConnection()
cursor = connection.cursor()
where_parts = []
params = []
if prodotto is not None and prodotto.isdigit():
print("Trovato prodotto: ", prodotto)
where_parts.append("PRODOTTO = %s")
params.append(int(prodotto))
if area is not None and area.isdigit():
print("Trovato area: ", area)
where_parts.append("AREA = %s")
params.append(int(area))
if sottoarea is not None and sottoarea.isdigit():
print("Trovato sottoarea: ", sottoarea)
where_parts.append("SOTTOAREA = %s")
params.append(int(sottoarea))
# If none of the where condition is satisfied
if not where_parts:
if competence is not None and str(competence).isdigit():
print("Trovato competence: ", competence)
where_parts.append("COMPETENZA = %s")
params.append(int(competence))
else:
print("Trovato : ", "Nulla")
where_parts.append("FALSE")
main_sql = "SELECT PROBLEMA FROM segnalazioni WHERE " + " AND ".join(where_parts)
print(main_sql)
cursor.execute(main_sql, tuple(params))
publisher_records = cursor.fetchall()
count = cursor.rowcount
closeConnection(connection,cursor)
# if count <1:
# print("FFFFFF")
# return "Non ho trovato ticket con i filtri del ticket principale."
return publisher_records
def getTicketByProblema(problem):
connection = getMySqlConnection()
cursor = connection.cursor()
# Use placeholders for the query and pass the value as a parameter
select_problem = "SELECT TICKET, SOLUZIONE, PROBLEMA FROM segnalazioni WHERE PROBLEMA = %s"
cursor.execute(select_problem, (problem,))
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records[0]
def get_ticket_context(ticket):
connection = getMySqlConnection()
cursor = connection.cursor(dictionary=True)
query = """
SELECT
s.TICKET AS ticket_id,
s.OGGETTO AS subject,
s.PROBLEMA AS problem,
s.SOLUZIONE AS solution,
s.COMPETENZA AS competence_id,
c.COMPETENZA AS competence,
s.PRODOTTO AS product_id,
p.prodotto AS product,
s.AREA AS area_id,
a.AREA AS area,
s.SOTTOAREA AS subarea_id,
sa.SOTTOAREA AS subarea,
s.CLIENTE AS client_id,
cl.INTESTATARIO AS client,
s.GRAVITA AS gravity_id,
g.GRAVITA AS gravity,
s.STATO AS status_id,
st.STATO AS status,
s.APERTURA AS opened_at,
s.CHIUSURA AS closed_at,
s.PROGRAMMA AS program,
s.TIPO_PROGRAMMA AS program_type,
s.VERSIONE AS version_id,
s.PATCH AS patch_id,
s.CLASSIFICAZIONE AS classification_id,
s.SISTEMA_OPERATIVO AS os_id
FROM segnalazioni s
LEFT JOIN competenze c ON c.ID = s.COMPETENZA
LEFT JOIN prodotti p ON p.ID = s.PRODOTTO
LEFT JOIN aree a ON a.ID = s.AREA
LEFT JOIN sottoaree sa ON sa.ID = s.SOTTOAREA
LEFT JOIN clienti cl ON cl.ID = s.CLIENTE
LEFT JOIN gravita g ON g.ID = s.GRAVITA
LEFT JOIN stati st ON st.ID = s.STATO
WHERE s.TICKET = %s
"""
cursor.execute(query, (ticket,))
records = cursor.fetchall()
closeConnection(connection, cursor)
return records[0] if records else None
def fetch_solved_ticket_batch(last_ticket=0, limit=1000):
connection = getMySqlConnection()
cursor = connection.cursor(dictionary=True)
query = """
SELECT
s.TICKET AS ticket_id,
s.OGGETTO AS subject,
s.PROBLEMA AS problem,
s.SOLUZIONE AS solution,
s.COMPETENZA AS competence_id,
c.COMPETENZA AS competence,
s.PRODOTTO AS product_id,
p.prodotto AS product,
s.AREA AS area_id,
a.AREA AS area,
s.SOTTOAREA AS subarea_id,
sa.SOTTOAREA AS subarea,
s.CLIENTE AS client_id,
cl.INTESTATARIO AS client,
s.GRAVITA AS gravity_id,
g.GRAVITA AS gravity,
s.STATO AS status_id,
st.STATO AS status,
s.APERTURA AS opened_at,
s.CHIUSURA AS closed_at,
s.PROGRAMMA AS program,
s.TIPO_PROGRAMMA AS program_type,
s.VERSIONE AS version_id,
s.PATCH AS patch_id,
s.CLASSIFICAZIONE AS classification_id,
s.SISTEMA_OPERATIVO AS os_id
FROM segnalazioni s
LEFT JOIN competenze c ON c.ID = s.COMPETENZA
LEFT JOIN prodotti p ON p.ID = s.PRODOTTO
LEFT JOIN aree a ON a.ID = s.AREA
LEFT JOIN sottoaree sa ON sa.ID = s.SOTTOAREA
LEFT JOIN clienti cl ON cl.ID = s.CLIENTE
LEFT JOIN gravita g ON g.ID = s.GRAVITA
LEFT JOIN stati st ON st.ID = s.STATO
WHERE s.TICKET > %s
AND s.SOLUZIONE IS NOT NULL
AND TRIM(s.SOLUZIONE) <> ''
ORDER BY s.TICKET
LIMIT %s
"""
cursor.execute(query, (last_ticket, limit))
records = cursor.fetchall()
closeConnection(connection, cursor)
return records
# print(getTicketByProblema("CERCA SABRINA"))
# print(getTicketTest("P", "24"))

84
conn/tickets.py Normal file
View File

@@ -0,0 +1,84 @@
import connection
def getConn():
c = connection.conn()
return c
def closeConnection(connection,cursor):
if connection:
cursor.close()
connection.close()
# print("POstgres connection closed successfully")
# print("Connection was closed before")
# Insert a ticket if not exsists
def insertTicket(fid, ticket):
connection = getConn()
cursor = connection.cursor()
postgres_insert_query = """ INSERT INTO tickets (fid, ticket) VALUES (%s,%s)"""
record_to_insert = (fid, ticket)
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
if count <1:
closeConnection(connection,cursor)
return False
print(count, "Record inserted successfully into ticket table")
closeConnection(connection,cursor)
return True
def getTicketsByTid(tid):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = """select * from tickets join filters on tickets.fid = filters.fid where filters.tid = %s order by filters.tid"""
record_to_insert = (tid,)
cursor.execute(postgreSQL_select_Query, record_to_insert)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
# Returns all the ticket by filter id and ticket
def getTickets(fid, ticket):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = """select * from tickets where tickets.fid = %s and tickets.ticket = %s"""
record_to_insert = (fid, ticket)
cursor.execute(postgreSQL_select_Query, record_to_insert)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
def getTicketForTid(tid, ticket):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = """
select tickets.*
from tickets
join filters on tickets.fid = filters.fid
where filters.tid = %s and tickets.ticket = %s
limit 1
"""
record_to_insert = (tid, ticket)
cursor.execute(postgreSQL_select_Query, record_to_insert)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
# print(getTicketsByTid(145645559))

92
conn/user.py Normal file
View File

@@ -0,0 +1,92 @@
import connection
def getConn():
c = connection.conn()
return c
def closeConnection(connection,cursor):
if connection:
cursor.close()
connection.close()
'''Insert a telegram user if it doesnt exist
Return True if user has been inserted successfully, False otherwise
'''
def insertUser(tid, name, surname, status='P', admin=False ):
if getUserByTid(tid):
return False
connection = getConn()
cursor = connection.cursor()
postgres_insert_query = """ INSERT INTO users (tid, username, surname, status, user_admin) VALUES (%s,%s,%s,%s,%s)"""
record_to_insert = (tid, name, surname, status, admin)
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
closeConnection(connection,cursor)
if count <1:
return False
print(count, "Record inserted successfully into users table")
return True
# Returns all the user registered on bot
def getUsers():
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select users.tid from users"
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
''' Gets the user by telegram id '''
def getUserByTid(tid):
connection = getConn()
cursor = connection.cursor()
postgreSQL_select_Query = "select * from users where tid=" + str(tid)
cursor.execute(postgreSQL_select_Query)
publisher_records = cursor.fetchall()
closeConnection(connection,cursor)
return publisher_records
# if not publisher_records:
# return False
# else:
# return True
def approveUser(tid):
alreadyApproved = getUserByTid(tid)
connection = getConn()
cursor = connection.cursor()
postgreSQL_update_Query = "UPDATE users SET status = 'A' WHERE tid = %s"
cursor.execute(postgreSQL_update_Query, (tid,))
connection.commit()
count = cursor.rowcount
print(count, "Record updated successfully ")
closeConnection(connection,cursor)
return count > 0, alreadyApproved
# insertUser(22222, 'boh','bah')
#getUsers()
# if getUserByTid(145645559):
# print(getUserByTid(145645559)[0][3])