Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Scratchcat1 authored Dec 13, 2017
1 parent f238c0e commit 8a69765
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
172 changes: 172 additions & 0 deletions flask_app/Flask_Test_App.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
from flask import Flask, flash, redirect, render_template, request, session, abort
import random,os,ast,prettytable
from flask_app import forms

import AATC_Server_002 as AATC_Server
import HedaBot
COMMANDS = HedaBot.CreateCommandDictionary()
COMMANDS["AddFlight"][2]["Type"] = lambda x: HedaBot.SplitWaypoints(x,":")
COMMANDS["AddFlight"][2]["Query"] = COMMANDS["AddFlight"][2]["Query"].replace("returns","colons")

app = Flask(__name__)
app.config.from_object('config')





@app.route("/")
def home():
## session["UserID"] = random.randint(0,1000)
return render_template("base.html",user = {"Username":session.get("UserID"), "UserID":session.get("UserID")},Commands = COMMANDS)

@app.route("/help")
def help_page():
return render_template("help.html",name = session.get("UserID"),user = {"Username":session.get("UserID"), "UserID":session.get("UserID")})

@app.route("/base")
def base():
return render_template("base.html",user = {"Username":session.get("UserID"), "UserID":session.get("UserID")})

@app.route("/quote")
def quote():
quotes = ObtainQuote(3)
return render_template("quote.html", quotes = quotes,user = {"Username":session.get("UserID"), "UserID":session.get("UserID")})


@app.route("/login", methods=['GET', 'POST'])
def login():
form = forms.LoginForm()

if form.validate_on_submit():
print("Loggin in ...")
if form.Username.data == form.Password.data:
session["UserID"] = form.Username.data
else:
session["UserID"] = -1
return render_template("LoginForm.html",title = "Login",form = form,user = {"Username":session.get("UserID"), "UserID":session.get("UserID")})


@app.route("/dyno", methods=['GET', 'POST'])
def dyno():
items = [{"name":"Username"},{"name":"Password"}]

fields = [{"name":"Username","form":forms.wtforms.StringField('Username', validators=[forms.DataRequired()])},
{"name":"Password","form":forms.wtforms.StringField('Password', validators=[forms.DataRequired()])}]
#form = forms.DynoForm(fields = items)
form = forms.update_form(fields)
print(form.__dict__)


if form.validate_on_submit():
print("Loggin in ...")
print(form.fields.data)
if form.Username.data == form.Password.data:
session["UserID"] = form.Username.data
else:
session["UserID"] = -1

#print(form.fields.__dict__)
return render_template("DynamicForm.html",title = "Login",form = form,user = {"Username":session.get("UserID"), "UserID":session.get("UserID")},fields = fields)


@app.route("/command/<string:command>",methods=['GET', 'POST'])
def Dynamic_Form(command):
if command not in COMMANDS:
return "FAILURE COMMAND DOES NOT EXIST"

Fields = Generate_Fields(command)
form = forms.update_form(Fields)

if form.validate_on_submit():
packet = Evaluate_Form(command,form)
WebConnection = AATC_Server.WebConnection(session.get("UserID",-1))
Sucess,Message,Data = WebConnection.Main(packet)

if command == "Login":
session["UserID"] = Data
Data = []

rendered = RenderResults(Sucess,Message,Data)
print(rendered)

return render_template("DynamicForm2.html",title = "Output",form = form,user = {"Username":session.get("UserID"), "UserID":session.get("UserID")},fields = Fields ,Commands = COMMANDS, OUTPUT = True, rendered_result = rendered)

return render_template("DynamicForm2.html",title = "command",form = form,user = {"Username":session.get("UserID"), "UserID":session.get("UserID")},fields = Fields,Commands = COMMANDS)






def Generate_Fields(command):

Queries = COMMANDS[command]
Fields = []
for x in range(1,len(Queries)+1):
query_name = Queries[x]["Query"]
field = {"name":query_name ,"form":forms.wtforms.StringField(query_name, validators=[forms.DataRequired()])}
Fields.append(field)

return Fields


def Evaluate_Form(command,form):
Queries = COMMANDS[command]
Arguments = []
for x in range(1,len(Queries)+1):
Arguments.append( Queries[x]["Type"](form.__dict__[Queries[x]["Query"]].data))

packet = (command,Arguments)
return packet







def RenderResults(Sucess,Message,Data = None):
render = ""
render += "Sucess >>"+str(Sucess)+"\n"
render += "Message >>"+str(Message) +"\n"
if Data not in [None,[]]:
try:
Columns = ast.literal_eval(Message)
Table = prettytable.PrettyTable(Columns)
for row in Data:
Table.add_row(row)
render += str(Table)
except Exception as e:
render += "Error creating asthetic table"+str(e) +"\n"
for row in Data:
render += str(row)+"\n"
render += ""
rendered = render.split("\n")
return rendered





##def ObtainQuote(number = 1):
## with open(os.path.join(os.path.abspath(os.path.join(os.getcwd(), os.pardir)),"SkyrimDialogue.txt"),"r") as f:
## for i,line in enumerate(f):
## pass
##
## responses = []
## for f in range(number):
## lineNum = random.randint(0,i+1)
## with open(os.path.join(os.path.abspath(os.path.join(os.getcwd(), os.pardir)),"SkyrimDialogue.txt"),"r") as f:
## for x in range(lineNum):
## line = f.readline()
## responses.append( line.rstrip().split("\t")[-1:][0])
## return responses


def main_app(app):
app.secret_key = "abcewhfuhiwuhef"
app.run(host = "0.0.0.0")

if __name__ == "__main__":
main_app(app)
24 changes: 24 additions & 0 deletions flask_app/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask_wtf import FlaskForm
import wtforms
from wtforms.validators import DataRequired

class LoginForm(FlaskForm):
Username = wtforms.StringField('Username', validators=[DataRequired()])
Password = wtforms.StringField('Password', validators=[DataRequired()])


class DynoField(wtforms.Form):
name = wtforms.TextField()

class DynoForm(FlaskForm):
fields = wtforms.FieldList(wtforms.FormField(DynoField),min_entries = 1)

def update_form(fields):
class MyForm(FlaskForm):
pass

for item in fields:
setattr(MyForm,item["name"],item["form"])
print(MyForm.__dict__)

return MyForm()

0 comments on commit 8a69765

Please sign in to comment.