experiENS/old/monstage/models.py
2017-04-04 00:19:32 +02:00

136 lines
No EOL
5.4 KiB
Python

# coding: utf-8
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.contrib.gis.db import models as geomodels
from django.db.models.signals import post_save
from taggit.managers import TaggableManager
from monstage.listepays import PAYS_CHOICES
import ldap
TYPE_STAGE_CHOICES = (
('stage', _(u"Stage")),
)
TYPE_LIEU_CHOICES = (
('universite', _(u"Université")),
('entreprise', _(u"Entreprise")),
('centrerecherche', _(u"Centre de recherche")),
('administration', _(u"Administration")),
('autre', _(u"Autre")),
)
def choices_length (choices):
return reduce (lambda m, choice: max (m, len (choice[0])), choices, 0)
class Normalien(models.Model):
user = models.OneToOneField(User, related_name = "profil")
permanent_mail = models.CharField("Adresse e-mail permanente", max_length = 200, blank = True)
class Meta:
verbose_name = u"Profil élève"
verbose_name_plural = u"Profils élèves"
def __unicode__(self):
if self.user.first_name:
return unicode(self.user.first_name + " " + self.user.last_name)
return unicode(self.user)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Normalien.objects.get_or_create(user = instance)
try:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
l = ldap.initialize("ldaps://ldap.spi.ens.fr:636")
l.set_option(ldap.OPT_REFERRALS, 0)
l.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
l.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
l.set_option( ldap.OPT_X_TLS_DEMAND, True )
l.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
info = l.search_s('dc=spi,dc=ens,dc=fr',ldap.SCOPE_SUBTREE,'(uid=%s)' % (instance.username,),['cn','mail'])
if len(info) > 0:
noms = info[0][1]['cn'][0].split(" ")
instance.first_name = noms[0]
instance.last_name = ' '.join(noms[1:])
instance.save()
except ldap.LDAPError:
pass
post_save.connect(create_user_profile, sender = User)
class Lieu(models.Model):
name = models.CharField(_(u"Nom de l'institution d'accueil"), max_length = 250)
ville = models.CharField(_(u"Ville"), max_length = 200)
pays = models.CharField(_(u"Pays"),
choices = PAYS_CHOICES,
max_length = choices_length (TYPE_STAGE_CHOICES))
coord = geomodels.PointField(_(u"Coordonnées"), geography = True)
objects = geomodels.GeoManager() # Requis par GeoDjango
type_lieu = models.CharField( _(u"Type de structure d'accueil"),
default = "universite",
choices = TYPE_LIEU_CHOICES,
max_length = choices_length (TYPE_LIEU_CHOICES))
def __unicode__(self):
return u"%s (%s)" % (self.name, self.ville)
class Meta:
verbose_name = "Lieu"
verbose_name_plural = "Lieux"
class StageMatiere(models.Model):
name = models.CharField(_(u"Matière"), max_length = 30)
class Meta:
verbose_name = "Matière des stages"
verbose_name_plural = "Matières des stages"
def __unicode__(self):
return self.name
class Stage(models.Model):
profil_user = models.ForeignKey(Normalien, related_name = "stages")
published = models.BooleanField(_("Visible publiquement"), default = False)
type_stage = models.CharField (_(u"Type"),
default = "stage",
choices = TYPE_STAGE_CHOICES,
max_length = choices_length (TYPE_STAGE_CHOICES))
start_date = models.DateField(_(u"Date de début"), null = True)
end_date = models.DateField(_(u"Date de fin"), null = True)
sujet = models.CharField(_(u"Sujet"), max_length = 500)
thematiques = TaggableManager(_(u"Thématiques"), blank = True)
encadrants = models.CharField(_(u"Encadrants"), max_length = 500, blank = True)
lieux = models.ManyToManyField(Lieu, related_name = "stages", through = "LieuStage")
matieres = models.ManyToManyField(StageMatiere, related_name = "stages")
# Avis
avis_encadrants = models.TextField(_(u"Avis sur les encadrants"), blank = True)
avis_equipe = models.TextField(_(u"Avis sur l'équipe"), blank = True)
avis_stage = models.TextField(_(u"Intérêt du stage"), blank = True)
avis_admin = models.TextField(_(u"Visa et administratif"), blank = True)
def __unicode__(self):
return u"%s : %s" % (self.profil_user.user.username, self.sujet)
class Meta:
verbose_name = "Stage"
verbose_name_plural = "Stages"
class LieuStage(models.Model):
stage = models.ForeignKey(Stage)
lieu = models.ForeignKey(Lieu)
# Avis
avis_global = models.TextField(_(u"Ressenti global"), blank = True)
avis_lieudevie = models.TextField(_(u"Mon lieu de vie"), blank = True)
avis_lieustage = models.TextField(_(u"Le lieu du stage"), blank = True)
avis_pratique = models.TextField(_(u"S'installer / Conseils pratiques"), blank = True)
avis_visite = models.TextField(_(u"Que voir / que faire"), blank = True)
avis_anecdotes = models.TextField(_(u"Anecdotes"), blank = True)
class Meta:
verbose_name = "Avis sur un lieu de stage"
verbose_name_plural = "Avis sur un lieu de stage"