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