Merge branch 'dev' into test
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / models.py
1 # -*- encoding: utf-8 -*-
2 import caldav
3 import datetime
4 import feedparser
5 import operator
6 import os
7 import pytz
8 import random
9 import uuid
10 import vobject
11 from backend_config import RESOURCES
12 from babel.dates import get_timezone_name
13 from caldav.lib import error
14 from babel.dates import get_timezone_name
15 from datamaster_modeles.models import Region, Pays, Thematique
16 from django.contrib.auth.models import User
17 from django.db import models
18 from django.db.models import Q, Max
19 from django.db.models.signals import pre_delete
20 from django.utils.encoding import smart_unicode
21 from djangosphinx.models import SphinxQuerySet, SearchError
22 from savoirs.globals import META
23 from settings import CALENDRIER_URL, SITE_ROOT_URL
24
25 # Fonctionnalités communes à tous les query sets
26
27 class RandomQuerySetMixin(object):
28 """Mixin pour les modèles.
29
30 ORDER BY RAND() est très lent sous MySQL. On a besoin d'une autre
31 méthode pour récupérer des objets au hasard.
32 """
33
34 def random(self, n=1):
35 """Récupère aléatoirement un nombre donné d'objets."""
36 count = self.count()
37 positions = random.sample(xrange(count), min(n, count))
38 return [self[p] for p in positions]
39
40 class SEPQuerySet(models.query.QuerySet, RandomQuerySetMixin):
41 pass
42
43 class SEPSphinxQuerySet(SphinxQuerySet, RandomQuerySetMixin):
44 """Fonctionnalités communes aux query sets de Sphinx."""
45
46 def __init__(self, model=None, index=None, weights=None):
47 SphinxQuerySet.__init__(self, model=model, index=index,
48 mode='SPH_MATCH_EXTENDED2',
49 rankmode='SPH_RANK_PROXIMITY_BM25',
50 weights=weights)
51
52 def add_to_query(self, query):
53 """Ajoute une partie à la requête texte."""
54
55 # Assurons-nous qu'il y a un nombre pair de guillemets
56 if query.count('"') % 2 != 0:
57 # Sinon, on enlève le dernier (faut choisir...)
58 i = query.rindex('"')
59 query = query[:i] + query[i+1:]
60
61 new_query = smart_unicode(self._query) + ' ' + query if self._query else query
62 return self.query(new_query)
63
64 def search(self, text):
65 """Recherche ``text`` dans tous les champs."""
66 return self.add_to_query('@* ' + text)
67
68 def filter_discipline(self, discipline):
69 """Par défaut, le filtre par discipline cherche le nom de la
70 discipline dans tous les champs."""
71 return self.search('"%s"' % discipline.nom)
72
73 def filter_region(self, region):
74 """Par défaut, le filtre par région cherche le nom de la région dans
75 tous les champs."""
76 return self.search('"%s"' % region.nom)
77
78 def _get_sphinx_results(self):
79 try:
80 return SphinxQuerySet._get_sphinx_results(self)
81 except SearchError:
82 # Essayons d'enlever les caractères qui peuvent poser problème.
83 for c in '|!@()~/<=^$':
84 self._query = self._query.replace(c, ' ')
85 try:
86 return SphinxQuerySet._get_sphinx_results(self)
87 except SearchError:
88 # Ça ne marche toujours pas. Enlevons les guillemets et les
89 # tirets.
90 for c in '"-':
91 self._query = self._query.replace(c, ' ')
92 return SphinxQuerySet._get_sphinx_results(self)
93
94 class SEPManager(models.Manager):
95 """Lorsque les méthodes ``search``, ``filter_region`` et
96 ``filter_discipline`` sont appelées sur ce manager, le query set
97 Sphinx est créé, sinon, c'est le query set Django qui est créé."""
98
99 def query(self, query):
100 return self.get_sphinx_query_set().query(query)
101
102 def add_to_query(self, query):
103 return self.get_sphinx_query_set().add_to_query(query)
104
105 def search(self, text):
106 return self.get_sphinx_query_set().search(text)
107
108 def filter_region(self, region):
109 return self.get_sphinx_query_set().filter_region(region)
110
111 def filter_discipline(self, discipline):
112 return self.get_sphinx_query_set().filter_discipline(discipline)
113
114 # Disciplines
115
116 class Discipline(models.Model):
117 id = models.IntegerField(primary_key=True, db_column='id_discipline')
118 nom = models.CharField(max_length=765, db_column='nom_discipline')
119
120 def __unicode__ (self):
121 return self.nom
122
123 class Meta:
124 db_table = u'discipline'
125 ordering = ["nom",]
126
127 # Actualités
128
129 class SourceActualite(models.Model):
130 nom = models.CharField(max_length=255)
131 url = models.CharField(max_length=255, verbose_name='URL')
132
133 class Meta:
134 verbose_name = u'fil RSS syndiqué'
135 verbose_name_plural = u'fils RSS syndiqués'
136
137 def __unicode__(self,):
138 return u"%s" % self.nom
139
140 def update(self):
141 """Mise à jour du fil RSS."""
142 feed = feedparser.parse(self.url)
143 for entry in feed.entries:
144 if Actualite.all_objects.filter(url=entry.link).count() == 0:
145 ts = entry.updated_parsed
146 date = datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday)
147 a = self.actualites.create(titre=entry.title,
148 texte=entry.summary_detail.value,
149 url=entry.link, date=date)
150
151 class ActualiteQuerySet(SEPQuerySet):
152
153 def filter_date(self, min=None, max=None):
154 qs = self
155 if min:
156 qs = qs.filter(date__gte=min)
157 if max:
158 qs = qs.filter(date__lte=max)
159 return qs
160
161 class ActualiteSphinxQuerySet(SEPSphinxQuerySet):
162
163 def __init__(self, model=None):
164 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_actualites',
165 weights=dict(titre=3))
166
167 def filter_date(self, min=None, max=None):
168 qs = self
169 if min:
170 qs = qs.filter(date__gte=min.toordinal()+365)
171 if max:
172 qs = qs.filter(date__lte=max.toordinal()+365)
173 return qs
174
175 class ActualiteManager(SEPManager):
176
177 def get_query_set(self):
178 return ActualiteQuerySet(self.model).filter(visible=True)
179
180 def get_sphinx_query_set(self):
181 return ActualiteSphinxQuerySet(self.model).order_by('-date')
182
183 def filter_date(self, min=None, max=None):
184 return self.get_query_set().filter_date(min=min, max=max)
185
186 class Actualite(models.Model):
187 id = models.AutoField(primary_key=True, db_column='id_actualite')
188 titre = models.CharField(max_length=765, db_column='titre_actualite')
189 texte = models.TextField(db_column='texte_actualite')
190 url = models.CharField(max_length=765, db_column='url_actualite')
191 date = models.DateField(db_column='date_actualite')
192 visible = models.BooleanField(db_column='visible_actualite', default=False)
193 ancienid = models.IntegerField(db_column='ancienId_actualite', blank=True, null=True)
194 source = models.ForeignKey(SourceActualite, blank=True, null=True, related_name='actualites')
195 disciplines = models.ManyToManyField(Discipline, blank=True, related_name="actualites")
196 regions = models.ManyToManyField(Region, blank=True, related_name="actualites", verbose_name='régions')
197
198 objects = ActualiteManager()
199 all_objects = models.Manager()
200
201 class Meta:
202 db_table = u'actualite'
203 ordering = ["-date"]
204
205 def __unicode__ (self):
206 return "%s" % (self.titre)
207
208 def assigner_disciplines(self, disciplines):
209 self.disciplines.add(*disciplines)
210
211 def assigner_regions(self, regions):
212 self.regions.add(*regions)
213
214 # Agenda
215
216 class EvenementQuerySet(SEPQuerySet):
217
218 def filter_type(self, type):
219 return self.filter(type=type)
220
221 def filter_debut(self, min=None, max=None):
222 qs = self
223 if min:
224 qs = qs.filter(debut__gte=min)
225 if max:
226 qs = qs.filter(debut__lt=max+datetime.timedelta(days=1))
227 return qs
228
229 class EvenementSphinxQuerySet(SEPSphinxQuerySet):
230
231 def __init__(self, model=None):
232 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_evenements',
233 weights=dict(titre=3))
234
235 def filter_type(self, type):
236 return self.add_to_query('@type "%s"' % type)
237
238 def filter_debut(self, min=None, max=None):
239 qs = self
240 if min:
241 qs = qs.filter(debut__gte=min.toordinal()+365)
242 if max:
243 qs = qs.filter(debut__lte=max.toordinal()+365)
244 return qs
245
246 class EvenementManager(SEPManager):
247
248 def get_query_set(self):
249 return EvenementQuerySet(self.model).filter(approuve=True)
250
251 def get_sphinx_query_set(self):
252 return EvenementSphinxQuerySet(self.model).order_by('-debut')
253
254 def filter_type(self, type):
255 return self.get_query_set().filter_type(type)
256
257 def filter_debut(self, min=None, max=None):
258 return self.get_query_set().filter_debut(min=min, max=max)
259
260 def build_time_zone_choices(pays=None):
261 timezones = pytz.country_timezones[pays] if pays else pytz.common_timezones
262 result = []
263 now = datetime.datetime.now()
264 for tzname in timezones:
265 tz = pytz.timezone(tzname)
266 fr_name = get_timezone_name(tz, locale='fr_FR')
267 offset = tz.utcoffset(now)
268 seconds = offset.seconds + offset.days * 86400
269 (hours, minutes) = divmod(seconds // 60, 60)
270 offset_str = 'UTC%+d:%d' % (hours, minutes) if minutes else 'UTC%+d' % hours
271 result.append((seconds, tzname, '%s - %s' % (offset_str, fr_name)))
272 result.sort()
273 return [(x[1], x[2]) for x in result]
274
275 class Evenement(models.Model):
276 TYPE_CHOICES = ((u'Colloque', u'Colloque'),
277 (u'Conférence', u'Conférence'),
278 (u'Appel à contribution', u'Appel à contribution'),
279 (u'Journée d\'étude', u'Journée d\'étude'),
280 (u'Autre', u'Autre'))
281 TIME_ZONE_CHOICES = build_time_zone_choices()
282
283 uid = models.CharField(max_length=255, default=str(uuid.uuid1()))
284 approuve = models.BooleanField(default=False, verbose_name=u'approuvé')
285 titre = models.CharField(max_length=255)
286 discipline = models.ForeignKey('Discipline', related_name = "discipline",
287 blank = True, null = True)
288 discipline_secondaire = models.ForeignKey('Discipline', related_name="discipline_secondaire",
289 verbose_name=u"discipline secondaire",
290 blank=True, null=True)
291 mots_cles = models.TextField('Mots-Clés', blank=True, null=True)
292 type = models.CharField(max_length=255, choices=TYPE_CHOICES)
293 lieu = models.TextField()
294 debut = models.DateTimeField(default=datetime.datetime.now)
295 fin = models.DateTimeField(default=datetime.datetime.now)
296 pays = models.ForeignKey(Pays, related_name='evenements', null=True, blank=True)
297 fuseau = models.CharField(max_length=100, choices=TIME_ZONE_CHOICES, verbose_name='fuseau horaire')
298 description = models.TextField(blank=True, null=True)
299 contact = models.TextField(blank=True, null=True)
300 url = models.CharField(max_length=255, blank=True, null=True)
301 piece_jointe = models.FileField(upload_to='agenda/pj', blank=True, verbose_name='pièce jointe')
302 regions = models.ManyToManyField(Region, blank=True, related_name="evenements", verbose_name='régions')
303
304 objects = EvenementManager()
305 all_objects = models.Manager()
306
307 class Meta:
308 ordering = ['-debut']
309
310 def __unicode__(self,):
311 return "[%s] %s" % (self.uid, self.titre)
312
313 def duration_display(self):
314 delta = self.fin - self.debut
315 minutes, seconds = divmod(delta.seconds, 60)
316 hours, minutes = divmod(minutes, 60)
317 days = delta.days
318 parts = []
319 if days == 1:
320 parts.append('1 jour')
321 elif days > 1:
322 parts.append('%d jours' % days)
323 if hours == 1:
324 parts.append('1 heure')
325 elif hours > 1:
326 parts.append('%d heures' % hours)
327 if minutes == 1:
328 parts.append('1 minute')
329 elif minutes > 1:
330 parts.append('%d minutes' % minutes)
331 return ' '.join(parts)
332
333 def piece_jointe_display(self):
334 return self.piece_jointe and os.path.basename(self.piece_jointe.name)
335
336 def clean(self):
337 from django.core.exceptions import ValidationError
338 if self.debut > self.fin:
339 raise ValidationError('La date de fin ne doit pas être antérieure à la date de début')
340
341 def save(self, *args, **kwargs):
342 """Sauvegarde l'objet dans django et le synchronise avec caldav s'il a été
343 approuvé"""
344 self.clean()
345 super(Evenement, self).save(*args, **kwargs)
346 self.update_vevent()
347
348 # methodes de commnunications avec CALDAV
349 def as_ical(self,):
350 """Retourne l'evenement django sous forme d'objet icalendar"""
351 cal = vobject.iCalendar()
352 cal.add('vevent')
353
354 # fournit son propre uid
355 if self.uid in [None, ""]:
356 self.uid = str(uuid.uuid1())
357
358 cal.vevent.add('uid').value = self.uid
359
360 cal.vevent.add('summary').value = self.titre
361
362 if self.mots_cles is None:
363 kw = []
364 else:
365 kw = self.mots_cles.split(",")
366
367 try:
368 kw.append(self.discipline.nom)
369 kw.append(self.discipline_secondaire.nom)
370 kw.append(self.type)
371 except: pass
372
373 kw = [x.strip() for x in kw if len(x.strip()) > 0 and x is not None]
374 for k in kw:
375 cal.vevent.add('x-auf-keywords').value = k
376
377 description = self.description
378 if len(kw) > 0:
379 if len(self.description) > 0:
380 description += "\n"
381 description += u"Mots-clés: " + ", ".join(kw)
382
383 cal.vevent.add('dtstart').value = combine(self.debut, pytz.timezone(self.fuseau))
384 cal.vevent.add('dtend').value = combine(self.fin, pytz.timezone(self.fuseau))
385 cal.vevent.add('created').value = combine(datetime.datetime.now(), "UTC")
386 cal.vevent.add('dtstamp').value = combine(datetime.datetime.now(), "UTC")
387 if len(description) > 0:
388 cal.vevent.add('description').value = description
389 if len(self.contact) > 0:
390 cal.vevent.add('contact').value = self.contact
391 if len(self.url) > 0:
392 cal.vevent.add('url').value = self.url
393 if len(self.lieu) > 0:
394 cal.vevent.add('location').value = self.lieu
395 if self.piece_jointe:
396 url = self.piece_jointe.url
397 if not url.startswith('http://'):
398 url = SITE_ROOT_URL + url
399 cal.vevent.add('attach').value = url
400 return cal
401
402 def update_vevent(self,):
403 """Essaie de créer l'évènement sur le serveur ical.
404 En cas de succès, l'évènement local devient donc inactif et approuvé"""
405 try:
406 if self.approuve:
407 event = self.as_ical()
408 client = caldav.DAVClient(CALENDRIER_URL)
409 cal = caldav.Calendar(client, url = CALENDRIER_URL)
410 e = caldav.Event(client, parent = cal, data = event.serialize(), id=self.uid)
411 e.save()
412 except:
413 self.approuve = False
414
415 def delete_vevent(self,):
416 """Supprime l'evenement sur le serveur caldav"""
417 try:
418 if self.approuve:
419 event = self.as_ical()
420 client = caldav.DAVClient(CALENDRIER_URL)
421 cal = caldav.Calendar(client, url = CALENDRIER_URL)
422 e = cal.event(self.uid)
423 e.delete()
424 except error.NotFoundError:
425 pass
426
427 def assigner_regions(self, regions):
428 self.regions.add(*regions)
429
430 def assigner_disciplines(self, disciplines):
431 if len(disciplines) == 1:
432 if self.discipline:
433 self.discipline_secondaire = disciplines[0]
434 else:
435 self.discipline = disciplines[0]
436 elif len(disciplines) >= 2:
437 self.discipline = disciplines[0]
438 self.discipline_secondaire = disciplines[1]
439
440 def delete_vevent(sender, instance, *args, **kwargs):
441 # Surcharge du comportement de suppression
442 # La méthode de connexion par signals est préférable à surcharger la méthode delete()
443 # car dans le cas de la suppression par lots, cell-ci n'est pas invoquée
444 instance.delete_vevent()
445 pre_delete.connect(delete_vevent, sender=Evenement)
446
447 # Ressources
448
449 class ListSet(models.Model):
450 spec = models.CharField(primary_key = True, max_length = 255)
451 name = models.CharField(max_length = 255)
452 server = models.CharField(max_length = 255)
453 validated = models.BooleanField(default = True)
454
455 def __unicode__(self,):
456 return self.name
457
458 class RecordSphinxQuerySet(SEPSphinxQuerySet):
459
460 def __init__(self, model=None):
461 SEPSphinxQuerySet.__init__(self, model=model, index='savoirsenpartage_ressources',
462 weights=dict(title=3))
463
464 class RecordManager(SEPManager):
465
466 def get_query_set(self):
467 """Ne garder que les ressources validées et qui sont soit dans aucun
468 listset ou au moins dans un listset validé."""
469 qs = SEPQuerySet(self.model)
470 qs = qs.filter(validated=True)
471 qs = qs.filter(Q(listsets__isnull=True) | Q(listsets__validated=True))
472 return qs.distinct()
473
474 def get_sphinx_query_set(self):
475 return RecordSphinxQuerySet(self.model)
476
477 class Record(models.Model):
478
479 #fonctionnement interne
480 id = models.AutoField(primary_key = True)
481 server = models.CharField(max_length = 255, verbose_name=u'serveur')
482 last_update = models.CharField(max_length = 255)
483 last_checksum = models.CharField(max_length = 255)
484 validated = models.BooleanField(default=True, verbose_name=u'validé')
485
486 #OAI
487 title = models.TextField(null=True, blank=True, verbose_name=u'titre')
488 creator = models.TextField(null=True, blank=True, verbose_name=u'auteur')
489 description = models.TextField(null=True, blank=True)
490 modified = models.CharField(max_length=255, null=True, blank=True)
491 identifier = models.CharField(max_length = 255, null = True, blank = True, unique = True)
492 uri = models.CharField(max_length = 255, null = True, blank = True, unique = True)
493 source = models.TextField(null = True, blank = True)
494 contributor = models.TextField(null = True, blank = True)
495 subject = models.TextField(null=True, blank=True, verbose_name='sujet')
496 publisher = models.TextField(null = True, blank = True)
497 type = models.TextField(null = True, blank = True)
498 format = models.TextField(null = True, blank = True)
499 language = models.TextField(null = True, blank = True)
500
501 listsets = models.ManyToManyField(ListSet, null = True, blank = True)
502
503 #SEP 2 (aucune données récoltées)
504 alt_title = models.TextField(null = True, blank = True)
505 abstract = models.TextField(null = True, blank = True)
506 creation = models.CharField(max_length = 255, null = True, blank = True)
507 issued = models.CharField(max_length = 255, null = True, blank = True)
508 isbn = models.TextField(null = True, blank = True)
509 orig_lang = models.TextField(null = True, blank = True)
510
511 # Metadata AUF multivaluées
512 disciplines = models.ManyToManyField(Discipline, blank=True)
513 thematiques = models.ManyToManyField(Thematique, blank=True, verbose_name='thématiques')
514 pays = models.ManyToManyField(Pays, blank=True)
515 regions = models.ManyToManyField(Region, blank=True, verbose_name='régions')
516
517 # Managers
518 objects = RecordManager()
519 all_objects = models.Manager()
520
521 class Meta:
522 verbose_name = 'ressource'
523
524 def __unicode__(self):
525 return "[%s] %s" % (self.server, self.title)
526
527 def getServeurURL(self):
528 """Retourne l'URL du serveur de provenance"""
529 return RESOURCES[self.server]['url']
530
531 def est_complet(self):
532 """teste si le record à toutes les données obligatoires"""
533 return self.disciplines.count() > 0 and \
534 self.thematiques.count() > 0 and \
535 self.pays.count() > 0 and \
536 self.regions.count() > 0
537
538 def assigner_regions(self, regions):
539 self.regions.add(*regions)
540
541 def assigner_disciplines(self, disciplines):
542 self.disciplines.add(*disciplines)
543
544 class Serveur(models.Model):
545 """Identification d'un serveur d'ou proviennent les références"""
546 nom = models.CharField(primary_key = True, max_length = 255)
547
548 def __unicode__(self,):
549 return self.nom
550
551 def conf_2_db(self,):
552 for k in RESOURCES.keys():
553 s, created = Serveur.objects.get_or_create(nom=k)
554 s.nom = k
555 s.save()
556
557 class Profile(models.Model):
558 user = models.ForeignKey(User, unique=True)
559 serveurs = models.ManyToManyField(Serveur, null = True, blank = True)
560
561 class HarvestLog(models.Model):
562 context = models.CharField(max_length = 255)
563 name = models.CharField(max_length = 255)
564 date = models.DateTimeField(auto_now = True)
565 added = models.IntegerField(null = True, blank = True)
566 updated = models.IntegerField(null = True, blank = True)
567 processed = models.IntegerField(null = True, blank = True)
568 record = models.ForeignKey(Record, null = True, blank = True)
569
570 @staticmethod
571 def add(message):
572 logger = HarvestLog()
573 if message.has_key('record_id'):
574 message['record'] = Record.all_objects.get(id=message['record_id'])
575 del(message['record_id'])
576
577 for k,v in message.items():
578 setattr(logger, k, v)
579 logger.save()