melon/modules/api/portal/add.py

35 lines
1.2 KiB
Python
Raw Permalink Normal View History

2022-08-17 15:09:08 +07:00
import mysql.connector as mariadb
import config.database as database
import config.globalvar as globalvar
class main:
def __init__(self):
pass
def result(self, params):
response = {
"message_action" : "success" ,
"message_desc" : "New portal created" ,
"message_data" : {
"title" : params['title' ],
"website" : params['website' ],
"logo" : params['logo' ],
"directory" : params['directory' ],
},
}
if params['logo']:
query = f"INSERT INTO portal VALUES (DEFAULT, '{params['title']}', NULL, '{params['website']}', '{params['logo']}', '{params['directory']}')"
else:
query = f"INSERT INTO portal VALUES (DEFAULT, '{params['title']}', NULL, '{params['website']}', NULL, '{params['directory']}')"
try:
con = mariadb.connect(**database.main_db)
cursor = con.cursor()
cursor.execute(query)
con.close()
except Exception as e:
print(f"Error: {e}")
response["message_action" ] = "failed",
response["message_desc" ] = f"error: {e}"
return response