scripts and tools to administer the lingy.in public unix / tilde
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

123 wiersze
3.8KB

  1. import glob
  2. from flask import Flask, redirect, url_for, render_template, request
  3. WORKING_DIR = "/home/gashapwn/lyadmin/";
  4. ACCOUNT_DIR = "test/";
  5. FULL_PATH = str(WORKING_DIR) + str(ACCOUNT_DIR)
  6. # Account requests are given ID numbers
  7. # the first request will have the below
  8. # id number
  9. INIT_REQ_ID = "00000"
  10. app=Flask(__name__)
  11. @app.route("/")
  12. def home():
  13. app.route('/')
  14. u_list = [];
  15. with open("user_list.txt") as u_file:
  16. for line in u_file.readlines():
  17. u_list.append(line.strip());
  18. return render_template("index.html", u_list=u_list, page_name="home")
  19. def rules():
  20. return render_template("rules.html")
  21. def home2(name):
  22. # app.route('/home2/<name>')
  23. # return redirect(url_for('home'))
  24. return render_template("home.html")
  25. def success(name):
  26. return "welcome %s" % name
  27. # this is a weird way to do this
  28. # right?
  29. def widg_fun(widg):
  30. if(widg.w_type == "input"):
  31. return "input id=id_%s name=%s type=text></input"%(widg.w_name, widg.w_name)
  32. elif(widg.w_type == "textarea"):
  33. return "textarea cols=40 id=id_%s name=%s rows=10 required=\"\""%(widg.w_name, widg.w_name)
  34. elif(widg.w_type == "check"):
  35. return "input id=id_%s name=%s type=checkbox required=\"\""%(widg.w_name, widg.w_name)
  36. return widg.w_type;
  37. def req():
  38. app.route('/req')
  39. class Widg:
  40. def __init__(self, w_name, w_type, w_opt):
  41. self.w_name = w_name
  42. self.w_type = w_type
  43. self.w_opt = w_opt
  44. rt = {
  45. "username": Widg("username", "input", None),
  46. "email for account lockout / registration confirmation (optional)": Widg("email", "input", None),
  47. "SSH public key": Widg("pub_key", "textarea", None),
  48. "shell of choice": Widg("shell", "choice", [("bash", "/bin/bash"), ("ksh", "/bin/ksh")]),
  49. "have you read the rules?": Widg("rule_read", "check", None)
  50. };
  51. # uhhh is this how you're supposed to do this?
  52. return render_template("req.html", req_tab = rt, widg_fun = widg_fun, page_name="req")
  53. def signup():
  54. app.route('/req/signup')
  55. username = request.form["username"]
  56. email = request.form["email"]
  57. shell = request.form["shell"]
  58. rule_read = request.form["rule_read"]
  59. is_email_user = False;
  60. if(rule_read != "on"):
  61. return redirect(url_for('req'))
  62. if(len(email) > 1):
  63. is_email_user = True
  64. else:
  65. email = "NO_EMAIL"
  66. if(len(glob.glob("./test/[0-9]*ident*")) == 0):
  67. new_id = int(INIT_REQ_ID)
  68. new_id_str = INIT_REQ_ID
  69. else:
  70. max_id = max(list(map( lambda path : path.split("/")[-1].split(".")[0] , glob.glob("./test/[0-9]*ident*"))))
  71. zpad = len(max_id)
  72. new_id = int(max_id)+1
  73. new_id_str = str(new_id).zfill(zpad)
  74. fn1 = str(FULL_PATH) + str(new_id_str) + ".ident"
  75. with open(fn1, "w") as ident_file:
  76. ident_file.write(str(username) + "\n")
  77. ident_file.write(str(email) + "\n")
  78. ident_file.write(str(shell) + "\n")
  79. ident_file.write(str(rule_read) + "\n")
  80. print(username + " " + email + " " + shell + " " + rule_read)
  81. return render_template("signup.html", is_email_user = is_email_user)
  82. def login():
  83. if request.method == "POST":
  84. user = request.form["nm"]
  85. return redirect(url_for('success', name = user))
  86. else:
  87. return redirect(url_for('home'))
  88. if __name__=="__main__":
  89. app.add_url_rule('/home2/<name>', 'home2', home2)
  90. app.add_url_rule('/rules', 'rules', rules)
  91. app.add_url_rule('/success/<name>', 'success', success)
  92. app.add_url_rule('/login', 'login', login, methods = ['POST', 'GET'])
  93. app.add_url_rule('/req', 'req', req, methods = ['POST', 'GET'])
  94. app.add_url_rule('/req/signup', 'signup', signup, methods = ['POST'])
  95. app.run(host="104.248.118.130",debug=True)