Commit | Line | Data |
---|---|---|
92c7413b | 1 | # -*- encoding: utf-8 -*- |
6d885e0c | 2 | from django.contrib.auth.models import User |
d15017b2 | 3 | from django.db import models |
6d885e0c | 4 | from django.db.models.signals import post_save |
da9020f3 | 5 | import simplejson |
92c7413b CR |
6 | import uuid, datetime |
7 | from timezones.fields import TimeZoneField | |
6d885e0c | 8 | from auf_savoirs_en_partage.backend_config import RESOURCES |
da9020f3 | 9 | from savoirs.globals import META |
e3c3296e | 10 | from datamaster_modeles.models import Thematique, Pays, Region |
d15017b2 CR |
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') | |
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 |
23 | class SourceActualite(models.Model): |
24 | nom = models.CharField(max_length=255) | |
25 | url = models.CharField(max_length=255) | |
26 | ||
d15017b2 | 27 | class 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 | ||
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 | ||
020f79a9 | 80 | def __unicode__(self,): |
81 | return "[%s] %s" % (self.uid, self.titre) | |
82 | ||
d972b61d | 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) | |
9eda5d6c | 87 | validated = models.BooleanField(default = True) |
d972b61d | 88 | |
10d37e44 | 89 | def __unicode__(self,): |
90 | return self.name | |
91 | ||
0cc5f772 | 92 | class Record(models.Model): |
23b5b3d5 | 93 | |
94 | #fonctionnement interne | |
0cc5f772 | 95 | id = models.AutoField(primary_key = True) |
23b5b3d5 | 96 | server = models.CharField(max_length = 255) |
97 | last_update = models.CharField(max_length = 255) | |
98 | last_checksum = models.CharField(max_length = 255) | |
c88d78dc | 99 | validated = models.BooleanField(default = True) |
23b5b3d5 | 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) | |
da9020f3 | 115 | |
c88d78dc | 116 | listsets = models.ManyToManyField(ListSet, null = True, blank = True) |
d972b61d | 117 | |
da9020f3 | 118 | #SEP 2 (aucune données récoltées) |
23b5b3d5 | 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) | |
da9020f3 | 125 | |
126 | # Metadata AUF multivaluées | |
127 | disciplines = models.ManyToManyField(Discipline) | |
128 | thematiques = models.ManyToManyField(Thematique) | |
e3c3296e | 129 | pays = models.ManyToManyField(Pays) |
130 | regions = models.ManyToManyField(Region) | |
0cc5f772 | 131 | |
6d885e0c | 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 | ||
d972b61d | 139 | |
0cc5f772 | 140 | def __unicode__(self): |
23b5b3d5 | 141 | return "R[%s] %s" % (self.id, self.title) |
da9020f3 | 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. | |
23b5b3d5 | 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) | |
da9020f3 | 171 | |
6d885e0c | 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) | |
0cc5f772 CR |
187 | |
188 | class HarvestLog(models.Model): | |
23b5b3d5 | 189 | context = models.CharField(max_length = 255) |
190 | name = models.CharField(max_length = 255) | |
0cc5f772 | 191 | date = models.DateTimeField(auto_now = True) |
23b5b3d5 | 192 | added = models.IntegerField(null = True, blank = True) |
193 | updated = models.IntegerField(null = True, blank = True) | |
a85ba76e | 194 | processed = models.IntegerField(null = True, blank = True) |
23b5b3d5 | 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() |