Skip to content

Commit 97dcb05

Browse files
19102024_upgrade model iterable
1 parent 92017e5 commit 97dcb05

19 files changed

Lines changed: 1602 additions & 299 deletions

File tree

flask_app/admin/editions/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ def modify_edition_page(event_name, edition_name):
8282
form.rdv_lng.render_kw = {}
8383
form.parcours.render_kw = {}
8484
if edition.first_inscription <= datetime.now():
85-
form.edition_date.render_kw["disabled"]= "disabled" # si ils peuvent s'iscrire ne plus modifier la date de l'edition
85+
# si ils peuvent s'iscrire ne plus modifier la date de l'edition
86+
form.edition_date.render_kw["disabled"]= "disabled"
8687
form.parcours.render_kw["disabled"]= "disabled"
88+
form.parcours.data = [str((p.name, p.description)) for p in edition.parcours]
8789
form.first_inscription.render_kw["disabled"]= "disabled"
8890
form.name.render_kw["disabled"]= "disabled"
8991
form.rdv_lat.render_kw["disabled"]= "disabled"
@@ -93,14 +95,13 @@ def modify_edition_page(event_name, edition_name):
9395
#? fin desactivation des champs
9496
ic(form.parcours.data)
9597
ic([str((p.name, p.description)) for p in edition.parcours])
96-
ic(request.form.to_dict())
9798

9899
if form.validate_on_submit():
99100
print(form.parcours.data)
100101
if form.name.data == edition.name or not event.editions.filter_by(name=form.name.data).first():
101102
edition.name = form.name.data
102103
edition.edition_date = form.edition_date.data
103-
edition.parcours = event.parcours.filter(Parcours.name.in_(form.parcours.data)).all()
104+
edition.parcours = event.parcours.filter(Parcours.name.in_([eval(p)[0] for p in form.parcours.data])).all()
104105
edition.first_inscription = form.first_inscription.data
105106
edition.last_inscription = form.last_inscription.data
106107
edition.rdv_lat = form.rdv_lat.data

flask_app/admin/editions/passages/__init__.py

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import time
22
from flask import Blueprint, redirect, render_template, flash, request, jsonify
33
from flask_app import admin_required, db, set_route, lang_url_for as url_for
4+
from flask_app.admin.parcours import calc_points_dist
45
from flask_login import login_required, current_user
5-
from flask_app.models import Event, Edition, PassageKey, Stand, Parcours, Passage, User, Inscription
6+
from flask_app.models import Event, Edition, PassageKey, Stand, Parcours, Passage, User, Inscription, Trace
67
from datetime import datetime
78
from flask_app.admin.editions.passages.form import NewKeyForm, ChronoLoginForm, ChronoLoginForm, SetPassageForm
89
import secrets
@@ -29,18 +30,18 @@ def dashboard(event_name, edition_name):
2930

3031
if form.validate_on_submit():
3132
if not edition.passage_keys.filter_by(name=form.name.data).first():
32-
stands = [Stand.query.get(int(stand.data)) for stand in form.stands]
33+
stands = [Stand.query.get(int(stand.data)) for stand in form.stands if stand.data]
3334
key_code=secrets.token_urlsafe(5)
3435
while PassageKey.query.filter_by(key=key_code).first():
3536
key_code=secrets.token_urlsafe(5)
3637
key = PassageKey(event_id=event.id,
3738
edition_id=edition.id,
38-
parcours_id=stand.parcours.id,
39-
stand_id=stand.id,
39+
stands=stands,
4040
key=key_code,
4141
name=form.name.data)
4242
db.session.add(key)
43-
#db.session.commit()
43+
db.session.commit()
44+
ic(key, edition, edition.id)
4445

4546
return redirect(url_for("admin.editions.passages.dashboard", event_name=event.name, edition_name=edition.name))
4647
else:
@@ -67,6 +68,40 @@ def chrono_home():
6768

6869
return render_template('chrono_home.html', user_data=user, form=form)
6970

71+
72+
def parcours_chrono_list_dist(parcours:Parcours, dist_stand:list[int]):
73+
part_list = []
74+
dist_list = []
75+
start = parcours.start_stand
76+
new_stand=start
77+
last_point=None
78+
dist=0
79+
# si aucun depart alors ne mettre aucun stand
80+
if start:
81+
part_list.append(start)
82+
turn_nb = 0
83+
while True:
84+
if new_stand == start:
85+
turn_nb +=1
86+
old_stand = new_stand
87+
# si l'ancien stand a une trace qui part d lui
88+
trace:Trace = old_stand.start_trace.filter_by(turn_nb=turn_nb).first()
89+
if trace :
90+
new_stand = trace.end
91+
dist += (calc_points_dist(new_stand.lat, new_stand.lng, last_point[0], last_point[1]) if last_point else 0)
92+
last_point = new_stand.lat, new_stand.lng
93+
94+
if new_stand.id==dist_stand[0]:
95+
dist_stand = dist_stand[1:]
96+
dist_list.append(dist)
97+
if len(dist_stand)==0:
98+
break
99+
100+
dist += trace.get
101+
else:
102+
break
103+
104+
70105
@set_route(passages, '/chrono/<key_code>')
71106
def chrono_page(key_code):
72107
user = current_user if current_user.is_authenticated else None
@@ -76,19 +111,69 @@ def chrono_page(key_code):
76111
flash('l\'edition n\'est pas aujourd\'hui', 'warning')
77112
return redirect(url_for("admin.editions.passages.chrono_home")) """
78113

79-
key_passages = Passage.query.filter_by(key=key).order_by(Passage.time_stamp.desc()).all()
114+
key_passages = []
115+
user_passages_stand = {}
116+
user_passages_passage = {}
117+
passage:Passage
118+
for passage in Passage.query.filter_by(key=key).order_by(Passage.time_stamp.asc()).all():
119+
ic(passage)
120+
passage_user = passage.inscription.inscrit
121+
passage_stand = passage.key.stands.filter_by(parcours=passage.inscription.parcours).first()
122+
passage_chronos_list = list(passage.inscription.parcours.iter_chrono_list())
123+
if passage_user.id not in user_passages_stand.keys():
124+
user_passages_stand[passage_user.id] = []
125+
user_passages_stand[passage_user.id].append(passage_stand.id)
126+
if passage_user.id not in user_passages_passage.keys():
127+
user_passages_passage[passage_user.id] = []
128+
user_passages_passage[passage_user.id].append(passage.id)
129+
130+
return_list = []
131+
delta_list = []
132+
offset = 0
133+
for index, passed_stand_id in enumerate(user_passages_stand[passage_user.id]):
134+
index += offset
135+
if index +1 >= len(passage_chronos_list):
136+
return_list.append(False)
137+
delta_list.append(None)
138+
139+
for stand in passage_chronos_list[index:]:
140+
if passed_stand_id == stand.id:
141+
ic(user_passages_passage[passage_user.id], Passage.query.get(user_passages_passage[passage_user.id][0]), Passage.query.get(user_passages_passage[passage_user.id][index]))
142+
return_list.append(True)
143+
delta = Passage.query.get(user_passages_passage[passage_user.id][index]).time_stamp-Passage.query.get(user_passages_passage[passage_user.id][0]).time_stamp
144+
days = delta.days
145+
hours, remainder = divmod(delta.seconds, 3600)
146+
minutes, seconds = divmod(remainder, 60)
147+
delta_list.append(f"{f'{days} days, 'if days>0 else ''}{hours:02}:{minutes:02}:{seconds:02}")
148+
break
149+
else:
150+
return_list.append(None)
151+
delta_list.append(None)
152+
offset+=1
153+
154+
dist_list = []
155+
dist = 0
156+
for element in passage.inscription.parcours:
157+
if isinstance(element, Stand):
158+
if element.chrono:
159+
dist_list.append(round(dist, 3))
160+
else:
161+
dist += element.get_dist()
162+
163+
key_passages.append((passage, passage_chronos_list, return_list, delta_list, dist_list))
164+
80165
ic(key_passages)
81-
return render_template('chrono.html', user_data=user, key=key, passages=key_passages)
166+
return render_template('chrono.html', user_data=user, key=key, passages=reversed(key_passages))
82167

83168
@set_route(passages, '/chrono/set', methods=['post'])
84169
def set_passage():
85170
form = SetPassageForm()
86171
inscription = Inscription.query.filter(Inscription.dossard == form.dossard.data).first()
87172
if not inscription:
88-
return jsonify({"success": False, 'error':'not valide dossard', 'request':{'dossard':form.dossard.data, 'time':form.time.data, 'key':form.key.data}})
173+
return jsonify({"success": False, 'saved':False, 'error':'not valide dossard', 'request':{'dossard':form.dossard.data, 'time':form.time.data, 'key':form.key.data}})
89174
key = PassageKey.query.filter_by(key=form.key.data).first()
90175
if not key:
91-
return jsonify({"success": False, 'error':'not valide key', 'request':{'dossard':form.dossard.data, 'time':form.time.data, 'key':form.key.data}})
176+
return jsonify({"success": False, 'saved':False, 'error':'not valide key', 'request':{'dossard':form.dossard.data, 'time':form.time.data, 'key':form.key.data}})
92177

93178
pass_time = datetime.fromtimestamp(form.time.data/1000)
94179
ic(key)#type:ignore
@@ -97,9 +182,11 @@ def set_passage():
97182
user_passages = Passage.query.filter_by(inscription=inscription).order_by(Passage.time_stamp).all()
98183
ic(user_passages) #type:ignore
99184

185+
stand = key.stands.filter_by(parcours=inscription.parcours).first()
186+
100187
parcours = inscription.parcours
101188

102189
passage = Passage(key_id = key.id, time_stamp=pass_time, inscription_id= inscription.id)
103190

104191

105-
return jsonify({"success": True, 'request':{'dossard':form.dossard.data, 'time':form.time.data, 'key':form.key.data}})
192+
return jsonify({"success": True, 'saved':True, 'request':{'dossard':form.dossard.data, 'time':form.time.data, 'key':form.key.data}})

0 commit comments

Comments
 (0)