workout/gym/models.py

163 lines
4.5 KiB
Python

from django.conf import settings
from django.db import models
class Room(models.Model):
class Meta:
verbose_name = 'salle'
verbose_name_plural = 'salles'
name = models.CharField('nom', max_length=300)
latitude = models.DecimalField('latitude', max_digits=11, decimal_places=8, blank=True, null=True)
longitude = models.DecimalField('longitude', max_digits=11, decimal_places=8, blank=True, null=True)
notes = models.TextField('notes', blank=True)
def __str__(self):
return self.name
class Unit(models.Model):
class Meta:
verbose_name = 'unité'
verbose_name_plural = 'unités'
name = models.CharField('nom', max_length=50)
def __str__(self):
return self.name
class WorkForm(models.Model):
class Meta:
verbose_name = 'forme de travail'
verbose_name_plural = 'formes de travail'
name = models.CharField('nom', max_length=300)
def __str__(self):
return self.name
class Equipment(models.Model):
class Meta:
verbose_name = 'machine'
verbose_name_plural = 'machines'
name = models.CharField('nom', max_length=300)
room = models.ForeignKey(
verbose_name='salle',
to=Room,
on_delete=models.SET_NULL,
related_name='equipments',
null=True
)
unit = models.ForeignKey(
verbose_name='unité',
to=Unit,
on_delete=models.PROTECT,
null=True
)
default_work_form = models.ForeignKey(
verbose_name='forme de travail par défaut',
to=WorkForm,
on_delete=models.SET_NULL,
null=True
)
default_repetition_number = models.PositiveIntegerField('nombre de répétitions par défaut', default=12)
@property
def last_theoretical_max(self):
return self.theoretical_maxs.order_by('-date', '-pk').first()
def __str__(self):
return f'{self.name} ({self.room.name})'
class Setting(models.Model):
class Meta:
verbose_name = 'réglage'
verbose_name_plural = 'réglages'
equipment = models.ForeignKey(
verbose_name='machine',
to=Equipment,
on_delete=models.CASCADE,
related_name='settings'
)
name = models.CharField('nom', max_length=200)
value = models.CharField('valeur', max_length=200)
def __str__(self):
return f'{self.name}={self.value}'
class TheoreticalMax(models.Model):
class Meta:
verbose_name = 'maximum théorique'
verbose_name_plural = 'maximums théoriques'
equipment = models.ForeignKey(
verbose_name='machine',
to=Equipment,
on_delete=models.CASCADE,
related_name='theoretical_maxs'
)
date = models.DateField('date')
value = models.FloatField('valeur')
def __str__(self):
return f'{self.value} {self.equipment.unit.name} le {self.date}'
class Session(models.Model):
class Meta:
verbose_name = 'séance'
verbose_name_plural = 'séances'
start = models.DateTimeField('date et heure de début')
room = models.ForeignKey(
verbose_name='salle',
to=Room,
on_delete=models.SET_NULL,
related_name='sessions',
null=True
)
default_theoretical_max_percentage = models.PositiveIntegerField('pourcentage par défaut', default=65)
notes = models.TextField('notes', blank=True)
def __str__(self):
import pytz
print(self.start)
server_timezone = pytz.timezone(settings.TIME_ZONE)
server = server_timezone.normalize(self.start.astimezone(server_timezone))
print(server)
return f'{self.room} {server:%d/%m/%Y %H:%M} ({settings.TIME_ZONE})'
class Round(models.Model):
class Meta:
verbose_name = 'série'
verbose_name_plural = 'séries'
repetition_number = models.PositiveIntegerField('nombre de répétitions')
equipment = models.ForeignKey(
verbose_name='machine',
to=Equipment,
on_delete=models.CASCADE,
related_name='rounds'
)
theoretical_max_percentage = models.PositiveIntegerField('pourcentage')
chosen_weight = models.FloatField('charge choisie')
session = models.ForeignKey(
verbose_name='séance',
to=Session,
on_delete=models.CASCADE,
related_name='rounds'
)
work_form = models.ForeignKey(
verbose_name='forme de travail',
to=WorkForm,
on_delete=models.PROTECT,
null=True
)
notes = models.TextField('notes', blank=True)