Skip to content

Commit

Permalink
WCM-248: Use feature_toggles.xml in zeit.connector after move to conf…
Browse files Browse the repository at this point in the history
…ig repo
  • Loading branch information
louika committed Sep 2, 2024
1 parent 0bc4c7c commit c95bb7e
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 43 deletions.
1 change: 1 addition & 0 deletions core/docs/changelog/WCM-248.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WCM-248: Use feature_toggle.xml source in zeit.conntector
14 changes: 7 additions & 7 deletions core/src/zeit/cms/content/dav.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import zope.xmlpickle

from zeit.cms.content.interfaces import WRITEABLE_ON_CHECKIN
from zeit.connector.interfaces import feature_toggle as connector_toggle
from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.connector.models import ContentWithMetadataColumns as ConnectorModel
from zeit.connector.resource import PropertyKey
import zeit.cms.content.caching
Expand Down Expand Up @@ -194,12 +194,12 @@ def __init__(self, context, properties, propertykey):
self.has_sql_type = ConnectorModel.column_by_name(*propertykey) is not None

def fromProperty(self, value):
if self.has_sql_type and connector_toggle('read_metadata_columns'):
if self.has_sql_type and FEATURE_TOGGLES.find('read_metadata_columns'):
return value
return super().fromProperty(value)

def toProperty(self, value):
if self.has_sql_type and connector_toggle('write_metadata_columns'):
if self.has_sql_type and FEATURE_TOGGLES.find('write_metadata_columns'):
return value
return super().toProperty(value)

Expand Down Expand Up @@ -330,7 +330,7 @@ def __init__(self, context, properties, propertykey):
self.has_sql_type = ConnectorModel.column_by_name(*propertykey) is not None

def fromProperty(self, value):
if self.has_sql_type and connector_toggle('read_metadata_columns'):
if self.has_sql_type and FEATURE_TOGGLES.find('read_metadata_columns'):
return value
return self._fromProperty(value)

Expand All @@ -339,7 +339,7 @@ def _fromProperty(value):
return value.lower() in ('yes', 'true')

def toProperty(self, value):
if self.has_sql_type and connector_toggle('write_metadata_columns'):
if self.has_sql_type and FEATURE_TOGGLES.find('write_metadata_columns'):
return value
return self._toProperty(value)

Expand All @@ -360,7 +360,7 @@ def __init__(self, context, properties, propertykey):
def fromProperty(self, value):
if not value:
return None
if self.has_sql_type and connector_toggle('read_metadata_columns'):
if self.has_sql_type and FEATURE_TOGGLES.find('read_metadata_columns'):
return value
return self._fromProperty(value)

Expand All @@ -372,7 +372,7 @@ def _fromProperty(value):
return date.in_tz('UTC')

def toProperty(self, value):
if self.has_sql_type and connector_toggle('write_metadata_columns'):
if self.has_sql_type and FEATURE_TOGGLES.find('write_metadata_columns'):
return value
return self._toProperty(value)

Expand Down
5 changes: 3 additions & 2 deletions core/src/zeit/connector/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import zope.app.file.image
import zope.interface

from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.connector.connector import CannonicalId
from zeit.connector.interfaces import ID_NAMESPACE, feature_toggle
from zeit.connector.interfaces import ID_NAMESPACE
from zeit.connector.models import ContentWithMetadataColumns as Content
import zeit.cms.config
import zeit.cms.content.dav
Expand Down Expand Up @@ -265,7 +266,7 @@ def _get_properties(self, id):
return properties

def _convert_sql_types(self, properties):
if not feature_toggle('read_metadata_columns'):
if not FEATURE_TOGGLES.find('read_metadata_columns'):
return
for key, value in properties.items():
column = Content.column_by_name(*key)
Expand Down
11 changes: 0 additions & 11 deletions core/src/zeit/connector/interfaces.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from ast import literal_eval
from enum import Enum

import zope.interface
import zope.interface.common.mapping
import zope.schema

import zeit.cms.config


class _DeleteProperty:
"""Singleton to indicate a property should be deleted."""
Expand Down Expand Up @@ -337,11 +334,3 @@ class LockStatus(Enum):
FOREIGN = 1
OWN = 2
TIMED_OUT = 3


def feature_toggle(key):
"""Temp workaround to use feature toggles in connector instead of feature toggles
from zeit.cms.content.sources.
Using feature toggles from a file which is stored inside the database
inside the connector is unfortunately not possible"""
return literal_eval(zeit.cms.config.get('zeit.connector', key, 'False'))
4 changes: 2 additions & 2 deletions core/src/zeit/connector/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pytz
import zope.event

from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.connector.connector import CannonicalId
from zeit.connector.filesystem import DefaultConverter
from zeit.connector.interfaces import (
Expand All @@ -21,7 +22,6 @@
LockedByOtherSystemError,
LockingError,
MoveError,
feature_toggle,
)
from zeit.connector.lock import lock_is_foreign
from zeit.connector.models import ContentWithMetadataColumns as Content
Expand Down Expand Up @@ -349,7 +349,7 @@ def _set_properties(self, id, properties):
stored_properties.pop((name, namespace), None)
continue

if feature_toggle('write_metadata_columns'):
if FEATURE_TOGGLES.find('write_metadata_columns'):
column = Content.column_by_name(name, namespace)
converter = zeit.connector.filesystem.IConverter(column)
value = converter.serialize(value)
Expand Down
7 changes: 4 additions & 3 deletions core/src/zeit/connector/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import pytz
import sqlalchemy

from zeit.connector.interfaces import INTERNAL_PROPERTY, DeleteProperty, LockStatus, feature_toggle
from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.connector.interfaces import INTERNAL_PROPERTY, DeleteProperty, LockStatus
from zeit.connector.lock import lock_is_foreign
import zeit.connector.interfaces

Expand Down Expand Up @@ -133,7 +134,7 @@ def to_webdav(self):
props[('is_collection', INTERNAL_PROPERTY)] = self.is_collection
props[('body_checksum', INTERNAL_PROPERTY)] = self._body_checksum()

if feature_toggle('read_metadata_columns'):
if FEATURE_TOGGLES.find('read_metadata_columns'):
for column in self._columns_with_name():
namespace, name = column.info['namespace'], column.info['name']
props[(name, self.NS + namespace)] = getattr(self, column.name)
Expand All @@ -159,7 +160,7 @@ def from_webdav(self, props):
if type:
self.type = type

if feature_toggle('write_metadata_columns'):
if FEATURE_TOGGLES.find('write_metadata_columns'):
for column in self._columns_with_name():
namespace, name = column.info['namespace'], column.info['name']
value = props.get((name, self.NS + namespace), self)
Expand Down
4 changes: 2 additions & 2 deletions core/src/zeit/connector/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import zope.interface
import zope.sqlalchemy

from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.cms.interfaces import DOCUMENT_SCHEMA_NS
from zeit.cms.repository.interfaces import ConflictError
from zeit.connector.interfaces import (
Expand All @@ -43,7 +44,6 @@
LockingError,
LockStatus,
MoveError,
feature_toggle,
)
from zeit.connector.models import ID_NAMESPACE
import zeit.cms.cli
Expand Down Expand Up @@ -601,7 +601,7 @@ def _build_filter(self, expr):
(var, value) = expr.operands
name = var.name
namespace = var.namespace.replace(self.Content.NS, '', 1)
if feature_toggle('read_metadata_columns'):
if FEATURE_TOGGLES.find('read_metadata_columns'):
column = self.Content.column_by_name(name, namespace)
if column is not None:
return column == value
Expand Down
10 changes: 3 additions & 7 deletions core/src/zeit/connector/tests/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import zope.interface.verify

from zeit.cms.checkout.helper import checked_out
from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.cms.testcontenttype.testcontenttype import ExampleContentType
from zeit.connector.interfaces import (
CopyError,
Expand Down Expand Up @@ -922,15 +923,10 @@ class ContractZopeSQL(
class ContractProperties:
def setUp(self):
super().setUp()
zeit.cms.config.set('zeit.connector', 'read_metadata_columns', 'True')
zeit.cms.config.set('zeit.connector', 'write_metadata_columns', 'True')
FEATURE_TOGGLES.set('read_metadata_columns', 'True')
FEATURE_TOGGLES.set('write_metadata_columns', 'True')
self.repository['testcontent'] = ExampleContentType()

def tearDown(self):
zeit.cms.config.set('zeit.connector', 'read_metadata_columns', 'False')
zeit.cms.config.set('zeit.connector', 'write_metadata_columns', 'False')
super().tearDown()

def test_converts_scalar_types_on_read(self):
self.repository.connector.changeProperties(
'http://xml.zeit.de/testcontent',
Expand Down
14 changes: 5 additions & 9 deletions core/src/zeit/connector/tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytz
import transaction

from zeit.cms.content.sources import FEATURE_TOGGLES
from zeit.cms.repository.interfaces import ConflictError
from zeit.connector.interfaces import INTERNAL_PROPERTY
from zeit.connector.models import Content, Lock
Expand Down Expand Up @@ -303,26 +304,21 @@ def test_binary_has_empty_checksum(self):
class PropertiesColumnTest(zeit.connector.testing.SQLTest):
layer = zeit.connector.testing.SQL_CONTENT_LAYER

def tearDown(self):
zeit.cms.config.set('zeit.connector', 'read_metadata_columns', 'False')
zeit.cms.config.set('zeit.connector', 'write_metadata_columns', 'False')
super().tearDown()

def test_properties_can_be_stored_in_separate_columns(self):
zeit.cms.config.set('zeit.connector', 'write_metadata_columns', 'True')
zeit.cms.config.set('zeit.connector', 'read_metadata_columns', 'True')
FEATURE_TOGGLES.set('write_metadata_columns', True)
FEATURE_TOGGLES.set('read_metadata_columns', True)
res = self.add_resource('foo', properties={('access', Content.NS + 'document'): 'foo'})
self.assertEqual('foo', res.properties[('access', Content.NS + 'document')])
content = self.connector._get_content(res.id)
self.assertEqual('foo', content.access)

def test_search_looks_in_columns_or_unsorted_depending_on_toggle(self):
zeit.cms.config.set('zeit.connector', 'write_metadata_columns', 'True')
FEATURE_TOGGLES.set('write_metadata_columns', True)

res = self.add_resource('foo', properties={('access', Content.NS + 'document'): 'foo'})
access = SearchVar('access', 'http://namespaces.zeit.de/CMS/document')
for toggle in [False, True]: # XXX parametrize would be nice
zeit.cms.config.set('zeit.connector', 'read_metadata_columns', str(toggle))
FEATURE_TOGGLES.set('read_metadata_columns', toggle)
if toggle:
self.connector._get_content(res.id).unsorted = {}
transaction.commit()
Expand Down

0 comments on commit c95bb7e

Please sign in to comment.