Commit dba3a519 authored by Håkon Finstad's avatar Håkon Finstad
Browse files

test

parent 4c64bfb2
Pipeline #161789 passed with stage
in 24 seconds
......@@ -47,8 +47,8 @@ class Workout(models.Model):
get_user_model(), on_delete=models.CASCADE, related_name="workouts"
)
difficulty = models.IntegerField(default=1)
members = models.ForeignKey(
"self", on_delete=models.CASCADE, related_name="groupworkouts", blank=True, null=True
group_members = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="group_member", blank=True, null=True
)
# Visibility levels
......
......@@ -78,7 +78,8 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
"visibility",
"exercise_instances",
"files",
"difficulty"
"difficulty",
"members"
]
extra_kwargs = {"owner": {"read_only": True}}
......
......@@ -159,7 +159,7 @@ async function updateWorkout(id) {
}
}
function generateWorkoutForm() {
async function generateWorkoutForm() {
let form = document.querySelector("#form-workout");
let formData = new FormData(form);
......@@ -171,6 +171,7 @@ function generateWorkoutForm() {
submitForm.append("notes", formData.get("notes"));
submitForm.append("visibility", formData.get("visibility"));
// adding exercise instances
let exerciseInstances = [];
let exerciseInstancesTypes = formData.getAll("type");
......@@ -183,8 +184,34 @@ function generateWorkoutForm() {
sets: exerciseInstancesSets[i]
});
}
submitForm.append("exercise_instances", JSON.stringify(exerciseInstances));
let rosterInputs = formData.getAll("member");
console.log(rosterInputs);
let body = [];
if(rosterInputs != null)
{
for (let rosterInput of rosterInputs) {
if (!rosterInput.disabled && rosterInput.value) {
// get user
let response = await sendRequest("GET", `${HOST}/api/users/${rosterInput.value}/`);
if (response.ok) {
let member = await response.json();
body.push(member);
} else {
let data = await response.json();
let alert = createAlert(`Could not retrieve user ${rosterInput.value}!`, data);
document.body.prepend(alert);
}
}
}
console.log(JSON.stringify(body))
if(body.length != 0)
submitForm.append("members", body);
}
// adding files
for (let file of formData.getAll("files")) {
submitForm.append("files", file);
......@@ -193,7 +220,7 @@ function generateWorkoutForm() {
}
async function createWorkout() {
let submitForm = generateWorkoutForm();
let submitForm = await generateWorkoutForm();
let response = await sendRequest("POST", `${HOST}/api/workouts/`, submitForm, "");
......@@ -320,7 +347,7 @@ function addAthleteRow(event) {
let newBlankRow = event.currentTarget.parentElement.cloneNode(true);
let newInput = newBlankRow.querySelector("input");
newInput.value = "";
let controls = document.querySelector("#controls");
let controls = document.querySelector("#div-members");
let button = newBlankRow.querySelector("button");
button.addEventListener("click", addAthleteRow);
controls.appendChild(newBlankRow);
......@@ -338,19 +365,19 @@ function removeAthleteRow(event) {
async function displayCurrentRoster(currentUser, workoutid) {
let templateFilledAthlete = document.querySelector("#template-filled-member");
let templateEmptyAthlete = document.querySelector("#template-empty-member");
let controls = document.querySelector("#controls");
let body = {workout: `${HOST}/api/workouts/${workoutid}/`};
let controls = document.querySelector("#div-members");
/* let body = {workout: `${HOST}/api/workouts/${workoutid}/`};
console.log(body);
console.log(workout);
console.log(workout.members);
*/
for (let member of workout.members) {
/*
for (let athleteUrl of currentUser.athletes) {
let response = await sendRequest("GET", athleteUrl);
let athlete = await response.json();
createFilledRow(templateFilledAthlete, athlete.username, controls, false);
}
}*/
let emptyClone = templateEmptyAthlete.content.cloneNode(true);
let emptyDiv = emptyClone.querySelector("div");
......@@ -382,6 +409,8 @@ window.addEventListener("DOMContentLoaded", async () => {
if (urlParams.has('id')) {
const id = urlParams.get('id');
let workoutData = await retrieveWorkout(id);
await retrieveComments(id);
if (workoutData["owner"] == currentUser.url || workoutData["members"].includes(currentUser.url)) {
......@@ -391,6 +420,7 @@ window.addEventListener("DOMContentLoaded", async () => {
okWorkoutButton.addEventListener("click", (async (id) => await updateWorkout(id)).bind(undefined, id));
postCommentButton.addEventListener("click", (async (id) => await createComment(id)).bind(undefined, id));
divCommentRow.className = divCommentRow.className.replace(" hide", "");
await displayCurrentRoster(currentUser, id);
}
......@@ -413,4 +443,5 @@ window.addEventListener("DOMContentLoaded", async () => {
divCommentRow.className += " hide";
}
});
\ No newline at end of file
......@@ -80,18 +80,12 @@
<input type="button" class="btn btn-danger hide" id="btn-remove-exercise" value="Remove exercise">
</div>
<div class="col-lg-6"></div>
<label class="form-label" for="controls">Members:</label>
<div id="div-members">
</form>
<form id="form-roster" class="row">
<div class="col-lg-6">
<label class="form-label" for="controls">Athletes</label>
<div id="controls">
</div>
<input type="button" class="btn btn-primary mt-1 mb-2" id="button-submit-roster" value="Submit">
</div>
<div class="col-lg-6"></div>
</form>
</form>
<div class="row bootstrap snippets bootdeys" id="div-comment-row">
<div class="col-md-8 col-sm-12">
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment