| author | ymh <ymh.work@gmail.com> |
| Sat, 25 Jan 2020 02:09:46 +0100 | |
| changeset 381 | f53e667ab25f |
| parent 334 | 169b7cfd1f58 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# -*- coding: utf-8 -*- |
| 334 | 2 |
# |
3 |
# Copyright Institut de Recherche et d'Innovation © 2014 |
|
4 |
# |
|
5 |
# contact@iri.centrepompidou.fr |
|
6 |
# |
|
7 |
# Ce code a été développé pour un premier usage dans JocondeLab, projet du |
|
8 |
# ministère de la culture et de la communication visant à expérimenter la |
|
9 |
# recherche sémantique dans la base Joconde |
|
10 |
# (http://jocondelab.iri-research.org/). |
|
11 |
# |
|
12 |
# Ce logiciel est régi par la licence CeCILL-C soumise au droit français et |
|
13 |
# respectant les principes de diffusion des logiciels libres. Vous pouvez |
|
14 |
# utiliser, modifier et/ou redistribuer ce programme sous les conditions |
|
15 |
# de la licence CeCILL-C telle que diffusée par le CEA, le CNRS et l'INRIA |
|
16 |
# sur le site "http://www.cecill.info". |
|
17 |
# |
|
18 |
# En contrepartie de l'accessibilité au code source et des droits de copie, |
|
19 |
# de modification et de redistribution accordés par cette licence, il n'est |
|
20 |
# offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, |
|
21 |
# seule une responsabilité restreinte pèse sur l'auteur du programme, le |
|
22 |
# titulaire des droits patrimoniaux et les concédants successifs. |
|
23 |
# |
|
24 |
# A cet égard l'attention de l'utilisateur est attirée sur les risques |
|
25 |
# associés au chargement, à l'utilisation, à la modification et/ou au |
|
26 |
# développement et à la reproduction du logiciel par l'utilisateur étant |
|
27 |
# donné sa spécificité de logiciel libre, qui peut le rendre complexe à |
|
28 |
# manipuler et qui le réserve donc à des développeurs et des professionnels |
|
29 |
# avertis possédant des connaissances informatiques approfondies. Les |
|
30 |
# utilisateurs sont donc invités à charger et tester l'adéquation du |
|
31 |
# logiciel à leurs besoins dans des conditions permettant d'assurer la |
|
32 |
# sécurité de leurs systèmes et ou de leurs données et, plus généralement, |
|
33 |
# à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. |
|
34 |
# |
|
35 |
# Le fait que vous puissiez accéder à cet en-tête signifie que vous avez |
|
36 |
# pris connaissance de la licence CeCILL-C, et que vous en avez accepté les |
|
37 |
# termes. |
|
38 |
# |
|
| 0 | 39 |
''' |
40 |
Created on Jun 8, 2013 |
|
41 |
||
42 |
@author: ymh |
|
43 |
''' |
|
44 |
||
|
91
3bbf7371378a
Model reorganization for user + migration.
ymh <ymh.work@gmail.com>
parents:
83
diff
changeset
|
45 |
from core import settings |
| 15 | 46 |
from django.db import models |
47 |
from django.utils.translation import ugettext as _ |
|
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
48 |
from mptt.models import MPTTModel, TreeForeignKey |
| 0 | 49 |
import datetime |
50 |
import logging |
|
|
91
3bbf7371378a
Model reorganization for user + migration.
ymh <ymh.work@gmail.com>
parents:
83
diff
changeset
|
51 |
from core.models import User |
| 0 | 52 |
|
53 |
logger = logging.getLogger(__name__) |
|
54 |
||
55 |
||
56 |
TERM_URL_STATUS_CHOICES = ( |
|
57 |
(0, "null_result"), |
|
58 |
(1, "redirection"), |
|
59 |
(2, "homonyme"), |
|
60 |
(3, "match"), |
|
61 |
(4, "unsematized"), |
|
62 |
) |
|
63 |
||
| 15 | 64 |
TERM_URL_STATUS_CHOICES_TRANS = ( |
65 |
(0, _("null_result")), |
|
66 |
(1, _("redirection")), |
|
67 |
(2, _("homonyme")), |
|
68 |
(3, _("match")), |
|
69 |
(4, _("unsematized")), |
|
70 |
) |
|
71 |
||
| 0 | 72 |
TERM_URL_STATUS_DICT = { |
73 |
"null_result":0, |
|
74 |
"redirection":1, |
|
75 |
"homonyme":2, |
|
76 |
"match":3, |
|
77 |
"unsemantized":4, |
|
78 |
} |
|
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
79 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
80 |
TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES = ( |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
81 |
(0, "--"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
82 |
(1, "EE"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
83 |
(2, "EI"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
84 |
(3, "BM"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
85 |
(4, "NM") |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
86 |
) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
87 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
88 |
TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS = ( |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
89 |
(0, _("--")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
90 |
(1, _("EE")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
91 |
(2, _("EI")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
92 |
(3, _("BM")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
93 |
(4, _("NM")) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
94 |
) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
95 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
96 |
TERM_WK_LINK_SEMANTIC_LEVEL_DICT = { |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
97 |
"--" : 0, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
98 |
"EE" : 1, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
99 |
"EI" : 2, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
100 |
"BM" : 3, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
101 |
"NM" : 4 |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
102 |
} |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
103 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
104 |
|
| 0 | 105 |
class Thesaurus(models.Model): |
106 |
label = models.CharField(max_length=128, unique=True, blank=False, null=False, db_index=True) |
|
107 |
title = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=False) |
|
108 |
description = models.TextField(blank=True, null=True) |
|
109 |
uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
110 |
||
111 |
class Meta: |
|
112 |
app_label = 'core' |
|
113 |
ordering = ['label'] |
|
114 |
||
115 |
def __unicode__(self): |
|
116 |
return self.label |
|
117 |
||
118 |
||
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
119 |
class Term(MPTTModel): |
| 0 | 120 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
121 |
lang = models.CharField(max_length=128, unique=False, blank=True, null=True, db_index=True) |
|
122 |
uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
123 |
normalized_label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True, editable=False) |
|
124 |
wp_label = models.CharField(max_length=1024, unique=False, blank=True, null=True, db_index=True) |
|
125 |
wp_alternative_label = models.CharField(max_length=1024, unique=False, blank=True, null=True, db_index=True) |
|
126 |
thesaurus = models.ForeignKey(Thesaurus, blank=False, null=False, db_index=True) |
|
127 |
created_at = models.DateTimeField(auto_now_add=True) |
|
128 |
wikipedia_url = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
129 |
wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True, db_index=True) |
|
130 |
wikipedia_revision_id = models.BigIntegerField(unique=False, blank=True, null=True) |
|
131 |
alternative_wikipedia_url = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
132 |
alternative_wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True, db_index=True) |
|
| 15 | 133 |
url_status = models.IntegerField(choices=TERM_URL_STATUS_CHOICES_TRANS, blank=True, null=True, default=None, db_index=True) |
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
134 |
link_semantic_level = models.IntegerField(choices=TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS, blank=True, null=True, default=None, db_index=True) |
| 0 | 135 |
dbpedia_uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
136 |
validation_date = models.DateTimeField(null=True, blank=True, serialize=False) |
|
137 |
validated = models.BooleanField(default=False, db_index=True) |
|
138 |
validator = models.ForeignKey(User, null=True, blank=True, serialize=False) |
|
|
48
f4fadc1b9d70
cache the nb_notice query to improve perf.
ymh <ymh.work@gmail.com>
parents:
35
diff
changeset
|
139 |
wikipedia_edition = models.BooleanField(default=False, blank=False, null=False) |
|
f4fadc1b9d70
cache the nb_notice query to improve perf.
ymh <ymh.work@gmail.com>
parents:
35
diff
changeset
|
140 |
|
|
f4fadc1b9d70
cache the nb_notice query to improve perf.
ymh <ymh.work@gmail.com>
parents:
35
diff
changeset
|
141 |
nb_notice = models.IntegerField(blank=False, null=False, default=0, db_index=True, editable=False) |
|
55
bcbd95da9be2
add preview of notices + add missing img for wp
ymh <ymh.work@gmail.com>
parents:
48
diff
changeset
|
142 |
notices = models.ManyToManyField('core.Notice', related_name="terms+", through="core.NoticeTerm") |
|
146
b5ce341745e3
Added nb_illustrated_notice and changed queries for keywords
veltr
parents:
110
diff
changeset
|
143 |
nb_illustrated_notice = models.IntegerField(blank=False, null=False, default=0, db_index=True, editable=False) |
| 0 | 144 |
|
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
145 |
parent = TreeForeignKey('self', null=True, blank=True, related_name='children') |
|
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
146 |
|
| 0 | 147 |
@property |
|
83
4cdef872c351
Amélioration du parcours et de la sélection dans les arbres de thésaurus
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
148 |
def children_with_descendants(self): |
|
4cdef872c351
Amélioration du parcours et de la sélection dans les arbres de thésaurus
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
149 |
return self.children.extra(where=['(rght-lft) > 1']) |
|
4cdef872c351
Amélioration du parcours et de la sélection dans les arbres de thésaurus
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
150 |
|
|
4cdef872c351
Amélioration du parcours et de la sélection dans les arbres de thésaurus
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
151 |
@property |
| 0 | 152 |
def alternative_labels_str(self): |
|
26
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
153 |
return " | ".join([l.label for l in self.alternative_labels.all() if l.label != self.label]) |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
154 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
155 |
@property |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
156 |
def alternative_labels_list(self): |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
157 |
return [l.label for l in self.alternative_labels.all() if l.label != self.label] |
| 0 | 158 |
|
159 |
@property |
|
| 67 | 160 |
def wikipedia_language_version(self): |
161 |
if not self.wikipedia_url: |
|
162 |
return None |
|
163 |
for lang, urls in settings.WIKIPEDIA_URLS.iteritems(): # @UndefinedVariable |
|
164 |
if self.wikipedia_url.startswith(urls['base_url']): |
|
165 |
return lang |
|
166 |
return None |
|
167 |
||
168 |
@property |
|
| 0 | 169 |
def wikipedia_revision_permalink(self): |
| 67 | 170 |
tmpl_str = settings.WIKIPEDIA_URLS.get(self.wikipedia_language_version, {}).get('permalink_tmpl',None) # @UndefinedVariable |
171 |
if tmpl_str: |
|
172 |
return tmpl_str % (unicode(self.wikipedia_revision_id)) |
|
173 |
else: |
|
174 |
return None |
|
|
24
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
175 |
|
|
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
176 |
@property |
|
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
177 |
def url_status_text(self): |
|
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
178 |
return TERM_URL_STATUS_CHOICES[self.url_status][1] |
| 0 | 179 |
|
180 |
@property |
|
|
24
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
181 |
def url_status_text_trans(self): |
| 15 | 182 |
return TERM_URL_STATUS_CHOICES_TRANS[self.url_status][1] |
|
62
33fd91a414cc
selection dialog for thesaurus tree
ymh <ymh.work@gmail.com>
parents:
61
diff
changeset
|
183 |
|
| 0 | 184 |
def validate(self, user): |
185 |
if not self.validated: |
|
186 |
self.validation_date = datetime.datetime.utcnow() |
|
187 |
self.validated = True |
|
188 |
self.validator = user |
|
189 |
self.save() |
|
190 |
||
191 |
def unvalidate(self): |
|
192 |
if self.validated: |
|
193 |
self.validated = False |
|
194 |
self.validator = None |
|
195 |
self.validation_date = None |
|
196 |
self.save() |
|
197 |
||
| 110 | 198 |
def __unicode__(self): |
199 |
return self.label |
|
200 |
||
| 0 | 201 |
class Meta: |
202 |
app_label = 'core' |
|
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
203 |
|
|
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
204 |
class MPTTMeta: |
|
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
205 |
order_insertion_by = ['normalized_label'] |
| 0 | 206 |
|
207 |
class TermLabel(models.Model): |
|
208 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
|
209 |
lang = models.CharField(max_length=128, unique=False, blank=True, null=True, db_index=True) |
|
210 |
term = models.ForeignKey(Term, blank=False, null=False, db_index=True, related_name="alternative_labels") |
|
211 |
||
212 |
class Meta: |
|
213 |
app_label = 'core' |