From 3deb367e8947a60f73550985396be48909a795a3 Mon Sep 17 00:00:00 2001 From: Kevin Puertas Ruiz Date: Fri, 3 Jul 2020 18:37:34 +0200 Subject: [PATCH] Changes to web gui and fixes --- admin/admin.py | 59 ++++++++++++++++++++++-------- admin/templates/admincomputer.tmpl | 3 +- admin/templates/admingroup.tmpl | 2 +- admin/templates/adminindex.tmpl | 8 ++-- admin/templates/firsttime.tmpl | 17 +++++++++ api.py | 2 +- client/client.ps1 | 23 ++++++++++-- docs/changelog.md | 4 ++ docs/howto_writecook.md | 1 + 9 files changed, 94 insertions(+), 25 deletions(-) create mode 100644 admin/templates/firsttime.tmpl diff --git a/admin/admin.py b/admin/admin.py index 3888d75..4301f60 100644 --- a/admin/admin.py +++ b/admin/admin.py @@ -38,12 +38,15 @@ def returnvalueapi(suburl,field="Name"): r=(requests.post(c_server+suburl+twopart, verify=False).text).replace("\'", "\"").replace('\\"',"'").replace(': None',': "None"') try: - #print(suburl+twopart+"->"+r) + #print(suburl+twopart+"->"+r) jsonobj = json.loads(r) + if isinstance(jsonobj, list): + print("LIST") + jsonobj = jsonobj[0] return jsonobj[field] except: - print('Error: '+str(r)) - return str({'TEXT':r,'RESULT':'ERROR'}) + print('Error: '+str(r)) + return str({'TEXT':r,'RESULT':'ERROR'}) def returnTable(suburl,field=["Name"]): global c_server @@ -82,7 +85,7 @@ def pAdminIndex(): #Admin Index page table = returnTable("/get/computers",['ID_C','Name','RAM','CPUName','SOVersion','SOCaption','HDD','LastConnection','RAMFree']) for lista in table: - lista.append(returnTable("/get/groups?ComputerID="+lista[0],["Name"])) #lista[9]¿ + lista.append(returnTable("/get/groups?ComputerID="+lista[0],["ID_G","Name"])) #lista[9]¿ if lista[6] is not None: try: lista[6]=json.loads(base64.b64decode(lista[6]).decode('UTF-8')) @@ -126,13 +129,18 @@ def pAdminComputer(): #Admin see one group computerid=request.args.get('ID_C') if computerid is not None and computerid.isnumeric(): computername = returnvalueapi('/get/computers?ComputerID='+str(computerid),"Name") - groups = returnTable("/get/groups?ComputerName="+str(computername),['ID_G','Name']) - #####IT DOESNT HAVE TEMPLATE CREATED + groups = returnTable("/get/groups?ComputerID="+str(computerid),['ID_G','Name']) + for key in range(len(groups)): #Cookgs of every group + print(groups[key]) + groups[key].append(returnTable("/get/cookgrp?GroupID="+groups[key][0],['CookName'])) + + return render_template('/admincomputer.tmpl', title="Computer "+computername,computerid=computerid,computername=computername,groups=groups) return redirect('/admin') @app.route("/admin/groups",methods=['GET']) +@app.route("/admin/groups/",methods=['GET']) def pAdminGroups(): #Admin Index groups if returnvalueapi('/check/password?Password='+str(request.cookies.get('admin_logued')),'EXITCODE') != '0': return render_template('/login.tmpl', title="Login") @@ -159,8 +167,14 @@ def pAdminGroup(): #Admin see one group for cook in tem: to = [] to.append(cook[0]) - to.append(returnvalueapi('/get/lastrevisioncook?CookName='+cook[0],"Revision")) - to.append(returnTable("/get/statuscook?CookName="+cook[0],'ASIS')) + to.append(int(returnvalueapi('/get/lastrevisioncook?CookName='+cook[0],"Revision"))) # Has to be a number + tabletemp = returnTable("/get/statuscook?CookName="+cook[0],'ASIS') + print (tabletemp) + for x in range(len(tabletemp)): + print(type(tabletemp[x]['Revision'])) + if type(tabletemp[x]['Revision']) != int: # Si no es un número, cambiar. + tabletemp[x]['Revision'] = 0 + to.append(tabletemp) cooksdata.append(to) tem = (returnTable("/get/cookall",'ASIS')) #Lista de todas las recetas @@ -243,29 +257,44 @@ def pAdminCookDelgroup(): # Add cook to group else: return "Error of arguments" - +#First time password set +@app.route("/firsttime",methods=['POST']) +def pFirstTime(): #Add computer form enter + passwd = request.args.get('password') + passwd = hashlib.sha256(passwd.encode()).hexdigest() + if passwd != None: + r = returnvalueapi("/upd/password?NewPassword="+passwd,field="RESULT") + if r == "OK": + res = make_response(redirect(url_for('pAdminIndex'))) + res.set_cookie("admin_logued",value=passwd) + else: #Hmm..Unespecified error + render_template('/firsttime.tmpl') + else: + return render_template('/firsttime.tmpl') @app.route("/",methods=['GET']) def pIndex(): #Index page howispasswd=returnvalueapi('/check/password?Password='+str(request.cookies.get('admin_logued')),'EXITCODE') if howispasswd == '0': # Logued return redirect(url_for('pAdminIndex')) - if howispasswd == '1' or howispasswd == '2': # Has to login + elif howispasswd == '1' or howispasswd == '2': # Has to login return render_template('/login.tmpl') - if howispasswd == '3': # No password yet - return 'Yet to do it..' + elif howispasswd == '3': # No password yet + return render_template('/firsttime.tmpl') @app.route("/login",methods=['GET','POST']) def pLogin(): #Login if request.values.get('password'): trpass = hashlib.sha256(request.values.get('password').encode()).hexdigest() - #print(returnvalueapi('/check/password?Password='+trpass,'EXITCODE')) - if returnvalueapi('/check/password?Password='+trpass,'EXITCODE') == '0': + howispasswd = returnvalueapi('/check/password?Password='+trpass,'EXITCODE') + if howispasswd == '0': res = make_response(redirect(url_for('pAdminIndex'))) res.set_cookie("admin_logued",value=trpass) return res - else: + elif howispasswd in (1,2): return render_template('/login.tmpl') + elif howispasswd == '3': # No password yet + return render_template('/firsttime.tmpl') else: return render_template('/login.tmpl') diff --git a/admin/templates/admincomputer.tmpl b/admin/templates/admincomputer.tmpl index 43e8fa2..5694f3d 100644 --- a/admin/templates/admincomputer.tmpl +++ b/admin/templates/admincomputer.tmpl @@ -15,8 +15,9 @@ - {% for item in tablegroups %} {#['ID_G','Name','Cooks[CookName]']#} + {% for item in groups %} {#['ID_G','Name','Cooks[CookName]']#} + {{item.0}} {{item.1}} {% for x in item.2 %} diff --git a/admin/templates/admingroup.tmpl b/admin/templates/admingroup.tmpl index 51053f3..0bff24e 100644 --- a/admin/templates/admingroup.tmpl +++ b/admin/templates/admingroup.tmpl @@ -130,7 +130,7 @@ {{item.1}} {% for subitem in item.2|sort(attribute='Revision')|sort(attribute='Name') %} {# item.2 = AppliedTo #} - {{subitem.Name}}{% if subitem.Revision|int < item.1|int %}({{subitem.Revision}}){% elif subitem.Error != 0 %}(Error r.{{subitem.Revision}}){% endif %}, + {{subitem.Name}}{% if subitem.Revision|int < item.1|int %}({{subitem.Revision}}){% elif subitem.Error > 0 %}(Error r.{{subitem.Revision}}){% endif %}, {% endfor %} {% endfor %} diff --git a/admin/templates/adminindex.tmpl b/admin/templates/adminindex.tmpl index 36abd5d..45e66b2 100644 --- a/admin/templates/adminindex.tmpl +++ b/admin/templates/adminindex.tmpl @@ -26,13 +26,13 @@ - +