Skip to content

Instantly share code, notes, and snippets.

@cacheleocode
Created January 27, 2015 19:16
Show Gist options
  • Select an option

  • Save cacheleocode/1017e46bed5ff8ab43b2 to your computer and use it in GitHub Desktop.

Select an option

Save cacheleocode/1017e46bed5ff8ab43b2 to your computer and use it in GitHub Desktop.
determine if image is in top half of article
def selected_image(self, lang='en', user_langs=None):
"""
For display at the top of an article, show the largest selected image, as long as it's not near the top of the article body
"""
# attachments
if self.image_attachments():
# sort by size descending
attachments = sorted(self.image_attachments(), key=lambda x: x.file.size, reverse=True)
# check all attachments for selected attribute, and return the first (biggest) one
for a in attachments:
if a.selected:
filename = os.path.basename(a.file.name) # just the filename
filename = quoteattr(filename)[1:-1] # remove automatic encapsulating quotes
filename = escape(filename) # HTML encode
filename = filename.strip() # trim whitespace
body = self.body
#a.description = a.description(lang, user_langs)
#a.description = re.sub('<p[^>]*>(.+?)</p\s*>','\\1', a.description)
#character count deemed acceptable for showing featured image, to reduce chances of showing the same image twice in proximity at the top of the article
threshold = 1750
"""
this is what is in the body!!!
New Year&#39;s food table Loomis_sm.jpg
BUT, it has an escaped ampersand, so THIS is what's in there?!?
New Year&amp;#39;s food table Loomis_sm.jpg
"""
# debug, returns true if greater than threshold
# return body.find(filename) >= threshold
# if selected image is in the second half of article or not in the article, show it
if body.find(filename) >= threshold or body.find(filename) == -1:
return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment