Commit b7e5d5b3d556fbbff27d5d3b99d4be5b06292a43

Authored by Luan
2 parents cf44a3ad 915e79f8

Merge branch 'master' of github.com:TracyWebTech/colab

puppet/bootstrap.sh
1 #!/bin/bash 1 #!/bin/bash
2 2
3 -PUPPET_VERSION=`dpkg -l | grep puppet-common | awk '{ print $3 }'` 3 +PUPPET_VERSION=`dpkg -l | grep puppet-common | awk '{ print $3 }' | cut -d- -f1`
  4 +dpkg --compare-versions "$PUPPET_VERSION" "<" "3.3" ; OUTDATED=$?
4 5
5 -if [ "$PUPPET_VERSION" != '3.2.3-1puppetlabs1' ] ; then 6 +if [ $OUTDATED -eq 0 ] ; then
6 wget -O /tmp/puppet_apt.deb http://apt.puppetlabs.com/puppetlabs-release-precise.deb &> /dev/null 7 wget -O /tmp/puppet_apt.deb http://apt.puppetlabs.com/puppetlabs-release-precise.deb &> /dev/null
7 dpkg -i /tmp/puppet_apt.deb 8 dpkg -i /tmp/puppet_apt.deb
8 DEBIAN_FRONTEND=noninteractive apt-get update -y 9 DEBIAN_FRONTEND=noninteractive apt-get update -y
src/super_archives/management/commands/import_emails.py
@@ -13,8 +13,10 @@ from django.db import transaction @@ -13,8 +13,10 @@ from django.db import transaction
13 from django.template.defaultfilters import slugify 13 from django.template.defaultfilters import slugify
14 from django.core.management.base import BaseCommand, CommandError 14 from django.core.management.base import BaseCommand, CommandError
15 15
16 -from super_archives.models import MailingList, Message, Thread, EmailAddress  
17 -from super_archives.management.commands.message import Message as CustomMessage 16 +from colab.super_archives.models import MailingList, Message, \
  17 + Thread, EmailAddress
  18 +from colab.super_archives.management.commands.message import Message as \
  19 + CustomMessage
18 20
19 21
20 class Command(BaseCommand, object): 22 class Command(BaseCommand, object):
@@ -149,6 +151,10 @@ class Command(BaseCommand, object): @@ -149,6 +151,10 @@ class Command(BaseCommand, object):
149 def save_email(self, list_name, email_msg, index): 151 def save_email(self, list_name, email_msg, index):
150 """Save email message into the database.""" 152 """Save email message into the database."""
151 153
  154 + msg_id = email_msg.get('Message-ID')
  155 + if not msg_id:
  156 + return
  157 +
152 # Update last imported message into the DB 158 # Update last imported message into the DB
153 mailinglist, created = MailingList.objects.get_or_create(name=list_name) 159 mailinglist, created = MailingList.objects.get_or_create(name=list_name)
154 mailinglist.last_imported_index = index 160 mailinglist.last_imported_index = index
@@ -162,7 +168,7 @@ class Command(BaseCommand, object): @@ -162,7 +168,7 @@ class Command(BaseCommand, object):
162 # If the message is already at the database don't do anything 168 # If the message is already at the database don't do anything
163 try: 169 try:
164 messages = Message.objects.get( 170 messages = Message.objects.get(
165 - message_id=email_msg.get('Message-ID'), 171 + message_id=msg_id,
166 thread__mailinglist=mailinglist 172 thread__mailinglist=mailinglist
167 ) 173 )
168 174
@@ -172,6 +178,9 @@ class Command(BaseCommand, object): @@ -172,6 +178,9 @@ class Command(BaseCommand, object):
172 mailinglist.save() 178 mailinglist.save()
173 179
174 def create_email(self, mailinglist, email_msg): 180 def create_email(self, mailinglist, email_msg):
  181 + received_time = email_msg.get_received_datetime()
  182 + if not received_time:
  183 + return
175 184
176 real_name, from_ = email_msg.get_from_addr() 185 real_name, from_ = email_msg.get_from_addr()
177 186
src/super_archives/management/commands/message.py
@@ -13,35 +13,35 @@ import chardet @@ -13,35 +13,35 @@ import chardet
13 def get_charset(message, default='ASCII'): 13 def get_charset(message, default='ASCII'):
14 """Get the message charset""" 14 """Get the message charset"""
15 15
16 - charset = message.get_content_charset()  
17 - 16 + charset = message.get_content_charset()
  17 +
18 if not charset: 18 if not charset:
19 charset = message.get_charset() 19 charset = message.get_charset()
20 - 20 +
21 if not charset: 21 if not charset:
22 charset = default 22 charset = default
23 - 23 +
24 try: 24 try:
25 codecs.lookup(charset) 25 codecs.lookup(charset)
26 except LookupError: 26 except LookupError:
27 charset = default 27 charset = default
28 - 28 +
29 return charset 29 return charset
30 - 30 +
31 31
32 class Message(mailbox.mboxMessage): 32 class Message(mailbox.mboxMessage):
33 - 33 +
34 RECEIVED_DELIMITER = re.compile('\n|;') 34 RECEIVED_DELIMITER = re.compile('\n|;')
35 - 35 +
36 def get_subject(self): 36 def get_subject(self):
37 subject = email.header.decode_header(self['Subject']) 37 subject = email.header.decode_header(self['Subject'])
38 - 38 +
39 if isinstance(subject, list): 39 if isinstance(subject, list):
40 new_subject = u'' 40 new_subject = u''
41 for text_part, encoding in subject: 41 for text_part, encoding in subject:
42 if not encoding: 42 if not encoding:
43 encoding = get_charset(self) 43 encoding = get_charset(self)
44 - 44 +
45 try: 45 try:
46 new_subject += unicode(text_part, encoding) 46 new_subject += unicode(text_part, encoding)
47 except (UnicodeDecodeError, LookupError): 47 except (UnicodeDecodeError, LookupError):
@@ -50,7 +50,7 @@ class Message(mailbox.mboxMessage): @@ -50,7 +50,7 @@ class Message(mailbox.mboxMessage):
50 except (UnicodeDecodeError, LookupError): 50 except (UnicodeDecodeError, LookupError):
51 encoding = chardet.detect(text_part)['encoding'] 51 encoding = chardet.detect(text_part)['encoding']
52 new_subject += unicode(text_part, encoding) 52 new_subject += unicode(text_part, encoding)
53 - 53 +
54 return ''.join(new_subject) 54 return ''.join(new_subject)
55 55
56 def get_body(self): 56 def get_body(self):
@@ -77,25 +77,27 @@ class Message(mailbox.mboxMessage): @@ -77,25 +77,27 @@ class Message(mailbox.mboxMessage):
77 get_charset(self), 77 get_charset(self),
78 "replace") 78 "replace")
79 return body.strip() 79 return body.strip()
80 - 80 +
81 def get_received_datetime(self): 81 def get_received_datetime(self):
  82 + if not self.has_key('Received'):
  83 + return None
82 # The time received should always be the last element 84 # The time received should always be the last element
83 - # in the `Received` attribute from the message headers 85 + # in the `Received` attribute from the message headers
84 received_header = self.RECEIVED_DELIMITER.split(self['Received']) 86 received_header = self.RECEIVED_DELIMITER.split(self['Received'])
85 received_time_header = received_header[-1].strip() 87 received_time_header = received_header[-1].strip()
86 - 88 +
87 date_tuple = email.utils.parsedate_tz(received_time_header) 89 date_tuple = email.utils.parsedate_tz(received_time_header)
88 utc_timestamp = email.utils.mktime_tz(date_tuple) 90 utc_timestamp = email.utils.mktime_tz(date_tuple)
89 utc_datetime = datetime.datetime.fromtimestamp(utc_timestamp, 91 utc_datetime = datetime.datetime.fromtimestamp(utc_timestamp,
90 pytz.utc) 92 pytz.utc)
91 93
92 return utc_datetime 94 return utc_datetime
93 - 95 +
94 def get_from_addr(self): 96 def get_from_addr(self):
95 real_name_raw, from_ = email.utils.parseaddr(self['From']) 97 real_name_raw, from_ = email.utils.parseaddr(self['From'])
96 real_name_str, encoding = email.header.decode_header(real_name_raw)[0] 98 real_name_str, encoding = email.header.decode_header(real_name_raw)[0]
97 if not encoding: 99 if not encoding:
98 encoding = 'ascii' 100 encoding = 'ascii'
99 - 101 +
100 real_name = unicode(real_name_str, encoding, errors='replace') 102 real_name = unicode(real_name_str, encoding, errors='replace')
101 return real_name, from_ 103 return real_name, from_