Skip to content

Commit

Permalink
WCM-316: add title_overrides in covers of volume
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinnaj94 committed Sep 9, 2024
1 parent abb2054 commit 22ebee4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
37 changes: 25 additions & 12 deletions core/src/zeit/content/volume/browser/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ def __init__(self, context, request):
required=False,
source=zeit.content.image.interfaces.imageGroupSource,
)
field.__name__ = 'cover_%s_%s' % (product.id, name)
field.__name__ = f'cover_{product.id}_{name}'
field.interface = ICovers
self.form_fields += zope.formlib.form.FormFields(field)
fieldnames.append(field.__name__)
# In addition to covers, we give the possibility to override the title
field = zope.schema.Text(title=_('Title'), required=False)
field.__name__ = f'title_{product.id}'
field.interface = ICovers
self.form_fields += zope.formlib.form.FormFields(field)
fieldnames.append(field.__name__)
self.field_groups += (
gocept.form.grouped.Fields(product.title, fieldnames, css_class='column-right'),
)
Expand Down Expand Up @@ -156,17 +162,24 @@ class Covers(grok.Adapter):
grok.context(zeit.content.volume.interfaces.IVolume)

def __getattr__(self, name):
if not name.startswith('cover_'):
return super().__getattr__(name)
name = name.replace('cover_', '', 1)
product, cover = name.split('_')
# We dont want the fallback in the UI
return self.context.get_cover(cover, product, use_fallback=False)
if name.startswith('title_'):
product = name.split('_')[1]
return self.context.get_cover_title(product)
elif name.startswith('cover_'):
name = name.replace('cover_', '', 1)
product, cover = name.split('_')
# We dont want the fallback in the UI
return self.context.get_cover(cover, product, use_fallback=False)
return super().__getattr__(name)

def __setattr__(self, name, value):
if not name.startswith('cover_'):
super().__setattr__(name, value)
if name.startswith('title_'):
product = name.split('_')[1]
self.context.set_cover_title(product, value)
return
name = name.replace('cover_', '', 1)
product, cover = name.split('_')
self.context.set_cover(cover, product, value)
elif name.startswith('cover_'):
name = name.replace('cover_', '', 1)
product, cover = name.split('_')
self.context.set_cover(cover, product, value)
return
super().__setattr__(name, value)
18 changes: 18 additions & 0 deletions core/src/zeit/content/volume/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def set_cover(cover_id, product_id, image):
Set an image as a cover of product.
"""

def get_cover_title(product_id):
"""
Get a title of a product.
For example volume.get_title('ZEI') returns the title of DIE ZEIT of
this specific volume.
:param product_id: str product ID set in products.xml
:return: str
"""

def set_cover_title(product_id, title):
"""
Set cover specific title.
For example volume.set_title('ZEI', 'DIE ZEIT') sets the title of DIE
ZEIT of this specific volume.
:param product_id: str product ID set in products.xml
:param title: str - title of the product
"""

def all_content_via_search(additional_query_contstraints):
"""
Get all Content for this volume with a Elasticsearch-Lookup.
Expand Down
16 changes: 16 additions & 0 deletions core/src/zeit/content/volume/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Volume(zeit.cms.content.xmlsupport.XMLContentBase):
<volume>
<head/>
<body/>
<title-overrides/>
<covers/>
</volume>
"""
Expand Down Expand Up @@ -204,6 +205,21 @@ def set_cover(self, cover_id, product_id, imagegroup):
self.xml.find('covers').append(node)
super().__setattr__('_p_changed', True)

def get_cover_title(self, product_id):
path = '//volume/title-overrides/title[@product_id="{}"]'.format(product_id)
node = self.xml.xpath(path)
return node[0].text if node else None

def set_cover_title(self, product_id, title):
path = '//volume/title-overrides/title[@product_id="{}"]'.format(product_id)
node = self.xml.xpath(path)
if node:
self.xml.find('title-overrides').remove(node[0])
if title is not None:
node = lxml.builder.E.title(title, product_id=product_id)
self.xml.find('title-overrides').append(node)
super().__setattr__('_p_changed', True)

def _is_valid_cover_id_and_product_id(self, cover_id, product_id):
cover_ids = list(zeit.content.volume.interfaces.VOLUME_COVER_SOURCE(self))
product_ids = [prod.id for prod in self._all_products]
Expand Down

0 comments on commit 22ebee4

Please sign in to comment.