Ejemplo n.º 1
0
def login():
    if request.method == 'GET':
        user_id = get_user_id()
        if user_id:
            return redirect(url_for('latest'))
        return render_template('login.html')
    elif request.method == 'POST':
        email = request.form['email'].strip()
        password = request.form['password'].strip()
        result = LoginForm(email=email, password=password).validate()
        if result.is_success:
            user = User.query_obj.get_by_email(email)
            if user is None:
                flash(u'该邮箱尚未注册')
            else:
                result = user.check(password)
                if result:
                    login_user(user)
                    previous_page = request.args.get('prev')
                    if previous_page:
                        return redirect(previous_page)
                    return redirect(url_for('latest'))
                else:
                    flash(u'用户名或密码错误')
        else:
            flash(result.message)
    return render_template('login.html', email=email)
Ejemplo n.º 2
0
def latest():
    user_id = get_user_id()
    note = Note.query_obj.get_recent_note_by_user(user_id)
    if not note:
        return redirect(url_for('no_notes'))
    note.weekday = get_local_weekday(note.time)
    note.time = get_local_date(note.time)
    return render_template('note.html', note=note)
Ejemplo n.º 3
0
def get_notes_by_date():
    user_id = get_user_id()
    datenum = int(request.args.get('datenum'))
    notes = Note.query_obj.get_notes_by_datenum(user_id,datenum)
    json_note_list = []
    for note in notes:
        note.weekday = get_local_weekday(note.time)
        note.time = get_local_date(note.time)
        json_note_list = json.dumps(new(note),cls=MyEncoder)
    return json.dumps(json_note_list)
Ejemplo n.º 4
0
def get_newer_note():
    note_id = int(request.args.get('note_id'))
    user_id = get_user_id()
    note = Note.query_obj.get_newer_note(user_id,note_id)
    note_id = note.id
    note_content = note.content
    note_weekday = get_local_weekday(note.time)
    note_time = get_local_date(note.time)
    return jsonify(
                id = note_id,
                weekday = note_weekday,
                time = note_time,
                content = note_content
            )
Ejemplo n.º 5
0
def notes(datenum=None):
    user_id = get_user_id()
    if datenum is None:
        notes = Note.query_obj.get_notes_by_author(user_id)
        datenum = ''
    else:
        notes = Note.query_obj.get_notes_by_datenum(user_id,datenum)
    for note in notes:
        note.weekday = get_local_weekday(note.time)
        note.time = get_local_date(note.time)

    date_list = [str(d) for d, in Note.query_obj.get_datenum_by_user(user_id)]
    date_list = sorted(date_list,reverse=True)
    return render_template('note_list.html', notes=notes, date_list=date_list, datenum=datenum)
Ejemplo n.º 6
0
def get_random_note():
    note_id = int(request.args.get('note_id'))
    user_id = get_user_id()
    note_list = Note.query_obj.get_notes_by_author(user_id)
    note = note_list[randint(0, len(note_list)-1)]
    note_id = note.id
    note_content = note.content
    note_weekday = get_local_weekday(note.time)
    note_time = get_local_date(note.time)
    return jsonify(
                id = note_id,
                weekday = note_weekday,
                time = note_time,
                content = note_content
            )
Ejemplo n.º 7
0
def write():
    if request.method == 'GET':
        return render_template('write.html')
    elif request.method == 'POST':
        note_date = request.form["note_date"]
        note_content = request.form["note_content"]
        note_content = cgi.escape(note_content)
        note_content = format_textarea(note_content)
        try:
            note_date = datetime.datetime.strptime(note_date, '%Y-%m-%d')
        except ValueError:
            note_date = datetime.datetime.now()

        user_id = get_user_id()
        Note.create(user_id,  note_content, note_date)
        return redirect(url_for("notes"))
Ejemplo n.º 8
0
def change_password():
    if request.method == 'GET':
        return render_template('change_password.html')
    if request.method == 'POST':
        user_id = get_user_id()
        old_password = request.form['old']
        new_password = request.form['new']
        confirm_password = request.form['confirm']
        result = ChangePasswordForm(new_password,confirm_password).validate()
        if result.is_success:
            user = User.query_obj.get_by_id(user_id)
            result = user.check(old_password)
            if result:
                user.set_password(new_password)
                flash(u'密码已修改')
            else:
                flash(u'原密码输入错误')
        else:
            flash(result.message)
        return render_template('change_password.html')
Ejemplo n.º 9
0
def register():
    if request.method == 'GET':
        user_id = get_user_id()
        if user_id != 0:
            return redirect(url_for('latest'))
        return render_template('regist.html')
    elif request.method == 'POST':
        email = request.form['email'].strip()
        result = RegistForm(email=email).validate()
        if result.is_success:
            user = User.query_obj.get_by_email(email)
            if user:
                flash(u'该邮箱已经注册')
            else:
                code = ''.join(random.sample('abcdefghijklmnopqrstuvwxyz',20))
                result = send_regist_mail(email,code)
                if result:
                    UserRegist.create(email, code)
                    return render_template('password.html',email=email)
                else:
                    flash(u'发送失败,请重试')
        else:
            flash(result.message)
        return redirect(url_for('register'))