|
47
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
|
|
66
|
3 |
from django.conf import settings |
|
47
|
4 |
from django.contrib.auth.models import User |
|
|
5 |
from django.db import models |
|
|
6 |
from hdabo.fields import SortedManyToManyField |
|
72
|
7 |
from hdabo.utils import Property, normalize |
|
47
|
8 |
import datetime |
|
|
9 |
|
|
|
10 |
class Organisation(models.Model): |
|
|
11 |
hda_id = models.CharField(max_length=512, unique=True, blank=False, null=False) |
|
|
12 |
name = models.CharField(max_length=512, unique=False, blank=False, null=False) |
|
|
13 |
location = models.CharField(max_length=512, unique=False, blank=True, null=True) |
|
|
14 |
website = models.CharField(max_length=2048, unique=False, blank=True, null=True) |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
class Author(models.Model): |
|
|
18 |
hda_id = models.CharField(max_length=512, unique=True, blank=False, null=False) |
|
|
19 |
lastname = models.CharField(max_length=512, unique=False, blank=True, null=True) |
|
|
20 |
firstname = models.CharField(max_length=512, unique=False, blank=True, null=True) |
|
|
21 |
|
|
|
22 |
class TimePeriod(models.Model): |
|
|
23 |
TIME_PERIOD_CHOICES = ( |
|
|
24 |
(1, u'Primaire'), |
|
|
25 |
(2, u'Collège'), |
|
|
26 |
(3, u'Lycée'), |
|
|
27 |
) |
|
|
28 |
TIME_PERIOD_DICT = { |
|
|
29 |
u'Primaire': 1, |
|
|
30 |
u'Collège': 2, |
|
|
31 |
u'Lycée': 3, |
|
|
32 |
} |
|
|
33 |
label = models.CharField(max_length=512, unique=False, blank=False, null=False) |
|
|
34 |
school_period = models.IntegerField(choices=TIME_PERIOD_CHOICES) |
|
|
35 |
|
|
|
36 |
class Meta: |
|
|
37 |
unique_together = ("label", "school_period") |
|
|
38 |
|
|
|
39 |
def __unicode__(self): |
|
|
40 |
return unicode(self.label) |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
class Domain(models.Model): |
|
|
44 |
DOMAIN_PERIOD_CHOICES = ( |
|
|
45 |
(0, u'Global'), |
|
|
46 |
(1, u'Primaire'), |
|
|
47 |
(2, u'Collège'), |
|
|
48 |
(3, u'Lycée'), |
|
|
49 |
) |
|
|
50 |
DOMAIN_PERIOD_DICT = { |
|
|
51 |
u'Global': 0, |
|
|
52 |
u'Primaire': 1, |
|
|
53 |
u'Collège': 2, |
|
|
54 |
u'Lycée': 3, |
|
|
55 |
} |
|
|
56 |
label = models.CharField(max_length=512, unique=False, blank=False, null=False) |
|
|
57 |
school_period = models.IntegerField(choices=DOMAIN_PERIOD_CHOICES) |
|
|
58 |
|
|
|
59 |
class Meta: |
|
|
60 |
unique_together = ("label", "school_period") |
|
|
61 |
|
|
|
62 |
def __unicode__(self): |
|
|
63 |
return unicode(self.label) |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
class DocumentFormat(models.Model): |
|
|
67 |
label = models.CharField(max_length=512, unique=True, blank=False, null=False) |
|
|
68 |
|
|
|
69 |
def __unicode__(self): |
|
|
70 |
return unicode(self.label) |
|
|
71 |
|
|
|
72 |
class TagCategory(models.Model): |
|
|
73 |
label = models.CharField(max_length=512, unique=True, blank=False, null=False) |
|
|
74 |
|
|
|
75 |
def __unicode__(self): |
|
|
76 |
return unicode(self.label) |
|
|
77 |
|
|
|
78 |
class Meta: |
|
|
79 |
verbose_name_plural = "TagCategories" |
|
|
80 |
|
|
|
81 |
class Tag(models.Model): |
|
|
82 |
TAG_URL_STATUS_CHOICES = ( |
|
|
83 |
(0, "null_result"), |
|
|
84 |
(1, "redirection"), |
|
|
85 |
(2, "homonyme"), |
|
|
86 |
(3, "match"), |
|
|
87 |
) |
|
|
88 |
|
|
|
89 |
TAG_URL_STATUS_DICT = { |
|
|
90 |
"null_result":0, |
|
|
91 |
"redirection":1, |
|
|
92 |
"homonyme":2, |
|
|
93 |
"match":3, |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
|
72
|
97 |
normalized_label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True, editable=False) |
|
69
|
98 |
original_label = models.CharField(max_length=1024, unique=False, blank=False, null=False, editable=False) |
|
47
|
99 |
alias = models.CharField(max_length=1024, unique=False, blank=True, null=True) |
|
|
100 |
category = models.ForeignKey(TagCategory, null=True, blank=True) |
|
|
101 |
wikipedia_url = models.URLField(verify_exists=False, max_length=2048, blank=True, null=True) |
|
|
102 |
wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True) |
|
|
103 |
url_status = models.IntegerField(choices=TAG_URL_STATUS_CHOICES, blank=True, null=True, default=None) |
|
|
104 |
dbpedia_uri = models.URLField(verify_exists=False, max_length=2048, blank=True, null=True) |
|
|
105 |
popularity = models.IntegerField(blank=False, null=False, default=0, db_index=True) |
|
|
106 |
|
|
|
107 |
@Property |
|
|
108 |
def url_status_text(): #@NoSelf |
|
|
109 |
def fget(self): |
|
|
110 |
return self.TAG_URL_STATUS_CHOICES[self.url_status][1] |
|
|
111 |
|
|
72
|
112 |
return locals() |
|
|
113 |
|
|
|
114 |
def save(self, *args, **kwargs): |
|
|
115 |
self.normalized_label = normalize(self.label) |
|
85
|
116 |
super(Tag, self).save(*args, **kwargs) |
|
|
117 |
|
|
|
118 |
class Meta: |
|
|
119 |
unique_together = (('label', 'original_label', 'url_status'),) |
|
47
|
120 |
|
|
|
121 |
class Location(models.Model): |
|
|
122 |
name = models.CharField(max_length=512, unique=False, blank=False, null=False) |
|
|
123 |
insee = models.CharField(max_length=5, unique=True, blank=False, null=False) |
|
|
124 |
|
|
|
125 |
def __unicode__(self): |
|
|
126 |
return unicode("%s : %s" % (self.name, self.insee)) |
|
|
127 |
|
|
|
128 |
class Datasheet(models.Model): |
|
|
129 |
hda_id = models.CharField(max_length=512, unique=True, blank=False, null=False) |
|
|
130 |
author = models.ForeignKey(Author, null=True, blank=True) |
|
|
131 |
organisation = models.ForeignKey(Organisation) |
|
|
132 |
title = models.CharField(max_length=2048, unique=False, blank=False, null=False) |
|
|
133 |
description = models.TextField(blank=True, null=True) |
|
|
134 |
url = models.URLField(verify_exists=False, max_length=2048, blank=True, null=True) |
|
|
135 |
domains = SortedManyToManyField(Domain, limit_choices_to={'school_period':Domain.DOMAIN_PERIOD_DICT[u'Global']}, related_name="datasheets") |
|
|
136 |
primary_periods = SortedManyToManyField(TimePeriod, limit_choices_to={'school_period':TimePeriod.TIME_PERIOD_DICT[u'Primaire']}, related_name="primary_periods_datasheets") |
|
|
137 |
college_periods = SortedManyToManyField(TimePeriod, limit_choices_to={'school_period':TimePeriod.TIME_PERIOD_DICT[u'Collège']}, related_name="college_periods_datasheets") |
|
|
138 |
highschool_periods = SortedManyToManyField(TimePeriod, limit_choices_to={'school_period':TimePeriod.TIME_PERIOD_DICT[u'Lycée']}, related_name="highschool_periods_datasheets") |
|
|
139 |
primary_themes = SortedManyToManyField(Domain, limit_choices_to={'school_period':Domain.DOMAIN_PERIOD_DICT[u'Primaire']}, related_name="primary_themes_datasheets") |
|
|
140 |
college_themes = SortedManyToManyField(Domain, limit_choices_to={'school_period':Domain.DOMAIN_PERIOD_DICT[u'Collège']}, related_name="college_themes_datasheets") |
|
|
141 |
highschool_themes = SortedManyToManyField(Domain, limit_choices_to={'school_period':Domain.DOMAIN_PERIOD_DICT[u'Lycée']}, related_name="highschool_themes_datasheets") |
|
|
142 |
town = models.ForeignKey(Location, null=True, blank=True) |
|
|
143 |
format = models.ForeignKey(DocumentFormat, null=True, blank=True) |
|
|
144 |
original_creation_date = models.DateField() |
|
|
145 |
original_modification_date = models.DateField() |
|
|
146 |
modification_datetime = models.DateTimeField(auto_now=True) |
|
|
147 |
validation_date = models.DateTimeField(null=True, blank=True) |
|
|
148 |
validated = models.BooleanField(default=False, db_index=True) |
|
|
149 |
validator = models.ForeignKey(User, null=True, blank=True) |
|
|
150 |
manual_order = models.BooleanField(default=False, db_index=True) |
|
|
151 |
tags = models.ManyToManyField(Tag, through='TaggedSheet') |
|
|
152 |
|
|
|
153 |
|
|
|
154 |
def validate(self, user): |
|
|
155 |
self.validation_date = datetime.datetime.now() |
|
|
156 |
self.validated = True |
|
|
157 |
self.validator = user |
|
|
158 |
self.save() |
|
|
159 |
|
|
|
160 |
def unvalidate(self): |
|
|
161 |
self.validation_date = datetime.datetime.min |
|
|
162 |
self.validated = False |
|
|
163 |
self.validator = None |
|
|
164 |
self.save() |
|
|
165 |
|
|
|
166 |
@Property |
|
|
167 |
def domains_list(): #@NoSelf |
|
|
168 |
def fget(self): |
|
|
169 |
return [d.label for d in self.domains.all()] |
|
|
170 |
|
|
|
171 |
return locals() |
|
|
172 |
|
|
|
173 |
@Property |
|
|
174 |
def domains_text(): #@NoSelf |
|
|
175 |
def fget(self): |
|
|
176 |
return "; ".join(self.domains_list) |
|
|
177 |
|
|
|
178 |
return locals() |
|
|
179 |
|
|
|
180 |
@Property |
|
|
181 |
def primary_periods_list(): #@NoSelf |
|
|
182 |
def fget(self): |
|
|
183 |
return [d.label for d in self.primary_periods.all()] |
|
|
184 |
|
|
|
185 |
return locals() |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
@Property |
|
|
189 |
def primary_periods_text(): #@NoSelf |
|
|
190 |
def fget(self): |
|
|
191 |
return "; ".join(self.primary_periods_list) |
|
|
192 |
|
|
|
193 |
return locals() |
|
|
194 |
|
|
|
195 |
@Property |
|
|
196 |
def college_periods_list(): #@NoSelf |
|
|
197 |
def fget(self): |
|
|
198 |
return [d.label for d in self.college_periods.all()] |
|
|
199 |
|
|
|
200 |
return locals() |
|
|
201 |
|
|
|
202 |
@Property |
|
|
203 |
def college_periods_text(): #@NoSelf |
|
|
204 |
def fget(self): |
|
|
205 |
return "; ".join(self.college_periods_list) |
|
|
206 |
|
|
|
207 |
return locals() |
|
|
208 |
|
|
|
209 |
@Property |
|
|
210 |
def highschool_periods_list(): #@NoSelf |
|
|
211 |
def fget(self): |
|
|
212 |
return [d.label for d in self.highschool_periods.all()] |
|
|
213 |
|
|
|
214 |
return locals() |
|
|
215 |
|
|
|
216 |
@Property |
|
|
217 |
def highschool_periods_text(): #@NoSelf |
|
|
218 |
def fget(self): |
|
|
219 |
return "; ".join(self.highschool_periods_list) |
|
|
220 |
|
|
|
221 |
return locals() |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
@Property |
|
|
225 |
def primary_themes_list(): #@NoSelf |
|
|
226 |
def fget(self): |
|
|
227 |
return [d.label for d in self.primary_themes.all()] |
|
|
228 |
|
|
|
229 |
return locals() |
|
|
230 |
|
|
|
231 |
|
|
|
232 |
@Property |
|
|
233 |
def primary_themes_text(): #@NoSelf |
|
|
234 |
def fget(self): |
|
|
235 |
return "; ".join(self.primary_themes_list) |
|
|
236 |
|
|
|
237 |
return locals() |
|
|
238 |
|
|
|
239 |
@Property |
|
|
240 |
def college_themes_list(): #@NoSelf |
|
|
241 |
def fget(self): |
|
|
242 |
return [d.label for d in self.college_themes.all()] |
|
|
243 |
|
|
|
244 |
return locals() |
|
|
245 |
|
|
|
246 |
@Property |
|
|
247 |
def college_themes_text(): #@NoSelf |
|
|
248 |
def fget(self): |
|
|
249 |
return "; ".join(self.college_themes_list) |
|
|
250 |
|
|
|
251 |
return locals() |
|
|
252 |
|
|
|
253 |
@Property |
|
|
254 |
def highschool_themes_list(): #@NoSelf |
|
|
255 |
def fget(self): |
|
|
256 |
return [d.label for d in self.highschool_themes.all()] |
|
|
257 |
|
|
|
258 |
return locals() |
|
|
259 |
|
|
|
260 |
@Property |
|
|
261 |
def highschool_themes_text(): #@NoSelf |
|
|
262 |
def fget(self): |
|
|
263 |
return "; ".join(self.highschool_themes_list) |
|
|
264 |
|
|
|
265 |
return locals() |
|
|
266 |
|
|
|
267 |
@Property |
|
|
268 |
def town_text(): #@NoSelf |
|
|
269 |
def fget(self): |
|
|
270 |
return self.town.name if self.town else "" |
|
|
271 |
|
|
|
272 |
return locals() |
|
|
273 |
|
|
|
274 |
@Property |
|
|
275 |
def tags_text(): #@NoSelf |
|
|
276 |
def fget(self): |
|
|
277 |
return "; ".join([t.label for t in self.tags.all()]) |
|
|
278 |
|
|
|
279 |
return locals() |
|
|
280 |
|
|
|
281 |
|
|
|
282 |
class TaggedSheet(models.Model): |
|
|
283 |
datasheet = models.ForeignKey(Datasheet) |
|
|
284 |
tag = models.ForeignKey(Tag) |
|
|
285 |
original_order = models.IntegerField(null=False, blank=False, default=0) |
|
|
286 |
order = models.IntegerField(null=False, blank=False, default=0, db_index=True) |
|
|
287 |
index_note = models.FloatField(null=False, blank=False, default=0.0, db_index=True) |
|
66
|
288 |
wikipedia_revision_id = models.BigIntegerField(unique=False, blank=True, null=True) |
|
|
289 |
|
|
|
290 |
@Property |
|
|
291 |
def wikipedia_verion_permalink(): #@NoSelf |
|
|
292 |
def fget(self): |
|
|
293 |
return settings.WIKIPEDIA_VERSION_PERMALINK_TEMPLATE % (unicode(self.wikipedia_revision_id)) |
|
|
294 |
|
|
|
295 |
return locals() |
|
47
|
296 |
|
|
|
297 |
|