def notes(datenum=None): if get_user_id(): user_id = get_user_id() notes = Note.query.filter_by(user_id=user_id).order_by('-id').all() date_list = [] if not notes: return redirect(url_for('no_notes')) elif datenum: for nn in notes: note = nn.time.date() if note.strftime('%Y%m') == datenum: date_list = list(set(date_list)) else: pass else: for nn in notes: note = nn.time.date() date_list.append(note.strftime('%Y%m')) date_list = list(set(date_list)) else: return redirect(url_for('login')) for nn in notes: nn.weekday = nn.time.strftime('%A') nn.time = nn.time.strftime('%Y-%m-%d') return render_template('note_list.html', notes=notes, date_list=date_list)
def latest(): if get_user_id(): user_id = get_user_id() note = Note.query.filter_by(user_id=user_id).order_by('-id').first() if not note: return redirect(url_for('no_notes')) note.weekday = note.time.strftime('%A') note.time = note.time.date() else: return redirect(url_for('login')) return render_template('note.html', note=note)
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_date = datetime.datetime.strptime(note_date, '%Y-%m-%d') if get_user_id(): user_id = get_user_id() write_note(user_id, note_date, note_content) else: return redirect(url_for('login')) return redirect(url_for('notes'))
def get_random_note(): if get_user_id(): note_id = int(request.args.get('note_id')) user_id = get_user_id() note_list = Note.query.filter_by(user_id=user_id).all() note = note_list[randint(0, len(note_list) - 1)] note_id = note.id note_content = note.content note_weekday = note.time.strftime('%A') note_time = note.time.strftime('%Y-%m-%d') return jsonify(id=note_id, weekday=note_weekday, time=note_time, content=note_content)
def get_older_note(): if get_user_id(): note_id = int(request.args.get('note_id')) user_id = get_user_id() note = Note.query.filter(Note.user_id == user_id, Note.id > note_id).order_by('id').first() if not note: note = Note.query.filter_by(user_id=user_id).order_by('id').first() note_id = note.id note_content = note.content note_weekday = note.time.strftime('%A') note_time = note.time.strftime('%Y-%m-%d') return jsonify(id=note_id, weekday=note_weekday, time=note_time, content=note_content)
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.user_query.get_by_email(email) if user is None: flash(u'该邮件尚未注册') else: result = user.check(password) if result: session["user_id"] = user.id 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)
def get_notes_by_date(): user_id = get_user_id() datenum = int(request.args.get('datenum')) blogs = Blog.blog_query.get_blogs_by_datenum(user_id, datenum) json_blog_list = [] for blog in blogs: blog.weekday = get_local_weekday(blog.time) blog.time = get_local_date(blog.time) json_note_list = json.dumps(new(blog),cls=MyEncoder) return json.dumps(json_note_list)
def get_notes_by_date(): user_id = get_user_id() datenum = int(request.args.get('datenum')) blogs = Blog.blog_query.get_blogs_by_datenum(user_id, datenum) json_blog_list = [] for blog in blogs: blog.weekday = get_local_weekday(blog.time) blog.time = get_local_date(blog.time) json_note_list = json.dumps(new(blog), cls=MyEncoder) return json.dumps(json_note_list)
def get_older_note(): note_id = int(request.args.get('note_id')) user_id = get_user_id() blog = BlogQuery.get_older_blog(user_id, note_id) if not blog: blog = Blog.blog_query.get_blog_by_id(note_id) blog_id = blog.id blog_content = blog.content blog_weekday = get_local_weekday(blog.time) blog_time = get_local_date(blog.time) return jsonify(id = blog_id, weekday = blog_weekday, time = blog_time, content = blog_content)
def setpasswd(): if request.method == 'GET': return render_template('change_password.html') elif request.method == 'POST': if get_user_id(): user_id = get_user_id() password = request.form['old'] newpassword = request.form['new'] print newpassword renewpassword = request.form['confirm'] uuser = User.query.filter_by(id=user_id).first() if newpassword != renewpassword or newpassword == '': flash(u'两次输入的密码不一致') return redirect(url_for('setpasswd')) elif user_login(uuser.salt, password) == uuser.password: update_password(user_id, uuser.salt, newpassword) flash(u'密码修改完成') else: flash(u'当前密码输入错误') return redirect(url_for('setpasswd')) return redirect(url_for('login'))
def get_random_note(): note_id = int(request.args.get('note_id')) user_id = get_user_id() note_list = BlogQuery.get_blog_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)
def get_newer_note(): note_id = int(request.args.get('note_id')) user_id = get_user_id() note = BlogQuery.get_newer_blog(user_id, note_id) if not note: note = BlogQuery.get_blog_by_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)
def get_random_note(): note_id = int(request.args.get('note_id')) user_id = get_user_id() note_list = BlogQuery.get_blog_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 )
def notes(datenum=None): user_id = get_user_id() blogs = Blog.blog_query.get_blog_by_author(user_id) if datenum is None: datenum = '' else: blogs = [blog for blog in blogs if str(blog.time) == datenum] if not blogs: return redirect(url_for('no_blogs')) for blog in blogs: blog.weekday = get_local_weekday(blog.time) blog.time = get_local_date(blog.time) date_list = [str(d) for d, in Blog.blog_query.get_datenum_by_user(user_id)] date_list = sorted(date_list,reverse=True) return render_template('note_list.html', notes = blogs, date_list = date_list, datenum = datenum)
def get_older_note(): note_id = int(request.args.get('note_id')) user_id = get_user_id() blog = BlogQuery.get_older_blog(user_id, note_id) if not blog: blog = Blog.blog_query.get_blog_by_id(note_id) blog_id = blog.id blog_content = blog.content blog_weekday = get_local_weekday(blog.time) blog_time = get_local_date(blog.time) return jsonify(id=blog_id, weekday=blog_weekday, time=blog_time, content=blog_content)
def get_newer_note(): note_id = int(request.args.get('note_id')) user_id = get_user_id() note = BlogQuery.get_newer_blog(user_id,note_id) if not note: note = BlogQuery.get_blog_by_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 )
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() Blog.create(user_id, note_content, note_date) return redirect(url_for("notes"))
def notes(datenum=None): user_id = get_user_id() blogs = Blog.blog_query.get_blog_by_author(user_id) if datenum is None: datenum = '' else: blogs = [blog for blog in blogs if str(blog.time) == datenum] if not blogs: return redirect(url_for('no_blogs')) for blog in blogs: blog.weekday = get_local_weekday(blog.time) blog.time = get_local_date(blog.time) date_list = [str(d) for d, in Blog.blog_query.get_datenum_by_user(user_id)] date_list = sorted(date_list, reverse=True) return render_template('note_list.html', notes=blogs, date_list=date_list, datenum=datenum)
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.user_query.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')
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.user_query.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')