Supprimé des sources qui ne fonctionnent plus
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / models.py
CommitLineData
92c7413b 1# -*- encoding: utf-8 -*-
88585b02 2
db2999fa
EMS
3import caldav
4import datetime
5import feedparser
db2999fa
EMS
6import os
7import pytz
8import random
4b89a7df 9import textwrap
db2999fa
EMS
10import uuid
11import vobject
5ffde8a4 12from pytz.tzinfo import AmbiguousTimeError, NonExistentTimeError
fdcf5874
EMS
13from urllib import urlencode
14
5212238e 15from backend_config import RESOURCES
86983865 16from babel.dates import get_timezone_name
5212238e 17from caldav.lib import error
6d885e0c 18from django.contrib.auth.models import User
fdcf5874 19from django.contrib.contenttypes.models import ContentType
4b89a7df 20from django.core.mail import EmailMultiAlternatives
fdcf5874 21from django.core.urlresolvers import reverse
d15017b2 22from django.db import models
88585b02 23from django.db.models import Q
b7a741ad 24from django.db.models.signals import pre_delete
4b89a7df 25from django.utils.encoding import smart_unicode, smart_str
122c4c3d 26from djangosphinx.models import SphinxQuerySet, SearchError
4b89a7df 27from markdown2 import markdown
fdcf5874 28
693c606b 29from auf.django.references.models import Region, Pays, Thematique
a83b8efb 30from settings import CALENDRIER_URL, SITE_ROOT_URL, CONTACT_EMAIL
88585b02
EMS
31from lib.calendrier import combine
32from lib.recherche import google_search
33
5212238e
EMS
34
35# Fonctionnalités communes à tous les query sets
d15017b2 36
15261361
EMS
37class RandomQuerySetMixin(object):
38 """Mixin pour les modèles.
88585b02 39
15261361
EMS
40 ORDER BY RAND() est très lent sous MySQL. On a besoin d'une autre
41 méthode pour récupérer des objets au hasard.
42 """
43
44 def random(self, n=1):
45 """Récupère aléatoirement un nombre donné d'objets."""
bae03b7b
EMS
46 count = self.count()
47 positions = random.sample(xrange(count), min(n, count))
48 return [self[p] for p in positions]
15261361 49
88585b02 50
5212238e 51class SEPQuerySet(models.query.QuerySet, RandomQuerySetMixin):
230671ff
EMS
52
53 def _filter_date(self, field, min=None, max=None):
54 """Limite les résultats à ceux dont le champ ``field`` tombe entre
55 les dates ``min`` et ``max``."""
56 qs = self
57 if min:
58 qs = qs.filter(**{field + '__gte': min})
59 if max:
88585b02
EMS
60 qs = qs.filter(**{
61 field + '__lt': max + datetime.timedelta(days=1)
62 })
230671ff 63 return qs
5212238e 64
88585b02 65
5212238e
EMS
66class SEPSphinxQuerySet(SphinxQuerySet, RandomQuerySetMixin):
67 """Fonctionnalités communes aux query sets de Sphinx."""
68
69 def __init__(self, model=None, index=None, weights=None):
70 SphinxQuerySet.__init__(self, model=model, index=index,
71 mode='SPH_MATCH_EXTENDED2',
72 rankmode='SPH_RANK_PROXIMITY_BM25',
73 weights=weights)
74
75 def add_to_query(self, query):
76 """Ajoute une partie à la requête texte."""
6c78c673
EMS
77
78 # Assurons-nous qu'il y a un nombre pair de guillemets
79 if query.count('"') % 2 != 0:
80 # Sinon, on enlève le dernier (faut choisir...)
81 i = query.rindex('"')
88585b02 82 query = query[:i] + query[i + 1:]
6c78c673 83
88585b02
EMS
84 new_query = smart_unicode(self._query) + ' ' + query \
85 if self._query else query
5212238e
EMS
86 return self.query(new_query)
87
88 def search(self, text):
89 """Recherche ``text`` dans tous les champs."""
90 return self.add_to_query('@* ' + text)
91
92 def filter_discipline(self, discipline):
93 """Par défaut, le filtre par discipline cherche le nom de la
94 discipline dans tous les champs."""
95 return self.search('"%s"' % discipline.nom)
96
97 def filter_region(self, region):
98 """Par défaut, le filtre par région cherche le nom de la région dans
99 tous les champs."""
100 return self.search('"%s"' % region.nom)
101
230671ff
EMS
102 def _filter_date(self, field, min=None, max=None):
103 """Limite les résultats à ceux dont le champ ``field`` tombe entre
104 les dates ``min`` et ``max``."""
105 qs = self
106 if min:
88585b02 107 qs = qs.filter(**{field + '__gte': min.toordinal() + 365})
230671ff 108 if max:
88585b02 109 qs = qs.filter(**{field + '__lte': max.toordinal() + 365})
230671ff
EMS
110 return qs
111
122c4c3d
EMS
112 def _get_sphinx_results(self):
113 try:
114 return SphinxQuerySet._get_sphinx_results(self)
115 except SearchError:
116 # Essayons d'enlever les caractères qui peuvent poser problème.
117 for c in '|!@()~/<=^$':
118 self._query = self._query.replace(c, ' ')
119 try:
120 return SphinxQuerySet._get_sphinx_results(self)
121 except SearchError:
122 # Ça ne marche toujours pas. Enlevons les guillemets et les
123 # tirets.
124 for c in '"-':
125 self._query = self._query.replace(c, ' ')
126 return SphinxQuerySet._get_sphinx_results(self)
127
88585b02 128
5212238e
EMS
129class SEPManager(models.Manager):
130 """Lorsque les méthodes ``search``, ``filter_region`` et
131 ``filter_discipline`` sont appelées sur ce manager, le query set
132 Sphinx est créé, sinon, c'est le query set Django qui est créé."""
133
134 def query(self, query):
135 return self.get_sphinx_query_set().query(query)
136
137 def add_to_query(self, query):
138 return self.get_sphinx_query_set().add_to_query(query)
139
140 def search(self, text):
141 return self.get_sphinx_query_set().search(text)
142
143 def filter_region(self, region):
144 return self.get_sphinx_query_set().filter_region(region)
145
146 def filter_discipline(self, discipline):
147 return self.get_sphinx_query_set().filter_discipline(discipline)
148
88585b02 149
5212238e
EMS
150# Disciplines
151
d15017b2
CR
152class Discipline(models.Model):
153 id = models.IntegerField(primary_key=True, db_column='id_discipline')
154 nom = models.CharField(max_length=765, db_column='nom_discipline')
6ef8ead4 155
88585b02 156 def __unicode__(self):
92c7413b 157 return self.nom
6ef8ead4 158
d15017b2
CR
159 class Meta:
160 db_table = u'discipline'
88585b02
EMS
161 ordering = ["nom"]
162
d15017b2 163
5212238e
EMS
164# Actualités
165
79c398f6 166class SourceActualite(models.Model):
011804bb
EMS
167 TYPE_CHOICES = (
168 ('actu', 'Actualités'),
169 ('appels', "Appels d'offres"),
170 )
171
79c398f6 172 nom = models.CharField(max_length=255)
011804bb 173 url = models.CharField(max_length=255, verbose_name='URL', blank=True)
88585b02
EMS
174 type = models.CharField(
175 max_length=10, default='actu', choices=TYPE_CHOICES
176 )
ac45d923 177 region = models.ForeignKey(Region, null=True, blank=True, verbose_name='région')
88585b02 178
db2999fa
EMS
179 class Meta:
180 verbose_name = u'fil RSS syndiqué'
181 verbose_name_plural = u'fils RSS syndiqués'
182
ccbc4363 183 def __unicode__(self,):
011804bb 184 return u"%s (%s)" % (self.nom, self.get_type_display())
79c398f6 185
db2999fa
EMS
186 def update(self):
187 """Mise à jour du fil RSS."""
011804bb
EMS
188 if not self.url:
189 return
db2999fa
EMS
190 feed = feedparser.parse(self.url)
191 for entry in feed.entries:
b9220afb
EMS
192 if not all(
193 x in entry for x in ('link', 'published_parsed', 'title')
194 ):
195 continue
db2999fa 196 if Actualite.all_objects.filter(url=entry.link).count() == 0:
baca417f 197 ts = entry.get('published_parsed')
7b93080b
EMS
198 date = datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday) \
199 if ts else datetime.date.today()
b9220afb
EMS
200 texte = entry.summary_detail.value \
201 if 'summary_detail' in entry else ''
ac45d923 202 actualite = self.actualites.create(
b9220afb
EMS
203 titre=entry.title, texte=texte, url=entry.link,
204 date=date
88585b02 205 )
ac45d923
PP
206 if self.region:
207 actualite.regions.add(self.region)
88585b02 208
db2999fa 209
5212238e 210class ActualiteQuerySet(SEPQuerySet):
2f9c4d6c 211
5212238e 212 def filter_date(self, min=None, max=None):
230671ff 213 return self._filter_date('date', min=min, max=max)
da44ce68 214
011804bb
EMS
215 def filter_type(self, type):
216 return self.filter(source__type=type)
217
88585b02 218
5212238e 219class ActualiteSphinxQuerySet(SEPSphinxQuerySet):
88585b02 220 TYPE_CODES = {'actu': 1, 'appels': 2}
c1b134f8 221
5212238e 222 def __init__(self, model=None):
88585b02
EMS
223 SEPSphinxQuerySet.__init__(
224 self, model=model, index='savoirsenpartage_actualites',
225 weights=dict(titre=3)
226 )
c1b134f8 227
7fb6bd61 228 def filter_date(self, min=None, max=None):
230671ff
EMS
229 return self._filter_date('date', min=min, max=max)
230
011804bb
EMS
231 def filter_type(self, type):
232 return self.filter(type=self.TYPE_CODES[type])
233
27effd89
PP
234 def filter_region(self, region):
235 return self.filter(region_ids=region.id)
236
237 def filter_discipline(self, discipline):
238 return self.filter(discipline_ids=discipline.id)
239
88585b02 240
5212238e 241class ActualiteManager(SEPManager):
88585b02 242
5212238e
EMS
243 def get_query_set(self):
244 return ActualiteQuerySet(self.model).filter(visible=True)
2f9c4d6c 245
5212238e
EMS
246 def get_sphinx_query_set(self):
247 return ActualiteSphinxQuerySet(self.model).order_by('-date')
2f9c4d6c 248
5212238e
EMS
249 def filter_date(self, min=None, max=None):
250 return self.get_query_set().filter_date(min=min, max=max)
bae03b7b 251
011804bb
EMS
252 def filter_type(self, type):
253 return self.get_query_set().filter_type(type)
254
88585b02 255
d15017b2 256class Actualite(models.Model):
4f262f90 257 id = models.AutoField(primary_key=True, db_column='id_actualite')
d15017b2
CR
258 titre = models.CharField(max_length=765, db_column='titre_actualite')
259 texte = models.TextField(db_column='texte_actualite')
260 url = models.CharField(max_length=765, db_column='url_actualite')
d15017b2 261 date = models.DateField(db_column='date_actualite')
db2999fa 262 visible = models.BooleanField(db_column='visible_actualite', default=False)
88585b02
EMS
263 ancienid = models.IntegerField(
264 db_column='ancienId_actualite', blank=True, null=True
265 )
011804bb 266 source = models.ForeignKey(SourceActualite, related_name='actualites')
88585b02
EMS
267 disciplines = models.ManyToManyField(
268 Discipline, blank=True, related_name="actualites"
269 )
270 regions = models.ManyToManyField(
271 Region, blank=True, related_name="actualites",
272 verbose_name='régions'
273 )
6ef8ead4 274
2f9c4d6c 275 objects = ActualiteManager()
c59dba82 276 all_objects = models.Manager()
2f9c4d6c 277
d15017b2
CR
278 class Meta:
279 db_table = u'actualite'
7020ea3d 280 ordering = ["-date"]
92c7413b 281
88585b02 282 def __unicode__(self):
264a3210
EMS
283 return "%s" % (self.titre)
284
230671ff
EMS
285 def get_absolute_url(self):
286 return reverse('actualite', kwargs={'id': self.id})
287
264a3210
EMS
288 def assigner_disciplines(self, disciplines):
289 self.disciplines.add(*disciplines)
290
291 def assigner_regions(self, regions):
292 self.regions.add(*regions)
293
81fe476e
PP
294
295class ActualiteVoir(Actualite):
296
297 class Meta:
298 proxy = True
429222f1
PP
299 verbose_name = 'actualité (visualisation)'
300 verbose_name_plural = 'actualités (visualisation)'
81fe476e 301
88585b02 302
5212238e 303# Agenda
4101cfc0 304
5212238e 305class EvenementQuerySet(SEPQuerySet):
4101cfc0 306
5212238e
EMS
307 def filter_type(self, type):
308 return self.filter(type=type)
c1b134f8 309
5212238e 310 def filter_debut(self, min=None, max=None):
a83b8efb
EMS
311 return self._filter_date('debut', min=min, max=max)
312
313 def filter_date_modification(self, min=None, max=None):
314 return self._filter_date('date_modification', min=min, max=max)
c1b134f8 315
88585b02 316
5212238e 317class EvenementSphinxQuerySet(SEPSphinxQuerySet):
4101cfc0 318
5212238e 319 def __init__(self, model=None):
88585b02
EMS
320 SEPSphinxQuerySet.__init__(
321 self, model=model, index='savoirsenpartage_evenements',
322 weights=dict(titre=3)
323 )
116db1fd 324
5212238e
EMS
325 def filter_type(self, type):
326 return self.add_to_query('@type "%s"' % type)
88585b02 327
5212238e 328 def filter_debut(self, min=None, max=None):
230671ff 329 return self._filter_date('debut', min=min, max=max)
7bbf600c 330
a83b8efb
EMS
331 def filter_date_modification(self, min=None, max=None):
332 return self._filter_date('date_modification', min=min, max=max)
333
27effd89
PP
334 def filter_region(self, region):
335 return self.add_to_query('@regions "%s"' % region.nom)
336
337 def filter_discipline(self, discipline):
338 return self.add_to_query('@disciplines "%s"' % discipline.nom)
339
88585b02 340
5212238e 341class EvenementManager(SEPManager):
5212238e 342
5212238e
EMS
343 def get_query_set(self):
344 return EvenementQuerySet(self.model).filter(approuve=True)
345
346 def get_sphinx_query_set(self):
347 return EvenementSphinxQuerySet(self.model).order_by('-debut')
348
349 def filter_type(self, type):
350 return self.get_query_set().filter_type(type)
351
352 def filter_debut(self, min=None, max=None):
353 return self.get_query_set().filter_debut(min=min, max=max)
bae03b7b 354
a83b8efb
EMS
355 def filter_date_modification(self, min=None, max=None):
356 return self.get_query_set().filter_date_modification(min=min, max=max)
357
88585b02 358
1719bf4e 359def build_time_zone_choices(pays=None):
1719bf4e
EMS
360 timezones = pytz.country_timezones[pays] if pays else pytz.common_timezones
361 result = []
86983865 362 now = datetime.datetime.now()
1719bf4e 363 for tzname in timezones:
86983865
EMS
364 tz = pytz.timezone(tzname)
365 fr_name = get_timezone_name(tz, locale='fr_FR')
5ffde8a4
EMS
366 try:
367 offset = tz.utcoffset(now)
368 except (AmbiguousTimeError, NonExistentTimeError):
369 # oups. On est en train de changer d'heure. Ça devrait être fini
370 # demain
371 offset = tz.utcoffset(now + datetime.timedelta(days=1))
86983865
EMS
372 seconds = offset.seconds + offset.days * 86400
373 (hours, minutes) = divmod(seconds // 60, 60)
88585b02
EMS
374 offset_str = 'UTC%+d:%d' % (hours, minutes) \
375 if minutes else 'UTC%+d' % hours
1719bf4e
EMS
376 result.append((seconds, tzname, '%s - %s' % (offset_str, fr_name)))
377 result.sort()
378 return [(x[1], x[2]) for x in result]
86983865 379
88585b02 380
92c7413b 381class Evenement(models.Model):
7bbf600c
EMS
382 TYPE_CHOICES = ((u'Colloque', u'Colloque'),
383 (u'Conférence', u'Conférence'),
384 (u'Appel à contribution', u'Appel à contribution'),
385 (u'Journée d\'étude', u'Journée d\'étude'),
ec81ec66 386 (u'Autre', u'Autre'))
86983865
EMS
387 TIME_ZONE_CHOICES = build_time_zone_choices()
388
74b087e5 389 uid = models.CharField(max_length=255, default=str(uuid.uuid1()))
a5f76eb4 390 approuve = models.BooleanField(default=False, verbose_name=u'approuvé')
92c7413b 391 titre = models.CharField(max_length=255)
88585b02
EMS
392 discipline = models.ForeignKey(
393 'Discipline', related_name="discipline",
394 blank=True, null=True
395 )
396 discipline_secondaire = models.ForeignKey(
397 'Discipline', related_name="discipline_secondaire",
398 verbose_name=u"discipline secondaire", blank=True, null=True
399 )
74b087e5 400 mots_cles = models.TextField('Mots-Clés', blank=True, null=True)
7bbf600c 401 type = models.CharField(max_length=255, choices=TYPE_CHOICES)
731ef7ab
EMS
402 adresse = models.TextField()
403 ville = models.CharField(max_length=100)
31249cf3
EMS
404 pays = models.ForeignKey(
405 Pays, null=True, related_name='evenements', to_field='code'
406 )
74b087e5
EMS
407 debut = models.DateTimeField(default=datetime.datetime.now)
408 fin = models.DateTimeField(default=datetime.datetime.now)
88585b02
EMS
409 fuseau = models.CharField(
410 max_length=100, choices=TIME_ZONE_CHOICES,
411 verbose_name='fuseau horaire'
412 )
fe254ccc 413 description = models.TextField()
731ef7ab
EMS
414 contact = models.TextField(null=True) # champ obsolète
415 prenom = models.CharField('prénom', max_length=100)
416 nom = models.CharField(max_length=100)
417 courriel = models.EmailField()
74b087e5 418 url = models.CharField(max_length=255, blank=True, null=True)
88585b02
EMS
419 piece_jointe = models.FileField(
420 upload_to='agenda/pj', blank=True, verbose_name='pièce jointe'
421 )
422 regions = models.ManyToManyField(
423 Region, blank=True, related_name="evenements",
424 verbose_name='régions additionnelles',
425 help_text="On considère d'emblée que l'évènement se déroule dans la "
426 "région dans laquelle se trouve le pays indiqué plus haut. Il est "
427 "possible de désigner ici des régions additionnelles."
428 )
429 date_modification = models.DateTimeField(
430 editable=False, auto_now=True, null=True
431 )
92c7413b 432
4101cfc0 433 objects = EvenementManager()
c59dba82 434 all_objects = models.Manager()
4101cfc0
EMS
435
436 class Meta:
437 ordering = ['-debut']
fe254ccc
EMS
438 verbose_name = u'évènement'
439 verbose_name_plural = u'évènements'
4101cfc0 440
230671ff 441 def __unicode__(self):
020f79a9 442 return "[%s] %s" % (self.uid, self.titre)
443
230671ff
EMS
444 def get_absolute_url(self):
445 return reverse('evenement', kwargs={'id': self.id})
446
8dfe5efa
EMS
447 def duration_display(self):
448 delta = self.fin - self.debut
449 minutes, seconds = divmod(delta.seconds, 60)
450 hours, minutes = divmod(minutes, 60)
451 days = delta.days
452 parts = []
453 if days == 1:
454 parts.append('1 jour')
455 elif days > 1:
456 parts.append('%d jours' % days)
457 if hours == 1:
458 parts.append('1 heure')
459 elif hours > 1:
460 parts.append('%d heures' % hours)
461 if minutes == 1:
462 parts.append('1 minute')
463 elif minutes > 1:
464 parts.append('%d minutes' % minutes)
465 return ' '.join(parts)
466
27fe0d70
EMS
467 def piece_jointe_display(self):
468 return self.piece_jointe and os.path.basename(self.piece_jointe.name)
469
fe254ccc
EMS
470 def courriel_display(self):
471 return self.courriel.replace(u'@', u' (à) ')
472
a83b8efb
EMS
473 @property
474 def lieu(self):
475 bits = []
476 if self.adresse:
477 bits.append(self.adresse)
478 if self.ville:
479 bits.append(self.ville)
480 if self.pays:
481 bits.append(self.pays.nom)
482 return ', '.join(bits)
483
73309469 484 def clean(self):
485 from django.core.exceptions import ValidationError
486 if self.debut > self.fin:
88585b02
EMS
487 raise ValidationError(
488 'La date de fin ne doit pas être antérieure à la date de début'
489 )
73309469 490
b7a741ad 491 def save(self, *args, **kwargs):
88585b02
EMS
492 """
493 Sauvegarde l'objet dans django et le synchronise avec caldav s'il a
494 été approuvé.
495 """
496 self.contact = '' # Vider ce champ obsolète à la première occasion...
73309469 497 self.clean()
b7a741ad 498 super(Evenement, self).save(*args, **kwargs)
acd5cd8f 499 self.update_vevent()
b7a741ad 500
501 # methodes de commnunications avec CALDAV
502 def as_ical(self,):
503 """Retourne l'evenement django sous forme d'objet icalendar"""
504 cal = vobject.iCalendar()
505 cal.add('vevent')
506
507 # fournit son propre uid
7f56d0d4 508 if self.uid in [None, ""]:
b7a741ad 509 self.uid = str(uuid.uuid1())
510
511 cal.vevent.add('uid').value = self.uid
88585b02 512
b7a741ad 513 cal.vevent.add('summary').value = self.titre
88585b02 514
b7a741ad 515 if self.mots_cles is None:
516 kw = []
517 else:
518 kw = self.mots_cles.split(",")
519
520 try:
521 kw.append(self.discipline.nom)
522 kw.append(self.discipline_secondaire.nom)
523 kw.append(self.type)
88585b02
EMS
524 except:
525 pass
b7a741ad 526
79b400f0 527 kw = [x.strip() for x in kw if len(x.strip()) > 0 and x is not None]
b7a741ad 528 for k in kw:
529 cal.vevent.add('x-auf-keywords').value = k
530
531 description = self.description
532 if len(kw) > 0:
533 if len(self.description) > 0:
534 description += "\n"
028f548f 535 description += u"Mots-clés: " + ", ".join(kw)
b7a741ad 536
88585b02
EMS
537 cal.vevent.add('dtstart').value = \
538 combine(self.debut, pytz.timezone(self.fuseau))
539 cal.vevent.add('dtend').value = \
540 combine(self.fin, pytz.timezone(self.fuseau))
541 cal.vevent.add('created').value = \
542 combine(datetime.datetime.now(), "UTC")
543 cal.vevent.add('dtstamp').value = \
544 combine(datetime.datetime.now(), "UTC")
79b400f0 545 if len(description) > 0:
b7a741ad 546 cal.vevent.add('description').value = description
547 if len(self.contact) > 0:
548 cal.vevent.add('contact').value = self.contact
549 if len(self.url) > 0:
550 cal.vevent.add('url').value = self.url
88585b02
EMS
551 cal.vevent.add('location').value = ', '.join(
552 x for x in [self.adresse, self.ville, self.pays.nom] if x
553 )
74b087e5
EMS
554 if self.piece_jointe:
555 url = self.piece_jointe.url
556 if not url.startswith('http://'):
557 url = SITE_ROOT_URL + url
558 cal.vevent.add('attach').value = url
b7a741ad 559 return cal
560
561 def update_vevent(self,):
562 """Essaie de créer l'évènement sur le serveur ical.
563 En cas de succès, l'évènement local devient donc inactif et approuvé"""
564 try:
565 if self.approuve:
566 event = self.as_ical()
567 client = caldav.DAVClient(CALENDRIER_URL)
88585b02
EMS
568 cal = caldav.Calendar(client, url=CALENDRIER_URL)
569 e = caldav.Event(
570 client, parent=cal, data=event.serialize(), id=self.uid
571 )
b7a741ad 572 e.save()
573 except:
574 self.approuve = False
575
576 def delete_vevent(self,):
577 """Supprime l'evenement sur le serveur caldav"""
578 try:
579 if self.approuve:
b7a741ad 580 client = caldav.DAVClient(CALENDRIER_URL)
88585b02 581 cal = caldav.Calendar(client, url=CALENDRIER_URL)
b7a741ad 582 e = cal.event(self.uid)
583 e.delete()
584 except error.NotFoundError:
585 pass
586
264a3210
EMS
587 def assigner_regions(self, regions):
588 self.regions.add(*regions)
589
590 def assigner_disciplines(self, disciplines):
591 if len(disciplines) == 1:
592 if self.discipline:
593 self.discipline_secondaire = disciplines[0]
594 else:
595 self.discipline = disciplines[0]
596 elif len(disciplines) >= 2:
597 self.discipline = disciplines[0]
598 self.discipline_secondaire = disciplines[1]
599
88585b02 600
b7a741ad 601def delete_vevent(sender, instance, *args, **kwargs):
5212238e 602 # Surcharge du comportement de suppression
88585b02
EMS
603 # La méthode de connexion par signals est préférable à surcharger la
604 # méthode delete() car dans le cas de la suppression par lots, cell-ci
605 # n'est pas invoquée
b7a741ad 606 instance.delete_vevent()
88585b02
EMS
607pre_delete.connect(delete_vevent, sender=Evenement)
608
b7a741ad 609
81fe476e
PP
610class EvenementVoir(Evenement):
611
612 class Meta:
613 proxy = True
429222f1
PP
614 verbose_name = 'événement (visualisation)'
615 verbose_name_plural = 'événement (visualisation)'
81fe476e 616
88585b02 617
5212238e 618# Ressources
b7a741ad 619
d972b61d 620class ListSet(models.Model):
88585b02
EMS
621 spec = models.CharField(primary_key=True, max_length=255)
622 name = models.CharField(max_length=255)
623 server = models.CharField(max_length=255)
624 validated = models.BooleanField(default=True)
d972b61d 625
10d37e44 626 def __unicode__(self,):
627 return self.name
628
88585b02 629
656b9c0f
PP
630class RecordCategorie(models.Model):
631 nom = models.CharField(max_length=255)
632
633 class Meta:
634 verbose_name = 'catégorie ressource'
635 verbose_name_plural = 'catégories ressources'
636
637 def __unicode__(self):
638 return self.nom
639
640
230671ff
EMS
641class RecordQuerySet(SEPQuerySet):
642
643 def filter_modified(self, min=None, max=None):
7ed3ee8f 644 return self._filter_date('modified', min=min, max=max)
230671ff 645
88585b02 646
5212238e 647class RecordSphinxQuerySet(SEPSphinxQuerySet):
f153be1b 648
5212238e 649 def __init__(self, model=None):
88585b02
EMS
650 SEPSphinxQuerySet.__init__(
651 self, model=model, index='savoirsenpartage_ressources',
652 weights=dict(title=3)
653 )
c1b134f8 654
230671ff 655 def filter_modified(self, min=None, max=None):
7ed3ee8f 656 return self._filter_date('modified', min=min, max=max)
230671ff 657
27effd89
PP
658 def filter_region(self, region):
659 return self.filter(region_ids=region.id)
660
661 def filter_discipline(self, discipline):
662 return self.filter(discipline_ids=discipline.id)
663
664
5212238e 665class RecordManager(SEPManager):
f12cc7fb 666
5212238e 667 def get_query_set(self):
f153be1b
EMS
668 """Ne garder que les ressources validées et qui sont soit dans aucun
669 listset ou au moins dans un listset validé."""
230671ff 670 qs = RecordQuerySet(self.model)
5212238e 671 qs = qs.filter(validated=True)
82f25472
EMS
672 qs = qs.filter(Q(listsets__isnull=True) | Q(listsets__validated=True))
673 return qs.distinct()
f153be1b 674
5212238e
EMS
675 def get_sphinx_query_set(self):
676 return RecordSphinxQuerySet(self.model)
77b0fac0 677
230671ff
EMS
678 def filter_modified(self, min=None, max=None):
679 return self.get_query_set().filter_modified(min=min, max=max)
680
88585b02 681
0cc5f772 682class Record(models.Model):
88585b02 683
23b5b3d5 684 #fonctionnement interne
88585b02
EMS
685 id = models.AutoField(primary_key=True)
686 server = models.CharField(max_length=255, verbose_name=u'serveur')
687 last_update = models.CharField(max_length=255)
688 last_checksum = models.CharField(max_length=255)
a5f76eb4 689 validated = models.BooleanField(default=True, verbose_name=u'validé')
23b5b3d5 690
691 #OAI
18dbd2cf
EMS
692 title = models.TextField(null=True, blank=True, verbose_name=u'titre')
693 creator = models.TextField(null=True, blank=True, verbose_name=u'auteur')
694 description = models.TextField(null=True, blank=True)
695 modified = models.CharField(max_length=255, null=True, blank=True)
88585b02
EMS
696 identifier = models.CharField(
697 max_length=255, null=True, blank=True, unique=True
698 )
699 uri = models.CharField(max_length=255, null=True, blank=True, unique=True)
700 source = models.TextField(null=True, blank=True)
701 contributor = models.TextField(null=True, blank=True)
18dbd2cf 702 subject = models.TextField(null=True, blank=True, verbose_name='sujet')
88585b02
EMS
703 publisher = models.TextField(null=True, blank=True)
704 type = models.TextField(null=True, blank=True)
705 format = models.TextField(null=True, blank=True)
706 language = models.TextField(null=True, blank=True)
da9020f3 707
88585b02 708 listsets = models.ManyToManyField(ListSet, null=True, blank=True)
d972b61d 709
da9020f3 710 #SEP 2 (aucune données récoltées)
88585b02
EMS
711 alt_title = models.TextField(null=True, blank=True)
712 abstract = models.TextField(null=True, blank=True)
713 creation = models.CharField(max_length=255, null=True, blank=True)
714 issued = models.CharField(max_length=255, null=True, blank=True)
715 isbn = models.TextField(null=True, blank=True)
716 orig_lang = models.TextField(null=True, blank=True)
717
718 categorie = models.ForeignKey(
719 RecordCategorie, blank=True, null=True, verbose_name='catégorie'
720 )
656b9c0f 721
da9020f3 722 # Metadata AUF multivaluées
a342f93a 723 disciplines = models.ManyToManyField(Discipline, blank=True)
88585b02
EMS
724 thematiques = models.ManyToManyField(
725 Thematique, blank=True, verbose_name='thématiques'
726 )
a342f93a 727 pays = models.ManyToManyField(Pays, blank=True)
88585b02
EMS
728 regions = models.ManyToManyField(
729 Region, blank=True, verbose_name='régions'
730 )
0cc5f772 731
5212238e 732 # Managers
da44ce68 733 objects = RecordManager()
c59dba82 734 all_objects = models.Manager()
da44ce68 735
18dbd2cf
EMS
736 class Meta:
737 verbose_name = 'ressource'
738
264a3210
EMS
739 def __unicode__(self):
740 return "[%s] %s" % (self.server, self.title)
741
230671ff
EMS
742 def get_absolute_url(self):
743 return reverse('ressource', kwargs={'id': self.id})
744
264a3210 745 def getServeurURL(self):
f98ad449 746 """Retourne l'URL du serveur de provenance"""
747 return RESOURCES[self.server]['url']
748
264a3210 749 def est_complet(self):
6d885e0c 750 """teste si le record à toutes les données obligatoires"""
751 return self.disciplines.count() > 0 and \
752 self.thematiques.count() > 0 and \
753 self.pays.count() > 0 and \
754 self.regions.count() > 0
755
264a3210
EMS
756 def assigner_regions(self, regions):
757 self.regions.add(*regions)
da9020f3 758
264a3210
EMS
759 def assigner_disciplines(self, disciplines):
760 self.disciplines.add(*disciplines)
88585b02
EMS
761
762
927764f9
PP
763class RecordEdit(Record):
764
765 class Meta:
766 proxy = True
767 verbose_name = 'ressource (édition)'
768 verbose_name_plural = 'ressources (édition)'
769
88585b02 770
6d885e0c 771class Serveur(models.Model):
b7a741ad 772 """Identification d'un serveur d'ou proviennent les références"""
88585b02 773 nom = models.CharField(primary_key=True, max_length=255)
6d885e0c 774
775 def __unicode__(self,):
776 return self.nom
777
778 def conf_2_db(self,):
779 for k in RESOURCES.keys():
780 s, created = Serveur.objects.get_or_create(nom=k)
781 s.nom = k
782 s.save()
783
88585b02 784
6d885e0c 785class Profile(models.Model):
786 user = models.ForeignKey(User, unique=True)
88585b02
EMS
787 serveurs = models.ManyToManyField(Serveur, null=True, blank=True)
788
0cc5f772
CR
789
790class HarvestLog(models.Model):
88585b02
EMS
791 context = models.CharField(max_length=255)
792 name = models.CharField(max_length=255)
793 date = models.DateTimeField(auto_now=True)
794 added = models.IntegerField(null=True, blank=True)
795 updated = models.IntegerField(null=True, blank=True)
796 processed = models.IntegerField(null=True, blank=True)
797 record = models.ForeignKey(Record, null=True, blank=True)
23b5b3d5 798
799 @staticmethod
800 def add(message):
801 logger = HarvestLog()
88585b02 802 if 'record_id' in message:
d566e9c1 803 message['record'] = Record.all_objects.get(id=message['record_id'])
23b5b3d5 804 del(message['record_id'])
805
88585b02 806 for k, v in message.items():
23b5b3d5 807 setattr(logger, k, v)
808 logger.save()
f09bc1c6 809
88585b02 810
f09bc1c6
EMS
811# Pages statiques
812
813class PageStatique(models.Model):
20430b8a 814 id = models.CharField(max_length=32, primary_key=True)
f09bc1c6
EMS
815 titre = models.CharField(max_length=100)
816 contenu = models.TextField()
817
818 class Meta:
819 verbose_name_plural = 'pages statiques'
fdcf5874 820
88585b02 821
fdcf5874
EMS
822# Recherches
823
824class GlobalSearchResults(object):
825
88585b02 826 def __init__(self, actualites=None, appels=None, evenements=None,
057c3327
PP
827 ressources=None, chercheurs=None, groupes=None,
828 sites=None, sites_auf=None):
fdcf5874
EMS
829 self.actualites = actualites
830 self.appels = appels
831 self.evenements = evenements
832 self.ressources = ressources
833 self.chercheurs = chercheurs
057c3327 834 self.groupes = groupes
fdcf5874
EMS
835 self.sites = sites
836 self.sites_auf = sites_auf
837
4b89a7df 838 def __nonzero__(self):
88585b02 839 return bool(self.actualites or self.appels or self.evenements or
057c3327
PP
840 self.ressources or self.chercheurs or self.groupes or
841 self.sites or self.sites_auf)
4b89a7df 842
88585b02 843
fdcf5874
EMS
844class Search(models.Model):
845 user = models.ForeignKey(User, editable=False)
846 content_type = models.ForeignKey(ContentType, editable=False)
847 nom = models.CharField(max_length=100, verbose_name="nom de la recherche")
88585b02
EMS
848 alerte_courriel = models.BooleanField(
849 verbose_name="Envoyer une alerte courriel"
850 )
851 derniere_alerte = models.DateField(
852 verbose_name="Date d'envoi de la dernière alerte courriel",
853 null=True, editable=False
854 )
855 q = models.CharField(
856 max_length=255, blank=True, verbose_name="dans tous les champs"
857 )
fdcf5874 858 discipline = models.ForeignKey(Discipline, blank=True, null=True)
88585b02
EMS
859 region = models.ForeignKey(
860 Region, blank=True, null=True, verbose_name='région',
861 help_text="La région est ici définie au sens, non strictement "
862 "géographique, du Bureau régional de l'AUF de référence."
863 )
fdcf5874
EMS
864
865 def query_string(self):
866 params = dict()
867 for field in self._meta.fields:
88585b02
EMS
868 if field.name in ['id', 'user', 'nom', 'search_ptr',
869 'content_type']:
fdcf5874
EMS
870 continue
871 value = getattr(self, field.column)
872 if value:
873 if isinstance(value, datetime.date):
874 params[field.name] = value.strftime('%d/%m/%Y')
875 else:
4b89a7df 876 params[field.name] = smart_str(value)
fdcf5874 877 return urlencode(params)
88585b02 878
fdcf5874
EMS
879 class Meta:
880 verbose_name = 'recherche transversale'
881 verbose_name_plural = "recherches transversales"
882
2094c7e5
PP
883 def __unicode__(self):
884 return self.nom
885
fdcf5874 886 def save(self):
a83b8efb
EMS
887 if self.alerte_courriel:
888 try:
889 original_search = Search.objects.get(id=self.id)
890 if not original_search.alerte_courriel:
891 # On a nouvellement activé l'alerte courriel. Notons la
892 # date.
88585b02
EMS
893 self.derniere_alerte = \
894 datetime.date.today() - datetime.timedelta(days=1)
a83b8efb 895 except Search.DoesNotExist:
88585b02
EMS
896 self.derniere_alerte = \
897 datetime.date.today() - datetime.timedelta(days=1)
fdcf5874 898 if (not self.content_type_id):
88585b02
EMS
899 self.content_type = ContentType.objects.get_for_model(
900 self.__class__
901 )
fdcf5874
EMS
902 super(Search, self).save()
903
904 def as_leaf_class(self):
905 content_type = self.content_type
906 model = content_type.model_class()
907 if(model == Search):
908 return self
909 return model.objects.get(id=self.id)
88585b02 910
4b89a7df 911 def run(self, min_date=None, max_date=None):
057c3327 912 from chercheurs.models import Chercheur, Groupe
fdcf5874
EMS
913 from sitotheque.models import Site
914
fdcf5874
EMS
915 actualites = Actualite.objects
916 evenements = Evenement.objects
917 ressources = Record.objects
918 chercheurs = Chercheur.objects
057c3327 919 groupes = Groupe.objects
fdcf5874
EMS
920 sites = Site.objects
921 if self.q:
922 actualites = actualites.search(self.q)
923 evenements = evenements.search(self.q)
924 ressources = ressources.search(self.q)
925 chercheurs = chercheurs.search(self.q)
057c3327 926 groupes = groupes.search(self.q)
fdcf5874
EMS
927 sites = sites.search(self.q)
928 if self.discipline:
929 actualites = actualites.filter_discipline(self.discipline)
930 evenements = evenements.filter_discipline(self.discipline)
931 ressources = ressources.filter_discipline(self.discipline)
932 chercheurs = chercheurs.filter_discipline(self.discipline)
933 sites = sites.filter_discipline(self.discipline)
934 if self.region:
935 actualites = actualites.filter_region(self.region)
936 evenements = evenements.filter_region(self.region)
937 ressources = ressources.filter_region(self.region)
938 chercheurs = chercheurs.filter_region(self.region)
939 sites = sites.filter_region(self.region)
4b89a7df
EMS
940 if min_date:
941 actualites = actualites.filter_date(min=min_date)
a83b8efb 942 evenements = evenements.filter_date_modification(min=min_date)
4b89a7df
EMS
943 ressources = ressources.filter_modified(min=min_date)
944 chercheurs = chercheurs.filter_date_modification(min=min_date)
945 sites = sites.filter_date_maj(min=min_date)
946 if max_date:
947 actualites = actualites.filter_date(max=max_date)
a83b8efb 948 evenements = evenements.filter_date_modification(max=max_date)
4b89a7df
EMS
949 ressources = ressources.filter_modified(max=max_date)
950 chercheurs = chercheurs.filter_date_modification(max=max_date)
951 sites = sites.filter_date_maj(max=max_date)
952
fdcf5874
EMS
953 try:
954 sites_auf = google_search(0, self.q)['results']
955 except:
956 sites_auf = []
957
958 return GlobalSearchResults(
959 actualites=actualites.order_by('-date').filter_type('actu'),
960 appels=actualites.order_by('-date').filter_type('appels'),
961 evenements=evenements.order_by('-debut'),
a2aa3978 962 ressources=ressources.order_by('-modified'),
fdcf5874 963 chercheurs=chercheurs.order_by('-date_modification'),
057c3327 964 groupes=groupes.order_by('nom'),
fdcf5874
EMS
965 sites=sites.order_by('-date_maj'),
966 sites_auf=sites_auf
967 )
968
969 def url(self):
c956b333
PP
970
971 if self.content_type.model != 'search':
972 obj = self.content_type.get_object_for_this_type(pk=self.pk)
973 return obj.url()
974
fdcf5874
EMS
975 url = ''
976 if self.discipline:
977 url += '/discipline/%d' % self.discipline.id
978 if self.region:
979 url += '/region/%d' % self.region.id
980 url += '/recherche/'
981 if self.q:
4b89a7df 982 url += '?' + urlencode({'q': smart_str(self.q)})
fdcf5874
EMS
983 return url
984
da5bf7e9
EMS
985 def rss_url(self):
986 return None
987
4b89a7df
EMS
988 def send_email_alert(self):
989 """Envoie une alerte courriel correspondant à cette recherche"""
990 yesterday = datetime.date.today() - datetime.timedelta(days=1)
991 if self.derniere_alerte is not None:
88585b02
EMS
992 results = self.as_leaf_class().run(
993 min_date=self.derniere_alerte, max_date=yesterday
994 )
4b89a7df
EMS
995 if results:
996 subject = 'Savoirs en partage - ' + self.nom
a83b8efb 997 from_email = CONTACT_EMAIL
4b89a7df 998 to_email = self.user.email
88585b02
EMS
999 text_content = u'Voici les derniers résultats ' \
1000 u'correspondant à votre recherche sauvegardée.\n\n'
1001 text_content += self.as_leaf_class() \
1002 .get_email_alert_content(results)
4b89a7df 1003 text_content += u'''
88585b02 1004
4b89a7df 1005Pour modifier votre abonnement aux alertes courriel de Savoirs en partage,
88585b02
EMS
1006rendez-vous sur le [gestionnaire de recherches sauvegardées](%s%s)''' % (
1007 SITE_ROOT_URL, reverse('recherches')
1008 )
1009 html_content = \
1010 '<div style="font-family: Arial, sans-serif">\n' + \
1011 markdown(smart_str(text_content)) + '</div>\n'
1012 msg = EmailMultiAlternatives(
1013 subject, text_content, from_email, [to_email]
1014 )
4b89a7df
EMS
1015 msg.attach_alternative(html_content, "text/html")
1016 msg.send()
1017 self.derniere_alerte = yesterday
1018 self.save()
4b89a7df
EMS
1019
1020 def get_email_alert_content(self, results):
1021 content = ''
1022 if results.chercheurs:
1023 content += u'\n### Nouveaux chercheurs\n\n'
1024 for chercheur in results.chercheurs:
88585b02
EMS
1025 content += u'- [%s %s](%s%s) \n' % (
1026 chercheur.nom.upper(), chercheur.prenom, SITE_ROOT_URL,
1027 chercheur.get_absolute_url()
1028 )
4b89a7df
EMS
1029 content += u' %s\n\n' % chercheur.etablissement_display
1030 if results.ressources:
1031 content += u'\n### Nouvelles ressources\n\n'
1032 for ressource in results.ressources:
88585b02
EMS
1033 content += u'- [%s](%s%s)\n\n' % (
1034 ressource.title, SITE_ROOT_URL,
1035 ressource.get_absolute_url()
1036 )
4b89a7df
EMS
1037 if ressource.description:
1038 content += '\n'
88585b02
EMS
1039 content += ''.join(
1040 ' %s\n' % line
1041 for line in textwrap.wrap(ressource.description)
1042 )
4b89a7df
EMS
1043 content += '\n'
1044
1045 if results.actualites:
1046 content += u'\n### Nouvelles actualités\n\n'
1047 for actualite in results.actualites:
88585b02
EMS
1048 content += u'- [%s](%s%s)\n\n' % (
1049 actualite.titre, SITE_ROOT_URL,
1050 actualite.get_absolute_url()
1051 )
4b89a7df
EMS
1052 if actualite.texte:
1053 content += '\n'
88585b02
EMS
1054 content += ''.join(
1055 ' %s\n' % line
1056 for line in textwrap.wrap(actualite.texte)
1057 )
4b89a7df
EMS
1058 content += '\n'
1059 if results.appels:
1060 content += u"\n### Nouveaux appels d'offres\n\n"
1061 for appel in results.appels:
1062 content += u'- [%s](%s%s)\n\n' % (appel.titre,
1063 SITE_ROOT_URL,
1064 appel.get_absolute_url())
1065 if appel.texte:
1066 content += '\n'
88585b02
EMS
1067 content += ''.join(
1068 ' %s\n' % line
1069 for line in textwrap.wrap(appel.texte)
1070 )
4b89a7df
EMS
1071 content += '\n'
1072 if results.evenements:
1073 content += u"\n### Nouveaux évènements\n\n"
1074 for evenement in results.evenements:
88585b02
EMS
1075 content += u'- [%s](%s%s) \n' % (
1076 evenement.titre, SITE_ROOT_URL,
1077 evenement.get_absolute_url()
1078 )
a83b8efb 1079 content += u' où ? : %s \n' % evenement.lieu
88585b02
EMS
1080 content += evenement.debut.strftime(
1081 ' quand ? : %d/%m/%Y %H:%M \n'
1082 )
1083 content += u' durée ? : %s\n\n' % \
1084 evenement.duration_display()
4b89a7df 1085 content += u' quoi ? : '
88585b02
EMS
1086 content += '\n '.join(
1087 textwrap.wrap(evenement.description)
1088 )
4b89a7df
EMS
1089 content += '\n\n'
1090 if results.sites:
1091 content += u"\n### Nouveaux sites\n\n"
1092 for site in results.sites:
1093 content += u'- [%s](%s%s)\n\n' % (site.titre,
1094 SITE_ROOT_URL,
1095 site.get_absolute_url())
1096 if site.description:
1097 content += '\n'
88585b02
EMS
1098 content += ''.join(
1099 ' %s\n' % line
1100 for line in textwrap.wrap(site.description)
1101 )
4b89a7df
EMS
1102 content += '\n'
1103 return content
1104
88585b02 1105
fdcf5874 1106class RessourceSearch(Search):
88585b02
EMS
1107 auteur = models.CharField(
1108 max_length=100, blank=True, verbose_name="auteur ou contributeur"
1109 )
fdcf5874
EMS
1110 titre = models.CharField(max_length=100, blank=True)
1111 sujet = models.CharField(max_length=100, blank=True)
88585b02
EMS
1112 publisher = models.CharField(
1113 max_length=100, blank=True, verbose_name="éditeur"
1114 )
1115 categorie = models.ForeignKey(
1116 RecordCategorie, blank=True, null=True, verbose_name='catégorie'
1117 )
fdcf5874
EMS
1118
1119 class Meta:
1120 verbose_name = 'recherche de ressources'
1121 verbose_name_plural = "recherches de ressources"
1122
4b89a7df 1123 def run(self, min_date=None, max_date=None):
fdcf5874
EMS
1124 results = Record.objects
1125 if self.q:
1126 results = results.search(self.q)
1127 if self.auteur:
88585b02
EMS
1128 results = results.add_to_query(
1129 '@(creator,contributor) ' + self.auteur
1130 )
fdcf5874
EMS
1131 if self.titre:
1132 results = results.add_to_query('@title ' + self.titre)
1133 if self.sujet:
1134 results = results.add_to_query('@subject ' + self.sujet)
1135 if self.publisher:
1136 results = results.add_to_query('@publisher ' + self.publisher)
ede02173
PP
1137 if self.categorie:
1138 results = results.add_to_query('@categorie %s' % self.categorie.id)
fdcf5874
EMS
1139 if self.discipline:
1140 results = results.filter_discipline(self.discipline)
1141 if self.region:
1142 results = results.filter_region(self.region)
4b89a7df
EMS
1143 if min_date:
1144 results = results.filter_modified(min=min_date)
1145 if max_date:
1146 results = results.filter_modified(max=max_date)
fdcf5874
EMS
1147 if not self.q:
1148 """Montrer les résultats les plus récents si on n'a pas fait
1149 une recherche par mots-clés."""
230671ff 1150 results = results.order_by('-modified')
fdcf5874
EMS
1151 return results.all()
1152
1153 def url(self):
1154 qs = self.query_string()
1155 return reverse('ressources') + ('?' + qs if qs else '')
1156
da5bf7e9
EMS
1157 def rss_url(self):
1158 qs = self.query_string()
1159 return reverse('rss_ressources') + ('?' + qs if qs else '')
1160
4b89a7df
EMS
1161 def get_email_alert_content(self, results):
1162 content = ''
1163 for ressource in results:
1164 content += u'- [%s](%s%s)\n\n' % (ressource.title,
1165 SITE_ROOT_URL,
1166 ressource.get_absolute_url())
1167 if ressource.description:
1168 content += '\n'
88585b02
EMS
1169 content += ''.join(
1170 ' %s\n' % line
1171 for line in textwrap.wrap(ressource.description)
1172 )
4b89a7df
EMS
1173 content += '\n'
1174 return content
1175
88585b02 1176
fdcf5874 1177class ActualiteSearchBase(Search):
88585b02
EMS
1178 date_min = models.DateField(
1179 blank=True, null=True, verbose_name="depuis le"
1180 )
1181 date_max = models.DateField(
1182 blank=True, null=True, verbose_name="jusqu'au"
1183 )
fdcf5874
EMS
1184
1185 class Meta:
1186 abstract = True
1187
4b89a7df 1188 def run(self, min_date=None, max_date=None):
fdcf5874
EMS
1189 results = Actualite.objects
1190 if self.q:
1191 results = results.search(self.q)
1192 if self.discipline:
1193 results = results.filter_discipline(self.discipline)
1194 if self.region:
1195 results = results.filter_region(self.region)
1196 if self.date_min:
1197 results = results.filter_date(min=self.date_min)
1198 if self.date_max:
1199 results = results.filter_date(max=self.date_max)
4b89a7df
EMS
1200 if min_date:
1201 results = results.filter_date(min=min_date)
1202 if max_date:
1203 results = results.filter_date(max=max_date)
fdcf5874
EMS
1204 return results.all()
1205
4b89a7df
EMS
1206 def get_email_alert_content(self, results):
1207 content = ''
1208 for actualite in results:
1209 content += u'- [%s](%s%s)\n\n' % (actualite.titre,
1210 SITE_ROOT_URL,
1211 actualite.get_absolute_url())
1212 if actualite.texte:
1213 content += '\n'
88585b02
EMS
1214 content += ''.join(
1215 ' %s\n' % line
1216 for line in textwrap.wrap(actualite.texte)
1217 )
4b89a7df
EMS
1218 content += '\n'
1219 return content
1220
88585b02 1221
fdcf5874
EMS
1222class ActualiteSearch(ActualiteSearchBase):
1223
1224 class Meta:
1225 verbose_name = "recherche d'actualités"
1226 verbose_name_plural = "recherches d'actualités"
88585b02 1227
4b89a7df 1228 def run(self, min_date=None, max_date=None):
88585b02
EMS
1229 return super(ActualiteSearch, self) \
1230 .run(min_date=min_date, max_date=max_date) \
1231 .filter_type('actu')
fdcf5874
EMS
1232
1233 def url(self):
1234 qs = self.query_string()
1235 return reverse('actualites') + ('?' + qs if qs else '')
1236
da5bf7e9
EMS
1237 def rss_url(self):
1238 qs = self.query_string()
1239 return reverse('rss_actualites') + ('?' + qs if qs else '')
88585b02
EMS
1240
1241
fdcf5874
EMS
1242class AppelSearch(ActualiteSearchBase):
1243
1244 class Meta:
1245 verbose_name = "recherche d'appels d'offres"
1246 verbose_name_plural = "recherches d'appels d'offres"
1247
4b89a7df 1248 def run(self, min_date=None, max_date=None):
88585b02
EMS
1249 return super(AppelSearch, self) \
1250 .run(min_date=min_date, max_date=max_date) \
1251 .filter_type('appels')
fdcf5874
EMS
1252
1253 def url(self):
1254 qs = self.query_string()
1255 return reverse('appels') + ('?' + qs if qs else '')
1256
da5bf7e9
EMS
1257 def rss_url(self):
1258 qs = self.query_string()
1259 return reverse('rss_appels') + ('?' + qs if qs else '')
1260
88585b02 1261
fdcf5874 1262class EvenementSearch(Search):
88585b02
EMS
1263 titre = models.CharField(
1264 max_length=100, blank=True, verbose_name="Intitulé"
1265 )
1266 type = models.CharField(
1267 max_length=100, blank=True, choices=Evenement.TYPE_CHOICES
1268 )
1269 date_min = models.DateField(
1270 blank=True, null=True, verbose_name="depuis le"
1271 )
1272 date_max = models.DateField(
1273 blank=True, null=True, verbose_name="jusqu'au"
1274 )
fdcf5874
EMS
1275
1276 class Meta:
1277 verbose_name = "recherche d'évènements"
1278 verbose_name_plural = "recherches d'évènements"
1279
4b89a7df 1280 def run(self, min_date=None, max_date=None):
fdcf5874
EMS
1281 results = Evenement.objects
1282 if self.q:
1283 results = results.search(self.q)
1284 if self.titre:
1285 results = results.add_to_query('@titre ' + self.titre)
1286 if self.discipline:
1287 results = results.filter_discipline(self.discipline)
1288 if self.region:
1289 results = results.filter_region(self.region)
1290 if self.type:
1291 results = results.filter_type(self.type)
1292 if self.date_min:
1293 results = results.filter_debut(min=self.date_min)
1294 if self.date_max:
1295 results = results.filter_debut(max=self.date_max)
4b89a7df 1296 if min_date:
a83b8efb 1297 results = results.filter_date_modification(min=min_date)
4b89a7df 1298 if max_date:
a83b8efb 1299 results = results.filter_date_modification(max=max_date)
fdcf5874
EMS
1300 return results.all()
1301
1302 def url(self):
1303 qs = self.query_string()
1304 return reverse('agenda') + ('?' + qs if qs else '')
1305
da5bf7e9
EMS
1306 def rss_url(self):
1307 qs = self.query_string()
1308 return reverse('rss_agenda') + ('?' + qs if qs else '')
4b89a7df
EMS
1309
1310 def get_email_alert_content(self, results):
1311 content = ''
1312 for evenement in results:
1313 content += u'- [%s](%s%s) \n' % (evenement.titre,
1314 SITE_ROOT_URL,
1315 evenement.get_absolute_url())
a83b8efb 1316 content += u' où ? : %s \n' % evenement.lieu
88585b02
EMS
1317 content += evenement.debut.strftime(
1318 ' quand ? : %d/%m/%Y %H:%M \n'
1319 )
4b89a7df
EMS
1320 content += u' durée ? : %s\n\n' % evenement.duration_display()
1321 content += u' quoi ? : '
88585b02
EMS
1322 content += '\n '.join(
1323 textwrap.wrap(evenement.description)
1324 )
4b89a7df
EMS
1325 content += '\n\n'
1326 return content