Skip to content

Commit

Permalink
Fix: use correct link URL when using attachment items
Browse files Browse the repository at this point in the history
Lektor sometimes yields invalid URLs for attachment records that have ‘alt’ set.
We work around this by forcing the primary alt when linking to attachments.
  • Loading branch information
eigengrau committed Aug 9, 2018
1 parent 202e366 commit cfe1101
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lektor_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ def build_artifact(self, artifact):
item,
ctx.env
) or blog_author

# FIXME Work-around Lektor #583. When the item is an attachment,
# we will get an invalid path here unless we force the
# `_primary` alt.
url = (
item.url_to(item.path, external=True, alt='_primary')
if item.is_attachment
else url_to(item, external=True)
)
base_url = url_to(
item.parent if item.is_attachment else item,
external=True
Expand All @@ -175,7 +182,7 @@ def build_artifact(self, artifact):
title,
body,
xml_base=base_url,
url=url_to(item, external=True),
url=url,
content_type='html',
id=get_id(u'%s/%s' % (
ctx.env.project.id,
Expand Down
9 changes: 9 additions & 0 deletions tests/demo-project/configs/atom.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ item_title = {{ this.title }} ({{ this.pub_date | dateformat }})

[feed-five.de]
name = Feed Fünf

[feed-six]
name = Attachments feed
source_path = /attachments-feed
items = site.get("/attachments-feed").attachments
item_title = {{ this.title }}
item_body =
blog_summary = Lots of downloads
blog_author = Kim Dotcom
Empty file.
2 changes: 2 additions & 0 deletions tests/demo-project/content/attachments-feed/1.txt.lr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: ASCII ramblings
---
6 changes: 6 additions & 0 deletions tests/demo-project/content/attachments-feed/contents.lr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_model: attachments-page
---
author: A. Jesse Jiryu Davis
---
summary: My Summary
---
6 changes: 6 additions & 0 deletions tests/demo-project/models/attachment.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[model]
name = Attached document
label = Attachment

[fields.title]
type = string
6 changes: 6 additions & 0 deletions tests/demo-project/models/attachments-page.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[model]
name = Document downloads page
label = Document downloads

[attachments]
model = attachment
Empty file.
30 changes: 29 additions & 1 deletion tests/test_lektor_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_discover_all(pad):
'''
all_feeds = set(['feed-one', 'feed-two',
'feed-three', 'feed-four',
'feed-five'])
'feed-five', 'feed-six'])
feeds_discovered = feeds_from_template(pad, template)
assert feeds_discovered == all_feeds

Expand Down Expand Up @@ -201,3 +201,31 @@ def test_localized_config(pad):
assert plugin.get_atom_config('feed-five', 'name', alt='de') \
== 'Feed Fünf'


def test_attachments_feed(pad, builder):
failures = builder.build_all()
assert not failures

def test_feed(path, prefix=''):
feed_path = os.path.join(builder.destination_path, prefix, path)
feed = objectify.parse(open(feed_path)).getroot()

assert 'Attachments feed' == feed.title
assert 'Lots of downloads' == str(feed.subtitle).strip()
assert 'html' == feed.subtitle.attrib['type']
assert 'Kim Dotcom' == feed.author.name
assert 'http://x.com/%sattachments-feed/' % prefix \
== feed.link[0].attrib['href']
assert 'http://x.com/%sattachments-feed/feed.xml' % prefix \
== feed.link[1].attrib['href']

assert len(feed.entry) == 1
attachment1 = feed.entry[0]
assert attachment1.title == "ASCII ramblings"
base = attachment1.attrib['{http://www.w3.org/XML/1998/namespace}base']
assert base == 'http://x.com/attachments-feed/'
assert attachment1.link.attrib['href'] \
== 'http://x.com/attachments-feed/1.txt'

test_feed('attachments-feed/feed.xml')
test_feed('attachments-feed/feed.xml', prefix='de/')

0 comments on commit cfe1101

Please sign in to comment.