Example #1
0
def note_new_function():

    # 初始化 笔记表单的公开分类
    note_new_form = NoteForm()
    note_new_form.public.choices = [('0', u"私有笔记,不公开")]
    for c in NoteCate.objects(belong=User.objects(email=session['user']['email']).first()):

        note_new_form.public.choices.append((c.abbname, c.name))

    # 数据提交处理
    if request.method == 'POST':
        if note_new_form.validate_on_submit():
            # 公开分类处理
            if note_new_form.public.data != '0':
                public_status = '1'
                public_cate = NoteCate.objects(
                    abbname=note_new_form.public.data,
                    belong=User.objects(email=session['user']['email']).first()
                ).first()
            else:
                public_status = '0'
                public_cate = None

            # 保存笔记
            Note(
                noteid=getnextseq(),
                title=note_new_form.title.data,
                subtitle=note_new_form.subtitle.data,
                content=note_new_form.content.data,
                public_status=public_status,
                public_cate=public_cate,
                tag=note_new_form.tag.data.split(","),
                belong=(User.objects(email=session['user']['email']).first())
            ).save()

            return redirect(url_for('note_module.note_new_function'))

        # 数据填写不完整
        else:
            flash(u"内容填写不完整")
            return redirect(url_for('note_module.note_new_function'))
    return render_template('note/note_new.html', note_new_form=note_new_form)
Example #2
0
def note_edit_function(noteid):

    # 笔记存在性判断
    if Note.objects(noteid=noteid,
                    belong=User.objects(email=session['user']['email']).first()
                    ).count() == 0:

        flash(u"找不到这篇文章,不要乱来了。")
        return redirect(url_for('note_module.mynote_function'))

    # 读取该笔记
    this_note = Note.objects(noteid=noteid).first()

    # 编辑表单 设置
    note_edit_form = NoteForm(
        title=this_note.title,
        subtitle=this_note.subtitle,
        content=this_note.content,
        tag=",".join(this_note.tag)
    )
    # 加载笔记公开分类
    note_edit_form.public.choices = [('0', u"私有笔记,不公开")]
    for c in NoteCate.objects(belong=User.objects(
        email=session['user']['email']
    ).first()):
        note_edit_form.public.choices.append((c.abbname, c.name))

    # 当GET页面时 显示默认的笔记分类
    if request.method == 'GET' and \
            this_note.public_status == NOTECONSTANTS.PUBLIC:

        note_edit_form.public.data = this_note.public_cate.abbname

    # 数据提交检验
    if request.method == 'POST':

        if note_edit_form.validate_on_submit():

            # 数据提交
            this_note.title = note_edit_form.title.data
            this_note.subtitle = note_edit_form.subtitle.data
            this_note.subtitle = note_edit_form.subtitle.data
            this_note.content = note_edit_form.content.data
            this_note.tag = note_edit_form.tag.data.split(',')
            if note_edit_form.public.data != '0':
                this_note.public_status = '1'
                this_note.public_cate = NoteCate.objects(
                    abbname=note_edit_form.public.data,
                    belong=User.objects(email=session['user']['email']).first()
                ).first()
            else:
                this_note.public_status = '0'
                this_note.public_cate = None
            this_note.save()
            flash(u"笔记修改成功")
            return redirect(url_for('note_module.one_note_function',
                                    noteid=this_note.noteid))

        else:
            flash(u"笔记内容不完整")

    return render_template('/note/note_edit.html',
                           note_edit_form=note_edit_form,
                           this_note=this_note)