2024-01-14 14:48:31 +01:00
from datetime import date , datetime
2023-04-25 17:06:14 +02:00
from django . shortcuts import render , get_object_or_404
from django . contrib . auth . decorators import login_required
from django . views . decorators . http import require_http_methods
2024-02-16 16:54:10 +01:00
from django . http import HttpResponse , HttpResponseRedirect
from django . db . models import Q
2023-04-25 17:06:14 +02:00
from django . urls import reverse
from django . conf import settings
from django . contrib . auth import get_user_model
2023-04-28 12:20:55 +02:00
from django . contrib . contenttypes . models import ContentType
2023-04-25 17:06:14 +02:00
from django . core . mail import send_mail
2024-01-14 14:48:31 +01:00
import pendulum
2024-02-03 17:20:42 +01:00
from jarvis . core . models import Email
2023-04-25 17:06:14 +02:00
from jarvis . people . models import Gymnast
2024-02-13 12:59:35 +01:00
from jarvis . people . views import gymnast_listing
2023-04-25 17:06:14 +02:00
from jarvis . objective . models import Skill
2024-01-14 14:48:31 +01:00
from jarvis . tools . date_week_transition import from_date_to_week_number
2023-04-25 17:06:14 +02:00
from . models import (
Plan ,
Note ,
Point ,
Chrono ,
LearnedSkill ,
GymnastHasRoutine ,
SeasonInformation ,
NumberOfRoutineDone ,
)
from . forms import (
PlanForm ,
NoteForm ,
ScoreForm ,
LearnedSkillForm ,
SeasonInformationForm ,
NumberOfRoutineDoneForm ,
)
2024-02-16 16:54:10 +01:00
from . email_vars import MAIL_HEADER , MAIL_FOOTER
2023-04-25 17:06:14 +02:00
2024-02-16 16:54:10 +01:00
User = get_user_model ( )
2023-04-25 17:06:14 +02:00
@login_required
@require_http_methods ( [ " GET " ] )
def note_listing ( request , gymnast_id = None ) :
"""
2024-02-11 09:25:20 +01:00
Récupère les notes des gymnastes autorisé ( e ) s
2023-05-14 19:10:34 +02:00
Args :
gymnast_id ( int ) identifiant d ' un gymnaste
2023-04-25 17:06:14 +02:00
"""
gymnast = None
2024-02-11 14:34:00 +01:00
if gymnast_id and (
request . user . is_superuser
or (
request . session . has_key ( " available_gymnast " )
and gymnast_id in request . session [ " available_gymnast " ]
)
2024-02-11 12:12:40 +01:00
) :
2024-02-11 09:25:20 +01:00
note_list = Note . objects . filter ( gymnast = gymnast_id )
gymnast = Gymnast . objects . get ( pk = gymnast_id )
2023-04-25 17:06:14 +02:00
else :
2024-02-11 09:25:20 +01:00
if request . user . is_superuser :
note_list = Note . objects . all ( )
else :
note_list = Note . objects . filter (
gymnast__in = request . session [ " available_gymnast " ]
)
2023-04-25 17:06:14 +02:00
context = { " note_list " : note_list , " gymnast " : gymnast }
return render ( request , " notes/list.html " , context )
@login_required
@require_http_methods ( [ " GET " ] )
def note_details ( request , note_id ) :
"""
2024-02-11 09:25:20 +01:00
Récupère toutes les informations d ' un note et vérifie si le demandeur peut avoir accès à la
note . S ' il ne peut pas il est redirigé vers le listing des notes.
2023-05-14 19:10:34 +02:00
Args :
note_id ( int ) identifiant d ' une note
2023-04-25 17:06:14 +02:00
"""
note = get_object_or_404 ( Note , pk = note_id )
2024-02-11 09:25:20 +01:00
2024-02-13 12:59:35 +01:00
if not request . user . is_superuser and (
request . session . has_key ( " available_gymnast " )
2024-02-11 14:34:00 +01:00
and note . gymnast . id not in request . session [ " available_gymnast " ]
) :
2024-02-11 09:25:20 +01:00
return note_listing ( request )
2023-04-25 17:06:14 +02:00
return render ( request , " notes/details.html " , { " note " : note } )
@login_required
@require_http_methods ( [ " GET " , " POST " ] )
def note_create_or_update ( request , note_id = None , gymnast_id = None ) :
2024-02-16 16:54:10 +01:00
""" Création ou modification d ' une note
2023-04-25 17:06:14 +02:00
Args :
note_id ( int ) identifiant d ' une note
gymnast_id ( int ) identifiant d ' un gymnaste
2023-05-14 19:10:34 +02:00
TODO : pq ne puis - je pas récuperer l ' idantifiant du coach via le form ?
2023-04-25 17:06:14 +02:00
"""
coach = User . objects . get ( pk = request . user . id )
if note_id :
note = get_object_or_404 ( Note , pk = note_id )
2024-02-13 12:59:35 +01:00
if not request . user . is_superuser and (
request . session . has_key ( " available_gymnast " )
and note . gymnast . id not in request . session [ " available_gymnast " ]
) :
return note_listing ( request )
2023-04-25 17:06:14 +02:00
data = {
" coach " : coach . id ,
" gymnast " : note . gymnast . id ,
" gymnast_related " : str ( note . gymnast ) ,
}
else :
note = None
2023-11-14 14:17:55 +01:00
today = pendulum . now ( ) . date ( )
season , week_number = from_date_to_week_number ( today )
2023-11-14 14:19:15 +01:00
data = {
" coach " : coach . id ,
" title " : f " Note of the week { week_number } ( { season } ) " ,
}
2023-04-25 17:06:14 +02:00
if gymnast_id is not None :
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
data [ " gymnast " ] = gymnast_id
data [ " gymnast_related " ] = str ( gymnast )
if request . method == " POST " :
form = NoteForm ( request . POST , instance = note )
if form . is_valid ( ) :
new_note = form . save ( )
2023-07-05 09:33:03 +02:00
if (
( new_note . gymnast . user . email or new_note . gymnast . email_trainer )
and ( ( not note_id ) or ( note_id and note . status == 0 ) )
and new_note . status == 1
) :
2023-10-31 12:34:53 +01:00
url = request . build_absolute_uri (
reverse ( " gymnast_details_tab " , args = ( new_note . gymnast . id , " event " ) )
)
2024-02-03 18:19:39 +01:00
receivers = [
new_note . gymnast . user . email ,
new_note . gymnast . email_trainer ,
]
title = f " { new_note . gymnast } : Nouvelle note "
body = f """ <p>Bonjour,</p><p>Une nouvelle note vous a été envoyée. Vous pouvez la consulter en cliquant <a href= ' { url } ' >ici</a>.</p> """
2023-10-31 12:34:53 +01:00
2023-07-05 09:33:03 +02:00
send_mail (
2024-02-03 18:19:39 +01:00
title ,
2023-07-05 09:33:03 +02:00
" Une nouvelle note vous a été envoyée " ,
settings . EMAIL_HOST_USER ,
2024-02-03 18:19:39 +01:00
receivers ,
2023-07-05 09:33:03 +02:00
fail_silently = False ,
2024-02-03 18:19:39 +01:00
html_message = body + MAIL_FOOTER ,
2023-07-05 09:33:03 +02:00
)
2023-04-25 17:06:14 +02:00
return HttpResponseRedirect (
2024-02-03 19:56:07 +01:00
reverse ( " gymnast_details_tab " , args = ( new_note . gymnast . id , " event " ) )
2023-04-25 17:06:14 +02:00
)
2024-01-14 14:48:31 +01:00
return render ( request , " notes/create.html " , { " form " : form } )
last_note = (
Note . objects . filter ( gymnast = gymnast_id , status = 1 ) . order_by ( " -date " ) . first ( )
)
if last_note :
data [ " informations " ] = last_note . informations
2023-04-25 17:06:14 +02:00
form = NoteForm ( instance = note , initial = data )
context = { " form " : form , " note_id " : note_id }
return render ( request , " notes/create.html " , context )
@login_required
@require_http_methods ( [ " POST " ] )
def gymnast_learn_skill ( request ) :
"""
Lie un gymnast à une figure .
"""
# utiliser un FORM pour cette fonction.
gymnast_id = request . POST . get ( " gymnast_id " , None )
skill_id = request . POST . get ( " skill_id " , None )
learning_step = request . POST . get ( " learning_step " , 0 )
if gymnast_id and skill_id :
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
skill = Skill . objects . get ( pk = skill_id )
learned_skill = LearnedSkill (
gymnast = gymnast ,
skill = skill ,
learning_step = learning_step ,
date = datetime . now ( ) ,
)
learned_skill . save ( )
return HttpResponse ( status = 200 )
if gymnast_id :
print ( " Error : can not link Gymnast and skill. Missing Skill_ID. " )
else :
print ( " Error : can not link Gymnast and skill. Missing Gymnast_ID. " )
return HttpResponse ( status = 500 )
@login_required
@require_http_methods ( [ " GET " , " POST " ] )
def learnedskill_create_or_update ( request , gymnast_id = None ) :
"""
Formulaire de creation et modification d ' un lien skill/gymnaste.
2023-05-14 19:10:34 +02:00
Args :
gymnast_id ( int ) identifiant d ' un gymnaste
2023-04-25 17:06:14 +02:00
"""
if gymnast_id :
2024-02-13 12:59:35 +01:00
if not request . user . is_superuser and (
request . session . has_key ( " available_gymnast " )
and gymnast_id not in request . session [ " available_gymnast " ]
) :
return gymnast_listing ( request )
2023-04-25 17:06:14 +02:00
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
data = {
" gymnast " : gymnast . id ,
" gymnast_related " : str ( gymnast ) ,
}
else :
data = None
if request . method == " POST " :
form = LearnedSkillForm ( request . POST )
if form . is_valid ( ) :
2024-02-03 17:20:42 +01:00
learned_skill = form . save ( )
2023-04-25 17:06:14 +02:00
# notification
2024-02-03 17:20:42 +01:00
receivers = [ ]
2023-04-28 12:20:55 +02:00
functionality = ContentType . objects . get ( model = " learnedskill " )
for notification in gymnast . notifications . filter (
functionality = functionality
) :
2024-02-03 17:20:42 +01:00
receivers . append ( notification . user . email )
title = f " { learned_skill . gymnast } : Nouveau skill appris "
html_message = f """ <p>Bonjour,</p><p> { learned_skill . gymnast } a appris { learned_skill . skill } ( { learned_skill . learning_step } ).</p> """
Email . objects . create (
receivers = receivers ,
title = title ,
body = html_message ,
2023-04-29 15:31:14 +02:00
)
2023-04-25 17:06:14 +02:00
2024-02-03 17:39:12 +01:00
send_mail (
title ,
f " { learned_skill . gymnast } a appris une nouvelle figure ( { date } ) " ,
settings . EMAIL_HOST_USER ,
receivers ,
fail_silently = False ,
html_message = html_message + MAIL_FOOTER ,
)
2024-02-03 17:20:42 +01:00
2023-04-25 17:06:14 +02:00
return HttpResponseRedirect (
reverse ( " gymnast_details " , args = ( form . cleaned_data [ " gymnast " ] . id , ) )
)
2024-01-14 14:48:31 +01:00
2024-02-03 17:20:42 +01:00
return render ( request , " learnedskills/create.html " , { " form " : form } )
2023-04-25 17:06:14 +02:00
form = LearnedSkillForm ( initial = data )
context = { " form " : form , " gymnast_id " : gymnast_id }
return render ( request , " learnedskills/create.html " , context )
@login_required
@require_http_methods ( [ " GET " , " POST " ] )
def score_create_or_update ( request , score_id = None , gymnast_id = None ) :
"""
Formulaire de création d ' un nouveau score.
2023-05-14 19:10:34 +02:00
Args :
score_id ( int ) identifiant d ' un score
gymnast_id ( int ) identifiant d ' un gymnaste
2023-04-25 17:06:14 +02:00
"""
if score_id :
score = get_object_or_404 ( Point , pk = score_id )
2024-02-13 12:59:35 +01:00
if not request . user . is_superuser and (
request . session . has_key ( " available_gymnast " )
and score . gymnast . id not in request . session [ " available_gymnast " ]
) :
return score_listing ( request )
2023-04-25 17:06:14 +02:00
data = {
" gymnast_related " : str ( score . gymnast ) ,
" event_related " : str ( score . event ) ,
}
else :
score = None
data = None
if gymnast_id is not None :
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
data = { " gymnast " : gymnast_id , " gymnast_related " : str ( gymnast ) }
if request . method == " POST " :
form = ScoreForm ( request . POST , instance = score )
if form . is_valid ( ) :
2024-02-03 17:20:42 +01:00
score = form . save ( )
2023-04-25 17:06:14 +02:00
# notification
2024-02-03 17:20:42 +01:00
receivers = [ ]
2023-04-28 12:20:55 +02:00
functionality = ContentType . objects . get ( model = " point " )
2024-02-03 17:20:42 +01:00
for notification in score . gymnast . notifications . filter (
2023-04-28 12:20:55 +02:00
functionality = functionality
) :
2024-02-03 17:20:42 +01:00
receivers . append ( notification . user . email )
title = f " { score . gymnast } : Nouveau score enregistré "
html_message = f """ <p>Bonjour,</p><p>Un nouveau score a été enregistré pour { score . gymnast } ( { score . event } ) : { score . routine_type } - { score . total } .</p> """
Email . objects . create (
receivers = receivers ,
title = title ,
body = html_message ,
2023-04-29 15:31:14 +02:00
)
2023-04-25 17:06:14 +02:00
2024-02-03 17:39:12 +01:00
send_mail (
title ,
f " { score . gymnast } a ajouté un nouveau score ( { date } ) " ,
settings . EMAIL_HOST_USER ,
receivers ,
fail_silently = False ,
html_message = html_message + MAIL_FOOTER ,
)
2024-02-03 17:20:42 +01:00
2023-04-25 17:06:14 +02:00
if form . cleaned_data [ " add_to_chrono " ] :
new_chrono = Chrono (
gymnast = form . cleaned_data [ " gymnast " ] ,
chrono_type = form . cleaned_data [ " routine_type " ] ,
score_type = 1 ,
score = form . cleaned_data [ " point_time_of_flight " ] ,
tof = form . cleaned_data [ " point_time_of_flight " ] ,
date = form . cleaned_data [ " event " ] . date_begin ,
)
new_chrono . save ( )
return HttpResponseRedirect (
reverse (
" gymnast_details_tab " ,
args = ( form . cleaned_data [ " gymnast " ] . id , " scores " ) ,
)
)
2024-01-14 14:48:31 +01:00
2024-02-03 17:20:42 +01:00
return render ( request , " scores/create.html " , { " form " : form } )
2023-04-25 17:06:14 +02:00
form = ScoreForm ( instance = score , initial = data )
context = { " form " : form , " score_id " : score_id }
return render ( request , " scores/create.html " , context )
@login_required
@require_http_methods ( [ " GET " ] )
def score_listing ( request , gymnast_id = None ) :
"""
Revoie la liste des scores
2023-05-14 19:10:34 +02:00
Args :
gymnast_id ( int ) identifiant d ' un gymnaste
2023-04-25 17:06:14 +02:00
"""
2024-02-11 09:25:20 +01:00
# pattern = request.GET.get("pattern", None)
# JE NE SAIS PLUS POURQUOI JE GERE UN PATTERN -> code commenté.
# if pattern:
# score_list = Point.objects.filter(
# Q(event__icontains=pattern) | Q(gymnast__icontains=pattern)
# )
# el
2023-04-25 17:06:14 +02:00
gymnast = None
2024-02-11 14:34:00 +01:00
if gymnast_id and (
request . user . is_superuser
or (
request . session . has_key ( " available_gymnast " )
and gymnast_id in request . session [ " available_gymnast " ]
)
2024-02-11 12:12:40 +01:00
) :
2023-04-25 17:06:14 +02:00
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
2024-02-11 09:25:20 +01:00
score_list = Point . objects . filter ( gymnast = gymnast_id )
2023-04-25 17:06:14 +02:00
else :
2024-02-11 09:25:20 +01:00
if request . user . is_superuser :
score_list = Point . objects . all ( )
else :
score_list = Point . objects . filter (
gymnast__in = request . session [ " available_gymnast " ]
)
2023-04-25 17:06:14 +02:00
context = { " score_list " : score_list , " gymnast " : gymnast }
return render ( request , " scores/list.html " , context )
@login_required
@require_http_methods ( [ " GET " ] )
2024-02-16 16:54:10 +01:00
def routine_done_listing ( request , gymnast_id = None ) :
2023-04-25 17:06:14 +02:00
"""
2024-02-16 16:54:10 +01:00
Liste tous les record de la table NumberOfRoutineDone
Args :
gymnast_id ( int ) identifiant d ' un gymnaste
2023-04-25 17:06:14 +02:00
"""
2024-02-11 09:25:20 +01:00
gymnast = None
2024-02-11 14:34:00 +01:00
if gymnast_id and (
request . user . is_superuser
or (
request . session . has_key ( " available_gymnast " )
and gymnast_id in request . session [ " available_gymnast " ]
)
2024-02-11 12:12:40 +01:00
) :
2024-02-11 09:25:20 +01:00
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
2024-02-16 16:54:10 +01:00
routine_done_list = gymnast . number_of_routine_done . all ( )
2024-02-11 09:25:20 +01:00
else :
2024-02-11 11:44:37 +01:00
if request . user . is_superuser :
2024-02-16 16:54:10 +01:00
routine_done_list = NumberOfRoutineDone . objects . all ( )
2024-02-11 09:25:20 +01:00
else :
2024-02-16 16:54:10 +01:00
routine_done_list = NumberOfRoutineDone . objects . filter (
2024-02-11 09:25:20 +01:00
gymnast__in = request . session [ " available_gymnast " ]
2023-04-25 17:06:14 +02:00
)
2024-02-16 16:54:10 +01:00
context = { " routine_done_list " : routine_done_list , " gymnast " : gymnast }
return render ( request , " routinedone/list.html " , context )
2023-04-25 17:06:14 +02:00
2024-02-16 16:54:10 +01:00
@require_http_methods ( [ " POST " ] )
def increment_routinedone ( request ) :
2023-04-25 17:06:14 +02:00
"""
2024-02-16 16:54:10 +01:00
Incrémente le nombre de routine faite pour aujourd ' hui et incrémente, si besoin, le nombre
de routine réussie
2023-04-25 17:06:14 +02:00
"""
2024-02-16 16:54:10 +01:00
data = {
" gymnast " : get_object_or_404 ( Gymnast , pk = request . POST . get ( " gymnast_id " , None ) ) ,
" routine_type " : request . POST . get ( " routine_type " , 1 ) ,
" date " : request . POST . get ( " date " , date . today ( ) ) ,
}
routine = (
data [ " gymnast " ]
. has_routine . filter ( routine_type = request . POST . get ( " routine_type " , 1 ) )
. first ( )
)
data [ " routine " ] = routine . routine
routinedone , _ = NumberOfRoutineDone . objects . get_or_create (
date = data [ " date " ] , gymnast = data [ " gymnast " ] , routine_type = data [ " routine_type " ]
)
data [ " number_of_try " ] = routinedone . number_of_try + 1
success = request . POST . get ( " success " , 0 )
if int ( success ) == 1 :
data [ " number_of_successes " ] = routinedone . number_of_successes + 1
2023-04-25 17:06:14 +02:00
else :
2024-02-16 16:54:10 +01:00
data [ " number_of_successes " ] = routinedone . number_of_successes
form = NumberOfRoutineDoneForm ( data , instance = routinedone )
2023-04-25 17:06:14 +02:00
2024-02-16 16:54:10 +01:00
if form . is_valid ( ) :
form . save ( )
return HttpResponse ( status = 200 )
2023-04-25 17:06:14 +02:00
2024-02-16 16:54:10 +01:00
return HttpResponse ( status = 406 )
2023-04-25 17:06:14 +02:00
@login_required
@require_http_methods ( [ " GET " , " POST " ] )
def routinedone_create_or_update ( request , routinedone_id = None , gymnast_id = None ) :
2023-05-14 19:10:34 +02:00
""" Création ou modification d ' un nombre de série tentée.
Args :
routinedone_id ( int ) identifiant d ' une routinedone
gymnast_id ( int ) identifiant d ' un gymnaste
"""
2023-04-25 17:06:14 +02:00
if routinedone_id :
routinedone = get_object_or_404 ( NumberOfRoutineDone , pk = routinedone_id )
data = {
" gymnast " : routinedone . gymnast . id ,
" gymnast_related " : str ( routinedone . gymnast ) ,
}
if routinedone . routine :
data [ " routine " ] = routinedone . routine . id
data [ " routine_related " ] = str ( routinedone . routine )
else :
routine = (
GymnastHasRoutine . objects . filter ( gymnast = routinedone . gymnast )
. filter ( routine_type = routinedone . routine_type )
. filter ( date_begin__lte = routinedone . date )
. filter ( Q ( date_end__gte = routinedone . date ) | Q ( date_end__isnull = True ) )
. first ( )
)
if routine :
data [ " routine " ] = routine . id
data [ " routine_related " ] = str ( routine )
else :
data = None
routinedone = None
if gymnast_id is not None :
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
data = { " gymnast " : gymnast_id , " gymnast_related " : gymnast }
if request . method == " POST " :
form = NumberOfRoutineDoneForm ( request . POST , instance = routinedone )
if form . is_valid ( ) :
2024-02-03 17:20:42 +01:00
routine_done = form . save ( )
2023-04-25 17:06:14 +02:00
# notification
2024-02-03 17:20:42 +01:00
receivers = [ ]
2023-04-25 17:06:14 +02:00
gymnast = Gymnast . objects . get ( pk = form . cleaned_data [ " gymnast " ] . id )
2023-04-28 12:20:55 +02:00
functionality = ContentType . objects . get ( model = " numberofroutinedone " )
2024-02-03 17:20:42 +01:00
for notification in routine_done . gymnast . notifications . filter (
2023-04-28 12:20:55 +02:00
functionality = functionality
) :
2024-02-03 17:20:42 +01:00
receivers . append ( notification . user . email )
title = f " { routine_done . gymnast } : Nouvelle série comptabilisée "
body = f """ <p>Bonjour,</p>
< p > Nouvelle série comptabilisée pour { routine_done . gymnast } . < / p > """
Email . objects . create ( receivers = receivers , title = title , body = body )
2024-02-03 17:39:12 +01:00
send_mail (
title ,
f " Nouvelle série comptabilisée pour { routine_done . gymnast } " ,
settings . EMAIL_HOST_USER ,
receivers ,
fail_silently = False ,
2024-02-04 14:12:02 +01:00
html_message = body + MAIL_FOOTER ,
2024-02-03 17:39:12 +01:00
)
2023-04-25 17:06:14 +02:00
return HttpResponseRedirect (
reverse (
" gymnast_details_tab " ,
args = ( form . cleaned_data [ " gymnast " ] . id , " routine " ) ,
)
)
2024-01-14 14:48:31 +01:00
return render ( request , " routinedone/create.html " , { " form " : form } )
2023-04-25 17:06:14 +02:00
form = NumberOfRoutineDoneForm ( instance = routinedone , initial = data )
context = { " form " : form , " routinedone_id " : routinedone_id }
return render ( request , " routinedone/create.html " , context )
@login_required
@require_http_methods ( [ " GET " , " POST " ] )
def plan_create_or_update ( request , plan_id = None , gymnast_id = None , skill_id = None ) :
""" Création d ' un plan.
Args :
2023-05-14 19:10:34 +02:00
plan_id ( int ) identifiant d ' un plan (classe <Plan>).
gymnast_id ( int ) identifiant d ' un gymnaste (classe <Gymnast>).
skill_id ( int ) identifiant d ' un skill (classe <Skill>).
2023-04-25 17:06:14 +02:00
"""
if plan_id :
plan = get_object_or_404 ( Plan , pk = plan_id )
data = {
" gymnast " : plan . gymnast . id ,
" gymnast_related " : str ( plan . gymnast ) ,
" educative " : plan . educative . id ,
" educative_related " : str ( plan . educative ) ,
}
else :
plan = None
data = { }
if gymnast_id :
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
data [ " gymnast " ] = gymnast_id
data [ " gymnast_related " ] = str ( gymnast )
if skill_id :
skill = get_object_or_404 ( Skill , pk = skill_id )
data [ " educative " ] = skill_id
data [ " educative_related " ] = str ( skill )
if request . method == " POST " :
form = PlanForm ( request . POST , instance = plan )
if form . is_valid ( ) :
plan = form . save ( )
# notification
2024-02-03 17:20:42 +01:00
receivers = [ ]
2023-06-13 14:35:29 +02:00
date = form . cleaned_data [ " date " ]
2023-04-28 12:20:55 +02:00
functionality = ContentType . objects . get ( model = " plan " )
2024-02-03 17:20:42 +01:00
for notification in plan . gymnast . notifications . filter (
2023-04-28 12:20:55 +02:00
functionality = functionality
) :
2024-02-03 17:20:42 +01:00
receivers . append ( notification . user . email )
title = f " { plan . gymnast } : Nouvel objectif fixé "
body = f """ <p>Bonjour,</p>
< p > Nouvel objectif fixé pour { plan . gymnast } : { plan . educative } ( { plan . learning_step } ) pour le { date . strftime ( ' %d % B % Y ' ) } au plus tard . < / p > """
Email . objects . create ( receivers = receivers , title = title , body = body )
2024-02-03 17:39:12 +01:00
send_mail (
title ,
2024-02-07 14:05:05 +01:00
f " Nouvel objectif fixé pour { plan . gymnast } " ,
2024-02-03 17:39:12 +01:00
settings . EMAIL_HOST_USER ,
receivers ,
fail_silently = False ,
2024-02-04 14:12:02 +01:00
html_message = body + MAIL_FOOTER ,
2024-02-03 17:39:12 +01:00
)
2023-04-25 17:06:14 +02:00
return HttpResponseRedirect (
reverse ( " gymnast_details " , args = ( form . cleaned_data [ " gymnast " ] . id , ) )
)
2024-01-14 14:48:31 +01:00
return render ( request , " plan/create.html " , { " form " : form } )
2023-04-25 17:06:14 +02:00
form = PlanForm ( instance = plan , initial = data )
context = { " form " : form , " plan_id " : plan_id }
return render ( request , " plan/create.html " , context )
@login_required
@require_http_methods ( [ " GET " , " POST " ] )
def season_information_create_or_update (
request , season_information_id = None , gymnast_id = None
) :
""" Création d ' un record de la class Intentity.
Args :
season_information_id ( int ) : identifiant d ' un plan (classe <SeasonInformation>).
gymnast_id ( int ) : identifiant d ' un gymnaste (classe <Gymnast>).
"""
if season_information_id :
season_information = get_object_or_404 (
SeasonInformation , pk = season_information_id
)
2024-02-13 12:59:35 +01:00
if not request . user . is_superuser and (
request . session . has_key ( " available_gymnast " )
and season_information . gymnast . id
not in request . session [ " available_gymnast " ]
) :
return season_information_listing ( request )
2023-04-25 17:06:14 +02:00
data = {
" gymnast " : season_information . gymnast . id ,
" gymnast_related " : str ( season_information . gymnast ) ,
" club " : season_information . club . id ,
" club_related " : str ( season_information . club ) ,
}
else :
season_information = None
data = { }
if gymnast_id :
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
data [ " gymnast " ] = gymnast_id
data [ " gymnast_related " ] = str ( gymnast )
if request . method == " POST " :
form = SeasonInformationForm ( request . POST , instance = season_information )
if form . is_valid ( ) :
season_information = form . save ( )
# notification
2024-02-03 17:20:42 +01:00
receivers = [ ]
2023-04-28 12:20:55 +02:00
functionality = ContentType . objects . get ( model = " seasoninformation " )
2024-02-03 17:20:42 +01:00
for notification in season_information . gymnast . notifications . filter (
2023-04-28 12:20:55 +02:00
functionality = functionality
) :
2024-02-03 17:20:42 +01:00
receivers . append ( notification . user . email )
title = f " { season_information . gymnast } : Nouvelle information de saison "
body = f """ <p>Bonjour,</p>
2024-02-04 12:15:01 +01:00
< p > Une nouvelle information de saison enregistrée pour { season_information . gymnast } . < / p > """
2024-02-03 17:20:42 +01:00
Email . objects . create ( receivers = receivers , title = title , body = body )
2024-02-03 17:39:12 +01:00
send_mail (
title ,
2024-02-07 14:05:05 +01:00
f " Une nouvelle information de saison enregistrée pour { season_information . gymnast } " ,
2024-02-03 17:39:12 +01:00
settings . EMAIL_HOST_USER ,
receivers ,
fail_silently = False ,
2024-02-04 14:12:02 +01:00
html_message = body + MAIL_FOOTER ,
2024-02-03 17:39:12 +01:00
)
2023-04-25 17:06:14 +02:00
return HttpResponseRedirect (
reverse (
" gymnast_details " ,
args = ( form . cleaned_data [ " gymnast " ] . id , ) ,
)
)
2024-01-14 14:48:31 +01:00
return render ( request , " seasoninformations/create.html " , { " form " : form } )
2023-04-25 17:06:14 +02:00
form = SeasonInformationForm ( instance = season_information , initial = data )
context = { " form " : form , " season_information_id " : season_information_id }
return render ( request , " seasoninformations/create.html " , context )
@login_required
@require_http_methods ( [ " GET " ] )
def season_information_listing ( request , gymnast_id = None ) :
2023-05-14 19:10:34 +02:00
""" Liste toutes les informations de saison pour un gymnaste.
Args :
gymnast_id ( int ) identifiant d ' un gymnaste
"""
2023-04-25 17:06:14 +02:00
2024-02-11 14:34:00 +01:00
if gymnast_id and (
request . user . is_superuser
or (
request . session . has_key ( " available_gymnast " )
and gymnast_id in request . session [ " available_gymnast " ]
)
2024-02-11 12:12:40 +01:00
) :
2024-02-11 09:25:20 +01:00
gymnast = get_object_or_404 ( Gymnast , pk = gymnast_id )
2023-04-25 17:06:14 +02:00
season_information_list = SeasonInformation . objects . filter ( gymnast = gymnast_id )
else :
2024-02-11 09:25:20 +01:00
if request . user . is_superuser :
season_information_list = SeasonInformation . objects . all ( )
else :
season_information_list = SeasonInformation . objects . filter (
gymnast__in = request . session [ " available_gymnast " ]
)
2023-04-25 17:06:14 +02:00
context = { " season_information_list " : season_information_list , " gymnast " : gymnast }
return render ( request , " seasoninformations/list.html " , context )