Embryon de recherche textuelle dans les actualités.
[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
b7a741ad 5from django.db.models.signals import pre_delete
92c7413b 6from timezones.fields import TimeZoneField
6d885e0c 7from auf_savoirs_en_partage.backend_config import RESOURCES
da9020f3 8from savoirs.globals import META
b7a741ad 9from settings import CALENDRIER_URL
e3c3296e 10from datamaster_modeles.models import Thematique, Pays, Region
b7a741ad 11from lib.calendrier import combine
12from caldav.lib import error
d15017b2
CR
13
14class Discipline(models.Model):
15 id = models.IntegerField(primary_key=True, db_column='id_discipline')
16 nom = models.CharField(max_length=765, db_column='nom_discipline')
6ef8ead4
CR
17
18 def __unicode__ (self):
92c7413b 19 return self.nom
6ef8ead4 20
d15017b2
CR
21 class Meta:
22 db_table = u'discipline'
23 ordering = ["nom",]
24
79c398f6
CR
25class SourceActualite(models.Model):
26 nom = models.CharField(max_length=255)
27 url = models.CharField(max_length=255)
ccbc4363 28
29 def __unicode__(self,):
30 return u"%s" % self.nom
79c398f6 31
2f9c4d6c
EMS
32class ActualiteManager(models.Manager):
33
34 def get_query_set(self):
35 return ActualiteQuerySet(self.model)
36
37class ActualiteQuerySet(models.query.QuerySet):
38
39 def search(self, text):
40 return self.filter(titre__icontains=text)
41
d15017b2 42class Actualite(models.Model):
4f262f90 43 id = models.AutoField(primary_key=True, db_column='id_actualite')
d15017b2
CR
44 titre = models.CharField(max_length=765, db_column='titre_actualite')
45 texte = models.TextField(db_column='texte_actualite')
46 url = models.CharField(max_length=765, db_column='url_actualite')
d15017b2 47 date = models.DateField(db_column='date_actualite')
f554ef70 48 visible = models.BooleanField(db_column='visible_actualite', default = False)
3b6456b0 49 ancienid = models.IntegerField(db_column='ancienId_actualite', blank = True, null = True)
ccbc4363 50 source = models.ForeignKey(SourceActualite, blank = True, null = True)
6ef8ead4 51
2f9c4d6c
EMS
52 objects = ActualiteManager()
53
6ef8ead4 54 def __unicode__ (self):
f554ef70 55 return "%s" % (self.titre)
6ef8ead4 56
d15017b2
CR
57 class Meta:
58 db_table = u'actualite'
59 ordering = ["-date",]
92c7413b 60
92c7413b 61class Evenement(models.Model):
c5b3da8b 62 uid = models.CharField(max_length = 255, default = str(uuid.uuid1()))
92c7413b
CR
63 approuve = models.BooleanField(default = False)
64 titre = models.CharField(max_length=255)
65 discipline = models.ForeignKey('Discipline', related_name = "discipline",
66 blank = True, null = True)
67 discipline_secondaire = models.ForeignKey('Discipline', related_name = \
68 "discipline_secondaire",
69 verbose_name = \
70 "Discipline secondaire",
71 blank = True, null = True)
72 mots_cles = models.TextField('Mots-Clés', blank = True, null = True)
73 type = models.CharField(max_length = 255, choices = \
74 (('Colloque', 'Colloque'),
75 ('Conférence', 'Conférence'),
76 ('Appel à contribution', 'Appel à contribution'),
77 ('Journée d\'étude', 'Journée d\'étude'),
78 (None, 'Autre')
79 )) #TODO: choices
80 fuseau = TimeZoneField(verbose_name = 'Fuseau horaire')
81 debut = models.DateTimeField(default = datetime.datetime.now)
82 fin = models.DateTimeField(default = datetime.datetime.now)
83 lieu = models.TextField()
84 description = models.TextField(blank = True, null = True)
85 #fichiers = TODO?
86 contact = models.TextField(blank = True, null = True)
87 url = models.CharField(max_length=255, blank = True, null = True)
88
020f79a9 89 def __unicode__(self,):
90 return "[%s] %s" % (self.uid, self.titre)
91
b7a741ad 92 def save(self, *args, **kwargs):
93 """Sauvegarde l'objet dans django et le synchronise avec caldav s'il a été
94 approuvé"""
95 self.update_vevent()
96 super(Evenement, self).save(*args, **kwargs)
97
98 # methodes de commnunications avec CALDAV
99 def as_ical(self,):
100 """Retourne l'evenement django sous forme d'objet icalendar"""
101 cal = vobject.iCalendar()
102 cal.add('vevent')
103
104 # fournit son propre uid
7f56d0d4 105 if self.uid in [None, ""]:
b7a741ad 106 self.uid = str(uuid.uuid1())
107
108 cal.vevent.add('uid').value = self.uid
109
110 cal.vevent.add('summary').value = self.titre
111
112 if self.mots_cles is None:
113 kw = []
114 else:
115 kw = self.mots_cles.split(",")
116
117 try:
118 kw.append(self.discipline.nom)
119 kw.append(self.discipline_secondaire.nom)
120 kw.append(self.type)
121 except: pass
122
79b400f0 123 kw = [x.strip() for x in kw if len(x.strip()) > 0 and x is not None]
b7a741ad 124 for k in kw:
125 cal.vevent.add('x-auf-keywords').value = k
126
127 description = self.description
128 if len(kw) > 0:
129 if len(self.description) > 0:
130 description += "\n"
028f548f 131 description += u"Mots-clés: " + ", ".join(kw)
b7a741ad 132
133 cal.vevent.add('dtstart').value = combine(self.debut, self.fuseau)
134 cal.vevent.add('dtend').value = combine(self.fin, self.fuseau)
135 cal.vevent.add('created').value = combine(datetime.datetime.now(), "UTC")
136 cal.vevent.add('dtstamp').value = combine(datetime.datetime.now(), "UTC")
79b400f0 137 if len(description) > 0:
b7a741ad 138 cal.vevent.add('description').value = description
139 if len(self.contact) > 0:
140 cal.vevent.add('contact').value = self.contact
141 if len(self.url) > 0:
142 cal.vevent.add('url').value = self.url
143 if len(self.lieu) > 0:
144 cal.vevent.add('location').value = self.lieu
145 return cal
146
147 def update_vevent(self,):
148 """Essaie de créer l'évènement sur le serveur ical.
149 En cas de succès, l'évènement local devient donc inactif et approuvé"""
150 try:
151 if self.approuve:
152 event = self.as_ical()
153 client = caldav.DAVClient(CALENDRIER_URL)
154 cal = caldav.Calendar(client, url = CALENDRIER_URL)
155 e = caldav.Event(client, parent = cal, data = event.serialize(), id=self.uid)
156 e.save()
157 except:
158 self.approuve = False
159
160 def delete_vevent(self,):
161 """Supprime l'evenement sur le serveur caldav"""
162 try:
163 if self.approuve:
164 event = self.as_ical()
165 client = caldav.DAVClient(CALENDRIER_URL)
166 cal = caldav.Calendar(client, url = CALENDRIER_URL)
167 e = cal.event(self.uid)
168 e.delete()
169 except error.NotFoundError:
170 pass
171
172
173# Surcharge du comportement de suppression
174# La méthode de connexion par signals est préférable à surcharger la méthode delete()
175# car dans le cas de la suppression par lots, cell-ci n'est pas invoquée
176def delete_vevent(sender, instance, *args, **kwargs):
177 instance.delete_vevent()
178
179pre_delete.connect(delete_vevent, sender = Evenement)
180
181
d972b61d 182class ListSet(models.Model):
183 spec = models.CharField(primary_key = True, max_length = 255)
184 name = models.CharField(max_length = 255)
185 server = models.CharField(max_length = 255)
9eda5d6c 186 validated = models.BooleanField(default = True)
d972b61d 187
10d37e44 188 def __unicode__(self,):
189 return self.name
190
0cc5f772 191class Record(models.Model):
23b5b3d5 192
193 #fonctionnement interne
0cc5f772 194 id = models.AutoField(primary_key = True)
23b5b3d5 195 server = models.CharField(max_length = 255)
196 last_update = models.CharField(max_length = 255)
197 last_checksum = models.CharField(max_length = 255)
c88d78dc 198 validated = models.BooleanField(default = True)
23b5b3d5 199
200 #OAI
201 title = models.TextField(null = True, blank = True)
202 creator = models.TextField(null = True, blank = True)
203 description = models.TextField(null = True, blank = True)
204 modified = models.CharField(max_length = 255, null = True, blank = True)
205 identifier = models.CharField(max_length = 255, null = True, blank = True, unique = True)
206 uri = models.CharField(max_length = 255, null = True, blank = True, unique = True)
207 source = models.TextField(null = True, blank = True)
208 contributor = models.TextField(null = True, blank = True)
209 subject = models.TextField(null = True, blank = True)
210 publisher = models.TextField(null = True, blank = True)
211 type = models.TextField(null = True, blank = True)
212 format = models.TextField(null = True, blank = True)
213 language = models.TextField(null = True, blank = True)
da9020f3 214
c88d78dc 215 listsets = models.ManyToManyField(ListSet, null = True, blank = True)
d972b61d 216
da9020f3 217 #SEP 2 (aucune données récoltées)
23b5b3d5 218 alt_title = models.TextField(null = True, blank = True)
219 abstract = models.TextField(null = True, blank = True)
220 creation = models.CharField(max_length = 255, null = True, blank = True)
221 issued = models.CharField(max_length = 255, null = True, blank = True)
222 isbn = models.TextField(null = True, blank = True)
223 orig_lang = models.TextField(null = True, blank = True)
da9020f3 224
225 # Metadata AUF multivaluées
226 disciplines = models.ManyToManyField(Discipline)
227 thematiques = models.ManyToManyField(Thematique)
e3c3296e 228 pays = models.ManyToManyField(Pays)
229 regions = models.ManyToManyField(Region)
0cc5f772 230
6d885e0c 231 def est_complet(self,):
232 """teste si le record à toutes les données obligatoires"""
233 return self.disciplines.count() > 0 and \
234 self.thematiques.count() > 0 and \
235 self.pays.count() > 0 and \
236 self.regions.count() > 0
237
0cc5f772 238 def __unicode__(self):
f554ef70 239 return "[%s] %s" % (self.server, self.title)
da9020f3 240
6d885e0c 241class Serveur(models.Model):
b7a741ad 242 """Identification d'un serveur d'ou proviennent les références"""
6d885e0c 243 nom = models.CharField(primary_key = True, max_length = 255)
244
245 def __unicode__(self,):
246 return self.nom
247
248 def conf_2_db(self,):
249 for k in RESOURCES.keys():
250 s, created = Serveur.objects.get_or_create(nom=k)
251 s.nom = k
252 s.save()
253
254class Profile(models.Model):
255 user = models.ForeignKey(User, unique=True)
256 serveurs = models.ManyToManyField(Serveur, null = True, blank = True)
0cc5f772
CR
257
258class HarvestLog(models.Model):
23b5b3d5 259 context = models.CharField(max_length = 255)
260 name = models.CharField(max_length = 255)
0cc5f772 261 date = models.DateTimeField(auto_now = True)
23b5b3d5 262 added = models.IntegerField(null = True, blank = True)
263 updated = models.IntegerField(null = True, blank = True)
a85ba76e 264 processed = models.IntegerField(null = True, blank = True)
23b5b3d5 265 record = models.ForeignKey(Record, null = True, blank = True)
266
267 @staticmethod
268 def add(message):
269 logger = HarvestLog()
270 if message.has_key('record_id'):
271 message['record'] = Record.objects.get(id=message['record_id'])
272 del(message['record_id'])
273
274 for k,v in message.items():
275 setattr(logger, k, v)
276 logger.save()