示例#1
0
def getDetail(request, type, slug, date=None):
  '''Get the response for a page or post.
  
  Args:
    request: Django HttpRequest object
    type: The type of response requested.  Must be 'page' or 'post'.
    slug: Name/ID of the item.
    date: Optional date of item used to reduce searching.
  
  Returns:
    An HttpResponse for the requested item.
  '''
  what = {'post': 'posts', 'page': 'pages'}
  xml_tree = ET.parse(util.checkBaseline())
  wp_tree = wp.loadXML()
  c = getDefaultContext(request)
  c['posts'] = getItems(xml_tree, what[type], isVisible)
  if not date or date.strftime('%Y-%m-%d') <= Config.WP_END_DATE:
    c['posts'] += wp.getItems(wp_tree, what[type])
  c['post'] = findContext(c['posts'], slug)
  if c['post']:
    c['post']['content'] = loadContent(c['post'])
    c['title'] += " - %s" % (c['post']['title'])
    assembleContext(c, xml_tree, wp_tree)
    t = util.getTemplate('detail.html')
    return HttpResponse(t.render(c))
  return get404(request, xml_tree, wp_tree)
示例#2
0
def assembleContext(context, xml_tree, wp_tree):
  '''Update a template context with info from both XML trees.
  
  Updated fields: posts, menu, recent, random, and categories.
    
  Args:
    context: Context to update (usually a default context).
    xml_tree: Standard XML tree object.
    wp_tree:  Wordpress XML tree object.
  '''
  context['posts'] = getItems(xml_tree, 'posts', isVisible) + \
    wp.getItems(wp_tree, 'posts', isVisible)
  context['menu'] = getMenu(xml_tree, wp_tree)
  context['recent'] = context['posts'][0:Config.POSTS_PER_PAGE]
  rdm_posts = len(context['posts'])
  if rdm_posts > Config.POSTS_PER_PAGE:
    rdm_posts = Config.POSTS_PER_PAGE
  context['random'] = random.sample(context['posts'], rdm_posts)
  context['categories'] = getCategories(context['posts'])
示例#3
0
def showList(request, category=None, tag=None):
  '''Get response for showing a list of posts.
    
  Args:
    request: View request object.
    category: Show only items containing this category string.
    tag: Show only items containing this tag string.
    
  Returns:
    A post list response.
  '''
  p = int(request.GET.get('p', 0))
  itemsPerPage = Config.POSTS_PER_PAGE
  xml_tree = ET.parse(util.checkBaseline())
  wp_tree = wp.loadXML()
  c = getDefaultContext(request)
  filter = isVisible
  if category:
    filter = lambda d: category in d['categories'] and isVisible(d)
  if tag:
    filter = lambda d: tag in d['tags'] and isVisible(d)
  all_posts = getItems(xml_tree, 'posts', filter) + \
    wp.getItems(wp_tree, 'posts', filter)
  posts = all_posts[p*itemsPerPage:(p+1)*itemsPerPage]
  c['prev'] = -1 if p == 0 else p-1
  c['next'] = -1 if p >= len(all_posts)/itemsPerPage else p+1
  if len(posts) > 0:
    for p in posts:
      p['content'] = loadContent(p)
    assembleContext(c, xml_tree, wp_tree)
    c['posts'] = posts
    tfile = 'index.html'
    if category or tag:
      tfile = 'cattag.html'
      c['title'] += " - Tagged %s" % (tag) if tag else " - Category %s" % (category)
    t = util.getTemplate(tfile)
    return HttpResponse(t.render(c))
  return get404(request, xml_tree, wp_tree)
示例#4
0
def getMenu(xml_tree, wp_tree=None):
  '''Build a menu using the XML trees.
    
  Args:
    xml_tree: XML tree object used to build the menu.
    wp_tree:  WordPress tree used to build the menu.
    
  Returns:
    A list of tuples structred like: (title, slug, [child tuples])
  '''
  pages = getItems(xml_tree, 'pages', isVisible)
  if wp_tree:
    pages.extend(wp.getItems(wp_tree, 'pages'))
  family_tree = {}
  for p in pages:
    id = p['post_id'] if 'post_id' in p else p['name']
    parent = p['parent'] if 'parent' in p and p['parent'] else '0'
    if parent not in family_tree:
      family_tree[parent] = [(p['title'], p['name'], id)]
    else:
      family_tree[parent].append((p['title'], p['name'], id))
    if id not in family_tree:
      family_tree[id] = []
  return getChildren(family_tree)