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