예제 #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 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)