ferme #460 ajout du dashboard pour connaitre les references à traiter
[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 class ListSet(models.Model):
81 spec = models.CharField(primary_key = True, max_length = 255)
82 name = models.CharField(max_length = 255)
83 server = models.CharField(max_length = 255)
84 validated = models.BooleanField(default = True)
85
86 def __unicode__(self,):
87 return self.name
88
89 class Record(models.Model):
90
91 #fonctionnement interne
92 id = models.AutoField(primary_key = True)
93 server = models.CharField(max_length = 255)
94 last_update = models.CharField(max_length = 255)
95 last_checksum = models.CharField(max_length = 255)
96 validated = models.BooleanField(default = True)
97
98 #OAI
99 title = models.TextField(null = True, blank = True)
100 creator = models.TextField(null = True, blank = True)
101 description = models.TextField(null = True, blank = True)
102 modified = models.CharField(max_length = 255, null = True, blank = True)
103 identifier = models.CharField(max_length = 255, null = True, blank = True, unique = True)
104 uri = models.CharField(max_length = 255, null = True, blank = True, unique = True)
105 source = models.TextField(null = True, blank = True)
106 contributor = models.TextField(null = True, blank = True)
107 subject = models.TextField(null = True, blank = True)
108 publisher = models.TextField(null = True, blank = True)
109 type = models.TextField(null = True, blank = True)
110 format = models.TextField(null = True, blank = True)
111 language = models.TextField(null = True, blank = True)
112
113 listsets = models.ManyToManyField(ListSet, null = True, blank = True)
114
115 #SEP 2 (aucune données récoltées)
116 alt_title = models.TextField(null = True, blank = True)
117 abstract = models.TextField(null = True, blank = True)
118 creation = models.CharField(max_length = 255, null = True, blank = True)
119 issued = models.CharField(max_length = 255, null = True, blank = True)
120 isbn = models.TextField(null = True, blank = True)
121 orig_lang = models.TextField(null = True, blank = True)
122
123 # Metadata AUF multivaluées
124 disciplines = models.ManyToManyField(Discipline)
125 thematiques = models.ManyToManyField(Thematique)
126 pays = models.ManyToManyField(Pays)
127 regions = models.ManyToManyField(Region)
128
129 def est_complet(self,):
130 """teste si le record à toutes les données obligatoires"""
131 return self.disciplines.count() > 0 and \
132 self.thematiques.count() > 0 and \
133 self.pays.count() > 0 and \
134 self.regions.count() > 0
135
136
137 def __unicode__(self):
138 return "R[%s] %s" % (self.id, self.title)
139
140 # Ces fonctions sont utilisées pour travailler directement sur les données JSON enregistrées tel quel
141 # sur la base de données. Lorsque le modèle est initialisé, les fields sont décodés, et lorsque l'objet
142 # est sauvegardé, on s'assure de remettre les données encodées en JSON.
143 # TODO : a terme, les données ne seront plus stockées au format JSON dans la BD et ces fonctions seront
144 # donc obsolètes.
145 #
146 # def save(self, *args, **kwargs):
147 #
148 # for field_name in [f for f in self._meta.get_all_field_names() if f in META.keys()]:
149 # v = getattr (self, field_name, None)
150 # setattr(self, field_name, simplejson.dumps(v))
151 #
152 # super(Record, self).save(*args, **kwargs)
153 #
154 #def decode_json(instance, **kwargs):
155 # for field_name in [f for f in instance._meta.get_all_field_names() if f in META.keys()]:
156 # json = getattr(instance, field_name)
157 # data = "-"
158 # v = getattr (instance, field_name, None)
159 # if v is not None:
160 # data = simplejson.loads(v)
161 # if not isinstance(data, basestring):
162 # decoded_value = u",".join(data)
163 # else:
164 # decoded_value = data
165 # setattr(instance, field_name, decoded_value)
166 #
167 #models.signals.post_init.connect(decode_json, Record)
168
169 class Serveur(models.Model):
170 nom = models.CharField(primary_key = True, max_length = 255)
171
172 def __unicode__(self,):
173 return self.nom
174
175 def conf_2_db(self,):
176 for k in RESOURCES.keys():
177 s, created = Serveur.objects.get_or_create(nom=k)
178 s.nom = k
179 s.save()
180
181 class Profile(models.Model):
182 user = models.ForeignKey(User, unique=True)
183 serveurs = models.ManyToManyField(Serveur, null = True, blank = True)
184
185 class HarvestLog(models.Model):
186 context = models.CharField(max_length = 255)
187 name = models.CharField(max_length = 255)
188 date = models.DateTimeField(auto_now = True)
189 added = models.IntegerField(null = True, blank = True)
190 updated = models.IntegerField(null = True, blank = True)
191 processed = models.IntegerField(null = True, blank = True)
192 record = models.ForeignKey(Record, null = True, blank = True)
193
194 @staticmethod
195 def add(message):
196 logger = HarvestLog()
197 if message.has_key('record_id'):
198 message['record'] = Record.objects.get(id=message['record_id'])
199 del(message['record_id'])
200
201 for k,v in message.items():
202 setattr(logger, k, v)
203 logger.save()