On Jun 24, 11:57 am, Wim Feijen <wimfei...@gmail.com> wrote: > > Looking at it again, I saw what was wrong and fixed it! For > completeness, I moved the NewsItem query within the NewsItemBlockNode
FWIW, code at the module top-level is only executed when the module is first imported. Now you'll know ;) > and everything works fine! The current code is: > > import re > > from django.template import Variable, VariableDoesNotExist, Node, > Library, Template, TemplateSynta > from django.conf import settings > from django.utils.safestring import mark_safe > from django.utils.html import escape > from django.utils.translation import ugettext as _ > > register = Library() > > from cms.news.models import NewsItem > from django.db.models import Q > from datetime import datetime > > class NewsItemBlockNode(Node): > def __init__(self, identifier, field): > self.identifier = str(identifier) > self.field = str(field) > self.image = False > > def render(self, context): > try: > newsitems = NewsItem.objects.filter(Q(expires_at__gt = > datetime.now()) | Q(expires_at_ > except: Never use a bare except clause, unless you reraise the exception. > newsitems = None > > number = -1 > numbers = { > 'large': 0, > 'large-small': 0, > 'small1': 1, > 'small2': 2, > 'small3': 3, > 'small4': 4, > 'medium': 5, > } > > # Only image > if self.field == 'teaser-image': > self.field = 'teaser' > self.image = True Never change a Node's attributes from within the render method - this will screw you sooner or later. Use local variables instead (with the added bonus of improved performances - local variables are way faster than attribute lookups). > if self.identifier in numbers: > number = numbers[self.identifier] > > try: > newsitem = getattr(newsitems[number], self.field) > > if type(newsitem).__name__ == 'datetime': you may want to read about "isintance" > if self.identifier in ['medium', 'large']: use a tuple instead of a list - it's cheaper. > newsitem = newsitem.strftime('%d [%m] %Y') > > newsitem = newsitem.replace('[01]', 'januari') > newsitem = newsitem.replace('[02]', 'februari') > newsitem = newsitem.replace('[03]', 'maart') > newsitem = newsitem.replace('[04]', 'april') > newsitem = newsitem.replace('[05]', 'mei') > newsitem = newsitem.replace('[06]', 'juni') > newsitem = newsitem.replace('[07]', 'juli') > newsitem = newsitem.replace('[08]', 'augustus') > newsitem = newsitem.replace('[09]', 'september') > newsitem = newsitem.replace('[10]', 'oktober') > newsitem = newsitem.replace('[11]', 'november') > newsitem = newsitem.replace('[12]', 'december') You may want to have a read at Django's and Python's localisation features... > newsitem = newsitem.upper() > else: > newsitem = newsitem.strftime('%d-%m-%Y') > > if self.image: > # Filter only image > src = re.search('<img(.+)src="(.*?)"(.+)/>', newsitem) regexps are not the safest way to parse html. > if src and src.group(2): > newsitem = src.group(2) > else: > newsitem = '/media/site/img/blank.gif' Use named urls and reverse() instead - unless you are happy with maintenance headache, of course ;) > elif self.field == 'teaser': > # Filter image > newsitem = re.sub('<img (.+) />', '', newsitem) > newsitem = re.sub('<a.*?>', '', newsitem) > newsitem = re.sub('</a>', '', newsitem) > newsitem = re.sub(' ', '', newsitem) Proverbial SquaredWheel alert. Django (and Python) have safer ways to strip a text from HTML tags and entities. > except: Same as above. Exceptions and traceback are here to HELP you fixing your code, so don't shoot yourself in the foot by silently passing them. HTH -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.