# -*- test-case-name: go.vumitools.tests.test_contact -*-

from uuid import uuid4
from datetime import datetime

from twisted.internet.defer import returnValue

from vumi.persist.model import Model, Manager
from vumi.persist.fields import Unicode, Timestamp


class UserAccount(Model):
    """A user account."""
    # key is uuid
    username = Unicode(max_length=255)
    created_at = Timestamp(default=datetime.utcnow)


class AccountStore(object):
    def __init__(self, manager):
        self.manager = manager
        self.users = self.manager.proxy(UserAccount)

    @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 PerAccountStore(object):
    def __init__(self, base_manager, user_account_key):
        self.base_manager = base_manager
        self.user_account_key = user_account_key
        self.manager = self.base_manager.sub_manager(user_account_key)
        self.setup_proxies()

    @classmethod
    def from_django_user(cls, user):
        """Convenience constructor for using this from Django."""
        user_account = user.userprofile.get_user_account()
        return cls.from_user_account(user_account)

    @classmethod
    def from_user_account(cls, user_account):
        """Convenience constructor for using this a UserAccount."""
        return cls(user_account.manager, user_account.key)

    def get_user_account(self):
        store = AccountStore(self.base_manager)
        return store.users.load(self.user_account_key)

    def setup_proxies(self):
        pass

@@ -231,16 +231,25 @@
 s import
+ Integer,
  Unicode
@@ -263,118 +263,473 @@
 tamp
-%0A%0A%0Aclass UserAccount(Model):%0A    %22%22%22A user account.%22%22%22%0A    # key is uuid%0A    username = Unicode(max_length=255
+, ManyToMany%0A%0A%0Aclass UserTagPermission(Model):%0A    %22%22%22A description of a tag a user account is allowed access to.%22%22%22%0A    # key is uuid%0A    tagpool = Unicode(max_length=255)%0A    max_keys = Integer(null=True)%0A%0A%0Aclass UserAccount(Model):%0A    %22%22%22A user account.%22%22%22%0A    # key is uuid%0A    username = Unicode(max_length=255)%0A    # TODO: tagpools can be made OneToMany once vumi.persist.fields%0A    #       gains a OneToMany field%0A    tagpools = ManyToMany(UserTagPermission
 )%0A  

Add tapools information to user accounts.