API modules for dashboard

This commit is contained in:
Dita Aji Pratama 2024-09-26 12:34:45 +07:00
parent 040002259f
commit 6a72836098
2 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,143 @@
import mysql.connector as mariadb
from mako.template import Template
from bottle import request
from config import database, globalvar
from scripts import loggorilla, tokenguard
import procedure.validation as procedure_validation
class roles:
def __init__(self):
self.db_main = mariadb.connect(**database.db_main)
self.cursor = self.db_main.cursor(dictionary=True)
def list(self, params):
APIADDR = "/api/dashboard/roles/list"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
token = params["token" ]
allowed_roles = [1,2] # Roles list is public or not?
loggorilla.prcss(APIADDR, "Account validation")
user_validation = procedure_validation.validation().account(APIADDR, allowed_roles, token)
user = user_validation['data']
self.cursor.execute("BEGIN;")
try:
self.cursor.execute("select auth_roles.id, auth_roles.name, (select count(*) from auth_profile_roles apr where apr.auth_roles = auth_roles.id) AS `count` from auth_roles;")
r_roles = self.cursor.fetchall()
response["status" ] = "success"
response["desc" ] = "data collected"
response["data" ] = r_roles
except Exception as e:
self.cursor.execute("ROLLBACK;")
loggorilla.error(APIADDR, str(e) )
response["status" ] = "failed"
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
finally:
self.cursor.execute("COMMIT;")
self.cursor.close()
self.db_main.close()
return response
def add(self, params):
APIADDR = "/api/dashboard/roles/add"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
token = params["token" ]
id = params["id" ]
name = params["name" ]
allowed_roles = [1]
loggorilla.prcss(APIADDR, "Account validation")
user_validation = procedure_validation.validation().account(APIADDR, allowed_roles, token)
user = user_validation['data']
self.cursor.execute("BEGIN;")
try:
self.cursor.execute("INSERT INTO `auth_roles` VALUES (%s, %s, NOW(), NULL) ;", (id, name) )
response["status" ] = "success"
response["desc" ] = "data added"
except Exception as e:
self.cursor.execute("ROLLBACK;")
loggorilla.error(APIADDR, str(e) )
response["status" ] = "failed"
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
finally:
self.cursor.execute("COMMIT;")
self.cursor.close()
self.db_main.close()
return response
def edit(self, params):
APIADDR = "/api/dashboard/roles/edit"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
token = params["token" ]
key = params["key" ]
id = params["id" ]
name = params["name" ]
allowed_roles = [1]
loggorilla.prcss(APIADDR, "Account validation")
user_validation = procedure_validation.validation().account(APIADDR, allowed_roles, token)
user = user_validation['data']
self.cursor.execute("BEGIN;")
try:
if key == 1 or id == 1:
response["status" ] = "failed"
response["desc" ] = "Cannot change super user"
else:
self.cursor.execute("UPDATE `auth_roles` SET `id` = %s, `name` = %s, `when_update` = NOW() WHERE `id` = %s ;", (id, name, key) )
response["status" ] = "success"
response["desc" ] = "data change"
except Exception as e:
self.cursor.execute("ROLLBACK;")
loggorilla.error(APIADDR, str(e) )
response["status" ] = "failed"
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
finally:
self.cursor.execute("COMMIT;")
self.cursor.close()
self.db_main.close()
return response
def remove(self, params):
APIADDR = "/api/dashboard/roles/remove"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
token = params["token" ]
key = params["key" ]
allowed_roles = [1]
loggorilla.prcss(APIADDR, "Account validation")
user_validation = procedure_validation.validation().account(APIADDR, allowed_roles, token)
user = user_validation['data']
self.cursor.execute("BEGIN;")
try:
if key == 1:
response["status" ] = "failed"
response["desc" ] = "Cannot change super user"
else:
self.cursor.execute("DELETE FROM `auth_roles` WHERE `id` = %s ;", (key,) )
response["status" ] = "success"
response["desc" ] = "data removed"
except Exception as e:
self.cursor.execute("ROLLBACK;")
loggorilla.error(APIADDR, str(e) )
response["status" ] = "failed"
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
finally:
self.cursor.execute("COMMIT;")
self.cursor.close()
self.db_main.close()
return response

View File

@ -0,0 +1,61 @@
import mysql.connector as mariadb
from mako.template import Template
from bottle import request
from config import database, globalvar
from scripts import loggorilla, tokenguard
import procedure.validation as procedure_validation
class users:
def __init__(self):
self.db_main = mariadb.connect(**database.db_main)
self.cursor = self.db_main.cursor(dictionary=True)
def list(self, params):
APIADDR = "/api/dashboard/users/list"
response = {}
loggorilla.prcss(APIADDR, "Define parameters")
token = params["token" ]
allowed_roles = [1,2]
loggorilla.prcss(APIADDR, "Account validation")
user_validation = procedure_validation.validation().account(APIADDR, allowed_roles, token)
user = user_validation['data']
self.cursor.execute("BEGIN;")
try:
r_profile = []
self.cursor.execute("select auth_profile.id, auth_profile.username, auth_profile.email, auth_profile.phone from auth_profile;")
l1 = self.cursor.fetchall()
c1 = 0
for d1 in l1:
r_profile.append({
"id" : d1["id" ],
"username" : d1["username" ],
"email" : d1["email" ],
"phone" : d1["phone" ],
"roles" : [],
"verification" : []
})
self.cursor.execute("select auth_roles.id, auth_roles.name from auth_profile_roles inner join auth_roles on auth_profile_roles.auth_roles = auth_roles.id where auth_profile_roles.auth_profile = %s ; ", ( d1["id"], ) )
r_profile[c1]["roles"] = self.cursor.fetchall()
self.cursor.execute("select `type`, `verified` from auth_profile_verification where auth_profile = %s ; ", ( d1["id"], ) )
r_profile[c1]["verification"] = self.cursor.fetchall()
c1 += 1
response["status" ] = "success"
response["desc" ] = "data collected"
response["data" ] = r_profile
except Exception as e:
self.cursor.execute("ROLLBACK;")
loggorilla.error(APIADDR, str(e) )
response["status" ] = "failed"
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
finally:
self.cursor.execute("COMMIT;")
self.cursor.close()
self.db_main.close()
return response