def delete_post(num): """ Delete a post """ if num is None: return render_template('noink_message.html', state=get_state(), title=_('No entry specified!'), message=_('You have not supplied an entry to delete.')) entry_db = EntryDB() entry = entry_db.find_by_id(num) if entry is None: return render_template('noink_message.html', state=get_state(), title=_('Entry not found!'), message=_('The entry "{0}" was not found!'.format(num))) if not is_deletable(entry): return render_template('noink_message.html', state=get_state(), title=_('Unable to delete entry!'), message=_('You do not have permission to delete this entry!')) if request.method == 'POST': if "delete" in request.form: entry_db.delete(entry) flash(_('Entry "{0}" deleted!'.format(num))) return redirect(url_for('list_entries.show')) elif "cancel" in request.form: return redirect(url_for("node.show_node", num=num)) return render_template('delete_post.html', state=get_state(), entry=entry, is_edit=True)
def show_node(num, name): """ Renders a page given it's entry id """ entry = None url = None editors = None if num < 0 and name: entry_db = EntryDB() try: entry = entry_db.find_by_URL(name)[0] except: abort(404) else: entry_db = EntryDB() entry = entry_db.find_by_id(num) if entry == None and url == None: abort(404) children = [] try: if entry is not None: es = entry_db.find_editors_by_entry(entry.id) if len(es) > 0: editors = es if entry.children: children.append(entry) children.extend(sorted(entry.children, key=lambda e: e.weight)) elif entry.parent_id is not None: te = entry_db.find_by_id(entry.parent_id) children.append(te) children.extend(sorted(te.children, key=lambda e: e.weight)) except: editors = None #import ipdb; ipdb.set_trace() return render_template('node.html', entry=entry, editors=editors, state=get_state(), children=children)
def _edit_post(eid=None): """ Edit a post """ # FIXME - This method has kind of gotten monsterous. Refactor. user_db = UserDB() role_db = RoleDB() entry_db = EntryDB() if current_user.is_authenticated() and current_user.is_active(): all_groups = set(user_db.get_users_groups(current_user)) all_roles = role_db.get_roles(current_user) role_groups = set(m.group for m in all_roles) role_by_groups = dict(((m.group, role_db.get_activities(m.role)) for m in all_roles)) # The available groups are ones which they are both a part of AND which # they have a role in! avail_groups = all_groups & role_groups groups = [] if current_user.primary_group in avail_groups: # Make sure primary group is first in the list, if it's there #avail_groups.remove(current_user.primary_group) groups.append(current_user.primary_group) groups.extend(avail_groups) parent_group = None parent = None if 'parent' in request.values: parent = entry_db.find_by_id(request.values['parent']) parent_group = parent.group # If everything else fails, we default to the top level if parent_group is None: parent_group = user_db.get_group(mainApp.config['TOP_LEVEL_GROUP']) if parent_group in avail_groups and parent_group in role_by_groups: if role_by_groups[parent_group].get('new_post', False): entry = None tags = [] if eid is None: if request.method == "POST": entry = process_entry_object(parent) if "tags" in request.form: tags = [x.strip() for x in request.form['tags'].split(',') if x != ''] if "submit" in request.form: entry_db.add_entry_object(entry) if len(tags) > 0: entry_db.add_tag(tags, entry) return redirect(url_for('node.show_node', num=entry.id)) else: entry = entry_db.find_by_id(eid) if entry is None: return render_template('noink_message.html', state=get_state(), title=_('Entry not found!'), message=_( 'The entry "{0}" was not found!'.format(eid))) if request.method == "POST": entry = update_entry_object(entry) if "tags" in request.form: tags = [x.strip() for x in request.form['tags'].split(',') if x != ''] if "submit" in request.form: entry_db.update_entry(entry) entry_db.update_editor(current_user, entry) if len(tags) > 0: entry_db.add_tag(tags, entry) return redirect(url_for('node.show_node', num=entry.id)) else: for tm in entry.tagmap: tags.append(tm.tag.tag) return render_template('new_post.html', state=get_state(), groups=groups, entry=entry, tags=tags, is_edit=True, title=_('New Post'), submit_button=_('Submit'), preview_button=_('Preview')) else: return not_authorized() else: return not_authorized() return render_template('noink_message.html', state=get_state(), title=_('Not authorized'), message=_('You must be logged in as a user to access this page!'))
class Icebox: __borg_state = {} def __init__(self): self.__dict__ = self.__borg_state try: self._setup except AttributeError: self._setup = False if not self._setup: self.event_log = EventLog() self.entry_db = EntryDB() self.icebox_path = abspath(mainApp.config['ICEBOX_PATH']) if mainApp.config['ICEBOX_STATIC_PATH'] is not None: self.static_path = abspath(mainApp.config['ICEBOX_STATIC_PATH']) else: self.static_path = None self.client = mainApp.test_client() self._setup = True def generate_pages(self, all_pages=False): """ Generate the pages. If all_pages is True, will regenerate the entire site. """ if not isdir(self.icebox_path): makedirs(self.icebox_path) state = State() state.icebox = True if all_pages: self._rebuild_site() else: for e in self.event_log.get_unprocessed(): if e.event in ('add_entry', 'update_entry'): pe = depickle(e.blob) entry = self.entry_db.find_by_id(pe.id) if entry is not None: self._generate_page(entry) elif e.event == 'rebuild_static': self._rebuild_site() elif e.event == 'del_entry': pe = depickle(e.blob) self._remove_page(pe.id, pe.url) self.event_log.mark_as_processed(e) # Regenerate index self._generate_index() # Regenerate tags self._generate_tags() # Sync static pages if self.static_path: self.sync_static() def _rebuild_site(self): """ Rebuild the entire site """ self.clear_icebox_path() count = self.entry_db.count() per_page = mainApp.config['NUM_ENTRIES_PER_PAGE'][0] total_pages = int(ceil(float(count) / float(per_page))) for i in range(total_pages): entries = self.entry_db.find_recent_by_num(per_page, i * per_page) for e in entries: self._generate_page(e) def _generate_tags(self): """ Generate the tag pages. """ # Clean up old tags rmfiles = glob('{0}/tag/*'.format(self.icebox_path)) for f in rmfiles: if isfile(f): remove(f) all_tags = self.entry_db.get_tags() for t in all_tags: response = self.client.get("/tag/{0}".format(t.id), follow_redirects=True) html = response.data filename = 'tag/{0}.html'.format(t.id) self._write_page(html, filename) def sync_static(self): """ Synchronize static content """ base_dir = "{0}/s".format(self.icebox_path) if not isdir(base_dir): makedirs(base_dir) for root, dummy, files in walk(join(self.icebox_path, 's')): for f in files: if isfile(join(root, f)): remove(join(root, f)) for root, dummy, files in walk(self.static_path): for f in files: source_name = join(root, f) dest_name = join(join( '{0}/s'.format(self.icebox_path), relpath(root, self.static_path)), f) base_dest = dirname(dest_name) if not isdir(base_dest): makedirs(base_dest) link(source_name, dest_name) def clear_icebox_path(self): """ Will clear the icebox path. WARNING: This is destructive, and should only be used when you know what you're doing. This will nuke the contents of the icebox path. """ rmfiles = [] for root, dirs, files in walk(self.icebox_path): for d in dirs: rmfiles.append(join(root, d)) for f in files: rmfiles.append(join(root, f)) rmfiles.sort(key=len) rmfiles.reverse() for f in rmfiles: if isfile(f): remove(f) elif isdir(f): rmdir(f) def _convert_url_to_path(self, url): """ Given a URL, convert it to a filepath. Return filepath. """ if url.endswith('/'): url += 'index.html' else: url += '.html' if url.startswith('/'): return url[1:] else: return url def _generate_page(self, entry): """ Given an entry, will generate the page for it (including any aliases). """ response = self.client.get('/node/{0}'.format(entry.id), follow_redirects=True) html = response.data # First, generate the node filename = 'node/{0}.html'.format(entry.id) self._write_page(html, filename) if entry.url: filename = self._convert_url_to_path(entry.url) self._write_page(html, filename) def _generate_index(self): """ Generate the main index """ # First clear the old indexes out of the way rmfiles = ['{0}/index.html'.format(self.icebox_path)] rmfiles.extend(glob('{0}/index_page_*'.format(self.icebox_path))) for f in rmfiles: if isfile(f): remove(f) per_page = mainApp.config['NUM_ENTRIES_PER_PAGE'][0] count = self.entry_db.count() total_pages = 0 if count > per_page: total_pages = int(ceil(float(count) / float(per_page))) response = self.client.get('/', follow_redirects=True) html = response.data filename = 'index.html' self._write_page(html, filename) for i in range(total_pages): response = self.client.get('/?page={0}'.format(i), follow_redirects=True) html = response.data filename = 'index_page_{0}.html'.format(i) self._write_page(html, filename) def _write_page(self, html, filename): """ Write the actual page to filename """ fullname = "{0}/{1}".format(self.icebox_path, filename) base_dir = dirname(fullname) if not isdir(base_dir): makedirs(base_dir) with open(fullname, 'wb') as fd: fd.write(html) def _remove_page(self, entry_id, entry_url): """ Given an entry, will remove the page for it. """ node_fn = 'node/{0}.html'.format(entry_id) if isfile(node_fn): remove(node_fn) if entry_url: filename = self._convert_url_to_path(entry_url) if isfile(filename): remove(filename)