[manual] Remove now-unused User model.

I think all that one needs to do to deploy this commit is on developer
laptops, run `generate-fixtures --force`.

(imported from commit 34916341435fef0875b5a2c7f53c2f5606cd16cd)
This commit is contained in:
Tim Abbott
2013-04-01 10:57:50 -04:00
parent a8e89962d8
commit 1cec86eb2d
10 changed files with 37 additions and 130 deletions

View File

@@ -13,9 +13,7 @@ from StringIO import StringIO
from zephyr.lib.cache import cache_with_key
from zephyr.lib.queue import queue_json_publish
from zephyr.lib.timestamp import datetime_to_timestamp
from zephyr.lib.cache import user_profile_by_email_cache_key, \
user_profile_by_user_cache_key
from zephyr.lib.cache import user_profile_by_email_cache_key
from functools import wraps
import base64
@@ -53,10 +51,6 @@ def update_user_activity(request, user_profile):
# I like the all-lowercase name better
require_post = require_POST
@cache_with_key(user_profile_by_user_cache_key, timeout=3600*24*7)
def get_user_profile_by_user_id(user_id):
return UserProfile.objects.select_related().get(user_id=user_id)
def process_client(request, user_profile):
try:
# we want to take from either GET or POST vars

View File

@@ -100,10 +100,6 @@ def do_deactivate(user_profile):
user_profile.set_unusable_password()
user_profile.save(update_fields=["is_active", "password"])
user_profile.user.set_unusable_password()
user_profile.user.is_active = False
user_profile.user.save(update_fields=["is_active", "password"])
delete_user_sessions(user_profile)
log_event({'type': 'user_deactivated',
@@ -122,13 +118,9 @@ def do_deactivate(user_profile):
def do_change_user_email(user_profile, new_email):
old_email = user_profile.email
user_profile.email = new_email
user_profile.save(update_fields=["email"])
user_profile.user.email = new_email
user_profile.user.save(update_fields=["email"])
log_event({'type': 'user_email_changed',
'old_email': old_email,
'new_email': new_email})
@@ -496,18 +488,11 @@ def log_subscription_property_change(user_email, property, property_dict):
log_event(event)
def do_activate_user(user_profile, log=True, join_date=timezone.now()):
user = user_profile.user
user_profile.is_active = True
user_profile.set_password(initial_password(user_profile.email))
user_profile.date_joined = join_date
user_profile.save(update_fields=["is_active", "date_joined", "password"])
user.is_active = True
user.set_password(initial_password(user.email))
user.date_joined = join_date
user.save(update_fields=["is_active", "date_joined", "password"])
if log:
domain = user_profile.realm.domain
log_event({'type': 'user_activated',
@@ -516,16 +501,12 @@ def do_activate_user(user_profile, log=True, join_date=timezone.now()):
def do_change_password(user_profile, password, log=True, commit=True,
hashed_password=False):
user = user_profile.user
if hashed_password:
# This is a hashed password, not the password itself.
user.password = password
user_profile.set_password(password)
else:
user.set_password(password)
user_profile.set_password(password)
if commit:
user.save(update_fields=["password"])
user_profile.save(update_fields=["password"])
if log:
log_event({'type': 'user_change_password',

View File

@@ -1,10 +1,10 @@
from django.conf import settings
from zephyr.lib.initial_password import initial_password, initial_api_key
from zephyr.models import Realm, Stream, User, UserProfile, Huddle, \
from zephyr.models import Realm, Stream, UserProfile, Huddle, \
Subscription, Recipient, Client, Message, \
get_huddle_hash
from zephyr.lib.create_user import create_user_base
from zephyr.lib.create_user import create_user_profile
def bulk_create_realms(realm_list):
existing_realms = set(r.domain for r in Realm.objects.select_related().all())
@@ -18,41 +18,24 @@ def bulk_create_realms(realm_list):
def bulk_create_users(realms, users_raw):
"""
Creates and saves a User with the given email.
Creates and saves a UserProfile with the given email.
Has some code based off of UserManage.create_user, but doesn't .save()
"""
users = []
existing_users = set(u.email for u in User.objects.all())
existing_users = set(u.email for u in UserProfile.objects.all())
for (email, full_name, short_name, active) in users_raw:
if email in existing_users:
continue
users.append((email, full_name, short_name, active))
existing_users.add(email)
users_to_create = []
for (email, full_name, short_name, active) in users:
users_to_create.append(create_user_base(email, initial_password(email),
active=active))
User.objects.bulk_create(users_to_create)
users_by_email = {}
for user in User.objects.all():
users_by_email[user.email] = user
# Now create user_profiles
profiles_to_create = []
for (email, full_name, short_name, active) in users:
user = users_by_email[email]
domain = email.split('@')[1]
profile = UserProfile(user=user, pointer=-1,
is_active=user.is_active,
is_staff=user.is_staff,
date_joined=user.date_joined,
email=user.email,
password=user.password,
realm=realms[domain],
full_name=full_name, short_name=short_name)
profile.api_key = initial_api_key(email)
profile = create_user_profile(realms[domain], email,
initial_password(email), active,
full_name, short_name)
profiles_to_create.append(profile)
UserProfile.objects.bulk_create(profiles_to_create)

View File

@@ -62,29 +62,14 @@ def user_profile_by_email_cache_key(email):
# with high likelihood be ASCII-only for the foreseeable future.
return 'user_profile_by_email:%s' % (make_safe_digest(email),)
def user_profile_by_user_cache_key(user_id):
return 'user_profile_by_user_id:%d' % (user_id,)
def user_profile_by_id_cache_key(user_profile_id):
return "user_profile_by_id:%s" % (user_profile_id,)
def user_by_id_cache_key(user_id):
return 'user_by_id:%d' % (user_id,)
# Called by models.py to flush the user_profile cache whenever we save
# a user_profile object
def update_user_profile_cache(sender, **kwargs):
user_profile = kwargs['instance']
items_for_memcached = {}
items_for_memcached[user_profile_by_email_cache_key(user_profile.email)] = (user_profile,)
items_for_memcached[user_profile_by_user_cache_key(user_profile.user.id)] = (user_profile,)
items_for_memcached[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,)
djcache.set_many(items_for_memcached)
# Called by models.py to flush the user_profile cache whenever we save
# a user_profile object
def update_user_cache(sender, **kwargs):
user = kwargs['instance']
items_for_memcached = {}
items_for_memcached[user_by_id_cache_key(user.id)] = (user,)
djcache.set_many(items_for_memcached)

View File

@@ -5,8 +5,7 @@ from zephyr.models import Message, UserProfile, Stream, get_stream_cache_key, \
Recipient, get_recipient_cache_key, Client, get_client_cache_key, \
Huddle, huddle_hash_cache_key
from zephyr.lib.cache import cache_with_key, djcache, message_cache_key, \
user_profile_by_email_cache_key, user_profile_by_user_cache_key, \
user_by_id_cache_key, user_profile_by_id_cache_key
user_profile_by_email_cache_key, user_profile_by_id_cache_key
import logging
from django.db import connection
@@ -24,8 +23,6 @@ def message_cache_items(items_for_memcached, message):
def user_cache_items(items_for_memcached, user_profile):
items_for_memcached[user_profile_by_email_cache_key(user_profile.email)] = (user_profile,)
items_for_memcached[user_profile_by_user_cache_key(user_profile.user.id)] = (user_profile,)
items_for_memcached[user_by_id_cache_key(user_profile.user.id)] = (user_profile.user,)
items_for_memcached[user_profile_by_id_cache_key(user_profile.id)] = (user_profile,)
def stream_cache_items(items_for_memcached, stream):

View File

@@ -1,39 +1,39 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.models import UserManager
from django.utils import timezone
from zephyr.models import UserProfile
from zephyr.lib.initial_password import initial_api_key
from zephyr.models import UserProfile, Recipient, Subscription
import base64
import hashlib
# create_user_hack is the same as Django's User.objects.create_user,
# create_user_profile is based on 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, active):
#
# Only use this for bulk_create -- for normal usage one should use
# create_user (below) which will also make the Subscription and
# Recipient objects
def create_user_profile(realm, email, password, active, full_name, short_name):
now = timezone.now()
email = UserManager.normalize_email(email)
user = User(username=username, email=email,
is_staff=False, is_active=active, is_superuser=False,
last_login=now, date_joined=now)
user_profile = UserProfile(email=email, is_staff=False, is_active=active,
full_name=full_name, short_name=short_name,
last_login=now, date_joined=now, realm=realm,
pointer=-1)
if active:
user.set_password(password)
user_profile.set_password(password)
else:
user.set_unusable_password()
return user
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, active)
user_profile.set_unusable_password()
user_profile.api_key = initial_api_key(email)
return user_profile
def create_user(email, password, realm, full_name, short_name,
active=True):
user = create_user_base(email=email, password=password,
active=active)
user.save()
return UserProfile.create(user, realm, full_name, short_name)
user_profile = create_user_profile(realm, email, password, active,
full_name, short_name)
user_profile.save()
recipient = Recipient.objects.create(type_id=user_profile.id,
type=Recipient.PERSONAL)
Subscription.objects.create(user_profile=user_profile, recipient=recipient)
return user_profile

View File

@@ -1,7 +1,6 @@
from django.core.management.base import BaseCommand
from django.utils.timezone import utc, now
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from zephyr.models import Message, UserProfile, Stream, Recipient, Client, \
Subscription, Huddle, get_huddle, Realm, UserMessage, StreamColor, \

View File

@@ -1,10 +1,8 @@
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User, AbstractBaseUser, UserManager
from django.contrib.auth.models import AbstractBaseUser, UserManager
from zephyr.lib.cache import cache_with_key, update_user_profile_cache, \
update_user_cache, user_profile_by_id_cache_key, \
user_profile_by_email_cache_key
from zephyr.lib.initial_password import initial_api_key
user_profile_by_id_cache_key, user_profile_by_email_cache_key
from zephyr.lib.utils import make_safe_digest
import os
from django.db import transaction, IntegrityError
@@ -60,12 +58,6 @@ class UserProfile(AbstractBaseUser):
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
# User Legacy code: the object that goes with this UserProfile.
# Plan is to for a short time maintain them both in sync, then
# later we'll dump the old User field, and perhaps later than
# that, rename the surviving field to just User.
user = models.OneToOneField(User)
# Our custom site-specific fields
full_name = models.CharField(max_length=100)
short_name = models.CharField(max_length=100)
@@ -78,26 +70,6 @@ class UserProfile(AbstractBaseUser):
objects = UserManager()
@classmethod
def create(cls, user, realm, full_name, short_name):
"""When creating a new user, make a profile for him or her."""
if not cls.objects.filter(user=user):
profile = cls(user=user, pointer=-1, realm=realm,
# User Legacy code:
is_active=user.is_active,
is_staff=user.is_staff,
date_joined=user.date_joined,
email=user.email,
password=user.password,
# end User Legacy code
full_name=full_name, short_name=short_name)
profile.api_key = initial_api_key(user.email)
profile.save()
# Auto-sub to the ability to receive personals.
recipient = Recipient.objects.create(type_id=profile.id, type=Recipient.PERSONAL)
Subscription.objects.create(user_profile=profile, recipient=recipient)
return profile
def __repr__(self):
return (u"<UserProfile: %s %s>" % (self.email, self.realm)).encode("utf-8")
def __str__(self):
@@ -106,8 +78,6 @@ class UserProfile(AbstractBaseUser):
# Make sure we flush the UserProfile object from our memcached
# whenever we save it.
post_save.connect(update_user_profile_cache, sender=UserProfile)
# And the same for the User object
post_save.connect(update_user_cache, sender=User)
class PreregistrationUser(models.Model):
email = models.EmailField()
@@ -417,7 +387,7 @@ def filter_by_subscriptions(messages, user_profile):
return user_messages
def clear_database():
for model in [Message, Stream, UserProfile, User, Recipient,
for model in [Message, Stream, UserProfile, Recipient,
Realm, Subscription, Huddle, UserMessage, Client,
DefaultStream]:
model.objects.all().delete()

View File

@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.test import TestCase
from django.test.simple import DjangoTestSuiteRunner
from django.utils.timezone import now

View File

@@ -15,7 +15,7 @@ from django.core.mail import send_mail, mail_admins
from django.db import transaction
from zephyr.models import Message, UserProfile, Stream, Subscription, \
Recipient, get_huddle, Realm, UserMessage, \
PreregistrationUser, get_client, MitUser, User, UserActivity, \
PreregistrationUser, get_client, MitUser, UserActivity, \
MAX_SUBJECT_LENGTH, MAX_MESSAGE_LENGTH, get_stream, UserPresence, \
get_recipient, valid_stream_name
from zephyr.lib.actions import do_add_subscription, do_remove_subscription, \
@@ -35,8 +35,7 @@ from zephyr.decorator import require_post, \
has_request_variables, POST, authenticated_json_view, \
to_non_negative_int, json_to_dict, json_to_list, json_to_bool, \
JsonableError, RequestVariableMissingError, get_user_profile_by_email, \
get_user_profile_by_user_id, authenticated_rest_api_view, \
process_patch_as_post, REQ
authenticated_rest_api_view, process_patch_as_post, REQ
from zephyr.lib.query import last_n
from zephyr.lib.avatar import gravatar_hash
from zephyr.lib.response import json_success, json_error, json_response, json_method_not_allowed