Optimisations BDD

This commit is contained in:
Evarin 2018-12-29 16:23:57 +01:00
parent 70ff24c708
commit b9e128cafb
3 changed files with 17 additions and 16 deletions

View file

@ -14,12 +14,13 @@ from django.forms.widgets import DateInput
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.functional import cached_property
from django.utils.html import strip_tags from django.utils.html import strip_tags
from taggit_autosuggest.managers import TaggableManager from taggit_autosuggest.managers import TaggableManager
from tinymce.models import HTMLField as RichTextField from tinymce.models import HTMLField as RichTextField
from .utils import choices_length, en_scolarite from .utils import choices_length
from .statics import DEPARTEMENTS_DEFAUT, PAYS_OPTIONS, TYPE_LIEU_OPTIONS, TYPE_STAGE_OPTIONS, TYPE_LIEU_DICT, TYPE_STAGE_DICT, NIVEAU_SCOL_OPTIONS, NIVEAU_SCOL_DICT from .statics import DEPARTEMENTS_DEFAUT, PAYS_OPTIONS, TYPE_LIEU_OPTIONS, TYPE_STAGE_OPTIONS, TYPE_LIEU_DICT, TYPE_STAGE_DICT, NIVEAU_SCOL_OPTIONS, NIVEAU_SCOL_DICT
import ldap import ldap
@ -51,9 +52,10 @@ class Normalien(models.Model):
def stages_publics(self): def stages_publics(self):
return self.stages.filter(public=True).order_by('-date_debut') return self.stages.filter(public=True).order_by('-date_debut')
@property @cached_property
def en_scolarite(self): def en_scolarite(self):
return en_scolarite(self.user_id) return SocialAccount.objects.filter(user_id=self.user_id,
provider="clipper").exists()
def has_nonENS_email(self): def has_nonENS_email(self):
a = EmailAddress.objects.filter(user_id=self.user_id, a = EmailAddress.objects.filter(user_id=self.user_id,
@ -217,7 +219,11 @@ class Stage(models.Model):
@property @property
def niveau_scol_fancy(self): def niveau_scol_fancy(self):
return NIVEAU_SCOL_DICT.get(self.niveau_scol, "") return NIVEAU_SCOL_DICT.get(self.niveau_scol, "")
# Optimisation de requêtes
@cached_property
def all_lieux(self):
return self.lieux.all()
def get_absolute_url(self): def get_absolute_url(self):
return reverse('avisstage:stage', self) return reverse('avisstage:stage', self)

View file

@ -55,7 +55,7 @@
{% endif %} {% endif %}
<article class="stage"> <article class="stage">
<section class="misc"> <section class="misc">
<div class="misc-content {% if object.lieux.all %}withmap{% endif %}"> <div class="misc-content {% if object.all_lieux %}withmap{% endif %}">
<div class="desc"> <div class="desc">
<div class="misc-hdr"> <div class="misc-hdr">
<h1>{{ object.sujet }}</h1> <h1>{{ object.sujet }}</h1>
@ -65,8 +65,8 @@
a fait {{ object.type_stage_fem|yesno:"cette,ce" }} <b>{{ object.type_stage_fancy }}</b> a fait {{ object.type_stage_fem|yesno:"cette,ce" }} <b>{{ object.type_stage_fancy }}</b>
{% if object.niveau_scol %}{{ object.niveau_scol_fancy }},{% endif %} {% if object.niveau_scol %}{{ object.niveau_scol_fancy }},{% endif %}
{% if object.structure %}au sein de {{ object.structure }}{% endif %}{% if object.encadrants %}, supervisé par {{ object.encadrants }}{% endif %}.</p> {% if object.structure %}au sein de {{ object.structure }}{% endif %}{% if object.encadrants %}, supervisé par {{ object.encadrants }}{% endif %}.</p>
{% if object.lieux.all %}<p>Cela s'est passé à : {% if object.all_lieux %}<p>Cela s'est passé à :
{% for lieu in object.lieux.all %}{{ lieu.nom }} ({{ lieu.ville }}){% if not forloop.last %}, {% endif %}{% endfor %}.</p> {% for lieu in object.all_lieux %}{{ lieu.nom }} ({{ lieu.ville }}){% if not forloop.last %}, {% endif %}{% endfor %}.</p>
{% endif %} {% endif %}
<ul class="infos"> <ul class="infos">
@ -79,12 +79,12 @@
</ul> </ul>
</div> </div>
{% if object.lieux.all %} {% if object.all_lieux %}
<div class="map"> <div class="map">
<div id="stage-map"></div> <div id="stage-map"></div>
<script type="text/javascript"> <script type="text/javascript">
var lieux = [ var lieux = [
{% for lieu in object.lieux.all %} {% for lieu in object.all_lieux %}
{ {
coord: {lat: "{{ lieu.coord.y|escapejs }}", lon: "{{ lieu.coord.x|escapejs }}" }, coord: {lat: "{{ lieu.coord.y|escapejs }}", lon: "{{ lieu.coord.x|escapejs }}" },
popup: "<h3>{{ lieu.nom|escapejs }}</h3>" + popup: "<h3>{{ lieu.nom|escapejs }}</h3>" +

View file

@ -6,10 +6,5 @@ from functools import reduce
def choices_length (choices): def choices_length (choices):
return reduce (lambda m, choice: max (m, len (choice[0])), choices, 0) return reduce (lambda m, choice: max (m, len (choice[0])), choices, 0)
def en_scolarite(user_or_id): def en_scolarite(user):
if isinstance(user_or_id, int): return user.profil.en_scolarite
user_id = user_or_id
else:
user_id = user_or_id.id
return SocialAccount.objects.filter(user_id=user_id,
provider="clipper").exists()