Exemplo n.º 1
0
def add(request, key=None, type=FILE):
    to = key # lame but it does the trick for now
    if type == FOLDER:
        form = FolderForm(request.form)
    else:
        form = FileForm(request.form)
    if request.method == "POST" and form.validate():
        if len(form.slug.data) < 1:
            form.slug.data = slugify(form.name.data)
        if type == FOLDER:
            file = File.add(to=to,type=type, name=form.name.data,
                                            slug=form.slug.data,
                                            breadcrumb=form.breadcrumb.data,
                                            state=form.state.data,
                                            active=form.active.data,
                                            author=users.get_current_user(),
                                            updated=datetime.now())
        elif type == FILE:
            file = request.files.get('file')
            data = db.Blob(file.read())
            file = File.add(to=to,type=type, name=form.name.data,
                                            slug=form.slug.data,
                                            breadcrumb=form.breadcrumb.data,
                                            state=form.state.data,
                                            active=form.active.data,
                                            author=users.get_current_user(),
                                            updated=datetime.now(),
                                            content_type=file.content_type,
                                            data=data, size=len(data))

        if form.save.data is True:
            return redirect(url_for('nut:files/list'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:files/edit', key=file.key()), 301)
    return render_template('app:files/form.html', form=form)
Exemplo n.º 2
0
def add(request, key):
    """ add a new page
        to the set"""
    to = key # lame but it does the trick for now
    blocks = []
    form = PageForm(request.form)
    add  = BlockAddForm(request.form, prefix='_add')

    form.layout.choices =  Layout.get_key_to_path()
    if request.method == 'POST':
        # some logic to find __block elements.
        for key in request.form:
            if key.startswith('__block:'):
                name = key.split('__',2)[1][6:]
                blocks.append((name, BlockForm(request.form, prefix='__block:%s__' % name)))
        if add.validate() and add.add.data is True:
            blocks.append((add.name.data, BlockForm(prefix='__block:%s__' % add.name.data)))
            add = BlockAddForm(prefix='_add')
        elif form.validate() and all([block.validate() for _, block in blocks]):
            name = form.name.data
            slug = form.slug.data
            breadcrumb = form.breadcrumb.data
            state = form.state.data
            active = form.active.data
            if len(slug) < 1:
                slug = slugify(name)
            author = users.get_current_user()
            updated = datetime.now()

            description = form.description.data
            keywords = form.keywords.data
            body = form.body.data
            content_type = form.content_type.data
            if form.layout.data == 'Layout:None':
                layout = None
            else:
                layout = Layout.get(form.layout.data.split(':',1)[1])
            page = Node.add(to=to, name=name, slug=slug, breadcrumb=breadcrumb,
                            updated=updated, author=author, body=body,
                            description=description, keywords=keywords, layout=layout,
                            content_type=content_type,
                            state=state, active=active, type=PAGE)
            done = []
            try:
                for name, block in blocks:
                    b = Block(node=page, name=name, body=block.body.data)
                    b.put()
                    done.append(b)
            except:
                db.delete(done)
                Node.drop(page.get_key())
            if form.save.data is True:
                return redirect(url_for('nut:pages/list_pages'), 301)
            if form.cont.data is True:
                return redirect(url_for('nut:pages/edit', key=page.get_key()), 301)

    return render_template('app:pages/form.html', form=form, add=add, blocks=blocks)
Exemplo n.º 3
0
def add_folder(request):
    form = FolderForm(request.form)
    if request.method == 'POST' and form.validate():
        layout = Layout.add(name=form.name.data,
                            slug=slugify(form.name.data),
                            author=users.get_current_user(),
                            type=FOLDER)
        if form.save.data is True:
            return redirect(url_for('nut:layouts/list'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:layouts/edit', key=layout.get_key()), 301)
    return render_template('app:layouts/form.html', form=form)
Exemplo n.º 4
0
def add(request, key):
    form = LayoutForm(request.form)
    if request.method == 'POST' and form.validate():
        layout = Layout.add(to=key, type=LAYOUT,
                            name=form.name.data,
                            slug=slugify(form.name.data),
                            body=form.body.data,
                            author=users.get_current_user())
        layout.update_extends()
        layout.put()
        if form.save.data is True:
            return redirect(url_for('nut:layouts/list'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:layouts/edit', key=layout.get_key()), 301)
    return render_template('app:layouts/form.html', form=form)
Exemplo n.º 5
0
def prepare(form):
    """
    prepared the form data for the model
    """
    data = dict([(attr,getattr(form,attr).data) for attr in COPY_ATTR])
    data['topics'] = [tag.strip() for tag in form.topics.data.split(',')]
    data['slug']   = slugify(data['title'])
    dt = data['pub_date']
    if not dt:
        dt = datetime.now()
    data['lookup'] = u'%4d/%02d/%02d/%s' % \
                     (dt.year, dt.month, dt.day, data['slug'])
    data['sort_key'] = u'%s:%s' % (dt.strftime('%Y%m%d%H%M%S'),
                                   data['slug'])
    if data['published']:
        key_name = u'Published:%s' % data['lookup']
    else:
        key_name = u'Post:%s' % data['lookup']
    return key_name, data
Exemplo n.º 6
0
def add_folder(request):
    form = FolderForm(request.form)
    if request.method == 'POST' and form.validate():
        name = form.name.data
        slug = form.slug.data
        breadcrumb = form.breadcrumb.data
        state = form.state.data
        active = form.active.data
        if len(slug) < 1:
            slug = slugify(name)
        author = users.get_current_user()
        updated = datetime.now()

        page = Node.add(name=name, slug=slug, breadcrumb=breadcrumb,
                         updated=updated, author=author,
                         state=state, active=active, type=FOLDER)
        if form.save.data is True:
            return redirect(url_for('nut:pages/list_pages'), 301)
        if form.cont.data is True:
            return redirect(url_for('nut:pages/edit', key=page.get_key()), 301)
    return render_template('app:pages/form.html', form=form)