def ajax_create_post(): content = html.escape(request.form.get('content').strip()) auth_id = session.get('id_user_logged') user_id = request.form.get('user_id') if content: user = get_user_by_id(auth_id) if user and user['active']: res = create_post(user_id, auth_id, "text", "public", content, None, None) if res: user_first_name = user['first_name'] user_last_name = user['last_name'] user_avatar = user['avatar'] post = get_post_by_id(res) if post: if not post['id_user'] == auth_id: msg = "User: "******" " + user_last_name + " send you post" add_notification(user_id, msg) user2 = get_user_by_id(user_id) rating = user2['rating'] + 10 update_rating(rating, user_id) data = { 'user_avatar': user_avatar, 'user_first_name': user_first_name, 'user_last_name': user_last_name, 'auth_id': auth_id, 'type': "text", 'content': content, 'id_post': res, 'date_creation': post['date_creation'].split(" ")[0], 'time_creation': post['date_creation'].split(" ")[1] } return jsonify(data) return "Fail"
def ajax_like(): id_user = session.get('id_user_logged') id_post = request.form.get('post_id') if id_user: if liked(id_user, id_post): res = unlike(id_user, id_post) if not res: user_post = get_post_by_id(id_post) if not user_post['id_user_from'] == id_user: msg = "User: "******" " + \ session.get('user_data')['last_name'] + " unlike your post" add_notification(user_post['id_user_from'], msg) user = get_user_by_id(user_post['id_user_from']) rating = user['rating'] - 5 update_rating(rating, user_post['id_user_from']) return "unliked" else: res = like(id_user, id_post) if not res: user_post = get_post_by_id(id_post) if not user_post['id_user_from'] == id_user: msg = "User: "******" " + \ session.get('user_data')['last_name'] + " like your post" add_notification(user_post['id_user_from'], msg) user = get_user_by_id(user_post['id_user_from']) rating = user['rating'] + 5 update_rating(rating, user_post['id_user_from']) return "liked" return "Fail"
def list_messages(start_index, page_size, order_by): message_list = db.select('messages', what='id, title, message, message_group, creator, created, lastupdated, lastupdatedby, is_active', order=order_by, limit=page_size, offset=start_index).list() # get total users message_count = db.select('messages', what='count(0) as c')[0].c # initiate dictionary records = {} # initiate list resposta = [] if len(message_list) > 0: # set response message to OK records['Result'] = 'OK' # set per user informations for item in range(len(message_list)): # get 'creator' id from user u = get_user_by_id(message_list[item].get('creator')) # convert 'creator' id to 'creator' real name message_list[item].update(creator=u[0].get('name')) # get 'lastupdatedby' id from message u = get_user_by_id(message_list[item].get('lastupdatedby')) # convert 'lastupdatedby' id to 'lastupdatedby' real name if u: message_list[item].update(lastupdatedby=u[0].get('name')) # convert 'created' and 'lastupdated' from epoch to gmtime if message_list[item].get('created'): message_list[item].update(created=time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(message_list[item].get('created')))) if message_list[item].get('lastupdated'): message_list[item].update(lastupdated=time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(message_list[item].get('lastupdated')))) resposta.append(message_list[item]) records['Records'] = resposta else: # no items retrieved from table records['Result'] = 'ERROR' records['Message'] = 'Nenhuma mensagem encontrada' # total records from table records['TotalRecordCount'] = message_count return json.dumps(records)
def POST(self): email = web.input().email #检查是否变动 old_email = users.get_user_by_id(user.id).email if email == old_email: return '{"status":"n", "code":"n-wbh", "info":"邮箱地址未变化"}' #检查新邮箱是否已经存在 elif not users.is_email_available(email): return '{"status":"n", "code":"n-ybsy", "info":"此邮箱已被使用"}' else: #更新用户表中email字段 # users.update(user.id, email=email) token = md5.md5(time.ctime() + email).hexdigest() t = datetime.datetime.now() #更新邮箱验证表中email\confirmed\token字段 users.update_confirm_email_by_douban_id(user.douban_id, email, token, t) #发送通知邮件 email_templates.change_email(email, token) print '======email send======' # session.reset() return '{"status":"y", "code":"y-y", "info":"验证邮件已发送,请通过邮件中的链接来验证此邮箱。在未验证之前,提醒通知还是发到旧邮箱中。"}'
def GET(self, arg, p=1): node = nodeModel.getNodeByNodeId(arg) if node: timestrf = misc.timestrf page = int(p) perpage = 10 offset = (page - 1) * perpage node_author_info = users.get_user_by_id(node.node_author) postList = postModel.getPostListByNodeIdSortByScore(arg, offset, perpage).list() a = [] for post in postList: a += str(post.postAuthor).split() authors = [] for i in xrange(len(a)): authors += users.get_users_by_id(a[i]) #得到权限 if user.is_logged: per = users.get_permission_by_douid(user.douban_id) rights = per[0].rights #得到提醒 notification_results, notification_num = notification.get_unread_notification(user.id) #得到@提醒 notification_mention_results, mention_num= notification.get_unread_metion_notifition(user.id) #链表 得到提醒的详细id\名称等 ntf_posts = [] ntf_users = [] mtf_posts = [] mtf_users = [] ntf_list = notification_results.list() mtf_list = notification_mention_results.list() for x in xrange(len(ntf_list)): ntf_posts += postModel.getPostsByPostId(ntf_list[x].pid) ntf_users += users.get_users_by_id(ntf_list[x].uid) for x in xrange(len(mtf_list)): mtf_posts += postModel.getPostsByPostId(mtf_list[x].pid) mtf_users += users.get_users_by_id(mtf_list[x].uid) ntf_list = ntf_list + mtf_list ntf_posts = ntf_posts + mtf_posts ntf_users = ntf_users + mtf_users notification_num = notification_num+mention_num else: rights = 0 notification_results = None notification_num = None ntf_list = None ntf_posts = None ntf_users = None return view.base(view.node_show(timestrf, rights, node, postList, node_author_info, user, authors, sort='interesting'), user, siteName, rights, ntf_list, notification_num, ntf_posts, ntf_users) else: raise web.notfound()
def GET(self, id): img = image.get_img_by_imgid(id) if img: i = web.input(start=0, order=default_order, desc='desc', query='') start = int(i.start) user_id = session.is_logged() and session.get_user_id() author_id = img.userID results, num_results = image.query(author_id) pager = web.storage(paging.get_paging_results(start, num_results, int(id), results, results_per_page)) #tm = time.strftime('%Y年%m月%d日', time.localtime(img.creationTime)) author = users.get_user_by_id(img.userID) results = list(results) is_favorite = image.IsFavorite(user_id, img.id) #得到被多少人喜欢 #count = image.GetFavUserCount(img_id) #浏览人数加1 if user_id and image.user_not_viewed_image(user_id, id): image.add_img_hit(id) image.add_user_to_img_viewed(user_id, id) #得到评论 comments = image.get_comment_by_image_id(id).list() #得到评论者信息 comments_authors_ids = [] for i in xrange(len(comments)): #comments_authors_ids += str(comments[i].user_id).split() comments_authors_ids += users.get_users_by_id(comments[i].user_id).list() return view.base03(view.photo_single(img, pager, user_id, user, author, is_favorite, comments, comments_authors_ids), user, siteName, 2) else: raise web.notfound()
def POST(self): #note 需要判断数据库中用户是不是已经投过票了。 data = web.input() id = data.post_id uid = user.id #当前用户id p = postModel.getPostByPostId(id) #得到目标post magnitude = int(p.magnitude) #得到post的实际投票数 ts = p.creation_ts #得到post的创建时间 delta = (datetime.datetime.now() - ts).days #得到创建时间距今多少天 postModel.post_update(id, magnitude = magnitude + 1) #给post的实际投票数+1, 保存最终得票数 score = magnitude / (delta + 2)**2.1 #算出最终得票数 postModel.post_update(id, score = score) #入库 postModel.addVoteUser(id, uid) #记录片段ID和投票的用户ID/时间 #插入提醒 判断是不是自己喜欢自己的 if p.postAuthor != int(uid): notification.new_notification(p.postAuthor, uid, id, p.nodeId, tp=2) #如果开启了邮件提醒,给作者发送邮件 author = users.get_user_by_id(p.postAuthor) person = users.get_profile_by_user_id(p.postAuthor) if person.has_key('email_subscribe') and person.email_subscribe == 1 and author.email: email_templates.someone_like_ur_post(user, author, p) print '=== email send ==' return '{"status": "y", "info": "投票成功"}'
def POST(self, arg): data = web.input() postImage = data.post_Img postTitle = data.post_Name postCaption = data.post_Caption postArticle = data.post_Article postAuthor = user.id postTemp = data.temp nodeId = data.nodeID #入库 postModel.newPost(postImage, postTitle, postCaption, postArticle, postAuthor, postTemp, nodeId) #得到最新创建的post thisPost = postModel.getThisPostByUserId(postAuthor) node = nodeModel.getNodeByNodeId(nodeId) nodeAuthor = users.get_user_by_id(node.node_author) #判断是不是在自己创建的话题中发布片段 if nodeAuthor.id != int(postAuthor): #如果开启了邮件提醒,给此片段所属话题的作者发送邮件 person = users.get_profile_by_user_id(node.node_author) if person.has_key('email_subscribe') and person.email_subscribe == 1 and nodeAuthor.email: email_templates.someone_creat_new_post(user, nodeAuthor, node, thisPost) print '=== email send ==' #插入提醒 notification.new_notification(node.node_author, user.id, thisPost.id, nodeId, tp=4) return '{"post_id":'+ str(thisPost.id)+ ',"status":"y"}'
def POST(self, pid): data = web.input() uid = user.id aid = data.aid #note : 需要改成在后端通过pid得到aid,不能从前端传过来 p = postModel.getPostByPostId(pid) #得到目标post reg_regchar = '@([a-zA-Z0-9][\w\-\.\_]+)' comment = data.postComment comment = htmlquote(comment).strip().replace("\r\n", "<br/>") usernames = re.findall(reg_regchar, comment) nicknames = [] nickname_list = [] mid_list = [] # @提醒 for i in xrange(len(usernames)): if not users.is_username_available(usernames[i]): nicknames += users.get_user_by_username(usernames[i]).nickname.replace(' ', ' ').split() comment = comment.replace('@'+ usernames[i], '@<a href="/member/'+ usernames[i] +'">' + nicknames[i] + '</a>') #得到@的用户id 以|分割组成字符串 mid_list += str(users.get_user_by_username(usernames[i]).id).split() #去重 mid_list = sorted(set(mid_list),key=mid_list.index) #以字符串形势保存@到的uid,以|分割 # mention_id_list = '|'.join(mid_list) else: nicknames += usernames[i].split() comment = comment.replace('@'+ usernames[i], '@' + nicknames[i]) # @提醒 for mid in mid_list: if int(mid) != int(aid): notification.new_mention_notification(pid, p.nodeId, aid, uid,mid) print '=======notification send=====' # 评论提醒 tp=1 表示是评论类型的提醒 同时判断是不是本人评论本人 #这里很奇怪,不能直接判断aid 和 uid 是否相等,必须转成int if int(aid) != int(uid): notification.new_notification(aid, uid, pid, p.nodeId, tp=1) #如果开启了邮件提醒,给作者发送邮件 author = users.get_user_by_id(aid) person = users.get_profile_by_user_id(aid) p = postModel.getPostByPostId(pid) #得到目标post if person.has_key('email_subscribe') and person.email_subscribe == 1 and author.email: email_templates.someone_comment_ur_post(user, author, p) print '=== email send ==' postModel.add_post_comment(comment, uid, pid) #得到刚刚添加的comment的id,返回ajax给li添加id 供删除用 last_comment = postModel.get_just_added_comment(uid, pid).id return '{"status": "y", "comment_id": "'+ str(last_comment) +'"}'
def friends(): if session.get('id_user_logged'): data = { 'user': get_user_by_id(session.get('id_user_logged')), 'get_by_id': get_user_by_id, 'friends': all_friends_request(session.get('id_user_logged')), "all_friends": all_friends(session.get("id_user_logged")), 'users_online': get_users_online_list() } return render_template("newsfeed-friends.html", data=data) return redirect("/")
def user_friends(id_user=None): if not session.get('user_data') and not id_user: return redirect('/') if id_user: user = get_user_by_id(id_user) else: user = session.get('user_data') friends = all_friends(user['id_user']) data = { 'user': user, 'get_by_id': get_user_by_id, 'friends': friends, 'all_friends': friends } return render_template("timeline-friends.html", data=data)
def people_nearby(): if not session.get('id_user_logged'): return redirect('/') id_user = session.get('id_user_logged') user = get_user_by_id(id_user) not_friends = get_not_friends(id_user) session['not_friends'] = not_friends data = { 'user': user, 'all_friends': all_friends(user['id_user']), 'not_friends': not_friends, 'users_online': get_users_online_list() } return render_template("newsfeed-people-nearby.html", data=data)
def ajax_add_comment(): if session.get('id_user_logged'): id_user = session.get('id_user_logged') id_post = request.form.get('id_post') text = html.escape(request.form.get('text').strip()) if text: user = get_user_by_id(id_user) if user and user['active']: res = add_comment(id_user, id_post, text) if res: data = { 'user_avatar': user['avatar'], 'user_first_name': user['first_name'], 'id_user': id_user, 'id_post': id_post, 'id_comment': res, 'text': text } return jsonify(data) return "False"
def GET(self, id): img = image.get_img_by_imgid(id) if img: i = web.input(start=0, order=default_order, desc='desc', query='') start = int(i.start) user_id = session.is_logged() and session.get_user_id() author_id = img.userID results, num_results = image.query(author_id) pager = web.storage( paging.get_paging_results(start, num_results, int(id), results, results_per_page)) #tm = time.strftime('%Y年%m月%d日', time.localtime(img.creationTime)) author = users.get_user_by_id(img.userID) results = list(results) is_favorite = image.IsFavorite(user_id, img.id) #得到被多少人喜欢 #count = image.GetFavUserCount(img_id) #浏览人数加1 if user_id and image.user_not_viewed_image(user_id, id): image.add_img_hit(id) image.add_user_to_img_viewed(user_id, id) #得到评论 comments = image.get_comment_by_image_id(id).list() #得到评论者信息 comments_authors_ids = [] for i in xrange(len(comments)): #comments_authors_ids += str(comments[i].user_id).split() comments_authors_ids += users.get_users_by_id( comments[i].user_id).list() return view.base( view.photo_single(img, pager, user_id, user, author, is_favorite, comments, comments_authors_ids), user, siteName) else: raise web.notfound()
def POST(self, arg): data = web.input() postImage = data.post_Img postTitle = data.post_Name postCaption = data.post_Caption postArticle = data.post_Article postAuthor = user.id postTemp = data.temp nodeId = data.nodeID #入库 postModel.newPost(postImage, postTitle, postCaption, postArticle, postAuthor, postTemp, nodeId) #得到最新创建的post thisPost = postModel.getThisPostByUserId(postAuthor) node = nodeModel.getNodeByNodeId(nodeId) nodeAuthor = users.get_user_by_id(node.node_author) #判断是不是在自己创建的话题中发布片段 if nodeAuthor.id != int(postAuthor): #如果开启了邮件提醒,给此片段所属话题的作者发送邮件 person = users.get_profile_by_user_id(node.node_author) if person.has_key( 'email_subscribe' ) and person.email_subscribe == 1 and nodeAuthor.email: email_templates.someone_creat_new_post(user, nodeAuthor, node, thisPost) print '=== email send ==' #插入提醒 notification.new_notification(node.node_author, user.id, thisPost.id, nodeId, tp=4) return '{"post_id":' + str(thisPost.id) + ',"status":"y"}'
def ajax_dell_post(): if session.get('id_user_logged'): post_id = request.form.get('id_post') auth_id = session.get('id_user_logged') res = get_post_by_id(post_id) user_id = res['id_user'] if res and (res['id_user'] == auth_id or res['id_user_from'] == auth_id): dell = dell_post(post_id) if not user_id == res['id_user_from']: user = get_user_by_id(user_id) rating = user['rating'] - 10 update_rating(rating, user_id) if not dell: likes = dell_post_likes(post_id) if not likes: dislikes = dell_post_dislikes(post_id) if not dislikes: comment = dell_post_comments(post_id) if not comment: return "deleted" return "Fail"
def POST(self): #note 需要判断数据库中用户是不是已经投过票了。 data = web.input() id = data.post_id uid = user.id #当前用户id p = postModel.getPostByPostId(id) #得到目标post magnitude = int(p.magnitude) #得到post的实际投票数 ts = p.creation_ts #得到post的创建时间 delta = (datetime.datetime.now() - ts).days #得到创建时间距今多少天 postModel.post_update(id, magnitude=magnitude + 1) #给post的实际投票数+1, 保存最终得票数 score = magnitude / (delta + 2)**2.1 #算出最终得票数 postModel.post_update(id, score=score) #入库 postModel.addVoteUser(id, uid) #记录片段ID和投票的用户ID/时间 #插入提醒 判断是不是自己喜欢自己的 if p.postAuthor != int(uid): notification.new_notification(p.postAuthor, uid, id, p.nodeId, tp=2) #如果开启了邮件提醒,给作者发送邮件 author = users.get_user_by_id(p.postAuthor) person = users.get_profile_by_user_id(p.postAuthor) if person.has_key( 'email_subscribe' ) and person.email_subscribe == 1 and author.email: email_templates.someone_like_ur_post(user, author, p) print '=== email send ==' return '{"status": "y", "info": "投票成功"}'
def POST(self, pid): data = web.input() uid = user.id aid = data.aid #note : 需要改成在后端通过pid得到aid,不能从前端传过来 p = postModel.getPostByPostId(pid) #得到目标post reg_regchar = '@([a-zA-Z0-9][\w\-\.\_]+)' comment = data.postComment comment = htmlquote(comment).strip().replace("\r\n", "<br/>") usernames = re.findall(reg_regchar, comment) nicknames = [] nickname_list = [] mid_list = [] # @提醒 for i in xrange(len(usernames)): if not users.is_username_available(usernames[i]): nicknames += users.get_user_by_username( usernames[i]).nickname.replace(' ', ' ').split() comment = comment.replace( '@' + usernames[i], '@<a href="/member/' + usernames[i] + '">' + nicknames[i] + '</a>') #得到@的用户id 以|分割组成字符串 mid_list += str(users.get_user_by_username( usernames[i]).id).split() #去重 mid_list = sorted(set(mid_list), key=mid_list.index) #以字符串形势保存@到的uid,以|分割 # mention_id_list = '|'.join(mid_list) else: nicknames += usernames[i].split() comment = comment.replace('@' + usernames[i], '@' + nicknames[i]) # @提醒 for mid in mid_list: if int(mid) != int(aid): notification.new_mention_notification(pid, p.nodeId, aid, uid, mid) print '=======notification send=====' # 评论提醒 tp=1 表示是评论类型的提醒 同时判断是不是本人评论本人 #这里很奇怪,不能直接判断aid 和 uid 是否相等,必须转成int if int(aid) != int(uid): notification.new_notification(aid, uid, pid, p.nodeId, tp=1) #如果开启了邮件提醒,给作者发送邮件 author = users.get_user_by_id(aid) person = users.get_profile_by_user_id(aid) p = postModel.getPostByPostId(pid) #得到目标post if person.has_key( 'email_subscribe' ) and person.email_subscribe == 1 and author.email: email_templates.someone_comment_ur_post(user, author, p) print '=== email send ==' postModel.add_post_comment(comment, uid, pid) #得到刚刚添加的comment的id,返回ajax给li添加id 供删除用 last_comment = postModel.get_just_added_comment(uid, pid).id return '{"status": "y", "comment_id": "' + str(last_comment) + '"}'
def GET(self, arg, ntf_type=''): #ntf_type为提醒类型,用来让用户从提醒页面访问片段页面时可以看到对应提醒 post = postModel.getPostByPostId(arg) if post: post_author_info = users.get_user_by_id(post.postAuthor) node = nodeModel.getNodeByNodeId(post.nodeId) timestrf = misc.timestrf likers = postModel.get_voters_by_pid(post.id) likers_list = [] for i in xrange(len(likers)): likers_list += users.get_users_by_id(likers[i].uid) comments = postModel.get_comments_by_pid(post.id).list() commenters = [] for i in xrange(len(comments)): commenters += users.get_users_by_id(comments[i].uid) #是否登录, if user.is_logged: per = users.get_permission_by_douid(user.douban_id) rights = per[0].rights #是否投过票 is_voted = postModel.is_voted(arg, user.id) #得到提醒 notification_results, notification_num = notification.get_unread_notification( user.id) #得到@提醒 notification_mention_results, mention_num = notification.get_unread_metion_notifition( user.id) #链表 得到提醒的详细id\名称等 ntf_posts = [] ntf_users = [] mtf_posts = [] mtf_users = [] ntf_list = notification_results.list() mtf_list = notification_mention_results.list() for x in xrange(len(ntf_list)): ntf_posts += postModel.getPostsByPostId(ntf_list[x].pid) ntf_users += users.get_users_by_id(ntf_list[x].uid) for x in xrange(len(mtf_list)): mtf_posts += postModel.getPostsByPostId(mtf_list[x].pid) mtf_users += users.get_users_by_id(mtf_list[x].uid) #获取提醒页面传过来的提醒类型参数 ntf_type = web.input(ntf_type='').ntf_type ntf_list = ntf_list + mtf_list ntf_posts = ntf_posts + mtf_posts ntf_users = ntf_users + mtf_users notification_num = notification_num + mention_num else: rights = 0 is_voted = None notification_results = None notification_num = None ntf_list = None ntf_posts = None ntf_users = None return view.base( view.post_single(rights, node, post, post_author_info, user, timestrf, likers_list, comments, commenters, ntf_type, is_voted, misc), user, siteName, rights, ntf_list, notification_num, ntf_posts, ntf_users) else: raise web.notfound()
def reset(): user = users.get_user_by_id(get_user_id()) login(user.email)
def GET(self, arg, ntf_type=''): #ntf_type为提醒类型,用来让用户从提醒页面访问片段页面时可以看到对应提醒 post = postModel.getPostByPostId(arg) if post: post_author_info = users.get_user_by_id(post.postAuthor) node = nodeModel.getNodeByNodeId(post.nodeId) timestrf = misc.timestrf likers = postModel.get_voters_by_pid(post.id) likers_list = [] for i in xrange(len(likers)): likers_list += users.get_users_by_id(likers[i].uid) comments = postModel.get_comments_by_pid(post.id).list() commenters = [] for i in xrange(len(comments)): commenters += users.get_users_by_id(comments[i].uid) #是否登录, if user.is_logged: per = users.get_permission_by_douid(user.douban_id) rights = per[0].rights #是否投过票 is_voted = postModel.is_voted(arg, user.id) #得到提醒 notification_results, notification_num = notification.get_unread_notification(user.id) #得到@提醒 notification_mention_results, mention_num= notification.get_unread_metion_notifition(user.id) #链表 得到提醒的详细id\名称等 ntf_posts = [] ntf_users = [] mtf_posts = [] mtf_users = [] ntf_list = notification_results.list() mtf_list = notification_mention_results.list() for x in xrange(len(ntf_list)): ntf_posts += postModel.getPostsByPostId(ntf_list[x].pid) ntf_users += users.get_users_by_id(ntf_list[x].uid) for x in xrange(len(mtf_list)): mtf_posts += postModel.getPostsByPostId(mtf_list[x].pid) mtf_users += users.get_users_by_id(mtf_list[x].uid) #获取提醒页面传过来的提醒类型参数 ntf_type = web.input(ntf_type='').ntf_type ntf_list = ntf_list + mtf_list ntf_posts = ntf_posts + mtf_posts ntf_users = ntf_users + mtf_users notification_num = notification_num+mention_num else: rights = 0 is_voted = None notification_results = None notification_num = None ntf_list = None ntf_posts = None ntf_users = None return view.base(view.post_single(rights, node, post, post_author_info, user, timestrf, likers_list, comments, commenters, ntf_type, is_voted, misc), user, siteName, rights, ntf_list, notification_num, ntf_posts, ntf_users) else: raise web.notfound()
def GET(self): userid = session.get_user_id() user_data = users.get_user_by_id(userid) # print user_data return view.base(view.index(user_data))
def GET(self, name): userid = session.get_user_id() user_data = users.get_user_by_id(userid) return view.base(view.index(user_data))