Now working with web.py and changes to the database layout and new functions added.
This commit is contained in:
parent
f33af7fe50
commit
c26e627976
4 changed files with 106 additions and 9 deletions
109
app.py
109
app.py
|
@ -2,30 +2,127 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import web
|
||||
from web import form
|
||||
|
||||
# Framework initialization
|
||||
|
||||
render = web.template.render('views/')
|
||||
db = web.database(dbn = 'sqlite', db = 'game.db')
|
||||
|
||||
urls = (
|
||||
'/app', 'summary',
|
||||
'/app/', 'summary',
|
||||
'/app/config', 'config',
|
||||
'/app/config/', 'config',
|
||||
'/app/config/add', 'stat_add',
|
||||
'/app/config/remove/(.+)', 'remove_stat',
|
||||
'/app/config/edit/(.+)', 'edit_stat',
|
||||
)
|
||||
|
||||
# Forms used by the app
|
||||
add_stat = form.Form(
|
||||
form.Textbox('stat_name',
|
||||
description = 'Stat name:'),
|
||||
form.Textbox('default_value',
|
||||
description = 'Default value:',
|
||||
value = 0),
|
||||
form.Button('Add Stat')
|
||||
)
|
||||
|
||||
app = web.application(urls, globals())
|
||||
|
||||
def render_html(game_name, main_block):
|
||||
# Auxiliary functions
|
||||
|
||||
def render_web(game_name, main_block):
|
||||
"""
|
||||
Cobbles together all the pieces we've broken our
|
||||
templates, so we can have a kind of modular design
|
||||
"""
|
||||
sidebar = render.sidebar()
|
||||
return render.layout(main_block, sidebar, game_name)
|
||||
|
||||
# Server calls
|
||||
|
||||
class summary(object):
|
||||
def GET(self):
|
||||
# TODO: Get from main configuration file
|
||||
game_name = "My Game"
|
||||
player_num = 3
|
||||
stats_num = 10
|
||||
players = ['Reset', 'Logan', 'Estela']
|
||||
|
||||
summary = render.summary(game_name, player_num, stats_num, players)
|
||||
return render_html(game_name, summary)
|
||||
# Variable initialization
|
||||
player_num = 0
|
||||
stats_num = 0
|
||||
players = []
|
||||
for player in db.select("players"):
|
||||
player_num +=1
|
||||
players.append(player['name'])
|
||||
|
||||
# I know, this is pretty clumsy...
|
||||
for stat in db.select("stats"):
|
||||
stats_num += 1
|
||||
|
||||
# We render our main block and feed it to our
|
||||
# Web renderer
|
||||
summary = render.summary(game_name,
|
||||
player_num,
|
||||
stats_num,
|
||||
players)
|
||||
return render_web(game_name, summary)
|
||||
|
||||
|
||||
class config(object):
|
||||
def GET(self):
|
||||
game_name = "My Game"
|
||||
stats = []
|
||||
for stat in db.select("stats"):
|
||||
stats.append(stat)
|
||||
|
||||
add_stat_form = add_stat.render()
|
||||
|
||||
config = render.config(game_name,
|
||||
stats,
|
||||
add_stat_form)
|
||||
|
||||
return render_web(game_name, config)
|
||||
|
||||
|
||||
class stat_add(object):
|
||||
def POST(self):
|
||||
form = add_stat()
|
||||
if form.validates():
|
||||
db.insert('stats', name = form.d.stat_name, default_value = form.d.default_value)
|
||||
|
||||
raise web.seeother('/app/config')
|
||||
|
||||
|
||||
class remove_stat(object):
|
||||
def GET(self, index_to_remove):
|
||||
try:
|
||||
index = int(index_to_remove)
|
||||
|
||||
except Exception as e:
|
||||
return "Ooops! Wrong index!"
|
||||
|
||||
db.delete('stats', where = 'id = $index', vars = locals())
|
||||
raise web.seeother('/app/config')
|
||||
|
||||
class edit_stat(object):
|
||||
def GET(self, index_to_remove):
|
||||
game_name = "My Game"
|
||||
try:
|
||||
index = int(index_to_remove)
|
||||
|
||||
except Exception as e:
|
||||
return "Ooops! Wrong index!"
|
||||
|
||||
for s in db.select('stats', where = 'id = $index', vars = locals()):
|
||||
stat_info = s
|
||||
|
||||
edit_stat_form = add_stat.render()
|
||||
edit_stat = render.edit_stat(stat_info, edit_stat_form)
|
||||
|
||||
return render_web(game_name, edit_stat)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
|
|
Binary file not shown.
|
@ -2,12 +2,12 @@ $def with (main_block, sidebar_block, game_name)
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>TyeDye for $game_name</title>
|
||||
<title>TyeDye control panel for $game_name</title>
|
||||
<link rel='stylesheet' type='text/css' href='/static/main.css'>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>TyeDye $game_name</h1>
|
||||
<h1>TyeDye: $game_name control panel</h1>
|
||||
</header>
|
||||
<div class='container'>
|
||||
<div class='main-content'>
|
||||
|
|
|
@ -5,5 +5,5 @@ $def with (game_name, player_num, stats_num, player_list)
|
|||
<p>Player list:<br />
|
||||
<ul>
|
||||
$for player in player_list:
|
||||
<li>$player</li>
|
||||
<li><a href="/app/player/$player">$player</a></li>
|
||||
</ul>
|
||||
|
|
Loading…
Reference in a new issue