Example #1
0
def current_user(request):
    account = request.META.get('HTTP_X_AC')
    rid = request.META.get('HTTP_X_RID')

    try:
        if account:
            account = base64.b64decode(account)
            account = rc4.rc4(account, GAME_KEY)
            user = User.objects.filter(account=account).first()
        elif rid:
            rid = base64.b64decode(rid)
            rid = rc4.rc4(rid, GAME_KEY)
            user = User.objects.filter(rid=rid).first()
        else:
            return jsonify(data=None,
                           success=False,
                           errMsg='Lack of user info.')
    except:
        return jsonify(data=None,
                       success=False,
                       errMsg='User info decode error.')

    if not user:
        return jsonify(data=None, success=False, errMsg="User is not logined.")
    return user
Example #2
0
def fileUpload(request):
    """
    文件上传
    """
    upload_file = request.FILES.get("file")
    path = request.POST.get('path')
    if not upload_file:
        return jsonify(data=None, success=False, errMsg='未找到上传文件')
    if not path or not os.path.exists(path):
        return jsonify(data=None, success=False, errMsg='未找到文件夹')

    new_file_path = path + '/' + upload_file.name
    try:
        #如果新文件存在则删除
        if os.path.exists(path + '/' + upload_file.name):
            try:
                os.remove(path + '/' + upload_file.name)
            except:
                pass
        content = upload_file.read()
        fp = open(new_file_path, 'wb')
        fp.write(content)
        fp.close()

        return jsonify(data=new_file_path, success=True, errMsg='')
    except:
        import traceback
        traceback.print_exc()
        return jsonify(data=None, success=False, errMsg='Upload file failed.')
Example #3
0
def adminLogin(request):
    username = request.POST.get('username')
    password = request.POST.get('password')
    user_obj = authenticate(username=username, password=password)
    if user_obj:
        auth_login(request, user_obj)
        return jsonify(data=None, success=True, errMsg='操作成功')
    else:
        return jsonify(data=None, success=False, errMsg='登录失败')
Example #4
0
def userInfo(req):
    id = req.session.get('user_id', default=None)
    if not id:
        return jsonify(data=None,
                       code='nologin',
                       success=False,
                       errMsg='缺少请求参数.')

    user = User.objects.get(id=id)
    userInfo = user.to_dict_detail()
    return jsonify(data=userInfo, success=True, errMsg='操作成功')
Example #5
0
        def wrapper(*args, **kwargs):
            request = args[0]
            if request.method == 'POST':
                if request.content_type != content_type:
                    return jsonify(data=None, success=False, errMsg='Content-type error.')

                account = request.META.get('HTTP_X_AC')
                rid = request.META.get('HTTP_X_RID')
                if not account and not rid:
                    return jsonify(data=None, success=False, errMsg='Lack of user info in request headers.')

            return func(*args, **kwargs)
Example #6
0
def platformList(req):
    """
    获取全部平台列表
    """
    try:
        platforms = Platform.objects.all()
        json_list = []
        for platform in platforms:
            json_dict = {}
            json_dict['key'] = platform.key
            json_dict['name'] = platform.name
            json_list.append(json_dict)
        return jsonify(data=json_list, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='获取平台信息失败')
Example #7
0
def fetchTopArticle(req):
    """
    获取置顶文章列表
    """
    top_articles = Article.objects.filter(
        is_top=True, status='published').order_by('-create_dt')
    data = [e.to_dict() for e in top_articles]
    return jsonify(data=data, success=True, errMsg='操作成功')

    try:
        top_articles = Article.objects.filter(is_top=True)
        data = [e.to_dict() for e in top_articles]
        return jsonify(data=data, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='获取置顶文章失败')
Example #8
0
def updateInfo(req):
    nickname = req.POST.get('nickname')
    email = req.POST.get('email')
    try:
        user_id = req.session.get('user_id')
        email_exit = User.objects.exclude(id=user_id).filter(email=email)
        if email_exit.exists():
            return jsonify(data=None, success=False, errMsg='用户邮箱已存在')
        user = User.objects.get(id=user_id)
        user.nickname = nickname
        user.email = email
        user.save()
        return jsonify(data=None, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='更新失败')
Example #9
0
def updateIsTop(req):
    """
    修改文章置顶
    """
    articleId = req.POST.get('id')
    isTop = req.POST.get('isTop')
    if not articleId or not isinstance(isTop, bool):
        return jsonify(data=None, success=False, errMsg='param error.')
    try:
        article_obj = Article.objects.get(id=articleId)
        article_obj.is_top = isTop
        article_obj.save()
        return jsonify(data=None, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='置顶操作失败')
Example #10
0
def articleDetail(req):
    """
    查看文章详情
    """
    articleId = req.GET.get('id')
    if not articleId:
        return jsonify(data=None, success=False, errMsg='param error.')
    try:
        article_obj = Article.objects.get(id=articleId)
        article_obj.reads_count += 1
        article_obj.save()
        return jsonify(data=article_obj.to_detail(),
                       success=True,
                       errMsg='操作成功')
    except Exception as e:
        return jsonify(data=None, success=False, errMsg=str(e))
Example #11
0
def login(req):
    account = req.POST.get('account')
    password = req.POST.get('password')
    if not account or not password:
        return jsonify(data=None, success=False, errMsg='缺少请求参数.')

    user = User.objects.get(account=account)
    user_password = user.get_password()
    userInfo = user.to_dict_detail()
    if user is None:
        return jsonify(data=None, success=False, errMsg='用户名不存在!')
    elif password != user_password:
        return jsonify(data=None, success=False, errMsg='密码错误')
    else:
        req.session['user_id'] = userInfo['id']
        return jsonify(data=userInfo, success=True, errMsg='操作成功')
Example #12
0
def removeFile(req):
    """
    删除文件
    """
    pathName = req.POST.get('path')
    cover_url = pathName.replace(FILE_SAVE_PATH, OSS_URL, 1)
    is_link_article = Article.objects.filter(cover_url=cover_url)
    print is_link_article
    if is_link_article:
        return jsonify(data=None, success=False, errMsg='文件正在被文章使用,无法删除!')

    if (os.path.exists(pathName)):
        os.remove(pathName)
        return jsonify(data=None, success=True, errMsg='')
    else:
        return jsonify(data=None, success=False, errMsg='文件不存在!')
Example #13
0
def list(req):
    """
    获取文章列表
    """
    page = int(req.GET.get('page', 1))
    limit = int(req.GET.get('limit', 10))
    try:
        article_list = Article.objects.filter(
            status='published').order_by('-create_dt')[(page - 1) *
                                                       limit:page * limit]
        data = {}
        data['list'] = [e.to_list() for e in article_list]
        data['total'] = Article.objects.filter(status='published').count()
        return jsonify(data=data, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='查询失败')
Example #14
0
def imageAll(req):
    """
    获取所有图片
    """
    pathName = req.POST.get('path', OSS_PATH)
    files = list()
    data = DirAll(pathName, files)
    return jsonify(data=data, success=True, errMsg='')
Example #15
0
def imageUpload(request):
    """图片上传"""
    image = request.FILES.get("file")
    param = request.POST.get('param', 'common')
    if not image:
        return jsonify(data=None, success=False, errMsg='未找到图片文件')
    if param not in ['avatar', 'cover', 'content', 'common']:
        return jsonify(data=None, success=False, errMsg='param error.')

    try:
        service_name = save_block_file(image, param)
        print 'service_name: %s' % service_name
        return jsonify(data=service_name, success=True, errMsg='')
    except:
        import traceback
        traceback.print_exc()
        return jsonify(data=None, success=False, errMsg='Upload image failed.')
Example #16
0
def mkdir(req):
    """
    新建目录
    """
    path = req.POST.get('path')
    dirName = req.POST.get('dirName')
    print path
    print dirName
    print FILE_SAVE_PATH
    if not path or not dirName:
        return jsonify(data=None, success=False, errMsg='param error.')

    elif not os.path.exists(path):
        return jsonify(data=None, success=False, errMsg='path not exist.')
    try:
        os.makedirs(path + '/' + dirName)
        return jsonify(data=None, success=True, errMsg='')
    except Exception as e:
        return jsonify(data=None, success=False, errMsg=e)
Example #17
0
def allList(req):
    """
    获取全部文章列表
    """
    status = req.GET.get('status', None)
    page = int(req.GET.get('page', 1))
    limit = int(req.GET.get('limit', 10))
    try:
        if not status:
            article_list = Article.objects.all().order_by(
                '-create_dt')[(page - 1) * limit:page * limit]
        else:
            article_list = Article.objects.filter(
                status=status).order_by('-create_dt')[(page - 1) * limit:page *
                                                      limit]
        data = {}
        data['list'] = [e.to_dict() for e in article_list]
        data['total'] = Article.objects.all().count()
        return jsonify(data=data, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='查询失败')
Example #18
0
def update(req):
    """ 
    编辑文章
    """
    article_id = req.POST.get('id')
    title = req.POST.get('title')
    summary = req.POST.get('summary')
    content = req.POST.get('content')
    cover_url = req.POST.get('cover_url')
    status = req.POST.get('status')
    if not article_id:
        return jsonify(data=None, success=False, errMsg='param error.')
    try:
        article_obj = Article.objects.get(id=article_id)
        article_obj.title = title
        article_obj.summary = summary
        article_obj.content = content
        article_obj.cover_url = cover_url
        article_obj.status = status
        article_obj.save()
        return jsonify(data=None, success=True, errMsg='文章修改成功')
    except:
        return jsonify(data=None, success=False, errMsg='文章修改失败')
Example #19
0
def create(req):
    """
    新建文章
    """
    title = req.POST.get('title')
    summary = req.POST.get('summary')
    content = req.POST.get('content')
    cover_url = req.POST.get('cover_url')
    status = req.POST.get('status')
    platforms = req.POST.get('platforms')

    try:
        user = User.objects.get(id=req.session.get('user_id'))
        article_obj = Article.objects.create(title=title,
                                             summary=summary,
                                             content=content,
                                             cover_url=cover_url,
                                             user=user)
        for platform in platforms:
            platform_obj = Platform.objects.filter(key=platform)
            article_obj.platform.add(*platform_obj)
        return jsonify(data=None, success=True, errMsg='操作成功')
    except:
        return jsonify(data=None, success=False, errMsg='创建文章失败')
Example #20
0
def logout(req):
    try:
        req.session.flush()
    except KeyError:
        pass
    return jsonify(data=None, success=True, errMsg='操作成功')