<commit_before>from zeit.cms.i18n import MessageFactory as _
from zeit.push.twitter import twitterAccountSource
from zeit.push.facebook import facebookAccountSource
import grokcore.component as grok
import zeit.cms.browser.form
import zeit.edit.browser.form
import zope.formlib.form
import zope.interface


class Container(zeit.edit.browser.form.FoldableFormGroup):

    title = _('Social media')


class IAccounts(zope.interface.Interface):

    facebook = zope.schema.Bool(title=_('Enable Facebook'))
    facebook_ressort = zope.schema.Choice(
        title=_('Additional Facebook'),
        source=facebookAccountSource,
        required=False)
    twitter = zope.schema.Bool(title=_('Enable Twitter'))
    twitter_ressort = zope.schema.Choice(
        title=_('Additional Twitter'),
        source=twitterAccountSource,
        required=False)


class Social(zeit.edit.browser.form.InlineForm,
             zeit.cms.browser.form.CharlimitMixin):

    legend = _('')
    prefix = 'social'
    undo_description = _('edit social media')
    form_fields = (
        zope.formlib.form.FormFields(
            zeit.push.interfaces.IPushMessages).select('long_text')
        + zope.formlib.form.FormFields(
            IAccounts).select('facebook', 'facebook_ressort')
        +
        zope.formlib.form.FormFields(
            zeit.push.interfaces.IPushMessages).select('short_text')
        + zope.formlib.form.FormFields(
            IAccounts).select('twitter', 'twitter_ressort')
        + zope.formlib.form.FormFields(
            zeit.push.interfaces.IPushMessages).select('enabled')
    )

    def setUpWidgets(self, *args, **kw):
        super(Social, self).setUpWidgets(*args, **kw)
        self.set_charlimit('short_text')

    def success_handler(self, action, data, errors=None):
        message_config = [
            {'type': 'facebook',
             'enabled': data.get('facebook'),
             'account': facebookAccountSource(None).MAIN_ACCOUNT},
            {'type': 'twitter',
             'enabled': data.get('twitter'),
             'account': twitterAccountSource(None).MAIN_ACCOUNT}
        ]
        twitter_ressort = data.get('twitter_ressort')
        if twitter_ressort:
            message_config.append(
                {'type': 'twitter',
                 'enabled': True,
                 'account': twitter_ressort})
        zeit.push.interfaces.IPushMessages(
            self.context).message_config = message_config
        return super(Social, self).success_handler(action, data, errors)


class Accounts(grok.Adapter):

    grok.context(zeit.cms.interfaces.ICMSContent)
    grok.implements(IAccounts)

    @property
    def message_config(self):
        return zeit.push.interfaces.IPushMessages(
            self.context).message_config

    @property
    def facebook(self):
        for service in self.message_config:
            if service['type'] != 'facebook':
                continue
            if service['account'] != facebookAccountSource(None).MAIN_ACCOUNT:
                continue
            return service['enabled']
        return True

    @property
    def facebook_ressort(self):
        for service in self.message_config:
            if service['type'] != 'facebook':
                continue
            account = service.get('account')
            if account != facebookAccountSource(None).MAIN_ACCOUNT:
                return account
        return None

    @property
    def twitter(self):
        for service in self.message_config:
            if service['type'] != 'twitter':
                continue
            if service['account'] != twitterAccountSource(None).MAIN_ACCOUNT:
                continue
            return service['enabled']
        return True

    @property
    def twitter_ressort(self):
        for service in self.message_config:
            if service['type'] != 'twitter':
                continue
            account = service.get('account')
            if account != twitterAccountSource(None).MAIN_ACCOUNT:
                return account<commit_msg>Re VIV-371: Refactor to remove duplicated code<commit_after>from zeit.cms.i18n import MessageFactory as _
from zeit.push.twitter import twitterAccountSource
from zeit.push.facebook import facebookAccountSource
import grokcore.component as grok
import zeit.cms.browser.form
import zeit.edit.browser.form
import zope.formlib.form
import zope.interface


class Container(zeit.edit.browser.form.FoldableFormGroup):

    title = _('Social media')


class IAccounts(zope.interface.Interface):

    facebook = zope.schema.Bool(title=_('Enable Facebook'))
    facebook_ressort = zope.schema.Choice(
        title=_('Additional Facebook'),
        source=facebookAccountSource,
        required=False)
    twitter = zope.schema.Bool(title=_('Enable Twitter'))
    twitter_ressort = zope.schema.Choice(
        title=_('Additional Twitter'),
        source=twitterAccountSource,
        required=False)


class Social(zeit.edit.browser.form.InlineForm,
             zeit.cms.browser.form.CharlimitMixin):

    legend = _('')
    prefix = 'social'
    undo_description = _('edit social media')
    form_fields = (
        zope.formlib.form.FormFields(
            zeit.push.interfaces.IPushMessages).select('long_text')
        + zope.formlib.form.FormFields(
            IAccounts).select('facebook', 'facebook_ressort')
        +
        zope.formlib.form.FormFields(
            zeit.push.interfaces.IPushMessages).select('short_text')
        + zope.formlib.form.FormFields(
            IAccounts).select('twitter', 'twitter_ressort')
        + zope.formlib.form.FormFields(
            zeit.push.interfaces.IPushMessages).select('enabled')
    )

    def setUpWidgets(self, *args, **kw):
        super(Social, self).setUpWidgets(*args, **kw)
        self.set_charlimit('short_text')

    def success_handler(self, action, data, errors=None):
        message_config = [
            {'type': 'facebook',
             'enabled': data.get('facebook'),
             'account': facebookAccountSource(None).MAIN_ACCOUNT},
            {'type': 'twitter',
             'enabled': data.get('twitter'),
             'account': twitterAccountSource(None).MAIN_ACCOUNT}
        ]
        twitter_ressort = data.get('twitter_ressort')
        if twitter_ressort:
            message_config.append(
                {'type': 'twitter',
                 'enabled': True,
                 'account': twitter_ressort})
        zeit.push.interfaces.IPushMessages(
            self.context).message_config = message_config
        return super(Social, self).success_handler(action, data, errors)


class Accounts(grok.Adapter):

    grok.context(zeit.cms.interfaces.ICMSContent)
    grok.implements(IAccounts)

    @property
    def message_config(self):
        return zeit.push.interfaces.IPushMessages(
            self.context).message_config

    @property
    def facebook(self):
        service = self._get_service('facebook', main=True)
        if service is None:
            return True
        return service['enabled']

    @property
    def facebook_ressort(self):
        service = self._get_service('facebook', main=False)
        return service and service['account']

    @property
    def twitter(self):
        service = self._get_service('twitter', main=True)
        if service is None:
            return True
        return service['enabled']

    @property
    def twitter_ressort(self):
        service = self._get_service('twitter', main=False)
        return service and service['account']

    def _get_service(self, type_, main=True):
        source = {
            'twitter': twitterAccountSource,
            'facebook': facebookAccountSource,
        }[type_](None)

        for service in self.message_config:
            if service['type'] != type_:
                continue
            account = service.get('account')
            if not account:  # BBB
                continue
            is_main = (account == source.MAIN_ACCOUNT)
            if is_main == main:
                return service
{'fraction_non_alphanumeric': 0.10921968190854871, 'fraction_numerical': 0.0008697813121272366, 'mean_word_length': 2.4738886491152354, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1554521', 'n_tokens_mistral': 2281, 'n_tokens_neox': 2174, 'n_words': 418}