<commit_before># -*- test-case-name: go.apps.opt_out.tests.test_vumi_app -*-

from datetime import datetime

from twisted.internet.defer import returnValue

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

from go.vumitools.account import UserAccount, PerAccountStore


class OptOut(Model):
    """An opt_out"""
    user_account = ForeignKey(UserAccount)
    message = Unicode(null=True)
    created_at = Timestamp(default=datetime.utcnow)


class OptOutStore(PerAccountStore):
    def setup_proxies(self):
        self.opt_outs = self.manager.proxy(OptOut)

    def opt_out_id(self, addr_type, addr_value):
        return "%s:%s" % (addr_type, addr_value)

    @Manager.calls_manager
    def new_opt_out(self, addr_type, addr_value, message):
        opt_out_id = self.opt_out_id(addr_type, addr_value)
        opt_out = self.opt_outs(opt_out_id,
                user_account=self.user_account_key,
                message=unicode(message.get('message_id')))
        yield opt_out.save()
        returnValue(opt_out)

    def get_opt_out(self, addr_type, addr_value):
        return self.opt_outs.load(self.opt_out_id(addr_type, addr_value))

    @Manager.calls_manager
    def delete_opt_out(self, addr_type, addr_value):
        opt_out = yield self.get_opt_out(addr_type, addr_value)
        if opt_out:
            yield opt_out.delete()

    def list_opt_outs(self):
        return self.list_keys(self.opt_outs)

    def opt_outs_for_addresses(self, addr_type, addresses):
        keys = ["%s:%s" % (addr_type, address) for address in addresses]
        mr = self.manager.mr_from_keys(self.opt_outs, keys)
        mr.filter_not_found()
        return mr.get_keys()<commit_msg>Fix opt_outs_for_addresses so that it now handles empty addresses lists.<commit_after># -*- test-case-name: go.apps.opt_out.tests.test_vumi_app -*-

from datetime import datetime

from twisted.internet.defer import returnValue

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

from go.vumitools.account import UserAccount, PerAccountStore


class OptOut(Model):
    """An opt_out"""
    user_account = ForeignKey(UserAccount)
    message = Unicode(null=True)
    created_at = Timestamp(default=datetime.utcnow)


class OptOutStore(PerAccountStore):
    def setup_proxies(self):
        self.opt_outs = self.manager.proxy(OptOut)

    def opt_out_id(self, addr_type, addr_value):
        return "%s:%s" % (addr_type, addr_value)

    @Manager.calls_manager
    def new_opt_out(self, addr_type, addr_value, message):
        opt_out_id = self.opt_out_id(addr_type, addr_value)
        opt_out = self.opt_outs(opt_out_id,
                user_account=self.user_account_key,
                message=unicode(message.get('message_id')))
        yield opt_out.save()
        returnValue(opt_out)

    def get_opt_out(self, addr_type, addr_value):
        return self.opt_outs.load(self.opt_out_id(addr_type, addr_value))

    @Manager.calls_manager
    def delete_opt_out(self, addr_type, addr_value):
        opt_out = yield self.get_opt_out(addr_type, addr_value)
        if opt_out:
            yield opt_out.delete()

    def list_opt_outs(self):
        return self.list_keys(self.opt_outs)

    @Manager.calls_manager
    def opt_outs_for_addresses(self, addr_type, addresses):
        if not addresses:
            returnValue([])
        keys = ["%s:%s" % (addr_type, address) for address in addresses]
        mr = self.manager.mr_from_keys(self.opt_outs, keys)
        mr.filter_not_found()
        opt_out_keys = yield mr.get_keys()
        returnValue(opt_out_keys)
{'fraction_non_alphanumeric': 0.09880239520958084, 'fraction_numerical': 0.0, 'mean_word_length': 3.481707317073171, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2737497', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1202, 'n_words': 233}