def webchat(nickname): if not current_user.is_authenticated: return redirect(url_for('auth.login')) user = User.query.filter_by(nickname=nickname).first() if user is None or user == current_user or not current_user.is_friend( user): flash('请确认对方账号,或确认有无此朋友!') return redirect(url_for('main.index')) toshows = Message.query.filter( and_( or_( and_(Message.sender_id == user.id, Message.receiver_id == current_user.id), and_(Message.sender_id == current_user.id, Message.receiver_id == user.id)), Message.room_id == None)).order_by( Message.time_stamp).limit(500).all() if len(toshows): for toshow in toshows: if toshow.state == 'unshow': toshow.state = 'received' db.session.add(toshow) db.session.commit() else: toshows = None return render_template('webchat.html', user=user, toshows=toshows)
def view(id): imagepost = ImagePost.query.filter_by(id=id).first() if imagepost is None or not (current_user.is_friend(imagepost.author) or current_user == imagepost.author): flash('不能留言。') next = request.referrer if next is not None or not next.startswith('/'): next = url_for('main.index') return redirect(next) form = ViewForm() reply_to = (request.args.get('reply_to') if request.args.get('reply_to') is not None else None) if form.validate_on_submit(): view = View(author_id=current_user.id, imagepost_id=imagepost.id, view=(form.view.data if reply_to is None else "回复" + reply_to + ":" + form.view.data)) db.session.add(view) db.session.commit() attention_body = "您有新的留言回复。" attention_url = url_for('main.show_imagepost', id=imagepost.id) reply_to = ([imagepost.author] if current_user != imagepost.author else []) for view in imagepost.views.all(): if view.author != current_user: reply_to.append(view.author) store_attentions(reply_to, attention_body, attention_url) next = request.referrer if next is None or not next.startswith('/'): next = url_for('main.show_imagepost', id=imagepost.id) return redirect(next) return render_template('imagepost.html', imagepost=imagepost, form=form)
def delete_friend(id): friend = User.query.filter_by(id=id).first() if friend is not None and current_user.is_friend(friend): current_user.delete_friend(friend) db.session.commit() next = request.referrer if next is None or not next.startswith('/'): next = url_for('main.manage_friends') return redirect(next) return render_template('manage_friends.html')
def add_friend(id): user = User.query.filter_by(id=id).first() if user is None or current_user == user: flash('申请用户有误!') next_url = request.referrer if next_url is None or not next_url.startswith('/'): next_url = url_for('main.index') return redirect(next_url) elif current_user.request_sent(user) or current_user.is_friend(user): flash('您无需重复申请好友。') return redirect(url_for('main.user', id=user.id)) else: if current_user.is_requested_by(user): form = ConfirmForm() if form.validate_on_submit(): if form.submit_yes.data: current_user.add_friend(user) db.session.commit() attention_body = current_user.nickname + "同意了您的好友请求。" attention_url = url_for('main.webchat', nickname=current_user.nickname) store_attentions([user], attention_body, attention_url) elif form.submit_no.data: r = user.requests.filter_by(to_id=current_user.id).first() db.session.delete(r) db.session.commit() next_url = request.referrer if next_url is None or not next_url.startswith('/'): next_url = url_for('main.index') return redirect(next_url) return render_template('friendrequest.html', user=user, form=form) else: form = RequestForm() form.message = "你好,我是" + current_user.nickname + '。' if form.validate_on_submit(): f = FriendRequest(from_id=current_user.id, to_id=user.id, validation=form.message) db.session.add(f) db.session.commit() attention_body = current_user.nickname + "请求添加您为好友。" attention_url = url_for('main.add_friend', id=current_user.id) store_attentions([user], attention_body, attention_url) next_url = request.referrer if next_url is None or not next_url.startswith('/'): next_url = url_for('main.index') return redirect(next_url) return render_template('friendrequest.html', user=user, form=form)
def create_group(username=None): if username is None: flash('Please login to proceed.') return redirect(url_for('login')) elif username!=current_user.username: flash('Access Denied 3') return redirect(url_for('friends',username=current_user.username)) else: val=0 form_group=AddGroupForm() form_mem=AddFriendForm() if form_mem.validate_on_submit() and form_mem.addf.data: u=User.query.filter_by(email=form_mem.email.data).first() if u is None: flash('no one found') else: if current_user.is_friend(u) and u is not current_user: if u in mem_list: flash('Alredy Member') else: mem_list.append(u) flash(u.name + "added") else: flash('not in friend list') if form_group.validate_on_submit() and form_group.save.data: g=Groups() g.name=form_group.name.data g.description=form_group.desc.data g.creator=current_user.username g.num_mem=len(mem_list)+1 db.session.add(g) db.session.commit() g=Groups.query.filter_by(id=g.id).first() g.add_mem(current_user) for i in mem_list: g=Groups.query.filter_by(id=g.id).first() g.add_mem(i) del mem_list[:] return redirect(url_for('groups',username=current_user.username)) temp_list=mem_list[:] if form_group.save.data: return render_template('create_group.html',form_group=form_group,form_mem=form_mem,user=current_user,list=temp_list,val=1) elif form_mem.addf.data: return render_template('create_group.html',form_group=form_group,form_mem=form_mem,user=current_user,list=temp_list,val=2) else: return render_template('create_group.html',form_group=form_group,form_mem=form_mem,user=current_user,list=temp_list,val=0)
def user(id): user = User.query.filter_by(id=id).first() if user is not None: if not current_user.is_friend(user) and current_user != user: if request.cookies.get('user') == 'imageposts': imageposts = user.imageposts.limit(5) posts = None else: imageposts = None posts = user.posts.limit(10) return render_template('user.html', user=user, posts=posts, imageposts=imageposts) page = request.args.get('page', 1, type=int) if request.cookies.get('user') == 'imageposts': pagination = user.imageposts.order_by( ImagePost.time_stamp.desc()).paginate( page, per_page=current_app.config['FLASKY_IMAGEPOST_PER_PAGE'], error_out=False) imageposts = pagination.items posts = None else: pagination = user.posts.order_by(Post.time_stamp.desc()).paginate( page, per_page=current_app.config['FLASKY_POST_PER_PAGE'], error_out=False) posts = pagination.items imageposts = None return render_template('user.html', user=user, posts=posts, imageposts=imageposts, pagination=pagination) return redirect(url_for('.index'))