fournit un evenement lisible
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / models.py
1 # -*- encoding: utf-8 -*-
2 from django.contrib.auth.models import User
3 from django.db import models
4 from django.db.models.signals import post_save
5 import simplejson
6 import uuid, datetime
7 from timezones.fields import TimeZoneField
8 from auf_savoirs_en_partage.backend_config import RESOURCES
9 from savoirs.globals import META
10 from datamaster_modeles.models import Thematique, Pays, Region
11
12 class Discipline(models.Model):
13 id = models.IntegerField(primary_key=True, db_column='id_discipline')
14 nom = models.CharField(max_length=765, db_column='nom_discipline')
15
16 def __unicode__ (self):
17 return self.nom
18
19 class Meta:
20 db_table = u'discipline'
21 ordering = ["nom",]
22
23 class SourceActualite(models.Model):
24 nom = models.CharField(max_length=255)
25 url = models.CharField(max_length=255)
26
27 class Actualite(models.Model):
28 id = models.AutoField(primary_key=True, db_column='id_actualite')
29 titre = models.CharField(max_length=765, db_column='titre_actualite')
30 texte = models.TextField(db_column='texte_actualite')
31 url = models.CharField(max_length=765, db_column='url_actualite')
32 logo = models.CharField(max_length=765, db_column='logo_actualite')
33 date = models.DateField(db_column='date_actualite')
34 visible = models.CharField(max_length=3, db_column='visible_actualite')
35 ancienid = models.IntegerField(db_column='ancienId_actualite')
36
37 def __unicode__ (self):
38 return "Actualite %d: %s" % (self.id, self.titre)
39
40 class Meta:
41 db_table = u'actualite'
42 ordering = ["-date",]
43
44
45 class ActiveManager(models.Manager):
46 def get_query_set(self):
47 return super(ActiveManager, self).get_query_set().filter(actif=True)
48
49 class Evenement(models.Model):
50 actif = models.BooleanField(default = True)
51 uid = models.CharField(max_length = 255, default = uuid.uuid1)
52 approuve = models.BooleanField(default = False)
53 titre = models.CharField(max_length=255)
54 discipline = models.ForeignKey('Discipline', related_name = "discipline",
55 blank = True, null = True)
56 discipline_secondaire = models.ForeignKey('Discipline', related_name = \
57 "discipline_secondaire",
58 verbose_name = \
59 "Discipline secondaire",
60 blank = True, null = True)
61 mots_cles = models.TextField('Mots-Clés', blank = True, null = True)
62 type = models.CharField(max_length = 255, choices = \
63 (('Colloque', 'Colloque'),
64 ('Conférence', 'Conférence'),
65 ('Appel à contribution', 'Appel à contribution'),
66 ('Journée d\'étude', 'Journée d\'étude'),
67 (None, 'Autre')
68 )) #TODO: choices
69 fuseau = TimeZoneField(verbose_name = 'Fuseau horaire')
70 debut = models.DateTimeField(default = datetime.datetime.now)
71 fin = models.DateTimeField(default = datetime.datetime.now)
72 lieu = models.TextField()
73 description = models.TextField(blank = True, null = True)
74 #fichiers = TODO?
75 contact = models.TextField(blank = True, null = True)
76 url = models.CharField(max_length=255, blank = True, null = True)
77
78 objects = ActiveManager()
79
80 def __unicode__(self,):
81 return "[%s] %s" % (self.uid, self.titre)
82
83 class ListSet(models.Model):
84 spec = models.CharField(primary_key = True, max_length = 255)
85 name = models.CharField(max_length = 255)
86 server = models.CharField(max_length = 255)
87 validated = models.BooleanField(default = True)
88
89 def __unicode__(self,):
90 return self.name
91
92 class Record(models.Model):
93
94 #fonctionnement interne
95 id = models.AutoField(primary_key = True)
96 server = models.CharField(max_length = 255)
97 last_update = models.CharField(max_length = 255)
98 last_checksum = models.CharField(max_length = 255)
99 validated = models.BooleanField(default = True)
100
101 #OAI
102 title = models.TextField(null = True, blank = True)
103 creator = models.TextField(null = True, blank = True)
104 description = models.TextField(null = True, blank = True)
105 modified = models.CharField(max_length = 255, null = True, blank = True)
106 identifier = models.CharField(max_length = 255, null = True, blank = True, unique = True)
107 uri = models.CharField(max_length = 255, null = True, blank = True, unique = True)
108 source = models.TextField(null = True, blank = True)
109 contributor = models.TextField(null = True, blank = True)
110 subject = models.TextField(null = True, blank = True)
111 publisher = models.TextField(null = True, blank = True)
112 type = models.TextField(null = True, blank = True)
113 format = models.TextField(null = True, blank = True)
114 language = models.TextField(null = True, blank = True)
115
116 listsets = models.ManyToManyField(ListSet, null = True, blank = True)
117
118 #SEP 2 (aucune données récoltées)
119 alt_title = models.TextField(null = True, blank = True)
120 abstract = models.TextField(null = True, blank = True)
121 creation = models.CharField(max_length = 255, null = True, blank = True)
122 issued = models.CharField(max_length = 255, null = True, blank = True)
123 isbn = models.TextField(null = True, blank = True)
124 orig_lang = models.TextField(null = True, blank = True)
125
126 # Metadata AUF multivaluées
127 disciplines = models.ManyToManyField(Discipline)
128 thematiques = models.ManyToManyField(Thematique)
129 pays = models.ManyToManyField(Pays)
130 regions = models.ManyToManyField(Region)
131
132 def est_complet(self,):
133 """teste si le record à toutes les données obligatoires"""
134 return self.disciplines.count() > 0 and \
135 self.thematiques.count() > 0 and \
136 self.pays.count() > 0 and \
137 self.regions.count() > 0
138
139
140 def __unicode__(self):
141 return "R[%s] %s" % (self.id, self.title)
142
143 # Ces fonctions sont utilisées pour travailler directement sur les données JSON enregistrées tel quel
144 # sur la base de données. Lorsque le modèle est initialisé, les fields sont décodés, et lorsque l'objet
145 # est sauvegardé, on s'assure de remettre les données encodées en JSON.
146 # TODO : a terme, les données ne seront plus stockées au format JSON dans la BD et ces fonctions seront
147 # donc obsolètes.
148 #
149 # def save(self, *args, **kwargs):
150 #
151 # for field_name in [f for f in self._meta.get_all_field_names() if f in META.keys()]:
152 # v = getattr (self, field_name, None)
153 # setattr(self, field_name, simplejson.dumps(v))
154 #
155 # super(Record, self).save(*args, **kwargs)
156 #
157 #def decode_json(instance, **kwargs):
158 # for field_name in [f for f in instance._meta.get_all_field_names() if f in META.keys()]:
159 # json = getattr(instance, field_name)
160 # data = "-"
161 # v = getattr (instance, field_name, None)
162 # if v is not None:
163 # data = simplejson.loads(v)
164 # if not isinstance(data, basestring):
165 # decoded_value = u",".join(data)
166 # else:
167 # decoded_value = data
168 # setattr(instance, field_name, decoded_value)
169 #
170 #models.signals.post_init.connect(decode_json, Record)
171
172 class Serveur(models.Model):
173 nom = models.CharField(primary_key = True, max_length = 255)
174
175 def __unicode__(self,):
176 return self.nom
177
178 def conf_2_db(self,):
179 for k in RESOURCES.keys():
180 s, created = Serveur.objects.get_or_create(nom=k)
181 s.nom = k
182 s.save()
183
184 class Profile(models.Model):
185 user = models.ForeignKey(User, unique=True)
186 serveurs = models.ManyToManyField(Serveur, null = True, blank = True)
187
188 class HarvestLog(models.Model):
189 context = models.CharField(max_length = 255)
190 name = models.CharField(max_length = 255)
191 date = models.DateTimeField(auto_now = True)
192 added = models.IntegerField(null = True, blank = True)
193 updated = models.IntegerField(null = True, blank = True)
194 processed = models.IntegerField(null = True, blank = True)
195 record = models.ForeignKey(Record, null = True, blank = True)
196
197 @staticmethod
198 def add(message):
199 logger = HarvestLog()
200 if message.has_key('record_id'):
201 message['record'] = Record.objects.get(id=message['record_id'])
202 del(message['record_id'])
203
204 for k,v in message.items():
205 setattr(logger, k, v)
206 logger.save()