| author | veltr |
| Wed, 18 Sep 2013 18:31:29 +0200 | |
| changeset 110 | 597fa9d09973 |
| parent 91 | 3bbf7371378a |
| child 146 | b5ce341745e3 |
| permissions | -rw-r--r-- |
| 0 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Jun 8, 2013 |
|
4 |
||
5 |
@author: ymh |
|
6 |
''' |
|
7 |
||
|
91
3bbf7371378a
Model reorganization for user + migration.
ymh <ymh.work@gmail.com>
parents:
83
diff
changeset
|
8 |
from core import settings |
| 15 | 9 |
from django.db import models |
10 |
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
|
11 |
from mptt.models import MPTTModel, TreeForeignKey |
| 0 | 12 |
import datetime |
13 |
import logging |
|
|
91
3bbf7371378a
Model reorganization for user + migration.
ymh <ymh.work@gmail.com>
parents:
83
diff
changeset
|
14 |
from core.models import User |
| 0 | 15 |
|
16 |
logger = logging.getLogger(__name__) |
|
17 |
||
18 |
||
19 |
TERM_URL_STATUS_CHOICES = ( |
|
20 |
(0, "null_result"), |
|
21 |
(1, "redirection"), |
|
22 |
(2, "homonyme"), |
|
23 |
(3, "match"), |
|
24 |
(4, "unsematized"), |
|
25 |
) |
|
26 |
||
| 15 | 27 |
TERM_URL_STATUS_CHOICES_TRANS = ( |
28 |
(0, _("null_result")), |
|
29 |
(1, _("redirection")), |
|
30 |
(2, _("homonyme")), |
|
31 |
(3, _("match")), |
|
32 |
(4, _("unsematized")), |
|
33 |
) |
|
34 |
||
| 0 | 35 |
TERM_URL_STATUS_DICT = { |
36 |
"null_result":0, |
|
37 |
"redirection":1, |
|
38 |
"homonyme":2, |
|
39 |
"match":3, |
|
40 |
"unsemantized":4, |
|
41 |
} |
|
|
35
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
42 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
43 |
TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES = ( |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
44 |
(0, "--"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
45 |
(1, "EE"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
46 |
(2, "EI"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
47 |
(3, "BM"), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
48 |
(4, "NM") |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
49 |
) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
50 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
51 |
TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS = ( |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
52 |
(0, _("--")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
53 |
(1, _("EE")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
54 |
(2, _("EI")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
55 |
(3, _("BM")), |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
56 |
(4, _("NM")) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
57 |
) |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
58 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
59 |
TERM_WK_LINK_SEMANTIC_LEVEL_DICT = { |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
60 |
"--" : 0, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
61 |
"EE" : 1, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
62 |
"EI" : 2, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
63 |
"BM" : 3, |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
64 |
"NM" : 4 |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
65 |
} |
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
66 |
|
|
859862939996
add qualifier on the wikipedia link
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
67 |
|
| 0 | 68 |
class Thesaurus(models.Model): |
69 |
label = models.CharField(max_length=128, unique=True, blank=False, null=False, db_index=True) |
|
70 |
title = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=False) |
|
71 |
description = models.TextField(blank=True, null=True) |
|
72 |
uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
73 |
||
74 |
class Meta: |
|
75 |
app_label = 'core' |
|
76 |
ordering = ['label'] |
|
77 |
||
78 |
def __unicode__(self): |
|
79 |
return self.label |
|
80 |
||
81 |
||
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
82 |
class Term(MPTTModel): |
| 0 | 83 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
84 |
lang = models.CharField(max_length=128, unique=False, blank=True, null=True, db_index=True) |
|
85 |
uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
86 |
normalized_label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True, editable=False) |
|
87 |
wp_label = models.CharField(max_length=1024, unique=False, blank=True, null=True, db_index=True) |
|
88 |
wp_alternative_label = models.CharField(max_length=1024, unique=False, blank=True, null=True, db_index=True) |
|
89 |
thesaurus = models.ForeignKey(Thesaurus, blank=False, null=False, db_index=True) |
|
90 |
created_at = models.DateTimeField(auto_now_add=True) |
|
91 |
wikipedia_url = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
92 |
wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True, db_index=True) |
|
93 |
wikipedia_revision_id = models.BigIntegerField(unique=False, blank=True, null=True) |
|
94 |
alternative_wikipedia_url = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
95 |
alternative_wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True, db_index=True) |
|
| 15 | 96 |
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
|
97 |
link_semantic_level = models.IntegerField(choices=TERM_WK_LINK_SEMANTIC_LEVEL_CHOICES_TRANS, blank=True, null=True, default=None, db_index=True) |
| 0 | 98 |
dbpedia_uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
99 |
validation_date = models.DateTimeField(null=True, blank=True, serialize=False) |
|
100 |
validated = models.BooleanField(default=False, db_index=True) |
|
101 |
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
|
102 |
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
|
103 |
|
|
f4fadc1b9d70
cache the nb_notice query to improve perf.
ymh <ymh.work@gmail.com>
parents:
35
diff
changeset
|
104 |
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
|
105 |
notices = models.ManyToManyField('core.Notice', related_name="terms+", through="core.NoticeTerm") |
| 0 | 106 |
|
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
107 |
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
|
108 |
|
| 0 | 109 |
@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
|
110 |
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
|
111 |
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
|
112 |
|
|
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
|
113 |
@property |
| 0 | 114 |
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
|
115 |
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
|
116 |
|
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
117 |
@property |
|
758b9289aa9a
add check box to indicate that a wk page should be created
ymh <ymh.work@gmail.com>
parents:
24
diff
changeset
|
118 |
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
|
119 |
return [l.label for l in self.alternative_labels.all() if l.label != self.label] |
| 0 | 120 |
|
121 |
@property |
|
| 67 | 122 |
def wikipedia_language_version(self): |
123 |
if not self.wikipedia_url: |
|
124 |
return None |
|
125 |
for lang, urls in settings.WIKIPEDIA_URLS.iteritems(): # @UndefinedVariable |
|
126 |
if self.wikipedia_url.startswith(urls['base_url']): |
|
127 |
return lang |
|
128 |
return None |
|
129 |
||
130 |
@property |
|
| 0 | 131 |
def wikipedia_revision_permalink(self): |
| 67 | 132 |
tmpl_str = settings.WIKIPEDIA_URLS.get(self.wikipedia_language_version, {}).get('permalink_tmpl',None) # @UndefinedVariable |
133 |
if tmpl_str: |
|
134 |
return tmpl_str % (unicode(self.wikipedia_revision_id)) |
|
135 |
else: |
|
136 |
return None |
|
|
24
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
137 |
|
|
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
138 |
@property |
|
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
139 |
def url_status_text(self): |
|
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
140 |
return TERM_URL_STATUS_CHOICES[self.url_status][1] |
| 0 | 141 |
|
142 |
@property |
|
|
24
1d20eaea6169
small change of status link unsemantized.
ymh <ymh.work@gmail.com>
parents:
15
diff
changeset
|
143 |
def url_status_text_trans(self): |
| 15 | 144 |
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
|
145 |
|
| 0 | 146 |
def validate(self, user): |
147 |
if not self.validated: |
|
148 |
self.validation_date = datetime.datetime.utcnow() |
|
149 |
self.validated = True |
|
150 |
self.validator = user |
|
151 |
self.save() |
|
152 |
||
153 |
def unvalidate(self): |
|
154 |
if self.validated: |
|
155 |
self.validated = False |
|
156 |
self.validator = None |
|
157 |
self.validation_date = None |
|
158 |
self.save() |
|
159 |
||
| 110 | 160 |
def __unicode__(self): |
161 |
return self.label |
|
162 |
||
| 0 | 163 |
class Meta: |
164 |
app_label = 'core' |
|
|
61
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
165 |
|
|
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
166 |
class MPTTMeta: |
|
0048668779c0
change model for thesaurus tree. show level and ancestor
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
167 |
order_insertion_by = ['normalized_label'] |
| 0 | 168 |
|
169 |
class TermLabel(models.Model): |
|
170 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
|
171 |
lang = models.CharField(max_length=128, unique=False, blank=True, null=True, db_index=True) |
|
172 |
term = models.ForeignKey(Term, blank=False, null=False, db_index=True, related_name="alternative_labels") |
|
173 |
||
174 |
class Meta: |
|
175 |
app_label = 'core' |