def decorator(*args, **kwargs): length = { 'username': lambda value: 3 <= len(value) <= 20, 'password': lambda value: 3 <= len(value) <= 20, } babel = { 'username': _("Username"), 'password': _("Password"), 'email': _("Email"), #'captcha': _("Captcha") } #keys.append('captcha') post_data = request.json for key in keys: if not post_data.get(key): msg = _('The %(key)s is required', key=babel[key]) #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR,message=msg).to_response() return get_json(0, msg, {}) if not length.get(key, lambda value: True)(post_data[key]): msg = _( "The %(key)s's length must be between 4 to 20 characters", key=babel[key]) #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR,message=msg).to_response() return get_json(0, msg, {}) captcha = post_data['captcha'] session_captcha = session.pop('captcha', '00000') if captcha.lower() != session_captcha.lower(): msg = _('The captcha is error') #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR,message=msg).to_response() return get_json(0, msg, {}) return func(*args, **kwargs)
def post(self): if current_user.is_confirmed: #return HTTPResponse(HTTPResponse.USER_IS_CONFIRMED).to_response() return get_json(1, '用户已通过确认', {}) self.send_email(current_user) #return HTTPResponse(HTTPResponse.NORMAL_STATUS, # description=_('An email has been sent to your.Please receive')).to_response() return get_json(1, '一封邮件已发出', {})
def get(self, id): comment = Comments.query.filter_by(answers_id = id).order_by('-id').all() comments_list = [] for i in comment: comment_user = User.query.filter_by(id = i.author_id).first() replies = Replys.query.filter_by(comments_id = i.id).all() diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) comments_data = object_as_dict(i) comments_data['diff_time'] = diff_time comments_data['author'] = comment_user.username Avatar(comments_data, comment_user) comments_list.append(comments_data) replies_list = [] for j in replies: diff_time = time_diff(j.updated_at) reply_user = User.query.filter_by(id = j.author_id).first() j.created_at = str(j.created_at) j.updated_at = str(j.updated_at) reply_data = object_as_dict(j) reply_data['author'] = reply_user.username reply_data['diff_time'] = diff_time replies_list.append(reply_data) Avatar(reply_data, reply_user) comments_data['replies'] = replies_list return get_json(1, '回答的评论列表', comments_list)
def decorator(*args, **kwargs): if current_user.is_authenticated: #flash(_("You have logined in ,needn't login again")) msg = _("You have logined in ,needn't login again") #return redirect('/') return get_json(1, msg, {}) return func(*args, **kwargs)
def post(self): post_data = request.json username = post_data['username'] password = post_data['password'] remember = post_data.pop('remember', True) user = User.query.filter_by(username=username).first() if not user or not user.check_password(password): msg = _('Username or Password Error') #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR, message=msg).to_response() return get_json(0, msg, {}) user.login(remember) data = {"username":user.username} Avatar(data, user) #serializer = user.serializer if hasattr( # user, 'serializer') else Serializer(user, depth=1) #return HTTPResponse(HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response() return get_json(1, '登录成功', data)
def _is_confirmed(*args, **kwargs): if not current_user.is_authenticated: #return redirect(url_for('auth.login', next=request.path)) return get_json(0, '未登录', {}) if confirm_permission.can(): return func(*args, **kwargs) flash('请验证你的帐号', 'warning') return
def post(self): post_data = request.json username = post_data['username'] password = post_data['password'] confirm_password = post_data['confirm_password'] email = post_data['email'] phone = post_data['phone'] try: recommender_code = post_data['recommender_code'] except: recommender_code = None if User.query.filter_by(email=email).exists(): msg = _('The email has been registered') #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR, message=msg).to_response() return get_json(0, msg, {}) if User.query.filter_by(phone=phone).exists(): msg = _('The phone has been registered') return get_json(0, msg, {}) if User.query.filter_by(username=username).exists(): msg = _('The username has been registered') #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR, message=msg).to_response() return get_json(0, msg, {}) if password != confirm_password: msg = _('Two passwords are different') user_code = self.user_code() user = User(username=username, email=email, phone=phone, user_code=user_code, recommender_code=recommender_code, integral=100) recommender_user = User.query.filter_by(user_code=recommender_code).first() user.integral = user.integral+1 if recommender_user: recommender_user.integral = user.integral+1 recommender_user.save() user.set_password(password) user.save() user.login(True) #self.send_email(user) #flash(_('An email has been sent to your.Please receive')) #msg = _('An email has been sent to your.Please receive') msg = _('注册成功') data = {"username":user.username} Avatar(data, user) #serializer = user.serializer if hasattr( # user, 'serializer') else Serializer(user, depth=1) #return HTTPResponse(HTTPResponse.NORMAL_STATUS, data=serializer.data).to_response() return get_json(1, msg, data)
def post(self): post_data = request.json email = post_data['email'] user = User.query.filter_by(email=email).first() if not user: msg = 'The email is error' #return HTTPResponse(HTTPResponse.HTTP_PARA_ERROR, message=msg).to_response() return get_json(0, msg, {}) password = ''.join(sample(ascii_letters + digits, 8)) user.set_password(password) user.save() self.send_email(user, password) #flash( # _('An email has been sent to you.' # 'Please receive and update your password in time')) msg = _('An email has been sent to you.Please receive and update your password in time') #return HTTPResponse(HTTPResponse.NORMAL_STATUS).to_response() return get_json(1, msg, {})
def get(self, token): user = User.check_email_token(token) if not user: msg = _('The confirm link has been out of time.' 'Please confirm your email again') #flash(msg) #return redirect('/') return get_json(1, msg, {}) if user.is_confirmed: #flash(_('The email has been confirmed. Please login.')) msg = _('The email has been confirmed. Please login.') #return redirect('auth.login') return get_json(1, msg, {}) user.is_confirmed = True user.save() #flash('You have confirmed your account. Thanks!') msg = _('You have confirmed your account. Thanks!') return get_json(1, msg, {})
def get(self, topicId, thumb): topic = Topic.query.filter_by(id=topicId).first() if thumb == 'up': topic.is_good = int(topic.is_good) + 1 thumb = topic.is_good if thumb == 'down': topic.is_bad = int(topic.is_bad) + 1 thumb = topic.is_bad topic.save() return get_json(1, '成功', thumb)
def post(self): file_dict = request.files.to_dict() for filename in file_dict: Files = file_dict[filename] if Files and check_file(Files.filename): filename = secure_filename(Files.filename) n_filename = filename.rsplit('.', 1)[0] fix = filename.rsplit('.', 1)[1] newfilename = "%s%s%s%s%s%s"%(n_filename, '_', str(int(time())), str( randint(1000, 9999)), '.', fix) file_path = current_app.config['PICTURE_FOLDER'] file = os.path.join(file_path, newfilename) if not os.path.exists(file_path): os.makedirs(file_path) files = File( front_file = filename, file_path = file_path + '/' + newfilename) files.save() files.file_path = current_app.config['SERVER_URL'] + '/' + file_path + '/' + newfilename Files.save(file) else: return get_json(0, '格式错误', {}) return get_json(1, '上传成功', object_as_dict(files))
def get(self, id): bar_replies = Answers.query.filter_by(questions_id = id, is_reply = 1).order_by('-id').all() replies = [] for i in bar_replies: user = User.query.filter_by(id = i.author_id).first() diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) replies_data = object_as_dict(i) replies_data['author'] = user.username replies_data['diff_time'] = diff_time Avatar(replies_data, user) replies.append(replies_data) return get_json(1, '评论信息', replies)
def get(self, topicId, page): start = (page - 1) * 5 reply = Reply.query.filter_by(topic_id=topicId, is_reply=1).order_by( ('-id')).limit(5).offset(start) replies = [] for i in reply: user = User.query.filter_by(id=i.author_id).first() diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) replies_data = object_as_dict(i) replies_data['author'] = user.username replies_data['diff_time'] = diff_time Avatar(replies_data, user) replies.append(replies_data) return get_json(1, '评论信息', replies)
def post(self, id): #Comment = Answers.query.filter_by(id = id).first_or_404() post_data = request.data #user = request.user content = post_data.pop('content', None) answer_comment = Comments(content = content, answers_id = id) answer_comment.author_id = 7 answer_comment.save() diff_time = time_diff(answer_comment.updated_at) answer_user = User.query.filter_by(id = answer_comment.author_id).first() answer_comment.created_at = str(answer_comment.created_at) answer_comment.updated_at = str(answer_comment.updated_at) comments_data = object_as_dict(answer_comment) comments_data['author'] = answer_user.username comments_data['diff_time'] = diff_time Avatar(comments_data, answer_user) return get_json(1, '评论成功', comments_data)
def post(self, id): qusetion = Questions.query.filter_by(id = id).first_or_404() post_data = request.data #user = request.user content = post_data.pop('content', None) bar_answer = Answers(content = content, questions_id = id, is_reply = 0) bar_answer.author_id = 6 bar_answer.save() diff_time = time_diff(bar_answer.updated_at) answer_user = User.query.filter_by(id = bar_answer.author_id).first() bar_answer.created_at = str(bar_answer.created_at) bar_answer.updated_at = str(bar_answer.updated_at) answers_data = object_as_dict(bar_answer) answers_data['author'] = answer_user.username answers_data['diff_time'] = diff_time Avatar(answers_data, answer_user) return get_json(1, '回答成功', answers_data)
def post(self, id): qusetion = Questions.query.filter_by(id=id).first_or_404() post_data = request.data #user = request.user content = post_data.pop('content', None) bar_reply = Answers(content = content, questions_id = qusetion.id, is_reply = 1) bar_reply.author_id = 5 bar_reply.save() diff_time = time_diff(bar_reply.updated_at) reply_user = User.query.filter_by(id = bar_reply.author_id).first() bar_reply.created_at = str(bar_reply.created_at) bar_reply.updated_at = str(bar_reply.updated_at) replies_data = object_as_dict(bar_reply) replies_data['author'] = reply_user.username replies_data['diff_time'] = diff_time Avatar(replies_data, reply_user) return get_json(1, '评论成功', object_as_dict(bar_reply))
def post(self, id): comment = Comments.query.filter_by(id=id).first_or_404() post_data = request.data #user = request.user content = post_data.pop('content', None) comment_reply = Replys(content = content, comments_id = id) comment_reply.author_id = 5 comment_reply.save() diff_time = time_diff(comment_reply.updated_at) reply_user = User.query.filter_by(id = comment_reply.author_id).first() comment_reply.created_at = str(comment_reply.created_at) comment_reply.updated_at = str(comment_reply.updated_at) replies_data = object_as_dict(comment_reply) replies_data['author'] = reply_user.username replies_data['diff_time'] = diff_time Avatar(replies_data, reply_user) data = {'comment_content':comment.content,'replies':replies_data} return get_json(1, '评论成功', data)
def post(self, topicId): topic = Topic.query.filter_by(id=topicId).first_or_404() post_data = request.data user = request.user print(user, 88888888888888888888888888888888888888888888888888888) content = post_data.pop('content', None) reply = Reply(content=content, topic_id=topic.id, is_reply=1) #user = User.query.filter_by(id=1).first() reply.author_id = user.id reply.save() replies_data = object_as_dict(reply) replies_data['author'] = user.username # notice #MessageClient.topic(reply) # count #topic.board.post_count = 1 #reply.author.reply_count = 1 return get_json(1, '评论成功', replies_data)
def get(self, topicId, page): #form = ReplyForm() start = (page - 1) * 5 query_dict = request.data topic = Topic.query.filter_by(id=topicId).first_or_404() #page, number = self.page_info keys = ['title'] order_by = gen_order_by(query_dict, keys) filter_dict = gen_filter_dict(query_dict, keys) reply = topic.replies.filter_by(topic_id=topicId, is_reply=1).order_by( ('-id')).limit(5).offset(start) reply_count = topic.replies.filter_by(topic_id=topicId, is_reply=1).count() page_count = int(math.ceil(reply_count / 5)) replies = [] diff_time = time_diff(topic.updated_at) topic.created_at = str(topic.created_at) topic.updated_at = str(topic.updated_at) for i in reply: user = User.query.filter_by(id=i.author_id).first() diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) replies_data = object_as_dict(i) replies_data['author'] = user.username replies_data['diff_time'] = diff_time Avatar(replies_data, user) replies.append(replies_data) #topic.read_count = 1 topic_data = object_as_dict(topic) topic_user = User.query.filter_by(id=topic_data['author_id']).first() topic_data['author'] = topic_user.username topic_data['diff_time'] = diff_time Avatar(topic_data, topic_user) data = { #'title': topic['title'], #'form': object_as_dict(form), 'topic': topic_data, 'replies': replies, 'replies_count': reply_count, 'page_count': page_count } #topic.read_count = 1 return get_json(1, '文章详情', data)
def get(self): barlists = Bar.query.all() bar_questions = Questions.query.filter_by(bar_id = 1, is_bar=1).first() user = User.query.filter_by(id = bar_questions.author_id).first() bar = Bar.query.filter_by(id = bar_questions.bar_id).first() question = object_as_dict(bar_questions) Avatar(question, user) question['author'] = user.username question['bar'] = bar.title barlist = [] for i in barlists: user = User.query.filter_by(id = i.author_id).first() picture = current_app.config['SERVER_URL'] + '/' + i.picture bar_data = object_as_dict(i) bar_data['author'] = user.username bar_data['picture'] = picture barlist.append(bar_data) data = {'barlist': barlist, 'barquestion': question} return get_json(1, 'bar列表', data)
def get(self, id): bar_questions = Questions.query.filter_by(bar_id = id, is_bar=1).all() question_list = [] for i in bar_questions: user = User.query.filter_by(id = i.author_id).first() try: bar_reply = Answers.query.filter_by(bar_topic_id = i.id).count() except: bar_reply = 0 diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) questions_data = object_as_dict(i) questions_data['author'] = user.username questions_data['diff_time'] = diff_time questions_data['replies_count'] = bar_reply Avatar(questions_data, user) question_list.append(questions_data) return get_json(1, 'bar问题列表', question_list)
def post(self): user = request.user print(user, 88888888888888888888888888888888888888888888888888888) form = TopicForm() post_data = form.data title = post_data.pop('title', None) content = post_data.pop('content', None) #tags = post_data.pop('tags', None) content_type = post_data.pop('content_type', 0) token = post_data.pop('token', None) #board = post_data.pop('category', None) topic = Topic(title=title, content=content, content_type=content_type, token=token) #board_id=int(board)) #tags = tags.split(',') #topic_tags = [] #for tag in tags: # tag = tag.strip() # topic_tag = Tags.query.filter_by(name=tag).first() # if topic_tag is None: # topic_tag = Tags(name=tag, description=tag) # topic_tag.save() # topic_tags.append(topic_tag) #topic.tags = topic_tags #user = User.query.filter_by(id=1).first() topic.author = user topic.save() topic = object_as_dict(topic) Avatar(topic, user) topic['author'] = user.username #topic.board.topic_count = 1 #topic.board.post_count = 1 #topic.author.topic_count = 1 #topic.reply_count = 1 return get_json(1, '发表成功', topic)
def get(self, page): start = (page - 1) * 5 query_dict = request.data keys = ['title'] order_by = gen_topic_orderby(query_dict, keys) filter_dict = gen_topic_filter(query_dict, keys) title = _('All Topics') if request.path.endswith('good'): filter_dict.update(is_good=True) title = _('Good Topics') elif request.path.endswith('top'): filter_dict.update(is_bad=True) title = _('bad Topics') topics = Topic.query.filter_by(**filter_dict).order_by( *order_by).limit(5).offset(start) topic_count = FindAndCount(Topic) page_count = int(math.ceil(topic_count / 5)) topic = [] for i in topics: user = User.query.filter_by(id=i.author_id).first() reply_count = FindAndCount(Reply, topic_id=i.id, is_reply=1) diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) topics_data = object_as_dict(i) topics_data['author'] = user.username topics_data['diff_time'] = diff_time topics_data['replies_count'] = reply_count Avatar(topics_data, user) topic.append(topics_data) data = { 'classification': title, 'topics': topic, 'topic_count': topic_count, 'page_count': page_count } return get_json(1, '文章列表', data)
def get(self, id): bar_question = Questions.query.filter_by(id = id).first() bar_answers = Answers.query.filter_by(questions_id = id, is_reply = 0).order_by('-id').all() question_user = User.query.filter_by(id = bar_question.author_id).first() diff_time = time_diff(bar_question.updated_at) bar_question.created_at = str(bar_question.created_at) bar_question.updated_at = str(bar_question.updated_at) questions_data = object_as_dict(bar_question) questions_data['diff_time'] = diff_time questions_data['author'] = question_user.username Avatar(questions_data, question_user) answers_list = [] for i in bar_answers: answer_user = User.query.filter_by(id = i.author_id).first() #comment = Comments.query.filter_by(answers_id = i.id, is_reply = 0).all() diff_time = time_diff(i.updated_at) i.created_at = str(i.created_at) i.updated_at = str(i.updated_at) answers_data = object_as_dict(i) answers_data['author'] = answer_user.username answers_data['diff_time'] = diff_time #comment_list = [] #for j in comment: # diff_time = time_diff(j.updated_at) # comment_user = User.query.filter_by(id = j.author_id).first() # j.created_at = str(j.created_at) # j.updated_at = str(j.updated_at) # comment_data = object_as_dict(j) # comment_data['author'] = comment_user.username # comment_data['diff_time'] = diff_time # comment_list.append(comment_data) #answers_data['comments'] = comment_list Avatar(answers_data, answer_user) answers_list.append(answers_data) data = {'question':questions_data, 'answers':answers_list} return get_json(1, 'bar问题详情页', data)
def get(self, username): query_dict = request.data user = User.query.filter_by(username=username).first_or_404() #page, number = self.page_info keys = ['title'] order_by = gen_order_by(query_dict, keys) filter_dict = gen_filter_dict(query_dict, keys) filter_dict.update(author_id=user.id) topics = Topic.query.filter_by( **filter_dict).order_by(*order_by).all()#.paginate(page, number, True) setting = user.setting #topic_is_allowed = False #if setting.topic_list == 1 or (setting.topic_list == 2 and # current_user.is_authenticated): # topic_is_allowed = True #if current_user.is_authenticated and current_user.id == user.id: # topic_is_allowed = True print(topics,1111111111111111111111,user) data = { 'topics': [object_as_dict(i) for i in topics], 'user': object_as_dict(user) # 'topic_is_allowed': topic_is_allowed } return get_json(1, '个人中心', data)
def get(self, token): ori=requests.get('https://data.block.cc/api/v1/price?symbol=' + token) return get_json(1, token + '的价格', ori.json()['data'][0])
def get(self): domain.as_default() #return render_template('auth/login.html') return get_json(1, '登录', {})
def get(self): print() domain.as_default() #return render_template('auth/forget.html') return get_json(1, '忘记密码', {})
def get(self): domain.as_default() #return render_template('auth/register.html') return get_json(1, '注册', {})
def get(self): current_user.logout() #return redirect(request.args.get('next') or '/') return get_json(1, '登出成功', {})