Skip to content

Commit

Permalink
Ability to edit previous game sheets (#23)
Browse files Browse the repository at this point in the history
Added more security to API for saving sheet
Sort match sheets by ID (also order of games)
  • Loading branch information
fras2560 authored Jan 14, 2021
1 parent 09b58be commit 202b1d5
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 20 deletions.
163 changes: 157 additions & 6 deletions app/templates/edit_sheet.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,162 @@
{% extends "base.html" %}
{% block content %}
<h1>
Edit Match Scores
</h1>
<div id="editSheetsApp">
<h1>
Edit Match Scores
</h1>
<h2>
{{ match.home_team}}
VS.
{{ match.away_team}}
</h2>
<sheet-component
:sheet="sheet"
v-for="sheet in match.sheets">
</sheet-component>
</div>

<h5>
TODO
</h5>
{% endblock %}


{% block script %}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
$(document).ready(ready());
function ready() {

$.ajax({
type: "GET",
url: "{{match_link}}",
contentType: "application/json",
dataType: "json",
async: true,
success: function(match) {
startApp(match);
window.appReady = true;
}, error: function(request, error) {
console.error(request);
console.error(error);
}
});
}
function startApp(match) {
console.log(match);
Vue.component('sheet-component', {
props: {
sheet: Object
},
template: `
<div>
<div class="row left-align border-top my-3">
<div class="col-12 col-sm-6 col-md-2">
<label>Away Team score: [[sheet.away_score]]</label>
<input v-model="sheet.away_score" placeholder="Away Team Score" class="form-control" aria-label="Away Team Score">
</div>
<div class="col-12 col-sm-6 col-md-2">
<label>Away Team dingers:</label>
<input v-model="sheet.away_dingers" placeholder="Away Team Dingers" class="form-control" aria-label="Away Team Dingers">
</div>
<div class="col-12 col-sm-4 col-md-2">
<label>Away Team deuces:</label>
<input v-model="sheet.away_deuces" placeholder="Away Team Deuces" class="form-control" aria-label="Away Team Deuces">
</div>
<div class="col-12 col-sm-4 col-md-2">
<label>Away Team jams:</label>
<input v-model="sheet.away_jams" placeholder="Away Team Jams" class="form-control" aria-label="Away Team Jams">
</div>
<div class="col-12 col-sm-4 col-md-4">
<label>Away Team slot:</label>
<input type="checkbox" v-model="sheet.away_slot" class="form-control" aria-label="Away Team Slot">
</div>
<div class="col-12 col-sm-6 col-md-2">
<label>Home Team score:</label>
<input v-model="sheet.home_score" placeholder="Home Team Score" class="form-control" aria-label="Home Team Score">
</div>
<div class="col-12 col-sm-6 col-md-2">
<label>Home Team dingers:</label>
<input v-model="sheet.home_dingers" placeholder="Home Team Dingers" class="form-control" aria-label="Home Team Dingers">
</div>
<div class="col-12 col-sm-4 col-md-2">
<label>Home Team deuces:</label>
<input v-model="sheet.home_deuces" placeholder="Home Team Deuces" class="form-control" aria-label="Home Team Deuces">
</div>
<div class="col-12 col-sm-4 col-md-2">
<label>Home Team jams:</label>
<input v-model="sheet.home_jams" placeholder="Home Team Jams" class="form-control" aria-label="Home Team Jams">
</div>
<div class="col-12 col-sm-4 col-md-4">
<label>Home Team slot:</label>
<input type="checkbox" v-model="sheet.home_slot" class="form-control" aria-label="Home Team Slot">
</div>
<hr/>
</div>
</div>
`,
delimiters: ['[[', ']]'],
watch:{
"sheet": {
deep: true,
handler(newValue, oldValue) {
console.log("New Value");
console.log(newValue);
console.log(newValue.away_score != "")
console.log(newValue.home_score != "")
console.log(newValue.away_dingers != "")
console.log(newValue.home_dingers != "")
console.log(newValue.away_deuces != "")
console.log(newValue.home_deuces != "")
console.log(newValue.away_jams != "")
console.log(newValue.home_jams != "")
if(newValue.away_score !== "" &&
newValue.home_score !== "" &&
newValue.away_dingers !== "" &&
newValue.home_dingers !== "" &&
newValue.away_deuces !== "" &&
newValue.home_deuces !== "" &&
newValue.away_jams !== "" &&
newValue.home_jams !== ""
) {
console.log("Calling Save Sheet");
this.saveSheet(newValue);
}

},
}
},
methods: {
/** Save the match to a database. */
saveSheet: function(sheet) {
console.log("Save Sheet");
$.ajax({
type: "POST",
data: JSON.stringify(sheet),
url: "{{save_link}}",
contentType: "application/json",
dataType: "json",
async: true,
success: function(saved_sheet) {
console.log(saved_sheet);
}, error: function(request, error) {
alert("Something went wrong, write down your score");
console.error(request);
console.error(error);
}
});
}
}
});
const app = new Vue({
el: '#editSheetsApp',
data: {
match: match,
},
delimiters: ['[[', ']]']
});
if (window.Cypress) {
window.sheetApp = app;
}
console.log(app);
}
</script>
{% endblock %}
1 change: 1 addition & 0 deletions app/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def get_match(match_id):
sheets = []
for sheet in match.sheets:
sheets.append(sheet.json())
sheets.sort(key=lambda sheet: sheet['id'])
match_data["sheets"] = sheets
return Response(json.dumps(match_data),
status=200, mimetype="application/json")
34 changes: 20 additions & 14 deletions app/views/score_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
import json


def check_player_has_permissions_for_match(match_id):
"""Returns match if the player can submit scores for the given match id"""
match = Match.query.get(match_id)
if match is None:
raise NotFoundException(f"Unable to find match - {match_id}")
if not current_user.can_submit_scores(match):
raise LackScorePermissionException(
f"Not part of either team - {match_id}")
return match


@wjl_app.route("/submit_score")
@login_required
def submit_score():
Expand Down Expand Up @@ -44,15 +55,10 @@ def submit_score():


@wjl_app.route("/submit_sheet/<int:match_id>")
@login_required
@api_score_required
def submit_sheet(match_id: int):
"""A route to submit a gamesheet for some match."""
match = Match.query.get(match_id)
if match is None:
raise NotFoundException(f"Unable to find match - {match_id}")
if not current_user.can_submit_scores(match):
raise LackScorePermissionException(
f"Not part of either team - {match_id}")
match = check_player_has_permissions_for_match(match_id)
return render_template("submit_sheet.html",
base_data=get_base_data(),
match=match.json(),
Expand All @@ -68,6 +74,8 @@ def save_sheet():
try:
sheet = request.get_json(silent=True)
saved_sheet = Sheet.from_json(sheet)
# ensure the current users can actually save the sheet
check_player_has_permissions_for_match(saved_sheet.match_id)
DB.session.add(saved_sheet)
DB.session.commit()
LOGGER.info(
Expand All @@ -82,14 +90,12 @@ def save_sheet():


@wjl_app.route("/edit_sheet/<int:match_id>")
@login_required
@api_score_required
def edit_sheet(match_id: int):
"""A route to edit a match's gamesheets."""
match = Match.query.get(match_id)
if match is None:
raise NotFoundException("Unable to find match {match_id}")
if not current_user.can_submit_scores(match):
raise LackScorePermissionException("Not part of team")
match = check_player_has_permissions_for_match(match_id)
return render_template("edit_sheet.html",
base_data=get_base_data(),
match=match)
match=match,
match_link=url_for("get_match", match_id=match_id),
save_link=url_for("save_sheet"))

0 comments on commit 202b1d5

Please sign in to comment.