deploy.py 2.57 KB
#-*- coding: utf-8 -*-
from flask import Flask, json, request, url_for, render_template
import random

app = Flask(__name__)

TOKEN_LENGHT = 32
TOKEN_STRING = "\
1234567890\
qwertyuiopasdfghjklzxcvbnm\
QWERTYUIOPASDFGHJKLZXCVBNM"
sessions = {}

@app.after_request
def after_request(response):
    # headers permitidos para la conversación
    response.headers.add('Access-Control-Allow-Headers', '\
        Access-Control-Allow-Methods,\
        Access-Control-Allow-Origin,\
        Content-Type')
    # orígenes permitidos para CORS
    response.headers.add('Access-Control-Allow-Origin', '*')
    # métodos permitidos
    response.headers.add('Access-Control-Allow-Methods', 'GET,POST')
    return response

def validate(name,psw):
  if name == "admin" and psw == "admin":
    return True
  return False

def token():
  result = ""
  i = 0
  while i < TOKEN_LENGHT:
    char = random.randrange(0,len(TOKEN_STRING))
    result += str(TOKEN_STRING[char]) 
    i += 1
  return result

@app.route('/login', methods = ['POST'])
def login():
  data = request.json
  if validate(data['name'],data['pass']):
    sessions['name'] = token()
    return json.dumps({
      "error_code" : 0,
      "error" : "",
      "token" : sessions['name']
    })
  return json.dumps({
      "error_code" : 1,
      "error" : "fallo en la validacion",
      "token" : None
  })

@app.route('/', methods = ['GET', 'POST'])
def main():
  data = [
    {
      "title" : "first",
      "fields" : [
        {
          "title" : "1",
          "type" : "mail",
          "placeholder" : "example@example.com",
        },
        {
          "title" : "2",
          "type" : "file",
          "placeholder" : "999",
        },
        {
          "title" : "3",
          "type" : "checkbox",
          "placeholder" : "999",
        },
        {
          "title" : "4",
          "type" : "text",
          "required" : True
        }
      ]
    },
    {
      "title" : "second",
      "fields" : [
        {
          "title" : "first",
          "type" : "select",
          "placeholder" : "nedea",
          "options" : [
            "first",
            "second",
            "third"
          ]
        },
        {
          "title" : "second",
          "type" : "number",
          "placeholder" : "999",
          "options" : [
            "first",
            "second",
            "third"
          ],
          "required" : True
        }
      ]
    },
    {
      "title" : "third",
      "fields" : []
    }
  ]
  return render_template("form.html",tabs=data,title="Formulario de Prueba")

if __name__ == "__main__":
  app.run("0.0.0.0")