Ejemplo n.º 1
0
 def list(self, request, *args, **kwargs):
     user = request.user
     notifies = self.filter_queryset(self.get_queryset()).filter(target=user)
     page = self.paginate_queryset(NotifySerializer(notifies, many=True).data)
     if page is not None:
         return self.get_paginated_response(page)
     return json_response(page)
Ejemplo n.º 2
0
 def following(self, request, pk):
     user = get_object_or_404(User, uuid=pk)
     followers = user.followed.all()
     page = self.paginate_queryset(UserSerializer(followers, many=True).data)
     if page is not None:
         return self.get_paginated_response(page)
     return json_response(page)
Ejemplo n.º 3
0
 def list(self, request, *args, **kwargs):
     user_uuid = self.kwargs['parent_lookup_uuid'] if 'parent_lookup_uuid' \
                 in self.kwargs else request.user.uuid
     ins = Ins.objects.filter(owner__uuid=user_uuid).annotate(
         likes_count=Count('likes')).order_by('-created_at')
     page = self.paginate_queryset(InsSerializer(ins, many=True).data)
     if page is not None:
         return self.get_paginated_response(page)
     return json_response(page)
Ejemplo n.º 4
0
 def register(self, request):
     data = request.data
     check_body_keys(data, ['name', 'password'])
     name = data.pop('name')
     password = data.pop('password')
     user = User.objects.create_user(username=name, password=password, **data)
     if user is not None:
         login(request, user)
     return json_response(UserSerializer(user).data)
Ejemplo n.º 5
0
 def login(self, request):
     data = request.data
     check_body_keys(data, ['name', 'password'])
     user = authenticate(**data)
     if user is not None:
         login(request, user)
         return json_response(UserSerializer(user).data)
     else:
         return error_response('Login failed!', status=status.HTTP_401_UNAUTHORIZED)
Ejemplo n.º 6
0
def list_ins_from_tag(request, pk):
    tags = Tag.objects.filter(pk=pk)
    if not tags:
        return empty_response()
    ins = Ins.objects.filter(tags=tags.first())
    pagination = PageNumberPagination()
    page = pagination.paginate_queryset(InsSerializer(ins, many=True).data, request)
    if page is not None:
        return pagination.get_paginated_response(page)
    return json_response(page)
Ejemplo n.º 7
0
 def process_exception(self, request, exception):
     LOG.error("Capture exception. type: {}, info: {}".format(
         type(exception), exception))
     if type(exception) == AloneException:
         if type(exception.code) == int:
             return error_response(message=exception.message,
                                   status=exception.code)
         return json_response(data={
             'message': exception.message,
             'code': exception.code,
         },
                              status=get_status_code(exception.code))
Ejemplo n.º 8
0
def get_temp_cos_token(request):
    cos_config = {
        'secret_id': settings.COS_SECRET_ID,
        'secret_key': settings.COS_SECRET_KEY,
    }
    sts = Sts(cos_config)
    rep = sts.get_credential().content
    # data = {"credentials": {
    #     "sessionToken": "52641ddf39911142050f416afb3eaef962635be130001",
    #     "tmpSecretId": "AKIDWFPouj0aS0NUdWEOv5qljJwXMEg8NVHS",
    #     "tmpSecretKey": "RE3kEr8OvWY34pg1d1HPOExR1iOnheE4"},
    #         "expiredTime": 1534562396}
    return json_response(json.loads(rep)['data'])
Ejemplo n.º 9
0
    def retrieve(self, request, *args, **kwargs):
        current_user = request.user
        is_current_user = True if str(current_user.uuid) == kwargs['pk'] else False
        if is_current_user:
            user = current_user
            relation = None

        else:
            user = get_object_or_404(User, uuid=kwargs['pk'])
            if current_user in user.followers.all():
                relation = 'following'
            if user in current_user.followers.all():
                relation = 'followed'
            setattr(user, 'followers_count', user.followers.count())
            setattr(user, 'following_count', user.followed.count())
        setattr(user, 'relation', relation)
        setattr(user, 'post_count', user.post_ins.count())
        return json_response(UserSerializer(user).data)
Ejemplo n.º 10
0
 def unlike(self, request, pk=None):
     ins = get_object_or_404(Ins, uuid=pk)
     ins.unlike_by(request.user)
     return json_response(InsSerializer(ins).data)
Ejemplo n.º 11
0
 def like(self, request, pk=None):
     ins = get_object_or_404(Ins, uuid=pk)
     ins.like_by(request.user)
     create_notify(sender=request.user, target=ins.owner, ins=ins)
     return json_response(InsSerializer(ins).data)