|
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 _ |
|
0
|
12 |
import datetime |
|
|
13 |
import logging |
|
|
14 |
|
|
|
15 |
logger = logging.getLogger(__name__) |
|
|
16 |
|
|
|
17 |
User = get_user_model() |
|
|
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 |
} |
|
|
42 |
|
|
|
43 |
class Thesaurus(models.Model): |
|
|
44 |
label = models.CharField(max_length=128, unique=True, blank=False, null=False, db_index=True) |
|
|
45 |
title = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=False) |
|
|
46 |
description = models.TextField(blank=True, null=True) |
|
|
47 |
uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
|
48 |
|
|
|
49 |
class Meta: |
|
|
50 |
app_label = 'core' |
|
|
51 |
ordering = ['label'] |
|
|
52 |
|
|
|
53 |
def __unicode__(self): |
|
|
54 |
return self.label |
|
|
55 |
|
|
|
56 |
|
|
|
57 |
class Term(models.Model): |
|
|
58 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
|
|
59 |
lang = models.CharField(max_length=128, unique=False, blank=True, null=True, db_index=True) |
|
|
60 |
uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
|
61 |
normalized_label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True, editable=False) |
|
|
62 |
wp_label = models.CharField(max_length=1024, unique=False, blank=True, null=True, db_index=True) |
|
|
63 |
wp_alternative_label = models.CharField(max_length=1024, unique=False, blank=True, null=True, db_index=True) |
|
|
64 |
thesaurus = models.ForeignKey(Thesaurus, blank=False, null=False, db_index=True) |
|
|
65 |
created_at = models.DateTimeField(auto_now_add=True) |
|
|
66 |
wikipedia_url = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
|
67 |
wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True, db_index=True) |
|
|
68 |
wikipedia_revision_id = models.BigIntegerField(unique=False, blank=True, null=True) |
|
|
69 |
alternative_wikipedia_url = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
|
70 |
alternative_wikipedia_pageid = models.BigIntegerField(unique=False, blank=True, null=True, db_index=True) |
|
15
|
71 |
url_status = models.IntegerField(choices=TERM_URL_STATUS_CHOICES_TRANS, blank=True, null=True, default=None, db_index=True) |
|
0
|
72 |
dbpedia_uri = models.URLField(max_length=2048, blank=True, null=True, db_index=True) |
|
|
73 |
validation_date = models.DateTimeField(null=True, blank=True, serialize=False) |
|
|
74 |
validated = models.BooleanField(default=False, db_index=True) |
|
|
75 |
validator = models.ForeignKey(User, null=True, blank=True, serialize=False) |
|
|
76 |
|
|
|
77 |
@property |
|
|
78 |
def alternative_labels_str(self): |
|
|
79 |
return " | ".join([l.label for l in self.alternative_labels.all()]) |
|
|
80 |
|
|
|
81 |
@property |
|
|
82 |
def wikipedia_revision_permalink(self): |
|
|
83 |
return settings.WIKIPEDIA_VERSION_PERMALINK_TEMPLATE % (unicode(self.wikipedia_revision_id)) |
|
|
84 |
|
|
|
85 |
@property |
|
|
86 |
def url_status_text(self): |
|
15
|
87 |
return TERM_URL_STATUS_CHOICES_TRANS[self.url_status][1] |
|
0
|
88 |
|
|
|
89 |
def validate(self, user): |
|
|
90 |
if not self.validated: |
|
|
91 |
self.validation_date = datetime.datetime.utcnow() |
|
|
92 |
self.validated = True |
|
|
93 |
self.validator = user |
|
|
94 |
self.save() |
|
|
95 |
|
|
|
96 |
def unvalidate(self): |
|
|
97 |
if self.validated: |
|
|
98 |
self.validated = False |
|
|
99 |
self.validator = None |
|
|
100 |
self.validation_date = None |
|
|
101 |
self.save() |
|
|
102 |
|
|
|
103 |
class Meta: |
|
|
104 |
app_label = 'core' |
|
|
105 |
|
|
|
106 |
class TermLabel(models.Model): |
|
|
107 |
label = models.CharField(max_length=1024, unique=False, blank=False, null=False, db_index=True) |
|
|
108 |
lang = models.CharField(max_length=128, unique=False, blank=True, null=True, db_index=True) |
|
|
109 |
term = models.ForeignKey(Term, blank=False, null=False, db_index=True, related_name="alternative_labels") |
|
|
110 |
|
|
|
111 |
class Meta: |
|
|
112 |
app_label = 'core' |