fichier statique
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / models.py
CommitLineData
92c7413b 1# -*- encoding: utf-8 -*-
b7a741ad 2import simplejson, uuid, datetime, caldav, vobject, uuid
6d885e0c 3from django.contrib.auth.models import User
d15017b2 4from django.db import models
f12cc7fb 5from django.db.models import Q
b7a741ad 6from django.db.models.signals import pre_delete
92c7413b 7from timezones.fields import TimeZoneField
6d885e0c 8from auf_savoirs_en_partage.backend_config import RESOURCES
da9020f3 9from savoirs.globals import META
b7a741ad 10from settings import CALENDRIER_URL
e3c3296e 11from datamaster_modeles.models import Thematique, Pays, Region
b7a741ad 12from lib.calendrier import combine
13from caldav.lib import error
d15017b2
CR
14
15class Discipline(models.Model):
16 id = models.IntegerField(primary_key=True, db_column='id_discipline')
17 nom = models.CharField(max_length=765, db_column='nom_discipline')
6ef8ead4
CR
18
19 def __unicode__ (self):
92c7413b 20 return self.nom
6ef8ead4 21
d15017b2
CR
22 class Meta:
23 db_table = u'discipline'
24 ordering = ["nom",]
25
79c398f6
CR
26class SourceActualite(models.Model):
27 nom = models.CharField(max_length=255)
28 url = models.CharField(max_length=255)
ccbc4363 29
30 def __unicode__(self,):
31 return u"%s" % self.nom
79c398f6 32
2f9c4d6c
EMS
33class ActualiteManager(models.Manager):
34
35 def get_query_set(self):
36 return ActualiteQuerySet(self.model)
37
da44ce68
EMS
38 def search(self, text):
39 return self.get_query_set().search(text)
40
2f9c4d6c
EMS
41class ActualiteQuerySet(models.query.QuerySet):
42
43 def search(self, text):
c9cbc784 44 return self.filter(Q(titre__icontains=text) | Q(texte__icontains=text))
2f9c4d6c 45
d15017b2 46class Actualite(models.Model):
4f262f90 47 id = models.AutoField(primary_key=True, db_column='id_actualite')
d15017b2
CR
48 titre = models.CharField(max_length=765, db_column='titre_actualite')
49 texte = models.TextField(db_column='texte_actualite')
50 url = models.CharField(max_length=765, db_column='url_actualite')
d15017b2 51 date = models.DateField(db_column='date_actualite')
f554ef70 52 visible = models.BooleanField(db_column='visible_actualite', default = False)
3b6456b0 53 ancienid = models.IntegerField(db_column='ancienId_actualite', blank = True, null = True)
ccbc4363 54 source = models.ForeignKey(SourceActualite, blank = True, null = True)
6ef8ead4 55
2f9c4d6c
EMS
56 objects = ActualiteManager()
57
6ef8ead4 58 def __unicode__ (self):
f554ef70 59 return "%s" % (self.titre)
6ef8ead4 60
d15017b2
CR
61 class Meta:
62 db_table = u'actualite'
63 ordering = ["-date",]
92c7413b 64
4101cfc0
EMS
65class EvenementManager(models.Manager):
66
67 def get_query_set(self):
68 return EvenementQuerySet(self.model)
69
70 def search(self, text):
71 return self.get_query_set().search(text)
72
73class EvenementQuerySet(models.query.QuerySet):
74
75 def search(self, text):
76 qs = self
77 words = text.split()
78 for word in words:
79 qs = qs.filter(Q(titre__icontains=word) |
80 Q(mots_cles__icontains=word) |
81 Q(discipline__nom__icontains=word) |
82 Q(discipline_secondaire__nom__icontains=word) |
83 Q(type__icontains=word) |
84 Q(lieu__icontains=word) |
85 Q(description__icontains=word) |
86 Q(contact__icontains=word))
87 return qs
88
92c7413b 89class Evenement(models.Model):
c5b3da8b 90 uid = models.CharField(max_length = 255, default = str(uuid.uuid1()))
92c7413b
CR
91 approuve = models.BooleanField(default = False)
92 titre = models.CharField(max_length=255)
93 discipline = models.ForeignKey('Discipline', related_name = "discipline",
94 blank = True, null = True)
95 discipline_secondaire = models.ForeignKey('Discipline', related_name = \
96 "discipline_secondaire",
97 verbose_name = \
98 "Discipline secondaire",
99 blank = True, null = True)
100 mots_cles = models.TextField('Mots-Clés', blank = True, null = True)
101 type = models.CharField(max_length = 255, choices = \
102 (('Colloque', 'Colloque'),
103 ('Conférence', 'Conférence'),
104 ('Appel à contribution', 'Appel à contribution'),
105 ('Journée d\'étude', 'Journée d\'étude'),
106 (None, 'Autre')
107 )) #TODO: choices
108 fuseau = TimeZoneField(verbose_name = 'Fuseau horaire')
109 debut = models.DateTimeField(default = datetime.datetime.now)
110 fin = models.DateTimeField(default = datetime.datetime.now)
111 lieu = models.TextField()
112 description = models.TextField(blank = True, null = True)
113 #fichiers = TODO?
114 contact = models.TextField(blank = True, null = True)
115 url = models.CharField(max_length=255, blank = True, null = True)
116
4101cfc0
EMS
117 objects = EvenementManager()
118
119 class Meta:
120 ordering = ['-debut']
121
020f79a9 122 def __unicode__(self,):
123 return "[%s] %s" % (self.uid, self.titre)
124
b7a741ad 125 def save(self, *args, **kwargs):
126 """Sauvegarde l'objet dans django et le synchronise avec caldav s'il a été
127 approuvé"""
128 self.update_vevent()
129 super(Evenement, self).save(*args, **kwargs)
130
131 # methodes de commnunications avec CALDAV
132 def as_ical(self,):
133 """Retourne l'evenement django sous forme d'objet icalendar"""
134 cal = vobject.iCalendar()
135 cal.add('vevent')
136
137 # fournit son propre uid
7f56d0d4 138 if self.uid in [None, ""]:
b7a741ad 139 self.uid = str(uuid.uuid1())
140
141 cal.vevent.add('uid').value = self.uid
142
143 cal.vevent.add('summary').value = self.titre
144
145 if self.mots_cles is None:
146 kw = []
147 else:
148 kw = self.mots_cles.split(",")
149
150 try:
151 kw.append(self.discipline.nom)
152 kw.append(self.discipline_secondaire.nom)
153 kw.append(self.type)
154 except: pass
155
79b400f0 156 kw = [x.strip() for x in kw if len(x.strip()) > 0 and x is not None]
b7a741ad 157 for k in kw:
158 cal.vevent.add('x-auf-keywords').value = k
159
160 description = self.description
161 if len(kw) > 0:
162 if len(self.description) > 0:
163 description += "\n"
028f548f 164 description += u"Mots-clés: " + ", ".join(kw)
b7a741ad 165
166 cal.vevent.add('dtstart').value = combine(self.debut, self.fuseau)
167 cal.vevent.add('dtend').value = combine(self.fin, self.fuseau)
168 cal.vevent.add('created').value = combine(datetime.datetime.now(), "UTC")
169 cal.vevent.add('dtstamp').value = combine(datetime.datetime.now(), "UTC")
79b400f0 170 if len(description) > 0:
b7a741ad 171 cal.vevent.add('description').value = description
172 if len(self.contact) > 0:
173 cal.vevent.add('contact').value = self.contact
174 if len(self.url) > 0:
175 cal.vevent.add('url').value = self.url
176 if len(self.lieu) > 0:
177 cal.vevent.add('location').value = self.lieu
178 return cal
179
180 def update_vevent(self,):
181 """Essaie de créer l'évènement sur le serveur ical.
182 En cas de succès, l'évènement local devient donc inactif et approuvé"""
183 try:
184 if self.approuve:
185 event = self.as_ical()
186 client = caldav.DAVClient(CALENDRIER_URL)
187 cal = caldav.Calendar(client, url = CALENDRIER_URL)
188 e = caldav.Event(client, parent = cal, data = event.serialize(), id=self.uid)
189 e.save()
190 except:
191 self.approuve = False
192
193 def delete_vevent(self,):
194 """Supprime l'evenement sur le serveur caldav"""
195 try:
196 if self.approuve:
197 event = self.as_ical()
198 client = caldav.DAVClient(CALENDRIER_URL)
199 cal = caldav.Calendar(client, url = CALENDRIER_URL)
200 e = cal.event(self.uid)
201 e.delete()
202 except error.NotFoundError:
203 pass
204
205
206# Surcharge du comportement de suppression
207# La méthode de connexion par signals est préférable à surcharger la méthode delete()
208# car dans le cas de la suppression par lots, cell-ci n'est pas invoquée
209def delete_vevent(sender, instance, *args, **kwargs):
210 instance.delete_vevent()
211
212pre_delete.connect(delete_vevent, sender = Evenement)
213
214
d972b61d 215class ListSet(models.Model):
216 spec = models.CharField(primary_key = True, max_length = 255)
217 name = models.CharField(max_length = 255)
218 server = models.CharField(max_length = 255)
9eda5d6c 219 validated = models.BooleanField(default = True)
d972b61d 220
10d37e44 221 def __unicode__(self,):
222 return self.name
223
da44ce68
EMS
224class RecordManager(models.Manager):
225
226 def get_query_set(self):
227 return RecordQuerySet(self.model)
228
229 def search(self, text):
230 return self.get_query_set().search(text)
231
f153be1b
EMS
232 def validated(self):
233 return self.get_query_set().validated()
234
da44ce68
EMS
235class RecordQuerySet(models.query.QuerySet):
236
237 def search(self, text):
f12cc7fb 238 qs = self
da44ce68 239 words = text.split()
da44ce68 240
f12cc7fb
EMS
241 # Ne garder que les ressources qui contiennent tous les mots
242 # demandés.
243 for word in words:
244 qs = qs.filter(Q(title__icontains=word) | Q(description__icontains=word) |
245 Q(creator__icontains=word) | Q(contributor__icontains=word) |
246 Q(subject__icontains=word))
247
248 # On donne un point pour chaque mot présent dans le titre.
249 score_expr = ' + '.join(['(title LIKE %s)'] * len(words))
250 score_params = ['%' + word + '%' for word in words]
251 return qs.extra(
252 select={'score': score_expr},
253 select_params=score_params
254 ).order_by('-score')
255
256 def search_auteur(self, text):
257 qs = self
258 for word in text.split():
259 qs = qs.filter(Q(creator__icontains=word) | Q(contributor__icontains=word))
260 return qs
261
262 def search_sujet(self, text):
263 qs = self
264 for word in text.split():
265 qs = qs.filter(subject__icontains=word)
266 return qs
267
268 def search_titre(self, text):
269 qs = self
270 for word in text.split():
271 qs = qs.filter(title__icontains=word)
272 return qs
273
f153be1b
EMS
274 def validated(self):
275 """Ne garder que les ressources validées et qui sont soit dans aucun
276 listset ou au moins dans un listset validé."""
277 qs = self.filter(validated=True)
278 qs = qs.extra(where=['''((savoirs_record.id NOT IN (SELECT record_id FROM savoirs_record_listsets)) OR
279 ((SELECT MAX(l.validated) FROM savoirs_listset l
280 INNER JOIN savoirs_record_listsets rl ON rl.listset_id = l.spec
281 WHERE rl.record_id = savoirs_record.id) = TRUE))'''])
282 return qs
283
0cc5f772 284class Record(models.Model):
23b5b3d5 285
286 #fonctionnement interne
0cc5f772 287 id = models.AutoField(primary_key = True)
23b5b3d5 288 server = models.CharField(max_length = 255)
289 last_update = models.CharField(max_length = 255)
290 last_checksum = models.CharField(max_length = 255)
c88d78dc 291 validated = models.BooleanField(default = True)
23b5b3d5 292
293 #OAI
294 title = models.TextField(null = True, blank = True)
295 creator = models.TextField(null = True, blank = True)
296 description = models.TextField(null = True, blank = True)
297 modified = models.CharField(max_length = 255, null = True, blank = True)
298 identifier = models.CharField(max_length = 255, null = True, blank = True, unique = True)
299 uri = models.CharField(max_length = 255, null = True, blank = True, unique = True)
300 source = models.TextField(null = True, blank = True)
301 contributor = models.TextField(null = True, blank = True)
302 subject = models.TextField(null = True, blank = True)
303 publisher = models.TextField(null = True, blank = True)
304 type = models.TextField(null = True, blank = True)
305 format = models.TextField(null = True, blank = True)
306 language = models.TextField(null = True, blank = True)
da9020f3 307
c88d78dc 308 listsets = models.ManyToManyField(ListSet, null = True, blank = True)
d972b61d 309
da9020f3 310 #SEP 2 (aucune données récoltées)
23b5b3d5 311 alt_title = models.TextField(null = True, blank = True)
312 abstract = models.TextField(null = True, blank = True)
313 creation = models.CharField(max_length = 255, null = True, blank = True)
314 issued = models.CharField(max_length = 255, null = True, blank = True)
315 isbn = models.TextField(null = True, blank = True)
316 orig_lang = models.TextField(null = True, blank = True)
da9020f3 317
318 # Metadata AUF multivaluées
319 disciplines = models.ManyToManyField(Discipline)
320 thematiques = models.ManyToManyField(Thematique)
e3c3296e 321 pays = models.ManyToManyField(Pays)
322 regions = models.ManyToManyField(Region)
0cc5f772 323
da44ce68
EMS
324 # Manager
325 objects = RecordManager()
326
6d885e0c 327 def est_complet(self,):
328 """teste si le record à toutes les données obligatoires"""
329 return self.disciplines.count() > 0 and \
330 self.thematiques.count() > 0 and \
331 self.pays.count() > 0 and \
332 self.regions.count() > 0
333
0cc5f772 334 def __unicode__(self):
f554ef70 335 return "[%s] %s" % (self.server, self.title)
da9020f3 336
6d885e0c 337class Serveur(models.Model):
b7a741ad 338 """Identification d'un serveur d'ou proviennent les références"""
6d885e0c 339 nom = models.CharField(primary_key = True, max_length = 255)
340
341 def __unicode__(self,):
342 return self.nom
343
344 def conf_2_db(self,):
345 for k in RESOURCES.keys():
346 s, created = Serveur.objects.get_or_create(nom=k)
347 s.nom = k
348 s.save()
349
350class Profile(models.Model):
351 user = models.ForeignKey(User, unique=True)
352 serveurs = models.ManyToManyField(Serveur, null = True, blank = True)
0cc5f772
CR
353
354class HarvestLog(models.Model):
23b5b3d5 355 context = models.CharField(max_length = 255)
356 name = models.CharField(max_length = 255)
0cc5f772 357 date = models.DateTimeField(auto_now = True)
23b5b3d5 358 added = models.IntegerField(null = True, blank = True)
359 updated = models.IntegerField(null = True, blank = True)
a85ba76e 360 processed = models.IntegerField(null = True, blank = True)
23b5b3d5 361 record = models.ForeignKey(Record, null = True, blank = True)
362
363 @staticmethod
364 def add(message):
365 logger = HarvestLog()
366 if message.has_key('record_id'):
367 message['record'] = Record.objects.get(id=message['record_id'])
368 del(message['record_id'])
369
370 for k,v in message.items():
371 setattr(logger, k, v)
372 logger.save()