Commit a5a8a60b1ec2903bf89b095711449d43d643ff0a

Authored by Sergio Oliveira
2 parents 75a7b4e0 05753259

Merge branch 'import_emails_settings' into 'master'

Import emails settings
colab/settings.py
@@ -243,6 +243,11 @@ MESSAGE_TAGS = { @@ -243,6 +243,11 @@ MESSAGE_TAGS = {
243 messages.ERROR: 'alert-danger', 243 messages.ERROR: 'alert-danger',
244 } 244 }
245 245
  246 +# Super Archives
  247 +SUPER_ARCHIVES_PATH = '/var/lib/mailman/archives/public'
  248 +SUPER_ARCHIVES_EXCLUDE = []
  249 +SUPER_ARCHIVES_LOCK_FILE = '/var/lock/colab/import_emails.lock'
  250 +
246 # Feedzilla (planet) 251 # Feedzilla (planet)
247 from feedzilla.settings import * # noqa (flake8 ignore) 252 from feedzilla.settings import * # noqa (flake8 ignore)
248 FEEDZILLA_PAGE_SIZE = 5 253 FEEDZILLA_PAGE_SIZE = 5
colab/super_archives/management/commands/import_emails.py
@@ -10,6 +10,7 @@ import mailbox @@ -10,6 +10,7 @@ import mailbox
10 import logging 10 import logging
11 from optparse import make_option 11 from optparse import make_option
12 12
  13 +from django.conf import settings
13 from django.db import transaction 14 from django.db import transaction
14 from django.template.defaultfilters import slugify 15 from django.template.defaultfilters import slugify
15 from django.core.management.base import BaseCommand, CommandError 16 from django.core.management.base import BaseCommand, CommandError
@@ -17,10 +18,9 @@ from django.template.loader import render_to_string @@ -17,10 +18,9 @@ from django.template.loader import render_to_string
17 from django.utils.translation import ugettext as _ 18 from django.utils.translation import ugettext as _
18 19
19 from colab.super_archives.utils.email import colab_send_email 20 from colab.super_archives.utils.email import colab_send_email
20 -from colab.super_archives.models import MailingList, Message, \  
21 - Thread, EmailAddress  
22 -from colab.super_archives.management.commands.message import Message as \  
23 - CustomMessage 21 +from colab.super_archives.models import (MailingList, Message,
  22 + Thread, EmailAddress)
  23 +from message import Message as CustomMessage
24 24
25 25
26 class Command(BaseCommand, object): 26 class Command(BaseCommand, object):
@@ -28,30 +28,32 @@ class Command(BaseCommand, object): @@ -28,30 +28,32 @@ class Command(BaseCommand, object):
28 28
29 help = __doc__ 29 help = __doc__
30 30
31 - default_archives_path = '/var/lib/mailman/archives/private'  
32 RE_SUBJECT_CLEAN = re.compile('((re|res|fw|fwd|en|enc):)|\[.*?\]', 31 RE_SUBJECT_CLEAN = re.compile('((re|res|fw|fwd|en|enc):)|\[.*?\]',
33 - re.IGNORECASE) 32 + re.IGNORECASE)
34 THREAD_CACHE = {} 33 THREAD_CACHE = {}
35 EMAIL_ADDR_CACHE = {} 34 EMAIL_ADDR_CACHE = {}
36 35
37 - lock_file = '/var/lock/colab/import_emails.lock' 36 + lock_file = settings.SUPER_ARCHIVES_LOCK_FILE
38 37
39 # A new command line option to get the dump file to parse. 38 # A new command line option to get the dump file to parse.
40 option_list = BaseCommand.option_list + ( 39 option_list = BaseCommand.option_list + (
41 - make_option('--archives_path', 40 + make_option(
  41 + '--archives_path',
42 dest='archives_path', 42 dest='archives_path',
43 - help='Path of email archives to be imported. (default: %s)' %  
44 - default_archives_path,  
45 - default=default_archives_path), 43 + help=('Path of email archives to be imported. '
  44 + '(default: {})').format(settings.SUPER_ARCHIVES_PATH),
  45 + default=settings.SUPER_ARCHIVES_PATH),
46 46
47 - make_option('--exclude-list', 47 + make_option(
  48 + '--exclude-list',
48 dest='exclude_lists', 49 dest='exclude_lists',
49 help=("Mailing list that won't be imported. It can be used many" 50 help=("Mailing list that won't be imported. It can be used many"
50 "times for more than one list."), 51 "times for more than one list."),
51 action='append', 52 action='append',
52 - default=None), 53 + default=settings.SUPER_ARCHIVES_EXCLUDE),
53 54
54 - make_option('--all', 55 + make_option(
  56 + '--all',
55 dest='all', 57 dest='all',
56 help='Import all messages (default: False)', 58 help='Import all messages (default: False)',
57 action="store_true", 59 action="store_true",
@@ -88,11 +90,11 @@ class Command(BaseCommand, object): @@ -88,11 +90,11 @@ class Command(BaseCommand, object):
88 # tuple (as represented in the code down here) was a valid 90 # tuple (as represented in the code down here) was a valid
89 # option but its performance was too poor. 91 # option but its performance was too poor.
90 # 92 #
91 - #for message in tuple(mbox)[index:]:  
92 - # yield message 93 + # for message in tuple(mbox)[index:]:
  94 + # yield message
93 # 95 #
94 key = index 96 key = index
95 - while mbox.has_key(key): 97 + while key in mbox:
96 key += 1 98 key += 1
97 yield key-1, mbox[key-1] 99 yield key-1, mbox[key-1]
98 100
@@ -127,7 +129,8 @@ class Command(BaseCommand, object): @@ -127,7 +129,8 @@ class Command(BaseCommand, object):
127 else: 129 else:
128 try: 130 try:
129 mailinglist = MailingList.objects.get( 131 mailinglist = MailingList.objects.get(
130 - name=mailinglist_name) 132 + name=mailinglist_name
  133 + )
131 n_msgs = mailinglist.last_imported_index 134 n_msgs = mailinglist.last_imported_index
132 except MailingList.DoesNotExist: 135 except MailingList.DoesNotExist:
133 n_msgs = 0 136 n_msgs = 0
@@ -163,7 +166,9 @@ class Command(BaseCommand, object): @@ -163,7 +166,9 @@ class Command(BaseCommand, object):
163 return 166 return
164 167
165 # Update last imported message into the DB 168 # Update last imported message into the DB
166 - mailinglist, created = MailingList.objects.get_or_create(name=list_name) 169 + mailinglist, created = MailingList.objects.get_or_create(
  170 + name=list_name
  171 + )
167 mailinglist.last_imported_index = index 172 mailinglist.last_imported_index = index
168 173
169 if created: 174 if created:
@@ -174,7 +179,7 @@ class Command(BaseCommand, object): @@ -174,7 +179,7 @@ class Command(BaseCommand, object):
174 else: 179 else:
175 # If the message is already at the database don't do anything 180 # If the message is already at the database don't do anything
176 try: 181 try:
177 - messages = Message.all_objects.get( 182 + Message.all_objects.get(
178 message_id=msg_id, 183 message_id=msg_id,
179 thread__mailinglist=mailinglist 184 thread__mailinglist=mailinglist
180 ) 185 )
@@ -260,11 +265,11 @@ class Command(BaseCommand, object): @@ -260,11 +265,11 @@ class Command(BaseCommand, object):
260 def handle(self, *args, **options): 265 def handle(self, *args, **options):
261 """Main command method.""" 266 """Main command method."""
262 267
263 -  
264 # Already running, so quit 268 # Already running, so quit
265 if os.path.exists(self.lock_file): 269 if os.path.exists(self.lock_file):
266 - self.log(("This script is already running. (If your are sure it's "  
267 - "not please delete the lock file in %s')") % self.lock_file) 270 + self.log(("This script is already running. "
  271 + "(If your are sure it's not please "
  272 + "delete the lock file in {}')").format(self.lock_file))
268 sys.exit(0) 273 sys.exit(0)
269 274
270 if not os.path.exists(os.path.dirname(self.lock_file)): 275 if not os.path.exists(os.path.dirname(self.lock_file)):
@@ -274,17 +279,19 @@ class Command(BaseCommand, object): @@ -274,17 +279,19 @@ class Command(BaseCommand, object):
274 self.log('Using archives_path `%s`' % self.default_archives_path) 279 self.log('Using archives_path `%s`' % self.default_archives_path)
275 280
276 if not os.path.exists(archives_path): 281 if not os.path.exists(archives_path):
277 - raise CommandError('archives_path (%s) does not exist' %  
278 - archives_path) 282 + msg = 'archives_path ({}) does not exist'.format(archives_path)
  283 + raise CommandError(msg)
279 run_lock = file(self.lock_file, 'w') 284 run_lock = file(self.lock_file, 'w')
280 run_lock.close() 285 run_lock.close()
281 286
282 try: 287 try:
283 - self.import_emails(archives_path,  
284 - options.get('all'), options.get('exclude_lists')) 288 + self.import_emails(
  289 + archives_path,
  290 + options.get('all'),
  291 + options.get('exclude_lists'),
  292 + )
285 except Exception as e: 293 except Exception as e:
286 logging.exception(e) 294 logging.exception(e)
287 raise 295 raise
288 finally: 296 finally:
289 os.remove(self.lock_file) 297 os.remove(self.lock_file)
290 -