Esempio n. 1
0
def new():
    check_login()
    form = WiktionaryNewForm()
    all = Template.query.all()
    if all:
        form.template.choices = [(item.length, item.name + "-" + unicode(item.length)) for item in Template.query.all()]
    else:
        form.template.choices = [("General - 4", 4)]
    if form.validate_on_submit():
        title = request.form.get('title')
        section_titles = request.form.getlist('section_title')
        section_contents = request.form.getlist('section_content')
        text = u""
        for s_title, desc in zip(section_titles, section_contents):
            text += TITLE_SEPARATOR + s_title + TITLE_SEPARATOR + LINE_SPACE + desc + LINE_SPACE
        # Post to mediawiki
        try:
            result = post({u'action': u'edit', u'title': title, u'action': 'edit', u'section': u'new', u'text': text})
        except:
            return jsonify({'msg_type': u'failure', 'msg': u'Something Went wrong'})
        if u'error' in result:
            return jsonify({'url': url_for('new'),
                'msg': result[u'error'][u'info'],
                'msg_type': u'failure'})
        elif u'edit' in result:
            return jsonify({'url': url_for('index'),
                'msg': u'Added page %s ' % (title),
                'msg_type': u'success'})
    return render_template('new_wiktionary.html', form=form, title='Create',
        submit=u'Create', cancel_url=url_for('index'), ajax=True)
Esempio n. 2
0
def new():
    check_login()
    form = WiktionaryNewForm()
    all = Template.query.all()
    if all:
        form.template.choices = [(item.length, item.name + "-" + unicode(item.length)) for item in Template.query.all()]
    else:
        form.template.choices = [("General - 4", 4)]
    if form.validate_on_submit():
        s = set([])
        for key in request.form:
            # FIXME: document
            val = request.form[key]
            if key == u'title':
                title = unicode(val)
            else:
                if u"_" in unicode(key):
                    k = key.split('_')
                    try:
                        s.add(int(k[-1]))
                    except ValueError:
                        pass
        result = sorted(s)
        text = title + LINE_SPACE
        for x in xrange(result[0], result[-1] + 1):
            print request.form
            try:
                text  += "===" + request.form[u'title_' + unicode(x)] + "===" + LINE_SPACE + request.form[u'content_' + unicode(x)] + LINE_SPACE
            except KeyError:
                pass
        # Post to mediawiki
        post({u'action': u'edit', u'title': title, u'action': 'edit', u'section': u'new', u'text': text})
        flash("Added page %s " % (title), "success")
        return render_redirect(url_for('index'))
    return render_template('new_wiktionary.html', form=form, title='Create',
        submit=u'Create', cancel_url=url_for('index'), ajax=False)