Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
*~
*.db
tournament_venv
*.log
*.log
.idea
*.iml
8 changes: 6 additions & 2 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from flask import Flask
import logging

from flask import Flask

import config


app = Flask(__name__)

app.config.from_object('config')
Expand All @@ -11,6 +14,7 @@

logging.basicConfig(filename=config.LOGFILE,
level=logging.DEBUG,
format='%(asctime)s %(message)s',
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')

logging.getLogger().addHandler(logging.StreamHandler())
192 changes: 107 additions & 85 deletions app/api.py
Original file line number Diff line number Diff line change
@@ -1,166 +1,188 @@
from sqlite3 import IntegrityError

from flask import request
import json
import sqlite3

from app import app
from models import Player
import tournament_dao, player_dao, match_dao, tourney
import tournament_dao
import player_dao
import match_dao
import tourney
from deepJson import jsonify

@app.route('/api/tournament', methods = ['POST'])

@app.route('/api/tournament', methods=['POST'])
def post_tournament():
try:
description = request.form['description']
tourn_type = ''
tournament = tourney.create_tournament(description, tourn_type)
except sqlite3.IntegrityError:
except IntegrityError:
message = "DB ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify(tournament)

@app.route('/api/tournament/status/<status>', methods = ['GET'])

@app.route('/api/tournament/status/<status>', methods=['GET'])
def get_new_tournaments(status):
tourneys = tournament_dao.find_all_by_status(status)
return jsonify({'tournaments':tourneys})
return jsonify({'tournaments': tourneys})


@app.route('/api/tournament/<id>', methods = ['GET','POST','DELETE'])
def tournament2(id):
@app.route('/api/tournament/<tournament_id>', methods=['GET', 'POST', 'DELETE'])
def tournament_http(tournament_id):
if request.method == 'POST':
return _post_tourney_entries(id, request)
return _post_tourney_entries(tournament_id)
elif request.method == 'DELETE':
return _delete_tournament(id)
return _delete_tournament(tournament_id)
elif request.method == 'GET':
return _get_tournament(id)
return _get_tournament(tournament_id)


def _post_tourney_entries(id, request):
def _post_tourney_entries(tournament_id):
try:
player_ids = request.form['entries']
tourney.setup_round_robin(player_ids, id)
except sqlite3.IntegrityError:
tourney.setup_round_robin(player_ids, tournament_id)
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'success':True, 'id':id})
return jsonify({'success': True, 'tournament_id': tournament_id})

def _delete_tournament(id):

def _delete_tournament(tournament_id):
try:
tournament_dao.delete(id)
except sqlite3.IntegrityError:
tournament_dao.delete(tournament_id)
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'success':True, 'id':id})
return jsonify({'success': True, 'tournament_id': tournament_id})


def _get_tournament(id):
def _get_tournament(tournament_id):
try:
tournament = tournament_dao.find(id)
except sqlite3.IntegrityError:
tournament = tournament_dao.find(tournament_id)
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
if tournament:
return jsonify({'success':True, 'tournament':tournament})
return jsonify({'success': True, 'tournament': tournament})
else:
return jsonify({'success':False, 'message':'Not Found'}),404
return jsonify({'success': False, 'message': 'Not Found'}), 404


@app.route('/api/match/<id>', methods = ['POST','DELETE'])
def match(id):
@app.route('/api/match/<match_id>', methods=['POST', 'DELETE'])
def match_http(match_id):
if request.method == 'POST':
return _post_match(id, request)
return _post_match(match_id)
elif request.method == 'DELETE':
return _delete_match(id)
return _delete_match(match_id)

def _post_match(id, request):

def _post_match(match_id):
try:
params = request.form
match = tourney.update_match(id, params['player1_id'],
match = tourney.update_match(match_id, params['player1_id'],
params['player2_id'],
params['score1'], params['score2'])
except sqlite3.IntegrityError:
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'match':match})
return jsonify({'match': match})


def _delete_match(id):
def _delete_match(match_id):
try:
match = tourney.undo_match(id)
except sqlite3.IntegrityError:
match = tourney.undo_match(match_id)
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'match':match})

@app.route('/api/tournament/<id>/match', methods = ['GET'])
def get_matches(id):
matches = match_dao.find_by_tournament(id)
return jsonify({'matches':matches})

@app.route('/api/tournament/<id>/standings', methods = ['GET'])
def get_standings(id):
standings = tourney.find_standings(id)
return jsonify({'standings':standings})

@app.route('/api/tournament/<id>/player', methods = ['GET'])
def get_entries(id):
players = player_dao.find_in_tournament(id)
return jsonify({'players':players})

@app.route('/api/tournament/<tournament_id>/player/<player_id>',
methods = ['POST','DELETE'])
def add_or_delete_entry(tournament_id, player_id):
return jsonify({'match': match})


@app.route('/api/tournament/<tournament_id>/match', methods=['GET'])
def get_matches(tournament_id):
matches = match_dao.find_by_tournament(tournament_id)
return jsonify({'matches': matches})


@app.route('/api/tournament/<tournament_id>/standings', methods=['GET'])
def get_standings(tournament_id):
standings = tourney.find_standings(tournament_id)
return jsonify({'standings': standings})


@app.route('/api/tournament/<tournament_id>/player', methods=['GET'])
def get_entries(tournament_id):
players = player_dao.find_in_tournament(tournament_id)
return jsonify({'players': players})


@app.route('/api/tournament/<tournament_id>/player/<player_id>',
methods=['POST', 'DELETE'])
def entry_http(tournament_id, player_id):
try:
if request.method == 'POST':
player = player_dao.enter_tournament(player_id, tournament_id)
elif request.method == 'DELETE':
player = player_dao.unenter_tournament(player_id, tournament_id)
except sqlite3.IntegrityError:
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'success':True, 'player':player})
return jsonify({'success': True, 'player': player})


@app.route('/api/tournament/<tournament_id>/status/<status>',
methods = ['POST'])
@app.route('/api/tournament/<tournament_id>/status/<status>',
methods=['POST'])
def update_status(tournament_id, status):
try:
if status == '1':
tourney.setup_round_robin(tournament_id)
tournament_dao.update_status(tournament_id, status)
except sqlite3.IntegrityError:
except IntegrityError:
message = "ERROR!"
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'success':True, 'id':tournament_id})
return jsonify({'success': True, 'tournament_id': tournament_id})


@app.route('/api/player/<id>', methods = ['DELETE'])
def delete_player(id):
@app.route('/api/player/<player_id>', methods=['DELETE'])
def delete_player(player_id):
try:
player_dao.delete(id)
except sqlite3.IntegrityError:
player_dao.delete(player_id)
except IntegrityError:
message = "Players in tournaments cannot be deleted."
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'success':True, 'id':id})
return jsonify({'success': True, 'tournament_id': player_id})

@app.route('/api/player', methods = ['GET','POST'])
def api_player():

@app.route('/api/player', methods=['GET', 'POST'])
def player_http():
if request.method == 'POST':
return _post_player(request)
return _post_player()
elif request.method == 'GET':
return _get_player()

def _post_player(request):

def _post_player():
try:
fname = request.form['fname']
id = player_dao.create(Player(fname))
except sqlite3.IntegrityError:
player_id = player_dao.create(Player(fname))
except IntegrityError:
message = "Player name must be unique."
return jsonify({'success':False, 'message':message}),409
return jsonify({'success': False, 'message': message}), 409
else:
return jsonify({'id':id,'fname':fname})
return jsonify({'player_id': player_id, 'fname': fname})


def _get_player():
players = player_dao.find_all()
return jsonify({'players':players})
return jsonify({'players': players})

8 changes: 4 additions & 4 deletions app/deepJson.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import types
import json



def jsonify(obj):
"""Converts to JSON like json.dumps, but supports objects

Expand All @@ -10,16 +11,15 @@ def jsonify(obj):
The resulting collection is then passed into json.dumps()
which produces a JSON representation of the input.
"""

def _dictify(val):
if type(val) in [int, long, float, bool, str, unicode, types.NoneType]:
return val
elif type(val) in [list, tuple]:
return [_dictify(item) for item in val]
elif type(val) == dict:
return {k: _dictify(val[k]) for k in val}
elif type(val) == complex:
return {"real": val.real, "imag": val.imag}
elif hasattr(val,'__dict__'):
elif hasattr(val, '__dict__'):
return {k: _dictify(getattr(val, k)) for k in val.__dict__}
else:
raise TypeError('Type %s not serializable' % type(val))
Expand Down
Loading