Can return HTML content for a player

This commit is contained in:
Jack Jackson 2024-01-23 22:16:10 -08:00
parent 731bce91a7
commit 5af581923d
4 changed files with 29 additions and 2 deletions

View File

@ -3,6 +3,7 @@
- [X] Basic Game Definition - [X] Basic Game Definition
- [X] Basic Player Definition - [X] Basic Player Definition
- [ ] Basic Deck Definition - [ ] Basic Deck Definition
- [X] Figure out how to return JSON or html (`render_template`)
- [ ] Basic testing - [ ] Basic testing
- [ ] Swagger API - [ ] Swagger API
- [ ] Local development tool to clear/seed database - [ ] Local development tool to clear/seed database

View File

@ -1,4 +1,4 @@
from flask import Blueprint, request from flask import Blueprint, render_template, request
from . import db from . import db
from .models import Deck, Game, Player from .models import Deck, Game, Player
@ -18,5 +18,20 @@ def create_player():
db.session.commit() db.session.commit()
return {'id': player.id} return {'id': player.id}
@main.route("/player/<player_id>")
def get_player(player_id: str):
player_data = _jsonify(db.session.get(Player, int(player_id)))
content_type = request.headers.get('Content-Type')
if content_type == 'application/json':
return player_data
else: # Assume they want HTML
return render_template('player_detail.html', **player_data)
# TODO - implement a GET method - can it be a separate method, or must it be the same annotation with an `if method==`? # TODO - implement a GET method - can it be a separate method, or must it be the same annotation with an `if method==`?
# Time for testing, methinks! # Time for testing, methinks!
# TODO - would this be better as a method on a class extending `db.Model` that the classes in `models.py` could then
# extend?
# (Probably not, as we'd still need to explicitly call it - it wouldn't be implicitly called _by_ Flask)
def _jsonify(o):
return {k: v for (k, v) in o.__dict__.items() if k is not '_sa_instance_state'}

View File

@ -11,7 +11,7 @@ class Deck(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60), nullable=False) name = db.Column(db.String(60), nullable=False)
description = db.Column(db.String) description = db.Column(db.String)
owner = db.Column(db.String, db.ForeignKey(Player.__table__.c.id), nullable=False, ) owner = db.Column(db.String, db.ForeignKey(Player.__table__.c.id), nullable=False)
class Game(db.Model): class Game(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Player - {{ name }}</title>
</head>
<body>
<h1>Hello World!</h1>
<h2>This is the page for player {{ name }} who has id {{ id }}</h2>
</body>
</html>