def recent_comments(): comments_q = meta.Session.query(model.Comment).filter(model.Comment.approved==True) comments_q = comments_q.order_by(model.comments_table.c.created_on.desc()) recent_comments = comments_q.join(model.Post).limit(4) if recent_comments is None: return '' else: comments= [] template = """ <div id="wurdig-recent-comments" class="wurdig-sidebar-list"> <h2>Newest Comments</h2> <ul> %s </ul> </div> """ for comment in recent_comments: i = """ <li class="%s"> <span class="lone">%s shared: </span> <span>%s</span> <span>Shared in: %s</span> </li> """ name = comment.name if comment.url is not None: name = h.link_to(comment.name, comment.url) content = h.truncate(h.strip_tags(comment.content), 80) link = h.link_to( comment.posts.title, h.url_for( controller='post', action='view', year=comment.posts.posted_on.strftime('%Y'), month=comment.posts.posted_on.strftime('%m'), slug=comment.posts.slug, anchor=u"comment-" + str(comment.id) ) ) comments.append(i % (comment.id, name, content, link)) return template % '\n'.join(comments)
def page(content='', template=None, callback=None, css=None, js=None, title=None, search=None, actions=None, items=None, head=''): def render_search(value): if value == None: return '' elif value == '': clear = '<span class="clear"></span>' else: clear = '<span class="clear"><a href="%s"><img src="/static/images/remove_filter.png"></a></span>' % ('/'.join([''] + route + ['clear'])) return '<div class="search">%s</div>' % form_for('<input type="text" class="text_field" id="search_text" name="q" value="%s">%s<input class="search_button" type=submit value="Search">' % (value,clear), method='GET') header_layout = """ <table id="title_bar"><tr> <td id="title_bar_left"> <H1>%(title)s</H1> </td> <td id="title_bar_right"> %(actions)s %(search)s </td> </tr></table> """ if len(route)>1: breadcrumb = link_to(system.app.title,'/'+system.app.name) else: breadcrumb = '' system.result = page = Page() if title or actions: page_header = header_layout % dict(title=title or '', search=render_search(search), actions=as_actions(actions)) else: page_header = '' page.content = unisafe(page_header) + unisafe(render(unisafe(content), items)) page.css = css or '' page.js = js or '' page.callback = callback page.template = template or system.default_template page.head = head return page
def flickr(): flickr_feed = feedparser.parse('http://api.flickr.com/services/feeds/photos_public.gne?id=%s@N00&lang=en-us&format=atom' % config['flickr.id']) if len(flickr_feed.entries): items = [] template = """ <div id="wurdig-flickr-feed"> <h4>Public Flickr Stream</h4> <ul> %s </ul> </div> """ for entry in flickr_feed.entries[:12]: image = entry['enclosures'][0]['href'] image = image.replace('m.jpg', 's.jpg') i = '<li>%s</li>' % h.link_to( h.literal('<img src="%s" title="%s" alt="%s">' % (image, entry['title'], entry['title'])), entry['link'] ) items.append(i) return template % '\n'.join(items) else: return ''
def twitter(): twitter_feed = feedparser.parse("http://twitter.com/statuses/user_timeline/%s.rss" % config['twitter.user.screen_name']) if len(twitter_feed.entries): items = [] template = """ <div id="wurdig-twitter-feed" class="wurdig-sidebar-list"> <h4>Twitter Updates (<a href="http://twitter.com/%s">Follow</a>)</h4> <ul> %s </ul> </div> """ for entry in twitter_feed.entries[:4]: description = entry['description'].split(':', 1)[1] i = '<li><span class="lone">%s</span> <span>%s</span></li>' % (h.link_to( entry.updated[:14], entry['guid']), h.auto_link(description) ) items.append(i) return template % (config['twitter.user.screen_name'], '\n'.join(items)) else: return ''
def browse(items, labels=None, columns=None, fields=None, footer=None, title=None, actions=None, header=None, on_click=None, on_delete=None, on_remove=None, *args, **keywords): def trash_can(key,kind,action,**args): link = url_for(str(key),action,**args) tpl = '<td width=1px><a href="%(link)s"><img src="/themes/%(theme)s/images/%(kind)s.png" border=0 height=13px width=13px alt="%(kind)s"></a></td>' return tpl % dict(link=link, kind=kind, theme=system.theme) def getcol(item, name): try: return item[name] except: return '' if labels: if not columns: if len(items) and hasattr(items[0], 'get'): columns = [name_for(label).lower() for label in labels] elif len(items) and hasattr(items[0], '__len__') and len(items[0]): columns = range(len(labels)) else: columns = [name_for(label).lower() for label in labels] else: if columns: labels = columns else: if len(items) and hasattr(items[0], 'keys') and callable(getattr(items[0],'keys')): labels = columns = items[0].keys() elif len(items) and hasattr(items[0], '__dict__'): labels = columns = [items[0].__dict__.keys()] elif len(items) and hasattr(items[0], 'keys') and callable(getattr(items[0],'keys')): labels = columns = [items[0].keys()] elif len(items) and hasattr(items[0], '__len__') and len(items[0]): labels = items[0] columns = range(len(items[0])) items = items[1:] else: if len(items): raise Exception('%s' % hasattr(items[0],'__len__')) raise Exception('Unable to infer columns') columns = list(columns) labels = list(labels) if fields: #labels = [] lookup = fields.as_dict() for col in columns[len(labels):]: if col in lookup: label = lookup[col].label else: label = col labels.append(label) alist = [] for item in items: fields.initialize(item) flookup = fields.display_value() row = [flookup.get(col.upper(),getcol(item,col)) for col in columns] alist.append(row) else: alist = [[getcol(item,col) for col in columns] for item in items] if (on_click or on_delete or on_remove) and columns[0] <> '_id': columns = ['_id'] + columns t = [] for i in alist: t.append([i[0]] + i) alist = t odd_rowclass = 'dark' evn_rowclass = 'light' t = [] if labels: t.append( '<thead><tr>' ) colnum = 0 for label in labels: colnum += 1 t.append('<th>%s</th>' % label) if on_delete: t.append('<th colwidth=1px>%s</th>' % '') if on_remove: t.append('<th colwidth=1px>%s</th>' % '') t.append( '</tr></thead>' ) odd_rowclass = 'light' # if there is a header start line shading on 2nd line evn_rowclass = 'dark' t.append( '<tbody>' ) count = 0 for row in alist: count += 1 if count % 2: rowclass = odd_rowclass else: rowclass = evn_rowclass t.append( '<tr id="row-%s" class="%s">' % (count, rowclass) ) colnum = 0 for item in row: #wrapping = len(unicode(item)) < 40 and 'nowrap' or '' wrapping = len(unisafe(item)) < 80 and '<td nowrap>%s</td>' or '<td>%s</td>' if (on_click or on_delete or on_remove) and colnum == 0: key = item if on_click and (colnum == 1 or colnum==0 and len(row)==1): url = '/'.join(route[1:]+[str(key)]) #t.append('<td>%s</td>' % link_to(item, url, *args, **keywords)) t.append(wrapping % link_to(item, url, *args, **keywords)) elif colnum>0 or not (on_click or on_remove or on_delete): #t.append('<td %s>%s</td>' % (wrapping, item)) t.append(wrapping % unisafe(item)) colnum += 1 if on_delete: t.append(trash_can(key,'delete',on_delete)) if on_remove: t.append(trash_can(key,'remove',on_remove)) t.append( '</tr>' ) t.append( '</tbody>' ) if not count: t.append('<tr><td colspan=%s>None</td></tr>' % len(labels)) if not header: if title: header = '<div class="title">%s</div>' % title if actions: header += as_actions(actions) header_body = header and ('<div class="header">%s</div>' % header) or '' footer_body = footer and ('<div class="footer">%s</div>' % footer) or '' result = '\n'.join(['<div class="baselist">'] + [header_body] + ['<table>'] + t + ['</table>'] + [footer_body] + ['</div>']) return result
if head and on_delete: head.append(' ') if head and on_remove: head.append(' ') body = [] for count, row in enumerate(alist, start=1): body.append('<tr class="row-{}">'.format(count)) for colnum, item in enumerate(row): wrapping = (nowrap and len(unisafe(item)) < nowrap ) and '<td class="nowrap">{}</td>' or '<td>{}</td>' if (on_click or on_delete or on_remove) and colnum == 0: key = item if on_click and (colnum == 1 or colnum == 0 and len(row) == 1): url = '/'.join(route[1:] + [str(key)]) body.append( wrapping.format(link_to(item, url, *args, **keywords))) elif colnum > 0 or not (on_click or on_remove or on_delete): body.append(wrapping.format(unisafe(item))) if on_delete: body.append(trash_can(key, 'delete', on_delete)) if on_remove: body.append(trash_can(key, 'remove', on_remove)) body.append('</tr>') selector = hasattr(classed, '__iter__') and '.'.join(classed) or classed selector = "{}".format(selector.replace(' ', '.')) classed = hasattr(classed, '__iter__') and ' '.join(classed) or classed actions = actions and '<div class="actions">{}</div>'.format(
def render(self): def handle(tag, *args, **keywords): # first try filling tag with page attributes if hasattr(self, tag): attr = getattr(self, tag) if callable(attr): repl = attr(self, *args, **keywords) else: repl = attr return fill('<dz:', '>', repl, handle) # if that doesn't work look for an app helper elif tag in system.app.helpers: helper = system.app.helpers[tag] # if that doesn't work look for a system helper elif tag in system.helpers: helper = system.helpers[tag] # if that doesn't work look in the helpers module elif tag in helpers.__dict__ and callable(helpers.__dict__[tag]): """call functions in a module or module-like object""" helper = helpers.__dict__[tag] else: helper = None if helper: if callable(helper): repl = helper(*args, **keywords) else: repl = helper return fill('<dz:', '>', repl, handle) def set_setting(thing, name): if thing == 'template': self.template = name elif thing == 'app_title': self.app_title = name return '<!-- %s set to "%s" -->' % (thing, name) def render_snippet(system_snippet, page_snippet): return '\n'.join(system_snippet | OrderedSet([page_snippet])) def render_script_tags(system_scripts, page_scripts): scripts = system_scripts | page_scripts h = scripts and '\n <!-- Page Specific Scripts -->\n' or '' c = '\n'.join( ' <script type="text/javascript" src="{}"></script>'. format(t) for t in scripts) t = scripts and '\n\n' or '' return h + c + t def render_style_sheets(system_style_sheets, page_style_sheets): sheets = system_style_sheets | page_style_sheets h = sheets and '\n <!-- Page Specific Styles -->\n' or '' c = '\n'.join( ' <link rel="stylesheet" type="text/css" href="{}">'. format(t) for t in sheets) t = sheets and '\n\n' or '' return h + c + t DEFAULT_TEMPLATE = os.path.join(system.root, 'themes', 'default', 'default.html') self.theme = self.theme or system.app.theme or user.theme or system.theme if self.theme != system.theme: system.set_theme(self.theme) self.content = fill('<dz:set_', '>', self.content, set_setting) self.styles = render_style_sheets(system.styles, self.styles) self.css = render_snippet(system.css, self.css) self.libs = render_script_tags(system.libs, self.libs) self.js = render_snippet(system.js, self.js) self.head = render_snippet(system.head, self.head) self.tail = render_snippet(system.tail, self.tail) if len(route) > 1: breadcrumb = link_to(system.app.title, '/' + system.app.name) else: breadcrumb = '' template_pathname = system.theme_path if template_pathname: template_filename = os.path.join(template_pathname, self.template + '.html') if not os.path.exists(template_filename): if not self.template in ['index', 'content']: log.logger.warning('template missing (%s)' % (template_filename)) template_filename = os.path.join(template_pathname, 'default.html') self.tpl = template_pathname and tools.load( template_filename) or tools.load(DEFAULT_TEMPLATE) page_header = self.render_header() save_content = self.content self.content = page_header + self.content save_title = self.title del self.title content = fill('<dz:', '>', self.tpl, handle) self.title = save_title self.content = save_content if self.callback: content = fill('{{', '}}', content, self.callback) return HTMLResponse(content)
if count % 2: rowclass = odd_rowclass else: rowclass = evn_rowclass t.append( '<tr id="row-%s" class="%s">' % (count, rowclass) ) colnum = 0 for item in row: #wrapping = len(unicode(item)) < 40 and 'nowrap' or '' wrapping = len(unisafe(item)) < 80 and '<td nowrap>%s</td>' or '<td>%s</td>' if (on_click or on_delete or on_remove) and colnum == 0: key = item if on_click and (colnum == 1 or colnum==0 and len(row)==1): url = '/'.join(route[1:]+[str(key)]) #t.append('<td>%s</td>' % link_to(item, url, *args, **keywords)) t.append(wrapping % link_to(item, url, *args, **keywords)) elif colnum>0 or not (on_click or on_remove or on_delete): #t.append('<td %s>%s</td>' % (wrapping, item)) t.append(wrapping % unisafe(item)) colnum += 1 if on_delete: t.append(trash_can(key,'delete',on_delete)) if on_remove: t.append(trash_can(key,'remove',on_remove)) t.append( '</tr>' ) t.append( '</tbody>' )
head = labels[:] if head and on_delete: head.append(' ') if head and on_remove: head.append(' ') body = [] for count, row in enumerate(alist, start=1): body.append( '<tr class="row-{}">'.format(count) ) for colnum, item in enumerate(row): wrapping = (nowrap and len(unisafe(item)) < nowrap) and '<td class="nowrap">{}</td>' or '<td>{}</td>' if (on_click or on_delete or on_remove) and colnum == 0: key = item if on_click and (colnum == 1 or colnum==0 and len(row)==1): url = '/'.join(route[1:]+[str(key)]) body.append(wrapping.format(link_to(item, url, *args, **keywords))) elif colnum>0 or not (on_click or on_remove or on_delete): body.append(wrapping.format(unisafe(item))) if on_delete: body.append(trash_can(key,'delete',on_delete)) if on_remove: body.append(trash_can(key,'remove',on_remove)) body.append( '</tr>' ) selector = hasattr(classed, '__iter__') and '.'.join(classed) or classed selector = "{}".format(selector.replace(' ', '.')) classed = hasattr(classed, '__iter__') and ' '.join(classed) or classed actions = actions and '<div class="actions">{}</div>'.format(as_actions(actions)) or ''
rowclass = odd_rowclass else: rowclass = evn_rowclass t.append('<tr id="row-%s" class="%s">' % (count, rowclass)) colnum = 0 for item in row: #wrapping = len(unicode(item)) < 40 and 'nowrap' or '' wrapping = len( unisafe(item)) < 80 and '<td nowrap>%s</td>' or '<td>%s</td>' if (on_click or on_delete or on_remove) and colnum == 0: key = item if on_click and (colnum == 1 or colnum == 0 and len(row) == 1): url = '/'.join(route[1:] + [str(key)]) #t.append('<td>%s</td>' % link_to(item, url, *args, **keywords)) t.append(wrapping % link_to(item, url, *args, **keywords)) elif colnum > 0 or not (on_click or on_remove or on_delete): #t.append('<td %s>%s</td>' % (wrapping, item)) t.append(wrapping % unisafe(item)) colnum += 1 if on_delete: t.append(trash_can(key, 'delete', on_delete)) if on_remove: t.append(trash_can(key, 'remove', on_remove)) t.append('</tr>') t.append('</tbody>')
def render(self): def handle(tag, *args, **keywords): # first try filling tag with page attributes if hasattr(self, tag): attr = getattr(self, tag) if callable(attr): repl = attr(self, *args, **keywords) else: repl = attr return fill('<dz:','>', repl, handle) # if that doesn't work look for an app helper elif tag in system.app.helpers: helper = system.app.helpers[tag] # if that doesn't work look for a system helper elif tag in system.helpers: helper = system.helpers[tag] # if that doesn't work look in the helpers module elif tag in helpers.__dict__ and callable(helpers.__dict__[tag]): """call functions in a module or module-like object""" helper = helpers.__dict__[tag] else: helper = None if helper: if callable(helper): repl = helper(*args, **keywords) else: repl = helper return fill('<dz:','>', repl, handle) def set_setting(thing, name): if thing=='template': self.template = name elif thing=='app_title': self.app_title = name return '<!-- %s set to "%s" -->' % (thing, name) def render_snippet(system_snippet, page_snippet): return '\n'.join(system_snippet | OrderedSet([page_snippet])) def render_script_tags(system_scripts, page_scripts): scripts = system_scripts | page_scripts h = scripts and '\n <!-- Page Specific Scripts -->\n' or '' c = '\n'.join(' <script type="text/javascript" src="{}"></script>'.format(t) for t in scripts) t = scripts and '\n\n' or '' return h + c + t def render_style_sheets(system_style_sheets, page_style_sheets): sheets = system_style_sheets | page_style_sheets h = sheets and '\n <!-- Page Specific Styles -->\n' or '' c = '\n'.join(' <link rel="stylesheet" type="text/css" href="{}">'.format(t) for t in sheets) t = sheets and '\n\n' or '' return h + c + t DEFAULT_TEMPLATE = os.path.join(system.root,'themes','default','default.html') self.theme = self.theme or system.app.theme or user.theme or system.theme if self.theme != system.theme: system.set_theme(self.theme) self.content = fill('<dz:set_','>', self.content, set_setting) self.styles = render_style_sheets(system.styles, self.styles) self.css = render_snippet(system.css, self.css) self.libs = render_script_tags(system.libs, self.libs) self.js = render_snippet(system.js, self.js) self.head = render_snippet(system.head, self.head) self.tail = render_snippet(system.tail, self.tail) if len(route)>1: breadcrumb = link_to(system.app.title,'/'+system.app.name) else: breadcrumb = '' template_pathname = system.theme_path if template_pathname: template_filename = os.path.join(template_pathname, self.template+'.html') if not os.path.exists(template_filename): if not self.template in ['index','content']: log.logger.warning('template missing (%s)' % (template_filename)) template_filename = os.path.join(template_pathname, 'default.html') self.tpl = template_pathname and tools.load(template_filename) or tools.load(DEFAULT_TEMPLATE) page_header = self.render_header() save_content = self.content self.content = page_header + self.content save_title = self.title del self.title content = fill('<dz:','>', self.tpl, handle) self.title = save_title self.content = save_content if self.callback: content = fill('{{','}}', content, self.callback) return HTMLResponse(content)