示例#1
0
class UserList(ViewSet):
    """ Get user list """

    authentication_classes = [TokenAuthentication]
    permission_classes = [
        IsAuthenticated & partial(APIAccessPermission,
                                  API().get_api_name('user', 'list'))
    ]
    renderer_classes = [JSONRenderer]
    serializer_class = UserListSerializer

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.commons = Commons()
        self.status = Status()
        self.error_msg = ''

    def create(self, request):
        self.commons.active_language(language=request.META.get(
            'HTTP_LANGUAGE', getattr(settings, 'LANGUAGE_CODE')))
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            page = serializer.data['page']
            obj_friends = Users.objects.distinct().all() \
             .values('id', 'first_name', 'last_name', 'email', 'sex', 'city__id', 'social_network') \
             .exclude(is_superuser=True) \
             .exclude(id=request.user.id) \
             .order_by('first_name', 'last_name')

            data = self.commons.paginator(obj=obj_friends,
                                          page=page,
                                          data_on_page=30)
            return self.commons.response(_status=self.status.HTTP_2000_OK,
                                         data={
                                             'users': data.object_list,
                                             'page': data.number
                                         })
        else:
            self.error_msg = serializer.errors

        return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST,
                                     error_msg=self.error_msg)
示例#2
0
class FriendList(ViewSet):
    """ Get friend list """

    authentication_classes = [TokenAuthentication]
    permission_classes = [
        IsAuthenticated & partial(APIAccessPermission,
                                  API().get_api_name('friend', 'list'))
    ]
    renderer_classes = [JSONRenderer]
    serializer_class = FriendListSerializer

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.commons = Commons()
        self.status = Status()
        self.error_msg = _('Something wrong. Please try again.')

    @transaction.atomic()
    def create(self, request):
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            page = serializer.data['page']
            obj_friends = request.user.friend.all()\
             .values(
             'id', 'first_name', 'last_name', 'email', 'sex',
             'social_network__facebook', 'social_network__twitter',
             'social_network__github', 'social_network__instagram'
            ).order_by('first_name', 'last_name')
            data = self.commons.paginator(obj_friends, page, data_on_page=30)
            return self.commons.response(_status=self.status.HTTP_2000_OK,
                                         data={
                                             'friends': data.object_list,
                                             'page': data.number
                                         })
        else:
            self.error_msg = serializer.errors

        return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST,
                                     error_msg=self.error_msg)