示例#1
0
 def upload_image(self, file, username):
     """
     上传文件,保存在static/upload_image/username文件夹下,
     首先判断是否支持上传的文件类型
     首先判断文件夹是否存在,如果没有该文件夹会自动创建。
     为了防止文件重名以及安全性问题,上传的文件统一以系统时间命名
     :param file: 文件流
     :param username: 用户名
     :return: json
     """
     try:
         if file and self.allowed_file(file.filename):
             if not os.path.isdir(current_app.config['UPLOAD_FOLDER']):
                 os.mkdir(current_app.config['UPLOAD_FOLDER'])
             path = os.path.join(current_app.config['UPLOAD_FOLDER'],
                                 username)
             if os.path.isdir(path):
                 image_path = self.save_image2(path, file, username)
             else:
                 os.mkdir(path)
                 image_path = self.save_image2(path, file, username)
             return PublicMethod.true_return(data=image_path, msg='上传图片成功')
         else:
             return PublicMethod.false_return(data='error', msg='该类型文件无法上传')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='error', msg='图片上传失败,请联系管理员')
示例#2
0
文件: views.py 项目: wbin327/MyBlog
def edit_blog(blog_id):
    from app.util.public_method import PublicMethod
    try:
        form = ArticleForm()
        article = service.get_article(blog_id)
        if article:
            if not form.title.data and not form.introduce.data:
                form.title.data = article.title
                form.tags.data = article.tags
                form.introduce.data = article.introduce
                form.type.data = article.type
                form.content.data = article.content
                return render_template('/blog/edit_blog.html', form=form, json='', blog_id=article.id)

            if form.validate_on_submit() and request.method == 'POST':
                user = current_user
                json = service.update_article(request.form, user, blog_id)
                if json['status']:
                    return render_template('/blog/edit_blog.html', form=form, json=json, blog_id=article.id)
                else:
                    return render_template('/blog/edit_blog.html', form=form, json=json, blog_id=article.id)
            else:

                return render_template('/blog/edit_blog.html', form=form,
                                       json=PublicMethod.false_return(data='', msg=u'数据检验没通过,请检查数据格式'), blog_id=article.id)
        else:
            return render_template('/blog/edit_blog.html', form=form,
                                       json=PublicMethod.false_return(data='', msg=u'该博客不存在'), blog_id=blog_id)
    except Exception:
        traceback.print_exc()
        return redirect('/blog/error')
示例#3
0
 def download_content(self, blog_id):
     try:
         article = self.dao.get_article_by_id(blog_id)
         if article:
             return PublicMethod.true_return(data=article.content,
                                             msg='获取博客内容成功!')
         else:
             return PublicMethod.false_return(data='',
                                              msg='articl对象为空,具体原因请查看后台日志')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='', msg='后台抛出异常,请查看后台日志')
示例#4
0
文件: service.py 项目: wbin327/MyBlog
 def register(self, username, password):
     user = self.dao.query_user_by_name(username)
     if user:
         result = PublicMethod.false_return(data='', msg='用户名已存在')
     else:
         hash_password = generate_password_hash(password)
         excute = self.dao.add_user(username, hash_password)
         if excute is True:
             result = PublicMethod.true_return(data='', msg='注册成功')
         else:
             result = PublicMethod.false_return(data='', msg='注册失败')
     return result
示例#5
0
文件: service.py 项目: wbin327/MyBlog
 def get_son_list(self, request):
     try:
         father_id = request.values.get('father_id')
         if father_id:
             category_list = self.dao.get_son_by_father(father_id)
             son_list = []
             func = lambda id, name: {'id': id, 'name': name}
             for category in category_list:
                 son_list.append(func(category.id, category.name))
             return jsonify(PublicMethod.true_return(data=son_list, msg='请求成功'))
         else:
             return jsonify(PublicMethod.false_return(data='', msg='请求参数有误,请求失败'))
     except Exception:
         traceback.print_exc()
         return jsonify(PublicMethod.false_return(data='', msg='后台报错,请查看日志'))
示例#6
0
文件: service.py 项目: wbin327/MyBlog
 def get_blog_list(self, request):
     try:
         son_id = request.values.get('son_id')
         if son_id:
             category = self.dao.get_category_by_id(son_id)
             blog_list = []
             func = lambda id, title: {'id': id, 'title': title}
             for article in category.article:
                 blog_list.append(func(article.id, article.title))
             return jsonify(PublicMethod.true_return(data=blog_list, msg='请求成功'))
         else:
             return jsonify(PublicMethod.false_return(data='', msg='请求的参数有误,请求失败'))
     except Exception:
         traceback.print_exc()
         return jsonify(PublicMethod.false_return(data='', msg='后台报错,请查看日志'))
示例#7
0
文件: service.py 项目: wbin327/MyBlog
 def get_father_by_user(self):
     try:
         father_list = self.dao.get_blog_header()
         return father_list
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='', msg='后台抛出异常,请查看日志')
示例#8
0
文件: service.py 项目: wbin327/MyBlog
 def get_user(self, authorization):
     result = identify(authorization)
     if not isinstance(result, str):
         user = result
         return PublicMethod.true_return(data=user.serialize(), msg='success')
     else:
         return PublicMethod.false_return(data='', msg=result)
示例#9
0
 def add_sidebar(self, category_name, father_id, father_name):
     try:
         category_obj = Category(category_name, father_id, father_name)
         self.dao.add_category(category_obj)
         return PublicMethod.true_return(data='新增左侧边栏成功', msg='success')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='新增左侧边栏失败', msg='error')
示例#10
0
 def add_blog_header(self, name):
     try:
         header_obj = Category(name=name, father_id=0, father_name=0)
         self.dao.add_category(header_obj)
         return PublicMethod.true_return(data='新增成功', msg='success')
     except Exception as e:
         traceback.print_exc()
         return PublicMethod.false_return(data='新增异常,请修复', msg='error')
示例#11
0
 def delete_blog_header(self, header_id):
     try:
         blog_header = self.dao.getCategoryById(header_id)
         self.dao.delete_category(blog_header)
         return PublicMethod.true_return(data='删除成功', msg='success')
     except Exception as e:
         traceback.print_exc()
         return PublicMethod.false_return(data='删除异常,请修复', msg='error')
示例#12
0
 def delete_sidebar(self, id):
     try:
         category_obj = self.dao.getCategoryById(id)
         self.dao.delete_category(category_obj)
         return PublicMethod.true_return(data='删除左侧边栏成功', msg='success')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='删除左侧边栏失败', msg='error')
示例#13
0
文件: service.py 项目: wbin327/MyBlog
 def add_user(self, request):
     try:
         username = request.values.get('user_name')
         password = request.values.get('user_password')
         self.dao.add_user(username=username, password=password)
         return PublicMethod.true_return(data='', msg='添加用户成功!')
     except Exception:
         return PublicMethod.false_return(data='', msg='后台抛出异常,请查看日志')
示例#14
0
文件: service.py 项目: wbin327/MyBlog
 def delete_user(self, request):
     try:
         user = self.dao.get_user_by_id(request.values.get('user_id'))
         self.dao.delete_user(user)
         return PublicMethod.true_return(data='', msg='删除用户成功!')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='', msg='后台抛出异常,请查看日志')
示例#15
0
 def update_blog_header(self, id, name):
     try:
         header_obj = self.dao.getCategoryById(id)
         header_obj.name = name
         self.dao.update_category()
         return PublicMethod.true_return(data='更新成功', msg='success')
     except Exception as e:
         traceback.print_exc()
         return PublicMethod.false_return(data='更新异常,请修复', msg='error')
示例#16
0
 def get_user(self, authorization):
     result = JsonWebToken.identify(authorization)
     if isinstance(result, str):
         return PublicMethod.false_return(data='', msg=result)
     else:
         id = result['data']['id']
         user = self.dao.get_user_by_id(id)
         return PublicMethod.true_return(data=user.serialize(),
                                         msg='success')
示例#17
0
文件: service.py 项目: wbin327/MyBlog
    def logout(self, authorization):
        result = identify
        if not isinstance(result, str):
            user = result
            user.login_time = 0
            self.dao.add(user)

            return PublicMethod.true_return(data=user.serialize(), msg='success')
        else:
            return PublicMethod.false_return(data='', msg=result)
示例#18
0
文件: service.py 项目: wbin327/MyBlog
    def login_check(self, name, password):
        """
            token验证,登录成功返回token,并将登录时间写入数据库;登录失败则返回失败原因
            :param username: 用户名
            :param password: 密码
            :return: dict
            """
        user = self.dao.query_user_by_name(name)

        if user:
            result = check_password_hash(user.password, password)
            if result:
                user.login_time = time.time()
                self.dao.add(user)
                token = encode_auth_token(user_id=user.id, login_time=user.login_time)
                return PublicMethod.true_return(data=token, msg='登录成功')
            else:
                return PublicMethod.false_return(data='', msg='密码错误')
        else:
            return PublicMethod.false_return(data='', msg='用户名不存在')
示例#19
0
 def update_sidebar(self, id, name, father_id, father_name):
     try:
         category_obj = self.dao.getCategoryById(id)
         category_obj.name = name
         category_obj.father_id = father_id
         category_obj.father_name = father_name
         self.dao.update_category()
         return PublicMethod.true_return(data='编辑左侧边栏成功', msg='success')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='编辑左侧边栏失败', msg='error')
示例#20
0
 def add_article(self, form, user):
     try:
         article = Article(form=form)
         article.user_id = user.id
         article.read_count = 0
         category = self.dao.getCategoryById(form['category'])
         article.category = [category]
         self.dao.add_article(article)
         return PublicMethod.true_return(data='', msg=u'发布成功')
     except Exception, e:
         traceback.print_exc()
         return PublicMethod.false_return(data='', msg=u'后台报错,请联系管理员')
示例#21
0
 def search_blog(self, request):
     try:
         condition = request.values.get('condition')
         # 清除html标签,防止用户恶意输入
         bleach.clean(condition)
         article_list = self.dao.search_blog(condition)
         article_list = [article.serialize() for article in article_list]
         return jsonify(
             PublicMethod.true_return(data=article_list, msg='请求成功!'))
     except Exception:
         traceback.print_exc()
         return jsonify(
             PublicMethod.false_return(data='', msg='请求失败,具体原因请查看后台日志文件!'))
示例#22
0
文件: service.py 项目: wbin327/MyBlog
 def update_user(self, request):
     try:
         user_id = request.values.get('user_id')
         user_name = request.values.get('user_name')
         user_password = request.values.get('user_password')
         user_obj = self.dao.get_user_by_id(user_id)
         user_obj.username = user_name
         user_obj.password = user_obj.password_to_hash(user_password)
         self.dao.update_user(user_obj)
         return PublicMethod.true_return(data='', msg='更新用户成功!')
     except Exception:
         traceback.print_exc()
         return PublicMethod.false_return(data='', msg='后台抛出异常,请查看日志')
示例#23
0
文件: service.py 项目: wbin327/MyBlog
    def background_login_check(self, name, password,  remember=False):
        """
        后台登录认证,使用了flask_login插件
        :param name: 用户名
        :param password: 密码
        :param session: 会话
        :param remember:保持登录,boolean
        :return: dict
        """

        user = self.dao.query_user_by_name(name)

        if user:
            result = check_password_hash(user.password, password)
            if result:
                user.login_time = time.time()
                self.dao.add(user)
                login_user(user, remember=remember)
                return PublicMethod.true_return(data='', msg='login_success')
            else:
                return PublicMethod.false_return(data='', msg='password_error')
        else:
            return PublicMethod.false_return(data='', msg='none_user')
示例#24
0
文件: service.py 项目: wbin327/MyBlog
 def delete_blog(self, request):
     try:
         blog_id = request.values.get('blog_id')
         # 要将字符型转整型,否则sql查询结果为空
         # blog_id = int(filter(str.isdigit, blog_id.encode("utf-8")))
         if blog_id:
             article = self.dao.get_article_by_id(blog_id)
             excute = self.dao.delete_article(article)
             if excute:
                 return jsonify(PublicMethod.true_return(data='', msg='删除成功'))
             else:
                 return jsonify(PublicMethod.flase_return(data='', msg='删除失败'))
     except Exception:
         traceback.print_exc()
         return jsonify(PublicMethod.false_return(data='', msg='后台报错,请查看日志'))
示例#25
0
 def update_article(self, form, user, blog_id):
     try:
         old_article = self.dao.get_article_by_id(blog_id)
         old_article.user_id = user.id
         category = self.dao.getCategoryById(form['category'])
         old_article.category = [category]
         old_article.title = form['title']
         old_article.content = form['content']
         old_article.type = form['type']
         old_article.tags = form['tags']
         old_article.introduce = form['introduce']
         old_article.update_time = datetime.now()
         if str(form['type']) == str(1):
             old_article.type = '原创'
         elif str(form['type']) == str(2):
             old_article.type = '转载'
         else:
             old_article.type = '翻译'
         self.dao.session_commit()
         return PublicMethod.true_return(data='', msg=u'博客更新成功')
     except Exception, e:
         traceback.print_exc()
         return PublicMethod.false_return(data='', msg=u'后台报错,请联系管理员')