示例#1
0
    def post(self, request, format=None):
        try:
            ser = SubmitAricleSerializer(data=request.data)
            if ser._valid():
                title = ser.data.get('title')
                cover = request.FILES['cover']
                content = ser.data.get('content')
                category_id = ser.data.get('category_id')
                author_id = ser.data.get('author_id')
                promote = ser.data.get('promote')
            else:
                return Response({'status':'Bad request ...', status=status.HTTP_200_OK})
        
            user = User.objects.get(id=author_id)
            author = UserProfile.objects.get(user=user)
            category = Category.objects.get(id=category_id)
            
            article = Article()
            article.title = title
            article.cover = cover
            article.content = content
            article.category = category 
            article.author = author
            article.promote = promote
            article.save()

            return Response('status':'OK', status = status.HTTP_200_OK)

        except:
            return Response({'status': 'server error .....'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
示例#2
0
def add_article(request):
    title = request.POST['title']
    describe = request.POST['describe']
    cover = request.POST['cover']
    content = request.POST['content']
    token = request.POST['token']
    user_token = Token.objects.filter(key=token)
    if len(user_token) == 0:
        return Response('nologin')
    if len(title) == 0:
        return Response('notitle')
    new_article = Article(title=title)
    new_article.save()
    soup = BeautifulSoup(content, 'html.parser')  # 解析富文本html文档
    imgList = soup.find_all('img')
    for img in range(0, len(imgList)):
        src = imgList[img]['src']
        if 'http://' in src or 'https://' in src:
            image = requests.get(src)
            image_data = Image.open(BytesIO(image.content))
            image_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '-' + str(new_article.id) + '-' + str(img)
            image_data.save('upload/' + image_name + '.png')
            new_src = hostUrl + 'upload/' + image_name + ".png"
            content = content.replace(src, new_src)
            if cover == src:
                cover = new_src
        else:
            image_data = base64.b64decode(src.split(',')[1])
            image_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '-' + str(new_article.id) + '-' \
                         + str(img) + '.' + src.split(',')[0].split('/')[1].split(';')[0]
            image_url = os.path.join('upload', image_name).replace('\\', '/')
            with open(image_url, 'wb') as f:
                f.write(image_data)
            new_src = hostUrl + image_url
            content = content.replace(src, new_src)
            if cover == src:
                cover = new_src
    new_article.content = content
    new_article.belong = user_token[0].user
    new_article.cover = cover
    new_article.describe = describe

    new_article.save()
    return Response("ok")
示例#3
0
def add_article(request):
    token = request.POST['token']
    if request.method == "PUT":
        permList = ['blog.change_article']
        checkUser = userLoginAndPerm(token, permList)
        print(checkUser)
        if checkUser != 'perm_pass':
            return Response(checkUser)

        lanmu_id = request.POST['lanmu_id']
        article_id = request.POST['article_id']

        lanmu = Lanmu.objects.get(id=lanmu_id)
        article = Article.objects.get(id=article_id)
        article.belong_lanmu = lanmu
        article.save()
        return Response('ok')

    title = request.POST['title']
    describe = request.POST['describe']
    cover = request.POST['cover']
    content = request.POST['content']

    user_token = Token.objects.filter(key=token)
    if len(user_token) == 0:
        return Response('nologin')
    if len(title) == 0:
        return Response('notitle')

    # 保存文章
    new_article = Article(title=title)
    new_article.save()
    # 解析富文本html文档
    soup = BeautifulSoup(content, 'html.parser')
    # 获取所有img标签 图片
    imgList = soup.find_all('img')
    # print(imgList)
    for img in range(0, len(imgList)):
        src = imgList[img]['src']
        # 判断图片 是远程 还是 本地
        if 'http://' in src or 'https://' in src:
            # print('远程图片')
            # 请求远程图片
            image = requests.get(src)
            # 转化二进制
            image_data = Image.open(BytesIO(image.content))
            print(image_data)
            # 设定文件名称
            image_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S')+'-' + \
                str(new_article.id)+'-'+str(img)
            image_data.save("upload/" + image_name + ".png")
            new_src = hostUrl + "upload/" + image_name + ".png"
            content = content.replace(src, new_src)
            # 封面设定
            if cover == src:
                cover = new_src
        else:
            # print('本地图片')
            image_data = base64.b64decode(src.split(',')[1])
            image_name = datetime.datetime.now().strftime('%Y%m%d%H%M%S')+'-'+str(new_article.id) + \
                '-' + str(img)+'.' + \
                src.split(',')[0].split('/')[1].split(';')[0]
            # print(image_name)
            image_url = os.path.join('upload', image_name).replace('\\', '/')
            with open(image_url, 'wb') as f:
                f.write(image_data)
            # print(image_url)
            new_src = hostUrl + image_url
            content = content.replace(src, new_src)
            # 封面设定
            if cover == src:
                cover = new_src

    new_article.content = content
    new_article.describe = describe
    new_article.cover = cover
    new_article.belong = user_token[0].user
    new_article.save()
    return Response('ok')