def comment(): """ comment handler, add a new comment to DB give a new notification to post poster and send a validate email """ zid = session['zid'] user = User.findByKey(zid) if not user: return redirect(url_for('login')) pid = request.form.get('pid') poster_zid = request.form.get('poster_zid') m_user = User.findByKey(poster_zid) message = request.form.get('message') new_comment = Comment(pid=pid, zid=zid, message=message) new_comment.save() flash('Success: you have post a new comment.') notification = Notifications(from_zid=user.zid, to_zid=poster_zid, noti_type='reply', from_name=user.full_name, from_img=user.image, pid=pid) # notification.pid = pid notification.save() mail = MailUtils('*****@*****.**', 'MateLook Team', m_user.full_name) mail.notificaion() post = Post.findByKey(pid) poster = User.findByKey(post.zid) page = int(request.args.get('page', '1')) total = len(post.getComments()) pag = Pagination(page, total) return redirect(url_for('postlook', pid=pid, page=page))
def create_comment(post_id): user = SESSION.get(request.headers.get('Authorization')) if user is None: abort(400, {'message': 'TOKEN_NOT_FOUND'}) input_data = request.get_json() from model import Comment, Post post_exist = Post.find(post_id) if post_exist is None: abort(400, {'message','POST_NOT_FOUND'}) comment = Comment() comment.post_id = post_id comment.user_id = user.id comment.body = input_data['body'] comment.save() return jsonify("Created.")
def save_comment(self, commentsOfStatusDict, statusId): """ status_id:微博id commentsOfStatusDict:一条微博的所有评论 :return comments_ids """ commentsReferenceList = [] #comments_ids if commentsOfStatusDict: for comment in commentsOfStatusDict: commentId = comment.get('id') comment_existed = self.find_comment_exist(commentId) created_at_str = comment.get('created_at').decode('utf-8') created_at_time = datatimeFormat.strip_status_created_time( created_at_str) createdTime = datetime.datetime.strptime( created_at_time, '%a %b %d %H:%M:%S %Y') if comment_existed: #该条评论已经存在 if commentId in commentsReferenceList: pass else: commentsReferenceList.append(commentId) else: commentAuthorDict = comment.get('user') userSimple = { 'user_id': commentAuthorDict.get('id'), 'name': commentAuthorDict.get('name'), 'location': commentAuthorDict.get('location'), 'followers_count': commentAuthorDict.get('followers_count') } newComment = Comment( comment_id=comment.get('id'), text=comment.get('text'), created_at=createdTime, \ source=comment.get('source'), comment_status_id=statusId, reply_comment_id=comment.get('reply_comment_id'), \ user_simple=userSimple ) userManager = UserManager() userManager.save_user(comment.get('id'), commentAuthorDict, id_of_status=False) newComment.save() commentsReferenceList.append(commentId) return commentsReferenceList
def api_create_comment(id, request, *, content): user = request.__user__ if user is None: raise APIPermissionError('Please signin first.') if not content or not content.strip(): raise APIValueError('content') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) yield from comment.save() return comment
def api_create_comment(id,request,*,content): user = request.__user__ if user is None: raise APIPermissionError('please signin first...') if not content or not content.strip(): raise APIValueError('content') blog = yield from Blog.find(id) if blog is None: raise APIResourceNotFoundError('Blog') comment = Comment(blog_id = blog.id ,user_id = user.id,user_name=user.name,user_image = user.image,content = content.strip()) yield from comment.save() return comment
def api_craete_commnet(id, request, content): if not id or not id.strip(): raise APIValueError('id','empty id') if not content or not content.strip(): raise APIValueError('content','empty content') commnet = Comment(blog_id=id, user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, content=content) yield from commnet.save() r = aiohttp.web.Response() r.content_type = 'application/json' r.body = json.dumps(commnet,ensure_ascii=False).encode('utf-8') return r
def post(self): verify = self.request.get('sdamwegmw') if verify != "AfnGDASsdafSA" : return self.error(403) post_id_ = self.request.get('post_id') post = Post.get_by_id(int(post_id_)) if post is None: return self.redirect('/') comment = Comment() comment.post = post comment.author = self.request.get('comm_name') if users.is_current_user_admin(): comment.author_is_admin = True comment.authorEmail = self.request.get('comm_email') comment.authorWebsite = self.request.get('comm_url') comment.content = self.request.get('comment') comment.userIp = self.request.remote_addr user = users.get_current_user() if user is not None: comment.user = user comment.author = str(user.nickname()) comment.authorEmail = str(user.email()) cookies = Cookie.SimpleCookie() cookies['comm_name'] = urllib.quote(comment.author.encode('utf8')) cookies['comm_name']['path'] = '/' cookies['comm_name']['max-age'] = 3600*24*365 cookies['comm_email'] = comment.authorEmail cookies['comm_email']['path'] = '/' cookies['comm_email']['max-age'] = 3600*24*365 cookies['comm_url'] = comment.authorWebsite cookies['comm_url']['path'] = '/' cookies['comm_url']['max-age'] = 3600*24*365 output_headers = [] output_headers.append('%s\r\n' % cookies) for header in output_headers: sys.stdout.write(header) comment.save() util.flushRecentComment() return self.redirect(post.full_permalink())
def post(self): if (self.isAdmin): name = self.blog.author email = self.login_user.email() url = self.blog.systemURL else: name = self.param('author') email = self.param('email') url = self.param('url') key = self.param('key') content = self.param('comment') if (name and content): ##should be removed when deploy to real site content = content.decode('utf8') name = name.decode('utf8') ##should be removed when deploy to real site comment = Comment(author=name, content=content, authorEmail=email, authorURL=url, ownerBlog=Blog.get(key)) comment.save() date = comment.commentTime.strftime("%Y-%m-%d") time = comment.commentTime.strftime("%H:%M") commentData = { 'author': comment.author, 'id': comment.key().id(), 'url': comment.authorURL, 'date': date, 'time': time } self.response.headers['Content-Type'] = 'text/plain; charset=utf-8' self.response.out.write(simplejson.dumps(commentData)) # self.redirect(Blog.get(key).selfLink) else: self.error(501, 'Please input name and comment .')
def init_db(): """ init the DATA_BASE, include create all tables and insert the value :return: None """ create_tables() for usr_path in glob.glob(DATA_PATH + '/*'): # print usr_path if not os.path.isfile(os.path.join(usr_path, 'user.txt')): continue with open(os.path.join(usr_path, 'user.txt'), 'r') as file: zid = '' full_name = '' program = '' home_longitude = '' home_latitude = '' courses = '' mates = '' email = '' password = '' birthday = '' image = 'no_image.png' for line in file: line = line.strip() # users = dict([line.strip().split('=', 1)]) if line.startswith('zid='): zid = line.split('=')[1] if line.startswith('full_name='): full_name = line.split('=')[1] if line.startswith('program='): program = line.split('=')[1] if line.startswith('home_longitude='): home_longitude = line.split('=')[1] if line.startswith('home_latitude='): home_latitude = line.split('=')[1] if line.startswith('courses='): courses = line.split('=')[1][1:-1] courses_list = courses.replace(' ', '').split(',') courses = pickle.dumps(courses_list) if line.startswith('mates='): mates = line.split('=')[1][1:-1] mates_list = mates.replace(' ', '').split(',') mates = pickle.dumps(mates_list) if line.startswith('password='******'=')[1] if line.startswith('birthday='): birthday = line.split('=')[1] if line.startswith('email='): email = line.split('=')[1] if os.path.isfile(os.path.join(usr_path, 'profile.jpg')): image = zid + '.jpg' src = os.path.join(usr_path, 'profile.jpg') dst = os.path.join(IMG_DST, image) shutil.copy(src, dst) # sql = insert_a_user.format(zid, full_name, program, home_longitude, # home_latitude, courses, mates, email, password, birthday, # image) # executeDAO(sql) # method 1 zid_password = '******' % (zid, password) sha_password = hashlib.sha1( zid_password.encode('utf-8')).hexdigest() # method 2 # sha1 = hashlib.sha1() # sha1.update(zid.encode('utf-8')) # sha1.update(b':') # sha1.update(password.encode('utf-8')) # sha_pwd = sha1.hexdigest() user = User(zid=zid, password=sha_password, email=email, full_name=full_name, birthday=birthday, program=program, home_longitude=home_longitude, home_latitude=home_latitude, courses=courses, image=image, mates=mates) user.save() if not os.path.isdir(os.path.join(usr_path, 'posts')): continue posts_path = os.path.join(usr_path, 'posts') for post in glob.glob(posts_path + '/*'): pid = next_id() if not os.path.isfile(os.path.join(post, 'post.txt')): continue with open(os.path.join(post, 'post.txt'), 'r') as file: message = '' zid = '' time = '' for line in file: line = line.strip() if line.startswith('message='): message = line.split('=')[1] if line.startswith('from='): zid = line.split('=')[1] if line.startswith('time='): time = line.split('=')[1] # message = message.replace("'", "") # sql = insert_a_post.format(zid, message, time) # executeDAO(sql) # pid = insertDAO(sql) pid = next_id() upost = Post(pid=pid, zid=zid, time=time, message=message) upost.save() if not os.path.isdir(os.path.join(post, 'comments')): continue comments_path = os.path.join(post, 'comments') for comment in glob.glob(comments_path + '/*'): if not os.path.isfile(os.path.join(comment, 'comment.txt')): continue with open(os.path.join(comment, 'comment.txt'), 'r') as file: message = '' zid = '' time = '' for line in file: line = line.strip() if line.startswith('message='): message = line.split('=')[1] if line.startswith('from='): zid = line.split('=')[1] if line.startswith('time='): time = line.split('=')[1] # message = message.replace("'", "`") # sql = insert_a_comment.format(pid, zid, message, time) # executeDAO(sql) cid = next_id() ucomment = Comment(cid=cid, pid=pid, zid=zid, time=time, message=message) ucomment.save()