Example #1
0
class SimpleEntries:

    def __init__(self):
        self.testMain = testMain()

        self.userDB = UserDB()
        self.entryDB = EntryDB()
        self.roleDB = RoleDB()

        u = self.userDB.add("criswell", "password", "Sam Hart")
        u1 = self.userDB.add("charles", "password", "Charles Xavier")
        u2 = self.userDB.add("leif", "password", "Lief Ericsson")
        u3 = self.userDB.add("barf", "password", "Barfy Barferson")
        u4 = self.userDB.add("squiddy", "password", "Squidward Tentacles")
        editors = ['charles', 'leif', 'barf', 'squiddy']
        all_groups = ['junkbar', 'dollarbillyall', 'coldnwet']
        for g in all_groups:
            self.userDB.add_group(g)
        role_names = ['tomato', 'spaghetti', 'nuts']
        role_desc = ['Tomato Paste', 'Swirly Cheesy', 'Hardcore Nuts']
        for x in range(len(role_names)):
            self.roleDB.add_role(
                role_names[x],
                role_desc[x])
        parents = []
        for e in entries:
            parent = None
            if randint(0,5) > 3 and len(parents) > 1:
                parent = parents[randint(0,len(parents)-1)]
                print("A parent is {0}".format(parent))
            try:
                entry = self.entryDB.add(e[0], e[1], u, None, e[2], e[3], e[5], parent)
                if e[4]:
                    self.entryDB.add_tag(e[4], entry)

                if randint(0,5) > 2:
                    parents.append(entry)

                if randint(0,5) > 2:
                    for ed in sample(editors, randint(1,len(editors)-1)):
                        self.entryDB.update_editor(ed, entry)
            except:
                print("Dropping an entry due to some entryDB problem (likely duplicate URL")
                print("because of random URL generation- Should be safe to ignore)\n")
                pass # stupid, but for our tests we don't care we just may get duplicate URLs

    def __del__(self):
        del(self.testMain)

    def run(self, debug):
        print("mainApp.jinja_env")
        print("-----------------")
        print("\nmainApp.jinja_loader")
        print("___________________")
        print("\n%s" % mainApp.jinja_loader.searchpath)
        mainApp.run(host="0.0.0.0", debug=debug)
Example #2
0
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!'))