def get_event_more(viewname,page,slug): if viewname == 'all': if cache.get('event_list-all-html-page-'+str(page)): e_html = cache.get('event_list-all-html-page-'+str(page)) else: a = Event.query.all_list().paginate(page,10) e_html = '' for e in a.items: e_html += render_template( 'event_list.html',e=e) cache.set('event_list-all-html-page-'+str(page),e_html,10) else: if viewname == 'mine': event_user_list = Event_User.query.mine_list(g.user.id).paginate(page,10) elif viewname == 'talk': event_user_list = Event_User.query.talk_list(g.user.id).paginate(page,10) elif viewname == 'question': event_user_list = Event_User.query.question_list(g.user.id).paginate(page,10) elif viewname == 'topic': event_user_list = Event_User.query.topic_list(g.user.id).paginate(page,10) else: abort(404) e_html = '' #try: for eu in event_user_list.items: if cache.get('event-id-'+str(eu.event_id)): e_html += cache.get('event-id-'+str(eu.event_id)) else: e = Event.query.get(eu.event_id) h = render_template('event_list.html',e=e) cache.set('event-id-'+str(eu.event_id),h,100) e_html += h return e_html
def edit(post_id): is_login() post = Post.query.get_or_404(post_id) post.permissions.edit.test(403) form = PostForm(obj=post) if form.validate_on_submit(): form.populate_obj(post) db.session.commit() if g.user.id != post.author_id: body = render_template("emails/post_edited.html", post=post) message = Message(subject="Your post has been edited", body=body, recipients=[post.author.email]) mail.send(message) flash(_("The post has been updated"), "success") else: flash(_("Your post has been updated"), "success") return redirect(url_for("post.view", post_id=post_id)) return render_template("post/edit_post.html", post=post, form=form)
def saying_comment( answer_id, slug ): answer_comment = Answer_Comment.query.filter( Answer_Comment.answer_id == answer_id ).all() if answer_comment: render_template("ajax/get_answer_comment.html",\ answer_comment = answer_comment,answer_id=answer_id) else: return 'no'
def index(): #如果没有指定就默认为第一页 if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1 page_url = lambda page: '?page='+str(page) #热门小组,按加入人数排序 hot_group = Group.query.hot_group_list() #按是否登陆来处理 if g.user: topic_list = Group_Topic.query.get_my_list(g.user.id).\ paginate(page, per_page=30) #我加入的小组(近9) my_join_group = Group_User.query.my_join_group(g.user.id) return render_template( 'group/index.html',\ topic_list=topic_list, page_url=page_url,\ hot_group=hot_group,my_join_group=my_join_group) else: #获取该小组的主题列表 topic_list = Group_Topic.query.get_all_list().\ paginate(page, per_page=30) return render_template( 'group/no_login_index.html',\ topic_list=topic_list, page_url=page_url,hot_group=hot_group)
def report_abuse(comment_id): is_login() comment = Comment.query.get_or_404(comment_id) form = CommentAbuseForm() if form.validate_on_submit(): admins = current_app.config['ADMINS'] if admins: body = render_template("emails/report_abuse.html", comment=comment, complaint=form.complaint.data) message = Message(subject="Report Abuse", body=body, sender=g.user.email, recipients=admins) mail.send(message) flash(_("Your report has been sent to the admins"), "success") return redirect(comment.url) return render_template("comment/report_abuse.html", comment=comment, form=form)
def send_message(user_id): is_login() user = User.query.get_or_404(user_id) user.permissions.send_message.test(403) form = MessageForm() if form.validate_on_submit(): body = render_template("emails/send_message.html", user=user, subject=form.subject.data, message=form.message.data) subject = _("your friend %(name)s sent you a message", name=g.user.username) message = Message(subject=subject, body=body, recipients=[user.email]) mail.send(message) flash(_("Your message has been sent to %(name)s", name=user.username), "success") return redirect(url_for("user.posts", username=user.username)) return render_template("user/send_message.html", user=user, form=form)
def index(): if not g.user: loginform = LoginForm() signupform = SignupForm() return render_template( 'hi.html',loginform=loginform,signupform=signupform) if (request.args.get("view", '').strip()): viewname = request.args.get("view", '').strip() else: viewname = "mine"#如果没有指定就默认为全部 if viewname == 'all': if cache.get('event_list-all-html'): e_html = cache.get('event_list-all-html') else: a = Event.query.all_list().paginate(1,20) e_html = '' for e in a.items: e_html += render_template( 'event_list.html',e=e) cache.set('event_list-all-html',e_html,20) else: if viewname == 'mine': event_user_list = Event_User.query.mine_list(g.user.id).paginate(1,20) elif viewname == 'talk': event_user_list = Event_User.query.talk_list(g.user.id).paginate(1,20) elif viewname == 'question': event_user_list = Event_User.query.question_list(g.user.id).paginate(1,20) elif viewname == 'topic': event_user_list = Event_User.query.topic_list(g.user.id).paginate(1,20) else: abort(404) e_html = '' #try: for eu in event_user_list.items: if cache.get('event-id-'+str(eu.event_id)): e_html += cache.get('event-id-'+str(eu.event_id)) else: e = Event.query.get(eu.event_id) h = render_template('event_list.html',e=e) cache.set('event-id-'+str(eu.event_id),h,100) e_html += h ''' if cache.get('event-id-'+str(eu.id)): event_list.append(cache.get('event-id-'+str(eu.id))) else: event = Event.query.get(eu.id) #cache.set('event-id-'+str(eu.id),event,6000) event_list.append(event) ''' #except: # event_list = None #return '' return render_template( 'index.html',viewname=viewname,e_html=e_html)
def add_comment(post_id, parent_id=None): is_login() post = Post.query.get_or_404(post_id) post.permissions.view.test(403) parent = Comment.query.get_or_404(parent_id) if parent_id else None form = CommentForm() if form.validate_on_submit(): comment = Comment(post=post, parent=parent, author=g.user) form.populate_obj(comment) db.session.add(comment) db.session.commit() signals.comment_added.send(post) flash(_("Thanks for your comment"), "success") author = parent.author if parent else post.author if author.email_alerts and author.id != g.user.id: subject = _("Somebody replied to your comment") if parent else \ _("Somebody commented on your post") template = "emails/comment_replied.html" if parent else \ "emails/post_commented.html" body = render_template(template, author=author, post=post, parent=parent, comment=comment) mail.send_message(subject=subject, body=body, recipients=[post.author.email]) return redirect(comment.url) return render_template("post/add_comment.html", parent=parent, post=post, form=form)
def change_password(): is_login() user = None if g.user: user = User.query.filter_by( id = g.user.id ).first() elif 'activation_key' in request.values: user = User.query.filter_by( activation_key=request.values['activation_key']).first() if user is None: abort(403) form = ChangePasswordForm(activation_key=user.activation_key) if form.validate_on_submit(): user.password = form.password.data user.activation_key = None db.session.commit() #登出 session.pop('user_id', None) flash(u"密码修改成功,请重新登录。", "success") #清除缓存 cache.delete('session_login_uid_'+str(session['user_id'])) return redirect(url_for("account.login")) return render_template("account/change_password.html", form=form)
def signup(): form = SignupForm(next=request.args.get("next")) if form.validate_on_submit(): user = User() form.populate_obj(user) user.reg_ip = request.remote_addr db.session.add(user) user_icon = Users_icon() db.session.commit() user_icon.uid = user.id db.session.add(user_icon) #默认姓名就是用户名 user.name = user.username #广播一下 queue.push((101,user.id)) db.session.commit() send_msg(user.id,u'欢迎加入42ic.com',u'42ic是一个专注于电子行业的社区网站,欢迎您的加入!') flash(u"您已经注册成功!请登陆", "success") next_url = form.next.data if not next_url or next_url == request.path: next_url = url_for('account.login') #print('OK') return redirect(next_url) return render_template("account/signup.html", form=form)
def upload_file(): is_login() if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1#如果没有指定就默认为第一页 if request.method == 'POST': file = request.files['upfile'] if allowed_file( file.filename ): tmp = str(random.randrange(0,9))#0-9是随机的目录 filename = tmp+'/42ic.com-'+ str( int(time.time()) ) + "." + file.filename.rsplit( '.', 1 )[1] #存放原始文件 file.save( os.path.join( app.config['UPFILE_PATH'], filename ) ) filesize = os.path.getsize( app.config['UPFILE_PATH']+ filename) #如果是图片文件,就生成两组小图 if file_is_img(file.filename): im = Image.open( app.config['UPFILE_PATH'] + "/" + filename ).convert("RGB") #大图也加一个水印 im_artwork = im.copy() WriteText(im_artwork, 'www.42ic.com', os.path.join( app.config['UPFILE_PATH']+'artwork/', filename )) #生成2组小图并加水印 i = Graphics(im, app.config['UPFILE_PATH'],filename); i.run_thumb_all([200,400],[540,1000]); a = Attachment( g.user.id, file.filename.rsplit( '.', 1 )[1],\ filesize, file.filename, filename) db.session.add( a ) db.session.commit() flash(u"上传成功。", "success") file_list = Attachment.query.filter(Attachment.user_id==g.user.id).\ order_by(Attachment.id.desc()).paginate(page, per_page=10) page_url = lambda page: '?page='+str(page) return render_template("upload.html",file_list=file_list,page_url=page_url)
def group_requisition(): is_admin() action = request.args.get("action", '').strip() #如果通过申请 if action == 'pass': id = int(request.args.get("id", '').strip()) gr = Group_Requisition.query.get_or_404(id) group = Group(gr.name,gr.url_name,gr.author_id,gr.description) gr.status = 1 send_msg(gr.author_id,u'小组申请通过',u'恭喜您,您所申请的'+gr.name+u'小组已经通过审核。去http://www.42ic.com/group/'+gr.url_name+u',看看吧') db.session.add( group ) db.session.commit() #申请人自动加入 group_user = Group_User(group.id,gr.author_id) db.session.add( group_user ) db.session.commit() return redirect(url_for("admin.group_requisition")) if action == 'refusal': id = int(request.args.get("id", '').strip()) gr = Group_Requisition.query.get_or_404(id) gr.status = 2 send_msg(gr.author_id,u'小组申请失败',u'对不起,您申请的小组'+gr.name+u'资料不全,未能通过审核') db.session.commit() return redirect(url_for("admin.group_requisition")) req_list = Group_Requisition.query.order_by(Group_Requisition.status).all() #send_msg(1,'hi','lyping') return render_template("/admin/group_requisition.html",req_list = req_list)
def group_view(slug, page=1): groupinfo = Group.query.urlname_get_info(slug) #如果没有指定就默认为第一页 if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1 #检查是否有发言权限 if g.user: is_join = Group_User.query.is_join(groupinfo.id,g.user.id) if is_join is None: is_join = 0 else: is_join = 0 #最近加入小组的用户(有缓存) if cache.get('group-'+slug+'recently_join_user'): recently_join_user = cache.get('group-'+slug+'recently_join_user') else: recently_join_user = Group_User.query.recently_join_user(groupinfo.id) cache.set('group-'+slug+'recently_join_user',recently_join_user,600) #获取该小组的主题列表 topic_list = Group_Topic.query.get_topic_list(groupinfo.id).\ paginate(page, per_page=30) page_url = lambda page: '?page='+str(page) return render_template( 'group/g_index.html',groupinfo=groupinfo,\ is_join=is_join,recently_join_user=recently_join_user,\ topic_list=topic_list, page_url=page_url)
def topic_view(topic_id): topic = Group_Topic.query.get_or_404(topic_id) groupinfo = Group.query.id_get_info(topic.group_id) #如果已经删除,判断是否显示 if topic.deleteed: if topic.author_id == g.user.id or groupinfo.leader_id == g.user.id\ or g.user.role >=300: pass else: abort(404) #检查是否有发言权限 if g.user: is_join = Group_User.query.is_join(groupinfo.id,g.user.id) if is_join is None: is_join = 0 else: is_join = 0 #如果具有发言权,生成回复表单 if is_join: reply_form = Group_Topic_New_ReplyForm() else: reply_form = None #获取回应列表 reply_list = Group_Reply.query.get_reply_list(topic_id) return render_template( 'group/topic_view.html',groupinfo=groupinfo,\ topic=topic, reply_form=reply_form, reply_list=reply_list)
def user_icon(): is_login() if request.method == 'POST': xyh = request.form['imgpos'].split('_') ed_icon = Users_icon.query.filter_by( uid = g.user.id ).first() ed_icon.x = x = int(xyh[0]) ed_icon.y = y = int(xyh[1]) ed_icon.h = h = int(xyh[2]) db.session.commit() #处理图片并保存 import Image im = Image.open( app.config['AVATAR_IMAGE_PATH'] + "/" + ed_icon.image_name ) box = (x,y,x+h,y+h) #裁剪图片 image = im.crop(box) #缩略图片 image2 = pic_square( image, 48) del image tmp = str(random.randrange(0,9)) filename = tmp+'/'+str( g.user.id ) + "-" + str( int(time.time()) ) + "." + ed_icon.image_name.rsplit( '.', 1 )[1] image2.save( os.path.join( app.config['AVATAR_PATH'], filename ) ) #更新用户资料 ed_user = User.query.filter_by( id = g.user.id ).first() ed_user.avatar = filename db.session.commit() flash(u"头像编辑成功。", "success") #清除缓存 cache.delete('session_login_uid_'+str(session['user_id'])) d = {} ed_icon = Users_icon.query.filter_by( uid = g.user.id ).first() d['icon_image_url'] = "avatar/image/" + ed_icon.image_name d['icon_x'] = ed_icon.x d['icon_y'] = ed_icon.y d['icon_h'] = ed_icon.h return render_template( 'account/user_icon.html',**d)
def edit_group(slug): is_login() group = Group.query.urlname_get_info(slug) form = Group_EditForm(obj=group) #判断权限 if group.leader_id != g.user.id: flash(u'对不起,您无权操作!', "error") return redirect(url_for("group.group_view",slug=slug)) if form.validate_on_submit(): form.populate_obj(group) try: file = request.files['group_icon'] except: file = None #获取图标文件 if file: image_path = '/home/lyping/data/42ic/' + 'iapp/static/groupicon/image' icon_path = '/home/lyping/data/42ic/' + 'iapp/static/groupicon' ALLOWED_EXTENSIONS = set( ['bmp', 'png', 'jpg', 'jpeg', 'gif', 'BMP', 'PNG', 'JPG', 'JPEG', 'GIF'] ) #存储原文件 filename = str( group.id ) + "-" + str( int(time.time()) ) + "." + file.filename.rsplit( '.', 1 )[1] file.save( os.path.join( image_path, filename ) ) #产生缩略图 import Image from iapp.utils.pic import picopen, pic_square iconname = str( group.id ) + "-"+ str( int(time.time()) ) + file.filename.rsplit( '.', 1 )[1] im = Image.open( image_path + "/" + filename ) image2 = pic_square( im, 48 ) image2.save( os.path.join( icon_path, filename ) ) #存储图标名称 group.group_icon_url = filename db.session.commit() flash(u'资料修改成功!', "success") return redirect(url_for("group.group_view",slug=slug)) return render_template('group/edit_group.html', form = form,group=group)
def group_trash(slug): is_login() groupinfo = Group.query.urlname_get_info(slug) #如果没有指定就默认为第一页 action = request.args.get("action", '').strip() if action == 'restore': if groupinfo.leader_id == g.user.id or g.user.role >=300: id = int(request.args.get("id", '').strip()) topic = Group_Topic.query.get_or_404(id) topic.deleteed = 0 db.session.commit() flash(u'主题恢复成功', "success") if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1 #判断是不是组长 if groupinfo.leader_id == g.user.id: topic_list = Group_Topic.query.get_topic_trash_list(groupinfo.id).\ paginate(page, per_page=30) else: topic_list = Group_Topic.query.get_my_topic_trash_list(groupinfo.id,g.user.id).\ paginate(page, per_page=30) page_url = lambda page: '?page='+str(page) return render_template( 'group/group_trash.html',groupinfo=groupinfo,\ topic_list=topic_list, page_url=page_url)
def delete(post_id): is_login() post = Post.query.get_or_404(post_id) post.permissions.delete.test(403) Comment.query.filter_by(post=post).delete() db.session.delete(post) db.session.commit() if g.user.id != post.author_id: body = render_template("emails/post_deleted.html", post=post) message = Message(subject="Your post has been deleted", body=body, recipients=[post.author.email]) mail.send(message) flash(_("The post has been deleted"), "success") else: flash(_("Your post has been deleted"), "success") return jsonify(success=True, redirect_url=url_for('frontend.index'))
def search(): keywords = request.args.get("q", '').strip() group_submit = request.args.get("group_submit", '').strip() topic_submit = request.args.get("topic_submit", '').strip() if not keywords: return redirect(url_for("frontend.index")) if group_submit == u'搜索小组': viewname = 'group_submit' group_list = Group.query.search(keywords).all() return render_template( 'group/search.html',viewname=viewname\ ,group_list=group_list,keywords=keywords) if topic_submit == u'搜索发言': viewname = 'topic_submit' topic_list = Group_Topic.query.search(keywords).all() return render_template( 'group/search.html',viewname=viewname\ ,topic_list=topic_list,keywords=keywords) return redirect(url_for("frontend.index"))
def view(question_id): #我是否已经回答(用于处理模版的回答输入框) have_answered = 0 if g.user: if Answer.query.filter(Answer.question_id==question_id).filter(Answer.author_id==g.user.id).first(): have_answered = 1 question = Question.query.get_or_404(question_id) answer = Answer.query.answer_list( question_id ) return render_template( 'question/view.html', question = question, answer = answer,have_answered=have_answered)
def new_group(): is_login() form = Group_RequisitionForm() if form.validate_on_submit(): group_requisition = Group_Requisition() form.populate_obj(group_requisition) db.session.add(group_requisition) db.session.commit() flash(u'申请提交成功,审核通过后会站内信息通知您', "success") return redirect(url_for("group.index")) return render_template('group/new_group.html', form = form)
def question_search(): keywords = request.args.get("q", '').strip() if not keywords: return redirect(url_for("question.index")) if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1#如果没有指定就默认为第一页 question = Question.query.search(keywords).paginate(page, per_page=Question.PER_PAGE) page_url = lambda page: '?q='+keywords+'&page='+str(page) return render_template( 'question/search.html',question=question,\ page_url=page_url,keywords=keywords)
def question(username): peopleinfo = User.query.username_get_info(username) viewname = 'question' if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1#如果没有指定就默认为第一页 question_list = Question.query.filter(Question.author_id == peopleinfo.id)\ .filter(Question.deleteed == 0).paginate(page, per_page=Question.PER_PAGE) page_url = lambda page: '?page='+str(page) return render_template("people/question.html",peopleinfo=peopleinfo,\ viewname=viewname,question_list=question_list,page_url=page_url)
def public(): #如果没有指定就默认为第一页 if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1 page_url = lambda page: '?page='+str(page) topic_list = Group_Topic.query.get_all_list().\ paginate(page, per_page=30) #热门小组,按加入人数排序 hot_group = Group.query.hot_group_list() return render_template( 'group/no_login_index.html',\ topic_list=topic_list, page_url=page_url,hot_group=hot_group)
def notice_read(username,notice_id): peopleinfo = User.query.username_get_info(username) viewname = 'notece' msg = User_Message.query.get_or_404(notice_id) if msg.unread == 1: user = User.query.get_or_404(g.user.id) user.unread_message = user.unread_message - 1 #清除缓存 cache.delete('session_login_uid_'+str(session['user_id'])) msg.unread = 0 db.session.commit() return render_template("people/read_notice.html",peopleinfo=peopleinfo,\ viewname=viewname,msg=msg)
def topic(username): peopleinfo = User.query.username_get_info(username) viewname = 'topic' if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1#如果没有指定就默认为第一页 topic_list = Group_Topic.query.filter(Group_Topic.author_id == peopleinfo.id)\ .filter(Group_Topic.deleteed == 0).paginate(page, per_page=Group_Topic.PER_PAGE) page_url = lambda page: '?page='+str(page) return render_template("people/topic.html",peopleinfo=peopleinfo,\ viewname=viewname,topic_list=topic_list,page_url=page_url)
def namecard(): is_login() form = EditNameCardForm(g.user) if form.validate_on_submit(): userinfo = User.query.filter_by( id = g.user.id ).first() form.populate_obj(userinfo) db.session.commit() flash(u'资料已经更新', "success") #清除缓存 cache.delete('session_login_uid_'+str(session['user_id'])) return redirect(url_for("account.namecard")) return render_template("account/edit_namecard.html", form=form)
def ask(slug=u''): is_login() form = Question_AskForm() if form.validate_on_submit(): question = Question() form.populate_obj(question) question.ip = request.remote_addr db.session.add(question) db.session.commit() flash(u'问题发布成功', "success") queue.push((301,g.user.id,question.id,question.title,question.description[0:100]))#发任务 queue.push((202,g.user.id,2))#提问增加2积分 return redirect(url_for("question.view", question_id=question.id)) return render_template( 'question/ask.html', form=form, title=slug)
def my_topics(): is_login() #如果没有指定就默认为第一页 if (request.args.get("page", '').strip()): page = int(request.args.get("page", '').strip()) else: page = 1 page_url = lambda page: '?page='+str(page) topic_list = Group_Topic.query.get_my_topics_list(g.user.id).\ paginate(page, per_page=30) #我加入的小组(近9) my_join_group = Group_User.query.my_join_group(g.user.id) return render_template( 'group/my_topics.html',\ topic_list=topic_list, page_url=page_url,my_join_group=my_join_group)
def forgot_password(): form = RecoverPasswordForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: flash(u"临时登陆密码已发至"+form.email.data+u",请注意查收", "success") new_password = string.join( random.sample( ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '5', '6', '7', '8'], 6 ) ).replace( " ", "" ) user.password = new_password db.session.commit() body = render_template("emails/recover_password.html", new_password=new_password) message = Message(subject=u"42ic.com临时登陆密码", body=body, recipients=[user.email]) mail.send(message) return redirect(url_for("frontend.index")) else: flash(u"对不起,您的邮箱地址不存在", "error") return render_template("account/recover_password.html", form=form)