Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Merge branch 'feature/issue-1132-add-a-disable-optouts-field-to-usera…
Browse files Browse the repository at this point in the history
…ccount-model' into develop
  • Loading branch information
justinvdm committed Jan 6, 2015
2 parents 21ea17f + 23b2a3b commit a9913ab
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 2 deletions.
36 changes: 36 additions & 0 deletions go/vumitools/account/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,39 @@ def migrate_from_3(self, mdata):
mdata.set_value('$VERSION', 4)

return mdata

def migrate_from_4(self, mdata):
"""
Add the disable_optouts boolean and default it to ``False``
"""

# Copy stuff that hasn't changed between versions
mdata.copy_values(
'username', 'created_at', 'msisdn', 'confirm_start_conversation',
'tags', 'event_handler_config', 'routing_table',
'can_manage_optouts')
mdata.copy_indexes('tagpools_bin', 'applications_bin')

# set the default `disable_optouts` value
mdata.set_value('disable_optouts', False)

# increment version counter
mdata.set_value('$VERSION', 5)

return mdata

def reverse_from_5(self, mdata):
"""
Remove disable_optouts boolean.
"""
# Copy stuff that hasn't changed between versions
mdata.copy_values(
'username', 'created_at', 'msisdn', 'confirm_start_conversation',
'tags', 'event_handler_config', 'routing_table',
'can_manage_optouts')
mdata.copy_indexes('tagpools_bin', 'applications_bin')

# decrement version counter
mdata.set_value('$VERSION', 4)

return mdata
3 changes: 2 additions & 1 deletion go/vumitools/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UserAppPermission(Model):
class UserAccount(Model):
"""A user account."""

VERSION = 4
VERSION = 5
MIGRATOR = UserAccountMigrator

# key is uuid
Expand All @@ -42,6 +42,7 @@ class UserAccount(Model):
event_handler_config = Json(default=list)
msisdn = Unicode(max_length=255, null=True)
confirm_start_conversation = Boolean(default=False)
disable_optouts = Boolean(default=False)
can_manage_optouts = Boolean(default=False)
email_summary = Unicode(max_length=255, null=True)
tags = Json(default=[])
Expand Down
73 changes: 73 additions & 0 deletions go/vumitools/account/old_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def has_tagpool_permission(self, tagpool):
class UserAccountV3(Model):
"""A user account."""

bucket = "useraccount"
VERSION = 3
MIGRATOR = UserAccountMigrator

Expand Down Expand Up @@ -184,7 +185,9 @@ def has_tagpool_permission(self, tagpool):
class UserAccountV4(Model):
"""A user account."""

bucket = "useraccount"
VERSION = 4
MIGRATOR = UserAccountMigrator

# key is uuid
username = Unicode(max_length=255)
Expand All @@ -210,6 +213,38 @@ def has_tagpool_permission(self, tagpool):
returnValue(False)


class UserAccountV5(Model):
"""A user account."""

bucket = "useraccount"
VERSION = 5
MIGRATOR = UserAccountMigrator

# key is uuid
username = Unicode(max_length=255)
# TODO: tagpools can be made OneToMany once vumi.persist.fields
# gains a OneToMany field
tagpools = ManyToMany(UserTagPermissionVNone)
applications = ManyToMany(UserAppPermissionVNone)
created_at = Timestamp(default=datetime.utcnow)
event_handler_config = Json(default=list)
msisdn = Unicode(max_length=255, null=True)
confirm_start_conversation = Boolean(default=False)
disable_optouts = Boolean(default=False)
can_manage_optouts = Boolean(default=False)
email_summary = Unicode(max_length=255, null=True)
tags = Json(default=[])
routing_table = RoutingTableField(default=RoutingTable({}))

@Manager.calls_manager
def has_tagpool_permission(self, tagpool):
for tp_bunch in self.tagpools.load_all_bunches():
for tp in (yield tp_bunch):
if tp.tagpool == tagpool:
returnValue(True)
returnValue(False)


class AccountStoreV2(object):
def __init__(self, manager):
self.manager = manager
Expand All @@ -227,3 +262,41 @@ def new_user(self, username):

def get_user(self, key):
return self.users.load(key)


class AccountStoreV4(object):
def __init__(self, manager):
self.manager = manager
self.users = self.manager.proxy(UserAccountV4)
self.tag_permissions = self.manager.proxy(UserTagPermissionVNone)
self.application_permissions = self.manager.proxy(
UserAppPermissionVNone)

@Manager.calls_manager
def new_user(self, username):
key = uuid4().get_hex()
user = self.users(key, username=username)
yield user.save()
returnValue(user)

def get_user(self, key):
return self.users.load(key)


class AccountStoreV5(object):
def __init__(self, manager):
self.manager = manager
self.users = self.manager.proxy(UserAccountV5)
self.tag_permissions = self.manager.proxy(UserTagPermissionVNone)
self.application_permissions = self.manager.proxy(
UserAppPermissionVNone)

@Manager.calls_manager
def new_user(self, username):
key = uuid4().get_hex()
user = self.users(key, username=username)
yield user.save()
returnValue(user)

def get_user(self, key):
return self.users.load(key)
54 changes: 53 additions & 1 deletion go/vumitools/account/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from go.vumitools.account.models import AccountStore
from go.vumitools.account.old_models import (
AccountStoreVNone, AccountStoreV1, AccountStoreV2)
AccountStoreVNone, AccountStoreV1, AccountStoreV2,
AccountStoreV4)
from go.vumitools.routing_table import RoutingTable


Expand All @@ -18,10 +19,17 @@ def setUp(self):
self.store = AccountStore(riak_manager)

# Some old stores for testing migrations.
self.store_v4 = AccountStoreV4(riak_manager)
self.store_v2 = AccountStoreV2(riak_manager)
self.store_v1 = AccountStoreV1(riak_manager)
self.store_vnone = AccountStoreVNone(riak_manager)

def store_user_version(self, version):
# Configure the manager to save the older message version.
modelcls = self.store.users._modelcls
model_name = "%s.%s" % (modelcls.__module__, modelcls.__name__)
self.store.manager.store_versions[model_name] = version

def assert_user(self, user, **fields):
def assert_field(value, name, default):
self.assertEqual(fields.get(name, default), value, name)
Expand All @@ -34,6 +42,26 @@ def assert_field(value, name, default):
assert_field(user.msisdn, 'msisdn', None)
assert_field(user.confirm_start_conversation,
'confirm_start_conversation', False)
assert_field(user.can_manage_optouts, 'can_manage_optouts', False)
assert_field(user.disable_optouts, 'disable_optouts', False)
assert_field(user.email_summary, 'email_summary', None)
assert_field(user.tags, 'tags', [])
assert_field(user.routing_table, 'routing_table', RoutingTable({}))

def assert_user_v4(self, user, **fields):
def assert_field(value, name, default):
self.assertEqual(fields.get(name, default), value, name)

assert_field(user.username, 'username', u'testuser')
assert_field(user.tagpools.keys(), 'tagpools', [])
assert_field(user.applications.keys(), 'applications', [])
assert_field(user.created_at, 'created_at', UTCNearNow())
assert_field(user.event_handler_config, 'event_handler_config', [])
assert_field(user.msisdn, 'msisdn', None)
assert_field(user.confirm_start_conversation,
'confirm_start_conversation', False)
assert_field(user.can_manage_optouts, 'can_manage_optouts', False)
assert_field(user.email_summary, 'email_summary', None)
assert_field(user.tags, 'tags', [])
assert_field(user.routing_table, 'routing_table', RoutingTable({}))

Expand Down Expand Up @@ -104,3 +132,27 @@ def test_migrate_new_from_vnone(self):
self.assert_user_vnone(user_vnone)
user = yield self.store.get_user(user_vnone.key)
self.assert_user(user)

@inlineCallbacks
def test_migrate_new_from_4(self):
"""
A v4 model can be migrated to the current model version.
"""
user_v4 = yield self.store_v4.new_user(u'testuser')
self.assert_user_v4(user_v4)
user = yield self.store.get_user(user_v4.key)
self.assert_user(user)

@inlineCallbacks
def test_reverse_4_from_new(self):
"""
The current model version can be migrated to a v4 model.
"""
user = yield self.store.new_user(u'testuser')
self.assert_user(user)

self.store_user_version(4)
yield user.save()

user_v4 = yield self.store_v4.get_user(user.key)
self.assert_user_v4(user_v4)

0 comments on commit a9913ab

Please sign in to comment.