annotate ygroup/management/commands/sync_ygroup_posts.py @ 943:cf9918328c64
Haystack tweaks for Django 1.7.7.
I had to upgrade to Haystack 2.3.1 to get it to work with Django
1.7.7. I also had to update the Xapian backend. But I ran into
problems.
On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms
are greater than 245 chars (or something) when indexing. So I created
a custom field that would simply omit terms greater than 64 chars and
used this field everywhere I previously used a CharField.
Secondly, the custom search form was broken now. Something changed in
the Xapian backend and exact searches stopped working. Fortunately the
auto_query (which I was using originally and broke during an upgrade)
started working again. So I cut the search form back over to doing an
auto_query. I kept the form the same (3 fields) because I didn't want
to change the form and I think it's better that way.
author |
Brian Neal <bgneal@gmail.com> |
date |
Wed, 13 May 2015 20:25:07 -0500 |
parents |
ee87ea74d46b |
children |
|
rev |
line source |
bgneal@323
|
1 """
|
bgneal@323
|
2 sync_ygroup_posts.py - A management command to synchronize the yahoo group
|
bgneal@323
|
3 archives by recomputing the de-normalized fields in the post objects.
|
bgneal@323
|
4
|
bgneal@323
|
5 """
|
bgneal@323
|
6 import optparse
|
bgneal@323
|
7
|
bgneal@323
|
8 from django.core.management.base import NoArgsCommand, CommandError
|
bgneal@323
|
9 from django.core.urlresolvers import reverse
|
bgneal@323
|
10
|
bgneal@323
|
11 from ygroup.models import Thread, Post
|
bgneal@323
|
12 import ygroup.views
|
bgneal@323
|
13
|
bgneal@323
|
14
|
bgneal@323
|
15 class Command(NoArgsCommand):
|
bgneal@323
|
16 help = """\
|
bgneal@323
|
17 This command synchronizes the ygroup application's post objects
|
bgneal@323
|
18 by updating their de-normalized fields.
|
bgneal@323
|
19 """
|
bgneal@323
|
20 option_list = NoArgsCommand.option_list + (
|
bgneal@323
|
21 optparse.make_option("-p", "--progress", action="store_true",
|
bgneal@323
|
22 help="Output a . after every 100 posts to show progress"),
|
bgneal@323
|
23 )
|
bgneal@323
|
24
|
bgneal@323
|
25 def handle_noargs(self, **opts):
|
bgneal@323
|
26
|
bgneal@323
|
27 show_progress = opts.get('progress', False) or False
|
bgneal@323
|
28
|
bgneal@323
|
29 threads = {}
|
bgneal@323
|
30 self.stdout.write("Processing threads...\n")
|
bgneal@323
|
31 for thread in Thread.objects.iterator():
|
bgneal@323
|
32 threads[thread.id] = [reverse('ygroup-thread_view', args=[thread.id]),
|
bgneal@323
|
33 list(Post.objects.filter(thread=thread).values_list('id', flat=True))]
|
bgneal@323
|
34
|
bgneal@323
|
35 self.stdout.write("Processing posts...\n")
|
bgneal@323
|
36 n = 0
|
bgneal@323
|
37 for post in Post.objects.iterator():
|
bgneal@323
|
38 thread = threads[post.thread.id]
|
bgneal@323
|
39 pos = thread[1].index(post.id)
|
bgneal@323
|
40 page = pos / ygroup.views.POSTS_PER_PAGE + 1
|
bgneal@323
|
41 if page == 1:
|
bgneal@323
|
42 post.thread_url = thread[0] + '#p%d' % (post.id, )
|
bgneal@323
|
43 else:
|
bgneal@323
|
44 post.thread_url = thread[0] + '?page=%d#p%d' % (page, post.id)
|
bgneal@323
|
45 post.save()
|
bgneal@323
|
46
|
bgneal@323
|
47 n += 1
|
bgneal@323
|
48 if show_progress and n % 100 == 0:
|
bgneal@323
|
49 self.stdout.write('.')
|
bgneal@323
|
50 self.stdout.flush()
|
bgneal@323
|
51
|
bgneal@323
|
52 self.stdout.write('\n')
|
bgneal@323
|
53
|