Can return HTML content for a player
This commit is contained in:
parent
731bce91a7
commit
5af581923d
1
NOTES.md
1
NOTES.md
@ -3,6 +3,7 @@
|
||||
- [X] Basic Game Definition
|
||||
- [X] Basic Player Definition
|
||||
- [ ] Basic Deck Definition
|
||||
- [X] Figure out how to return JSON or html (`render_template`)
|
||||
- [ ] Basic testing
|
||||
- [ ] Swagger API
|
||||
- [ ] Local development tool to clear/seed database
|
||||
|
17
app/main.py
17
app/main.py
@ -1,4 +1,4 @@
|
||||
from flask import Blueprint, request
|
||||
from flask import Blueprint, render_template, request
|
||||
from . import db
|
||||
from .models import Deck, Game, Player
|
||||
|
||||
@ -18,5 +18,20 @@ def create_player():
|
||||
db.session.commit()
|
||||
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==`?
|
||||
# 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'}
|
@ -11,7 +11,7 @@ class Deck(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(60), nullable=False)
|
||||
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):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
|
11
app/templates/player_detail.html
Normal file
11
app/templates/player_detail.html
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user