def get(self, r, *args, **kwargs):
     if kwargs.get('cat_id'):
         cat = Category.objects.active().get_or_api_404(
             id=kwargs.get('cat_id'))
         return APIResponse(cat.to_dict())
     else:
         cats = Category.objects.active().all().pagination()
         return APIResponse(cats)
 def get(self, r, *args, **kwargs):
     pagination, order_by, filters, defer, search = parse_query_string(
         r.GET, 'post')
     if kwargs.get('tag_id'):
         tag = Post.objects.active().defer(*defer).get_or_api_404(
             id=kwargs.get('tag_id'))
         return APIResponse(tag.to_dict())
     else:
         tags = Tag.objects.active().defer(*defer).order_by(
             *order_by).pagination(**pagination)
         return APIResponse(tags)
 def get(self, r, *args, **kwargs):
     pagination, order_by, filters, defer, search = parse_query_string(
         r.GET, 'post')
     if kwargs.get('post_id'):
         post = Post.objects.active().defer(*defer).get_or_api_404(
             id=kwargs.get('post_id'))
         post.add_view_count()
         return APIResponse(post.to_dict())
     else:
         posts = Post.objects.active(search, **filters).defer(
             *defer).order_by(*order_by).pagination(**pagination)
         return APIResponse(posts)
예제 #4
0
def login(r):
    """登录"""
    data = json.loads(r.body)
    user = authenticate(**data)
    if user is not None:
        return APIResponse(user.token)
    else:
        return APIResponseError(10005)
 def post(self, r, *args, **kwargs):
     data = json.loads(r.body)
     # tags = data.pop('tags') if 'tags' in data.keys() else []
     if kwargs.get('post_id'):
         post = Post.objects.get_or_api_404(
             id=kwargs.get('post_id')).update_fields(**data)
     else:
         post = Post.create(**data, author=r.user)
     return APIResponse(post.to_dict())
예제 #6
0
 def get(self, r, *args, **kwargs):
     pagination, order_by, filters, defer, search = parse_query_string(
         r.GET, 'post')
     if kwargs.get('post_id'):
         post = Post.objects.active().defer(*defer).get_or_api_404(
             id=kwargs.get('post_id'))
         post.add_view_count()
         return APIResponse(post.to_dict())
     else:
         if r.user and r.user.username:
             share_filters = {
                 k: v
                 for k, v in filters.items()
                 if k not in ['private', 'is_publish', 'rate__gt']
             }
             query = search & (Q(**filters)
                               | Q(author=r.user, **share_filters))
         else:
             query = search & Q(**filters)
         posts = Post.objects.active(query).defer(*defer).order_by(
             *order_by).pagination(**pagination)
         return APIResponse(posts)
예제 #7
0
def upload_image(r):
    files = r.FILES
    image = files.get('image')
    if not image:
        return APIResponseError(12002)
    name: str = image.name
    try:
        ext = name.rsplit('.')[1]
    except IndexError:
        return APIResponseError(12001)
    file_name = uuid.uuid4().hex + '.' + ext
    path = os.path.join(settings.ENV_UPLOAD_PATH, 'images', file_name)

    url = os.path.join(settings.STATIC_URL, os.path.join('images', file_name))
    handle_upload_file(image, path)

    return APIResponse(url)
예제 #8
0
    def post(self, r, *args, **kwargs):
        """HTTP POST方法

        更新资源

        Args:
            r (request): [description]

        Returns:
            JSON
        """
        model_name = kwargs.get('model_name')
        model = self.get_model(model_name)
        data = json.loads(r.body)

        _id = kwargs.get('id')
        if _id:
            obj = model.objects.get_or_api_404(id=_id).update_fields(**data)
        else:
            obj = model.create(**data, auth=r.user)

        return APIResponse(obj.to_dict())
예제 #9
0
    def delete(self, r, *args, **kwargs):
        """HTTP DELETE方法

        Args:
            r (requests): request对象

        Raises:
            APIError: API错误

        Returns:
            JSON

        DELETE /post/1  删除 id = 1 的文章
        """
        model_name = kwargs.get('model_name')
        model = self.get_model(model_name)

        _id = kwargs.get('id')
        if _id:
            obj = model.objects.active().get_or_api_404(id=_id).delete()
        else:
            raise APIError(10009)
        return APIResponse()
예제 #10
0
    def get(self, r, *args, **kwargs):
        """HTTP GET方法

        Args:
            r (request): request对象 

        Returns:
            JSON对象或者错误信息
        """
        model_name = kwargs.get('model_name')
        model = self.get_model(model_name)

        _id = kwargs.get('id')
        if _id:
            data = model.objects.active().get_or_api_404(id=_id).to_dict()
        else:
            query = parse_query_string(r.GET, model_name)
            data = model.objects\
                .active(query.search, **query.filters)\
                .defer(*query.defer)\
                .order_by(*query.order_by)\
                .pagination(**query.pagination)

        return APIResponse(data)
예제 #11
0
 def publish_post(self, r, *args, **kwargs):
     return APIResponse(kwargs)
예제 #12
0
def logout(r):
    """注销"""
    return APIResponse()
예제 #13
0
def register(r):
    """注册"""
    data = json.loads(r.body)
    user = UserProfile.objects.create_user(**data)
    return APIResponse(user.token)
예제 #14
0
def index(r):
    return APIResponse(
        dict(name='hello world!',
             hostname=os.environ.get('HOSTNAME', 'none'),
             index=1))
예제 #15
0
 def delete(self, r, *args, **kwargs):
     if kwargs.get('post_id'):
         Post.objects.active(id=kwargs.get('post_id')).delete()
         return APIResponse()
     else:
         return APIResponse(code=10003)
예제 #16
0
def get_qiniu_token_view(r):
    token = get_qiniu_token(prefix='')
    return APIResponse(token)