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