1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
from base import *
from django.utils.translation import ugettext as _
class TagManager(models.Manager):
UPDATE_USED_COUNTS_QUERY = (
'UPDATE tag '
'SET used_count = ('
'SELECT COUNT(*) FROM question_tags '
'INNER JOIN question ON question_id=question.id '
'WHERE tag_id = tag.id AND question.deleted=False'
') '
'WHERE id IN (%s)')
def get_valid_tags(self, page_size):
tags = self.all().filter(deleted=False).exclude(used_count=0).order_by("-id")[:page_size]
return tags
def get_or_create_multiple(self, names, user):
"""
Fetches a list of Tags with the given names, creating any Tags
which don't exist when necesssary.
"""
tags = list(self.filter(name__in=names))
#Set all these tag visible
for tag in tags:
if tag.deleted:
tag.deleted = False
tag.deleted_by = None
tag.deleted_at = None
tag.save()
if len(tags) < len(names):
existing_names = set(tag.name for tag in tags)
new_names = [name for name in names if name not in existing_names]
tags.extend([self.create(name=name, created_by=user)
for name in new_names if self.filter(name=name).count() == 0 and len(name.strip()) > 0])
return tags
def update_use_counts(self, tags):
"""Updates the given Tags with their current use counts."""
if not tags:
return
cursor = connection.cursor()
query = self.UPDATE_USED_COUNTS_QUERY % ','.join(['%s'] * len(tags))
cursor.execute(query, [tag.id for tag in tags])
transaction.commit_unless_managed()
def get_tags_by_questions(self, questions):
question_ids = []
for question in questions:
question_ids.append(question.id)
question_ids_str = ','.join([str(id) for id in question_ids])
related_tags = self.extra(
tables=['tag', 'question_tags'],
where=["tag.id = question_tags.tag_id AND question_tags.question_id IN (" + question_ids_str + ")"]
).distinct()
return related_tags
class Tag(DeletableContent):
name = models.CharField(max_length=255, unique=True)
created_by = models.ForeignKey(User, related_name='created_tags')
# Denormalised data
used_count = models.PositiveIntegerField(default=0)
objects = TagManager()
class Meta(DeletableContent.Meta):
db_table = u'tag'
ordering = ('-used_count', 'name')
def __unicode__(self):
return self.name
class MarkedTag(models.Model):
TAG_MARK_REASONS = (('good',_('interesting')),('bad',_('ignored')))
tag = models.ForeignKey('Tag', related_name='user_selections')
user = models.ForeignKey(User, related_name='tag_selections')
reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)
class Meta:
app_label = 'forum'
|