from __future__ import absolute_import
from datetime import datetime
from dimagi.ext.couchdbkit import *
from dimagi.utils.decorators.memoized import memoized


TOGGLE_ID_PREFIX = 'hqFeatureToggle'


class Toggle(Document):
    """
    A very simple implementation of a feature toggle. Just a list of items
    attached to a slug.
    """
    slug = StringProperty()
    enabled_users = ListProperty()
    last_modified = DateTimeProperty()

    def save(self, **params):
        if ('_id' not in self._doc):
            self._doc['_id'] = generate_toggle_id(self.slug)
        self.last_modified = datetime.utcnow()
        super(Toggle, self).save(**params)

    @classmethod
    def get(cls, docid, rev=None, db=None, dynamic_properties=True):
        if not docid.startswith(TOGGLE_ID_PREFIX):
            docid = generate_toggle_id(docid)
        return super(Toggle, cls).get(docid, rev=None, db=None, dynamic_properties=True)

    def add(self, item):
        """
        Adds an item to the toggle. Only saves if necessary.
        """
        if item not in self.enabled_users:
            self.enabled_users.append(item)
            self.save()

    def remove(self, item):
        """
        Removes an item from the toggle. Only saves if necessary.
        """
        if item in self.enabled_users:
            self.enabled_users.remove(item)
            self.save()

    @memoized
    def get_supported_domain_names(self):
        return [user.split(':')[1] for user in self.enabled_users
                if user.split(':')[0] == 'domain' and user.split(':')[1]]


def generate_toggle_id(slug):
    # use the slug to build the ID to avoid needing couch views
    # and to make looking up in futon easier
    return '{prefix}-{slug}'.format(prefix=TOGGLE_ID_PREFIX, slug=slug)

@@ -102,62 +102,8 @@
 t *%0A
-from dimagi.utils.decorators.memoized import memoized%0A
 %0A%0ATO
@@ -1325,205 +1325,8 @@
 ()%0A%0A
-    @memoized%0A    def get_supported_domain_names(self):%0A        return %5Buser.split(':')%5B1%5D for user in self.enabled_users%0A                if user.split(':')%5B0%5D == 'domain' and user.split(':')%5B1%5D%5D%0A%0A
 %0Adef

remove get_supported_domain_names as not needed anymore [skip ci]