ferme #460 ajout du dashboard pour connaitre les references à traiter
[auf_savoirs_en_partage_django.git] / auf_savoirs_en_partage / savoirs / models.py
CommitLineData
92c7413b 1# -*- encoding: utf-8 -*-
6d885e0c 2from django.contrib.auth.models import User
d15017b2 3from django.db import models
6d885e0c 4from django.db.models.signals import post_save
da9020f3 5import simplejson
92c7413b
CR
6import uuid, datetime
7from timezones.fields import TimeZoneField
6d885e0c 8from auf_savoirs_en_partage.backend_config import RESOURCES
da9020f3 9from savoirs.globals import META
e3c3296e 10from datamaster_modeles.models import Thematique, Pays, Region
d15017b2
CR
11
12class 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')
6ef8ead4
CR
15
16 def __unicode__ (self):
92c7413b 17 return self.nom
6ef8ead4 18
d15017b2
CR
19 class Meta:
20 db_table = u'discipline'
21 ordering = ["nom",]
22
79c398f6
CR
23class SourceActualite(models.Model):
24 nom = models.CharField(max_length=255)
25 url = models.CharField(max_length=255)
26
d15017b2 27class Actualite(models.Model):
4f262f90 28 id = models.AutoField(primary_key=True, db_column='id_actualite')
d15017b2
CR
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')
40a5ebfb 35 ancienid = models.IntegerField(db_column='ancienId_actualite')
6ef8ead4
CR
36
37 def __unicode__ (self):
38 return "Actualite %d: %s" % (self.id, self.titre)
39
d15017b2
CR
40 class Meta:
41 db_table = u'actualite'
42 ordering = ["-date",]
92c7413b
CR
43
44
45class ActiveManager(models.Manager):
46 def get_query_set(self):
47 return super(ActiveManager, self).get_query_set().filter(actif=True)
48
49class 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
d972b61d 80class 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)
9eda5d6c 84 validated = models.BooleanField(default = True)
d972b61d 85
10d37e44 86 def __unicode__(self,):
87 return self.name
88
0cc5f772 89class Record(models.Model):
23b5b3d5 90
91 #fonctionnement interne
0cc5f772 92 id = models.AutoField(primary_key = True)
23b5b3d5 93 server = models.CharField(max_length = 255)
94 last_update = models.CharField(max_length = 255)
95 last_checksum = models.CharField(max_length = 255)
c88d78dc 96 validated = models.BooleanField(default = True)
23b5b3d5 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)
da9020f3 112
c88d78dc 113 listsets = models.ManyToManyField(ListSet, null = True, blank = True)
d972b61d 114
da9020f3 115 #SEP 2 (aucune données récoltées)
23b5b3d5 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)
da9020f3 122
123 # Metadata AUF multivaluées
124 disciplines = models.ManyToManyField(Discipline)
125 thematiques = models.ManyToManyField(Thematique)
e3c3296e 126 pays = models.ManyToManyField(Pays)
127 regions = models.ManyToManyField(Region)
0cc5f772 128
6d885e0c 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
d972b61d 136
0cc5f772 137 def __unicode__(self):
23b5b3d5 138 return "R[%s] %s" % (self.id, self.title)
da9020f3 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.
23b5b3d5 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)
da9020f3 168
6d885e0c 169class 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
181class Profile(models.Model):
182 user = models.ForeignKey(User, unique=True)
183 serveurs = models.ManyToManyField(Serveur, null = True, blank = True)
0cc5f772
CR
184
185class HarvestLog(models.Model):
23b5b3d5 186 context = models.CharField(max_length = 255)
187 name = models.CharField(max_length = 255)
0cc5f772 188 date = models.DateTimeField(auto_now = True)
23b5b3d5 189 added = models.IntegerField(null = True, blank = True)
190 updated = models.IntegerField(null = True, blank = True)
a85ba76e 191 processed = models.IntegerField(null = True, blank = True)
23b5b3d5 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()