Don't deliver messages to "fake" Zephyr users created by populate_db.

(imported from commit c88760a88382ffc07cf5f79f3093a2bfec429d53)
This commit is contained in:
Tim Abbott 2012-10-23 16:39:40 -04:00
parent afdd3af1b6
commit 1b67b6b711
2 changed files with 32 additions and 23 deletions

View File

@ -283,7 +283,8 @@ def restore_saved_messages():
if old_message["sender_email"] not in email_set:
user_set.add((old_message["sender_email"],
old_message["sender_full_name"],
old_message["sender_short_name"]))
old_message["sender_short_name"],
False))
if 'sending_client' in old_message:
client_set.add(old_message['sending_client'])
@ -293,13 +294,13 @@ def restore_saved_messages():
elif old_message['type'] == 'personal':
u = old_message["recipient"][0]
if u["email"] not in email_set:
user_set.add((u["email"], u["full_name"], u["short_name"]))
user_set.add((u["email"], u["full_name"], u["short_name"], False))
email_set.add(u["email"])
elif old_message['type'] == 'huddle':
for u in old_message["recipient"]:
user_set.add((u["email"], u["full_name"], u["short_name"]))
user_set.add((u["email"], u["full_name"], u["short_name"], False))
if u["email"] not in email_set:
user_set.add((u["email"], u["full_name"], u["short_name"]))
user_set.add((u["email"], u["full_name"], u["short_name"], False))
email_set.add(u["email"])
huddle_user_set.add(tuple(sorted(u["email"] for u in old_message["recipient"])))
else:
@ -462,14 +463,16 @@ def restore_saved_messages():
# subscriptions being out-of-date.
continue
for user_profile_id in subscribers[message.recipient_id]:
um = UserMessage(user_profile=users_by_id[user_profile_id],
message=message)
user_messages_to_create.append(um)
if users_by_id[user_profile_id].user.is_active:
um = UserMessage(user_profile_id=user_profile_id,
message=message)
user_messages_to_create.append(um)
if message.recipient_id in personal_recipients:
# Include the sender in huddle recipients
um = UserMessage(user_profile=users_by_id[message.sender_id],
message=message)
user_messages_to_create.append(um)
if users_by_id[message.sender_id].user.is_active:
um = UserMessage(user_profile_id=message.sender_id,
message=message)
user_messages_to_create.append(um)
print datetime.datetime.now(), "Importing usermessages, part 2..."
tot_user_messages = len(user_messages_to_create)

View File

@ -166,24 +166,27 @@ class PreregistrationUser(models.Model):
# create_user_hack is the same as Django's User.objects.create_user,
# except that we don't save to the database so it can used in
# bulk_creates
def create_user_hack(username, password, email):
def create_user_hack(username, password, email, active):
now = timezone.now()
email = UserManager.normalize_email(email)
user = User(username=username, email=email,
is_staff=False, is_active=True, is_superuser=False,
is_staff=False, is_active=active, is_superuser=False,
last_login=now, date_joined=now)
user.set_password(password)
if active:
user.set_password(password)
else:
user.set_unusable_password()
return user
def create_user_base(email, password):
def create_user_base(email, password, active=True):
# NB: the result of Base32 + truncation is not a valid Base32 encoding.
# It's just a unique alphanumeric string.
# Use base32 instead of base64 so we don't have to worry about mixed case.
# Django imposes a limit of 30 characters on usernames.
email_hash = hashlib.sha256(settings.HASH_SALT + email).digest()
username = base64.b32encode(email_hash)[:30]
return create_user_hack(username, password, email)
return create_user_hack(username, password, email, active)
def create_user(email, password, realm, full_name, short_name):
user = create_user_base(email=email, password=password)
@ -207,15 +210,16 @@ def bulk_create_users(realms, users_raw):
"""
users = []
existing_users = set(u.email for u in User.objects.all())
for (email, full_name, short_name) in users_raw:
for (email, full_name, short_name, active) in users_raw:
if email in existing_users:
continue
users.append((email, full_name, short_name))
users.append((email, full_name, short_name, active))
existing_users.add(email)
users_to_create = []
for (email, full_name, short_name) in users:
users_to_create.append(create_user_base(email, initial_password(email)))
for (email, full_name, short_name, active) in users:
users_to_create.append(create_user_base(email, initial_password(email),
active=active))
batch_bulk_create(User, users_to_create, 30)
users_by_email = {}
@ -224,7 +228,7 @@ def bulk_create_users(realms, users_raw):
# Now create user_profiles
profiles_to_create = []
for (email, full_name, short_name) in users:
for (email, full_name, short_name, active) in users:
domain = email.split('@')[1]
profile = UserProfile(user=users_by_email[email], pointer=-1,
realm_id=realms[domain].id,
@ -240,7 +244,7 @@ def bulk_create_users(realms, users_raw):
profiles_by_id[profile.user.id] = profile
recipients_to_create = []
for (email, _, _) in users:
for (email, _, _, _) in users:
recipients_to_create.append(Recipient(type_id=profiles_by_email[email].id,
type=Recipient.PERSONAL))
batch_bulk_create(Recipient, recipients_to_create)
@ -250,7 +254,7 @@ def bulk_create_users(realms, users_raw):
recipients_by_email[profiles_by_id[recipient.type_id].user.email] = recipient
subscriptions_to_create = []
for (email, _, _) in users:
for (email, _, _, _) in users:
subscriptions_to_create.append(\
Subscription(user_profile_id=profiles_by_email[email].id,
recipient=recipients_by_email[email]))
@ -444,7 +448,9 @@ def do_send_message(message, no_log=False):
# TODO: Use bulk_create here
with transaction.commit_on_success():
for user_profile in recipients:
UserMessage(user_profile=user_profile, message=message).save()
# Only deliver messages to "active" user accounts
if user_profile.user.is_active:
UserMessage(user_profile=user_profile, message=message).save()
# We can only publish messages to longpolling clients if the Tornado server is running.
if settings.HAVE_TORNADO_SERVER: