Commit 708d97c2ce578b1cf8107b557e289e179df3d983

Authored by Sergio Oliveira
1 parent bfb24ce4

Refactoring. Removing super_archives.queries

@@ -52,6 +52,7 @@ Interface @@ -52,6 +52,7 @@ Interface
52 Outros 52 Outros
53 ======= 53 =======
54 54
  55 +* Contabilizar votos dentro do modelo de mensagem. Com a implementação atual ordenar uma thread por votos é uma operação muito cara.
55 * Utilizar SOLR para listar documentos relevantes ao inves de thread.score 56 * Utilizar SOLR para listar documentos relevantes ao inves de thread.score
56 * Escrever casos de teste 57 * Escrever casos de teste
57 * Criacao de repositorios distribuidos pela interface do colab 58 * Criacao de repositorios distribuidos pela interface do colab
src/home/views.py
@@ -4,16 +4,18 @@ from collections import OrderedDict @@ -4,16 +4,18 @@ from collections import OrderedDict
4 from django.shortcuts import render 4 from django.shortcuts import render
5 from django.utils import timezone 5 from django.utils import timezone
6 6
7 -from haystack.query import SearchQuerySet  
8 -from super_archives import queries  
9 from search.utils import trans 7 from search.utils import trans
  8 +from haystack.query import SearchQuerySet
  9 +
  10 +from super_archives.models import Thread
10 11
11 12
12 def index(request): 13 def index(request):
13 """Index page view""" 14 """Index page view"""
14 15
15 - latest_threads = queries.get_latest_threads()  
16 - hottest_threads = queries.get_hottest_threads() 16 +
  17 + latest_threads = Thread.objects.all()[:6]
  18 + hottest_threads = Thread.highest_score.from_haystack()[:6]
17 19
18 count_types = OrderedDict() 20 count_types = OrderedDict()
19 six_months = timezone.now() - timezone.timedelta(days=180) 21 six_months = timezone.now() - timezone.timedelta(days=180)
@@ -25,7 +27,7 @@ def index(request): @@ -25,7 +27,7 @@ def index(request):
25 27
26 context = { 28 context = {
27 'hottest_threads': hottest_threads[:6], 29 'hottest_threads': hottest_threads[:6],
28 - 'latest_threads': latest_threads[:6], 30 + 'latest_threads': latest_threads,
29 'type_count': count_types, 31 'type_count': count_types,
30 'latest_results': SearchQuerySet().all().order_by( 32 'latest_results': SearchQuerySet().all().order_by(
31 '-modified', '-created' 33 '-modified', '-created'
src/rss/feeds.py
@@ -7,7 +7,6 @@ from django.utils.translation import ugettext as _ @@ -7,7 +7,6 @@ from django.utils.translation import ugettext as _
7 from haystack.query import SearchQuerySet 7 from haystack.query import SearchQuerySet
8 8
9 from super_archives.models import Thread 9 from super_archives.models import Thread
10 -from super_archives import queries  
11 10
12 11
13 class LatestThreadsFeeds(Feed): 12 class LatestThreadsFeeds(Feed):
@@ -15,7 +14,7 @@ class LatestThreadsFeeds(Feed): @@ -15,7 +14,7 @@ class LatestThreadsFeeds(Feed):
15 link = '/rss/threads/latest/' 14 link = '/rss/threads/latest/'
16 15
17 def items(self): 16 def items(self):
18 - return queries.get_latest_threads()[:20] 17 + return Thread.objects.all()[:20]
19 18
20 def item_link(self, item): 19 def item_link(self, item):
21 return item.latest_message.url 20 return item.latest_message.url
@@ -34,7 +33,7 @@ class HottestThreadsFeeds(Feed): @@ -34,7 +33,7 @@ class HottestThreadsFeeds(Feed):
34 link = '/rss/threads/hottest/' 33 link = '/rss/threads/hottest/'
35 34
36 def items(self): 35 def items(self):
37 - return queries.get_hottest_threads()[:20] 36 + return Thread.highest_score.all()[:20]
38 37
39 def item_link(self, item): 38 def item_link(self, item):
40 return item.latest_message.url 39 return item.latest_message.url
src/search/forms.py
@@ -158,7 +158,4 @@ class ColabSearchForm(SearchForm): @@ -158,7 +158,4 @@ class ColabSearchForm(SearchForm):
158 if self.cleaned_data['filename']: 158 if self.cleaned_data['filename']:
159 sqs = sqs.filter(filename=self.cleaned_data['filename']) 159 sqs = sqs.filter(filename=self.cleaned_data['filename'])
160 160
161 - if self.load_all:  
162 - sqs = sqs.load_all()  
163 -  
164 return sqs 161 return sqs
src/super_archives/managers.py 0 → 100644
@@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
  1 +
  2 +from django.db import models
  3 +
  4 +from haystack.query import SearchQuerySet
  5 +
  6 +
  7 +class NotSpamManager(models.Manager):
  8 + """Only return objects which are not marked as spam."""
  9 +
  10 + def get_query_set(self):
  11 + return super(NotSpamManager, self).get_query_set().exclude(spam=True)
  12 +
  13 +
  14 +class HighestScore(NotSpamManager):
  15 + def get_query_set(self):
  16 + queryset = super(HighestScore, self).get_query_set()
  17 + return queryset.order_by('-score', '-latest_message__received_time')
  18 +
  19 + def from_haystack(self):
  20 + return SearchQuerySet().filter(type='thread')
  21 +
  22 +
  23 +class MostVotedManager(NotSpamManager):
  24 + def get_query_set(self):
  25 + """Query for the most voted messages sorting by the sum of
  26 + voted and after by date."""
  27 +
  28 + queryset = super(MostVotedManager, self).get_query_set()
  29 +
  30 + sql = """
  31 + SELECT
  32 + count(sav.id)
  33 + FROM
  34 + super_archives_vote AS sav
  35 + WHERE
  36 + super_archives_message.id = sav.message_id
  37 + """
  38 +
  39 + messages = queryset.extra(
  40 + select={
  41 + 'vote_count': sql,
  42 + }
  43 + )
  44 + return messages.order_by('-vote_count', 'received_time')
src/super_archives/models.py
@@ -16,6 +16,7 @@ from haystack.query import SearchQuerySet @@ -16,6 +16,7 @@ from haystack.query import SearchQuerySet
16 from taggit.managers import TaggableManager 16 from taggit.managers import TaggableManager
17 from hitcounter.models import HitCounterModelMixin 17 from hitcounter.models import HitCounterModelMixin
18 18
  19 +from .managers import NotSpamManager, MostVotedManager, HighestScore
19 from .utils import blocks 20 from .utils import blocks
20 from .utils.etiquetador import etiquetador 21 from .utils.etiquetador import etiquetador
21 22
@@ -23,13 +24,6 @@ from .utils.etiquetador import etiquetador @@ -23,13 +24,6 @@ from .utils.etiquetador import etiquetador
23 User = get_user_model() 24 User = get_user_model()
24 25
25 26
26 -class NotSpamManager(models.Manager):  
27 - """Only return objects which are not marked as spam."""  
28 -  
29 - def get_query_set(self):  
30 - return super(NotSpamManager, self).get_query_set().exclude(spam=True)  
31 -  
32 -  
33 class EmailAddressValidation(models.Model): 27 class EmailAddressValidation(models.Model):
34 address = models.EmailField(unique=True) 28 address = models.EmailField(unique=True)
35 user = models.ForeignKey(User, null=True, 29 user = models.ForeignKey(User, null=True,
@@ -116,6 +110,7 @@ class Thread(models.Model, HitCounterModelMixin): @@ -116,6 +110,7 @@ class Thread(models.Model, HitCounterModelMixin):
116 score = models.IntegerField(default=0, verbose_name=_(u"Score"), help_text=_(u"Thread score")) 110 score = models.IntegerField(default=0, verbose_name=_(u"Score"), help_text=_(u"Thread score"))
117 spam = models.BooleanField(default=False) 111 spam = models.BooleanField(default=False)
118 112
  113 + highest_score = HighestScore()
119 all_objects = models.Manager() 114 all_objects = models.Manager()
120 objects = NotSpamManager() 115 objects = NotSpamManager()
121 tags = TaggableManager() 116 tags = TaggableManager()
@@ -252,6 +247,7 @@ class Message(models.Model): @@ -252,6 +247,7 @@ class Message(models.Model):
252 247
253 all_objects = models.Manager() 248 all_objects = models.Manager()
254 objects = NotSpamManager() 249 objects = NotSpamManager()
  250 + most_voted = MostVotedManager()
255 251
256 class Meta: 252 class Meta:
257 verbose_name = _(u"Message") 253 verbose_name = _(u"Message")
src/super_archives/queries.py
@@ -1,45 +0,0 @@ @@ -1,45 +0,0 @@
1 -  
2 -from django.core.exceptions import ObjectDoesNotExist  
3 -from .models import Thread, Vote, Message  
4 -  
5 -  
6 -def get_messages_by_date():  
7 - return Message.objects.order_by('received_time')  
8 -  
9 -  
10 -def get_messages_by_voted():  
11 - """Query for the most voted messages sorting by the sum of  
12 - voted and after by date."""  
13 -  
14 - sql = """  
15 - SELECT  
16 - count(sav.id)  
17 - FROM  
18 - super_archives_vote AS sav  
19 - WHERE  
20 - super_archives_message.id = sav.message_id  
21 - """  
22 - messages = Message.objects.extra(  
23 - select={  
24 - 'vote_count': sql,  
25 - }  
26 - )  
27 - return messages.order_by('-vote_count', 'received_time')  
28 -  
29 -  
30 -def get_first_message_in_thread(mailinglist, thread_token):  
31 - query = get_messages_by_date()  
32 - query = query.filter(thread__mailinglist__name=mailinglist)  
33 - try:  
34 - query = query.filter(thread__subject_token=thread_token)[0]  
35 - except IndexError:  
36 - raise ObjectDoesNotExist  
37 - return query  
38 -  
39 -  
40 -def get_latest_threads():  
41 - return Thread.objects.order_by('-latest_message__received_time')  
42 -  
43 -  
44 -def get_hottest_threads():  
45 - return Thread.objects.order_by('-score', '-latest_message__received_time')  
src/super_archives/views.py
@@ -20,30 +20,30 @@ from django.shortcuts import render, redirect @@ -20,30 +20,30 @@ from django.shortcuts import render, redirect
20 20
21 from haystack.query import SearchQuerySet 21 from haystack.query import SearchQuerySet
22 22
23 -from . import queries  
24 from .utils.email import send_verification_email 23 from .utils.email import send_verification_email
25 -from .models import MailingList, Thread, EmailAddress, EmailAddressValidation 24 +from .models import MailingList, Thread, EmailAddress, \
  25 + EmailAddressValidation, Message
26 26
27 27
28 class ThreadView(View): 28 class ThreadView(View):
29 http_method_names = [u'get', u'post'] 29 http_method_names = [u'get', u'post']
30 30
31 def get(self, request, mailinglist, thread_token): 31 def get(self, request, mailinglist, thread_token):
32 - try:  
33 - first_message = queries.get_first_message_in_thread(mailinglist,  
34 - thread_token)  
35 - except ObjectDoesNotExist:  
36 - raise http.Http404  
37 32
38 thread = Thread.objects.get(subject_token=thread_token, 33 thread = Thread.objects.get(subject_token=thread_token,
39 mailinglist__name=mailinglist) 34 mailinglist__name=mailinglist)
40 thread.hit(request) 35 thread.hit(request)
41 36
  37 + try:
  38 + first_message = thread.message_set.first()
  39 + except ObjectDoesNotExist:
  40 + raise http.Http404
  41 +
42 order_by = request.GET.get('order') 42 order_by = request.GET.get('order')
43 if order_by == 'voted': 43 if order_by == 'voted':
44 - msgs_query = queries.get_messages_by_voted() 44 + msgs_query = Message.most_voted
45 else: 45 else:
46 - msgs_query = queries.get_messages_by_date() 46 + msgs_query = Message.objects
47 47
48 msgs_query = msgs_query.filter(thread__subject_token=thread_token) 48 msgs_query = msgs_query.filter(thread__subject_token=thread_token)
49 msgs_query = msgs_query.filter(thread__mailinglist__name=mailinglist) 49 msgs_query = msgs_query.filter(thread__mailinglist__name=mailinglist)
src/templates/home.html
@@ -50,11 +50,10 @@ @@ -50,11 +50,10 @@
50 </a> 50 </a>
51 <ul class="message-list"> 51 <ul class="message-list">
52 {% for thread in hottest_threads %} 52 {% for thread in hottest_threads %}
53 - {% include "message-preview.html" with result=thread.latest_message %} 53 + {% include "message-preview.html" with result=thread %}
54 {% endfor %} 54 {% endfor %}
55 </ul> 55 </ul>
56 - <a class="column-align"  
57 - href="{% url 'thread_list' %}?order=hottest"> 56 + <a href="{% url 'haystack_search' %}?type=thread">
58 {% trans "View more discussions..." %} 57 {% trans "View more discussions..." %}
59 </a> 58 </a>
60 <div>&nbsp;</div> 59 <div>&nbsp;</div>
@@ -73,7 +72,7 @@ @@ -73,7 +72,7 @@
73 {% include "message-preview.html" with result=thread.latest_message %} 72 {% include "message-preview.html" with result=thread.latest_message %}
74 {% endfor %} 73 {% endfor %}
75 </ul> 74 </ul>
76 - <a class="column-align" href="{% url 'thread_list' %}"> 75 + <a href="{% url 'haystack_search' %}?type=thread&order=latest">
77 {% trans "View more discussions..." %} 76 {% trans "View more discussions..." %}
78 </a> 77 </a>
79 <div>&nbsp;</div> 78 <div>&nbsp;</div>