mirror of
https://gitlab.com/JKANetwork/powerfulcomputermanager.git
synced 2026-03-07 02:42:03 +01:00
First version with web gui
This commit is contained in:
66
api.py
66
api.py
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/python3
|
||||
BUILD = 26
|
||||
BUILD = 28
|
||||
API_VER = 2
|
||||
from flask import Flask, request
|
||||
import random
|
||||
@@ -72,7 +72,7 @@ def getComputerExists():
|
||||
|
||||
|
||||
##
|
||||
# getGroups: Know all groups or groups of a computer
|
||||
# getGroups: Know all groups, or groups of a computer
|
||||
# /get/groups?
|
||||
# @param ComputerName/ComputerID -> Computer to see groups (Not neccesary)
|
||||
# @param GroupID -> For know Name of a GroupID
|
||||
@@ -138,14 +138,27 @@ def getComputersGrp():
|
||||
return str(data)
|
||||
|
||||
##
|
||||
# getCookAll: Get the list of cooks added at least to one group
|
||||
# getCookAll: Get the list of all cooks (Or all added at least to one group)
|
||||
# /get/cookall?
|
||||
# NoParams
|
||||
# @param OnlyAdded: Show only added cooks
|
||||
##
|
||||
@app.route("/get/cookall",methods=['POST'])
|
||||
def getCookAll():
|
||||
logit(request.base_url)
|
||||
data = sql.select("SELECT DISTINCT CookName FROM COOKS_IDG ORDER BY CookName ASC") #All cooks
|
||||
|
||||
OnlyAdded =None
|
||||
for key, value in request.args.to_dict().items():
|
||||
if key == "OnlyAdded":
|
||||
OnlyAdded= 1
|
||||
if OnlyAdded is None:
|
||||
x = []
|
||||
for file in os.listdir("cooks"):
|
||||
if file.endswith(".yaml"):
|
||||
x.append(str(file.replace('.yaml','')).lower())
|
||||
data = {}
|
||||
data['CookName'] = x
|
||||
else:
|
||||
data = sql.select("SELECT DISTINCT CookName FROM COOKS_IDG ORDER BY CookName ASC") #All cooks
|
||||
return str(data)
|
||||
|
||||
|
||||
@@ -247,11 +260,12 @@ def delCookGrp(): #Delete cook from a group
|
||||
if key == "GroupID":
|
||||
GroupID = value
|
||||
|
||||
if GroupID is not None and CookName is not None:
|
||||
result = sql.insert("DELETE FROM COOKS_IDG WHERE ID_G='"+GroupID+"' AND CookName='"+CookName+"'")
|
||||
return str(result)
|
||||
else:
|
||||
return str({'TEXT': 'Error, no Group, or CookName does\'t exists','RESULT':'ERROR'})
|
||||
if GroupID is not None and CookName is not None:
|
||||
result = sql.insert("DELETE FROM COOKS_IDG WHERE ID_G='"+GroupID+"' AND CookName='"+CookName+"'")
|
||||
cleanDatabase()
|
||||
return str({'TEXT': '','RESULT':'0'}) #Return text str(result) is problematic (json inside json)
|
||||
else:
|
||||
return str({'TEXT': 'Error, no Group, or CookName doesnt exists','RESULT':'ERROR'})
|
||||
|
||||
##
|
||||
# delEmptyPcsGroup: Delete all computers from from a group
|
||||
@@ -273,7 +287,7 @@ def delEmptyPcsGroup():
|
||||
result = sql.insert("DELETE FROM COMPUTER_GROUP WHERE ID_G='"+GroupID+"'")
|
||||
return str(result)
|
||||
else:
|
||||
return str({'TEXT': 'Error, this group doesn\'t exists','RESULT':'ERROR'})
|
||||
return str({'TEXT': 'Error, this group doesnt exists','RESULT':'ERROR'})
|
||||
|
||||
|
||||
##
|
||||
@@ -460,7 +474,6 @@ def delCook():
|
||||
##
|
||||
@app.route("/del/groupscook",methods=['POST'])
|
||||
def delGroupsCook():
|
||||
|
||||
logit(request.base_url)
|
||||
CookName = None, None # Initialize
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -482,7 +495,6 @@ def delGroupsCook():
|
||||
##
|
||||
@app.route("/del/cleancook",methods=['POST'])
|
||||
def delCleanCook():
|
||||
|
||||
logit(request.base_url)
|
||||
CookName = None, None # Initialize
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -505,7 +517,6 @@ def delCleanCook():
|
||||
##
|
||||
@app.route("/upd/group",methods=['POST'])
|
||||
def updGroup():
|
||||
|
||||
logit(request.base_url)
|
||||
GroupID, GroupNewName= None, None
|
||||
for key,value in request.args.to_dict().items():
|
||||
@@ -534,7 +545,6 @@ def updGroup():
|
||||
##
|
||||
@app.route("/add/grpcomputer",methods=['POST'])
|
||||
def addGrpComputer():
|
||||
|
||||
logit(request.base_url)
|
||||
ComputerID, GroupID= None, None
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -561,7 +571,6 @@ def addGrpComputer():
|
||||
##
|
||||
@app.route("/del/grpcomputer",methods=['POST'])
|
||||
def delGrpComputer():
|
||||
|
||||
logit(request.base_url)
|
||||
ComputerID, GroupID= None, None
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -589,7 +598,6 @@ def delGrpComputer():
|
||||
##
|
||||
@app.route("/del/computer",methods=['POST'])
|
||||
def delComputer():
|
||||
|
||||
logit(request.base_url)
|
||||
ComputerID= None
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -669,7 +677,6 @@ def updComputer():
|
||||
##
|
||||
@app.route("/upd/cookname",methods=['POST'])
|
||||
def updCookName():
|
||||
|
||||
logit(request.base_url)
|
||||
CookName, CookNewName,Password= None, None,None #Initialize
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -705,7 +712,6 @@ def updCookName():
|
||||
##
|
||||
@app.route("/load/cook",methods=['POST'])
|
||||
def loadCook():
|
||||
|
||||
logit(request.base_url)
|
||||
CookName, ComputerID, UUID= None, None, None #Initialize
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -732,6 +738,15 @@ def loadCook():
|
||||
data = myfile.read()
|
||||
return str(yaml.safe_load(data))
|
||||
|
||||
##
|
||||
# setCookStatus: Set status of a executed Cook
|
||||
# /set/cookstatus?
|
||||
# @param ComputerID/ComputerName -> ID or Name of computer
|
||||
# @param CookName -> Name of the cook
|
||||
# @param Revision -> Revision of cook applied
|
||||
# @param Error -> Output, 0 if not error
|
||||
# @param ErrorDesc -> Optional, if error ocurred, description
|
||||
##
|
||||
@app.route("/set/cookstatus",methods=['POST'])
|
||||
def setCookStatus():
|
||||
logit(request.base_url)
|
||||
@@ -779,7 +794,6 @@ def setCookStatus():
|
||||
##
|
||||
@app.route("/check/password",methods=['POST'])
|
||||
def checkPassword(): #Check password (ERROR if password not valid or no password and 1 if valid)
|
||||
|
||||
logit(request.base_url)
|
||||
Password= None
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -807,7 +821,6 @@ def checkPassword(): #Check password (ERROR if password not valid or no passwor
|
||||
##
|
||||
@app.route("/upd/password",methods=['POST'])
|
||||
def updPassword(): #Update password (ERROR if password not valid or no password and 1 if changed)
|
||||
|
||||
logit(request.base_url)
|
||||
OldPassword,NewPassword= None, None
|
||||
for key, value in request.args.to_dict().items():
|
||||
@@ -851,7 +864,14 @@ def getOptionValue():
|
||||
return str({'TEXT': 'Invalid password','RESULT':'ERROR'})
|
||||
else:
|
||||
return sql.select("SELECT VALUE FROM OPTIONS WHERE Option='"+str(Option)+"'")[0]
|
||||
|
||||
|
||||
|
||||
def cleanDatabase():
|
||||
sql.insert("DELETE FROM COOKS_STATUS WHERE CookName NOT IN (SELECT CookName FROM COOKS_IDG)")
|
||||
sq = sql.select("SELECT ID_C FROM COMPUTERS")
|
||||
for val in sq:
|
||||
sql.insert("DELETE FROM COOKS_STATUS WHERE ID_C='"+str(val['ID_C'])+"' AND CookName NOT IN (SELECT DISTINCT CookName FROM COOKS_IDG WHERE ID_G IN (SELECT ID_G FROM COMPUTER_GROUP WHERE ID_C='"+str(val['ID_C'])+"'))")
|
||||
return 0
|
||||
|
||||
|
||||
@app.route('/get/apiver',methods=['POST'])
|
||||
@@ -861,5 +881,7 @@ def getApiVer():
|
||||
print("Build: "+str(BUILD))
|
||||
print("API Version: "+str(API_VER))
|
||||
|
||||
print("Cleaning database before start..")
|
||||
cleanDatabase()
|
||||
|
||||
app.run(debug=False,port=3333,ssl_context='adhoc',host='0.0.0.0',threaded=True)
|
||||
Reference in New Issue
Block a user