Esempio n. 1
0
def su_edit(request, slug):
  '''SU edit item page.'''
  if not request.user.is_authenticated():
    return redirect('django.contrib.auth.views.login')
  tree = ET.parse(util.checkBaseline())
  c = getSUContext(request, tree)
  c['upload'], c['dlg_title'], c['dlg_msg'] = handleUpload(request)
  c['post'] = findContext(c['pages']+c['posts']+c['trash'], slug)
  c['post']['content'] = loadContent(c['post'])
  parent_choices = [('0', '0')]
  if c['post']['type'] == 'page':
    pages = flattenMenu(getMenu(tree))
    if slug in pages:
      pages.remove(slug)
    page_choices = [(p, p) for p in pages]
    parent_choices.extend(page_choices)
  if request.method == 'POST':
    c['form'] = EditForm(request.POST, parents=parent_choices)
    if c['form'].is_valid():
      item = formToItem(c['form'], c['post'])
      if save(c['post']['name'], item):
        return redirect('su')
      c['dlg_title'] = "Failed to save %s" % (c['post']['type'])
      c['dlg_msg'] = "An error occurred while attempting to save.  Try again."
  else:
    c['form'] = itemToForm(c['post'], parent_choices)
  if c['post']['trash'] and c['post']['trash'].lower() == 'true':
    c['readonly'] = True
  t = util.getTemplate('su_edit.html', isSU=True)
  return HttpResponse(t.render(c))
Esempio n. 2
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)
Esempio n. 3
0
def trash(request, slug, delete=True):
  '''Mark or unmark an item for trash and save it to the filesystem.
    
  Args:
    request: The view request object.
    slug: The slug for the item.
    delete: Send the item to the trash if True; Restore otherwise.
    
  Returns:
    True on success; False on error.
  '''
  catalog_path = util.checkBaseline()
  xml_tree = ET.parse(catalog_path)
  root = xml_tree.getroot()
  element = findElement(xml_tree, slug)
  if not element:
    return False
  welement = util.ETWrap(element)
  item = elementToItem(element)
  item['tag'] = 'trash' if delete else welement.type
  item['trash'] = 'true' if delete else 'false'
  root.remove(element)
  updateElem(element, item)
  root.append(element)
  xml_tree.write(catalog_path, 'UTF-8')
  return True
Esempio n. 4
0
def su_new(request, type):
  '''SU create new page or post item.'''
  if not request.user.is_authenticated():
    return redirect('django.contrib.auth.views.login')
  if type != 'post' and type != 'page':
    return redirect('django.contrib.auth.views.login')
  catalog_path = util.checkBaseline()
  tree = ET.parse(catalog_path)
  new_post = newElem(type)
  tree.getroot().append(new_post)
  tree.write(catalog_path, 'UTF-8')
  writeContentFile(toContentElement(new_post))
  return redirect('su')
Esempio n. 5
0
def save(slug, item=None):
  '''Write an item to the filesystem (both catalog and content file).
    
  Args:
    slug: The original slug of the item.  item['name'] will become the new slug.
      NOTE - Set slug to 'post' or 'page' to create and save a new item.
    item: The item to save to the filesystem.
    
  Returns:
    True on success; False on error.
    
  Raises:
    Exception: If the item is None and the slug isn't "post" or "page".
  '''
  catalog_path = util.checkBaseline()
  xml_tree = ET.parse(catalog_path)
  root = xml_tree.getroot()
  new_elem = None
  content = None
  oldFilepath = None
  if slug != 'post' and slug != 'page':
    if item == None:
      raise Exception('Argument cannot be None')
    if slug != item['name']:
      if findElement(xml_tree, item['name']):
        return False  # Name collision
    # Update old element (delete and recreate)
    element = findElement(xml_tree, slug)
    if element:
      welement = util.ETWrap(element)
      os.remove(welement.filepath)
      root.remove(element)
    new_elem = ET.Element(item['type'])
    updateElem(new_elem, item)
    content = item['content']
  else: # Create new element
    new_elem = newElem(slug)
  root.append(new_elem)
  xml_tree.write(catalog_path, 'UTF-8')
  writeContentFile(toContentElement(new_elem, content))
  return True
Esempio n. 6
0
def getSU(request, err_title_msg=None):
  '''Get the SU page response
  
  Args:
    request: Django HttpRequest object
    err_title_msg: Tuple with structure: (error title, error message)
  
  Returns:
    An HttpResponse for the SU page.
  '''
  if not request.user.is_authenticated():
    return redirect('django.contrib.auth.views.login')
  tree = ET.parse(util.checkBaseline())
  c = getSUContext(request, tree)
  if err_title_msg:
    c['dlg_title'] = err_title_msg[0]
    c['dlg_msg'] = err_title_msg[1]
  else:
    c['upload'], c['dlg_title'], c['dlg_msg'] = handleUpload(request)
  t = util.getTemplate('su.html', isSU=True)
  return HttpResponse(t.render(c))
Esempio n. 7
0
def trash(path):
    catalog_path = util.checkBaseline()
    tree = ET.parse(catalog_path)
    root = tree.getroot()
    items = tree.findall("/trash")
    files_to_trash = []
    for i in items:
        witem = util.ETWrap(i)
        files_to_trash.append(witem.filepath)
        root.remove(i)
    if files_to_trash:
        tree.write(catalog_path, "UTF-8")
        for f in files_to_trash:
            if path:
                new_path = os.path.join(path, os.path.basename(f))
                os.rename(f, new_path)
            else:
                os.remove(f)
        print "Success: The trash has been taken out."
    else:
        print "Success: Trash was already empty."
    return True
Esempio n. 8
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)