class Logout(ViewSet): """ Logout to system """ authentication_classes = [TokenAuthentication] permission_classes = [ IsAuthenticated & partial(APIAccessPermission, API().get_api_name('auth', 'logout')) ] renderer_classes = [JSONRenderer] def __init__(self, **kwargs): super().__init__(**kwargs) self.status = Status() self.commons = Commons() self.message = _('Logout successful.') self.error_msg = _('You must be login to system.') def create(self, request): self.commons.active_language(language=request.META.get( 'HTTP_LANGUAGE', getattr(settings, 'LANGUAGE_CODE'))) try: self.request.user.auth_token.delete() logout(request) self.commons.logs(level=1, message=self.message, name=__name__) return self.commons.response(_status=self.status.HTTP_2000_OK, message=self.message) except Exception as e: self.error_msg = str(e) self.commons.logs(level=3, message=self.error_msg, name=__name__) self.commons.logs(level=2, message=self.error_msg, name=__name__) return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg)
class AddFriend(ViewSet): """ Add a new friend """ authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated & partial(APIAccessPermission, API().get_api_name('friend', 'add'))] renderer_classes = [JSONRenderer] serializer_class = AddFriendSerializer def __init__(self, **kwargs): super().__init__(**kwargs) self.commons = Commons() self.status = Status() self.error_msg = _('Something wrong. Please try again.') 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(raise_exception=True): try: obj_user = Users.objects.get(id=serializer.data['user_id']) obj_friend, created = Friend.objects.update_or_create( id=obj_user.id, first_name=obj_user.first_name, last_name=obj_user.last_name, sex=obj_user.sex, email=obj_user.email, social_network=obj_user.social_network, city=obj_user.city ) request.user.friend.add(obj_friend) return self.commons.response( _status=self.status.HTTP_2000_OK, message=_('You have successfully sent a friend request.') ) except Users.DoesNotExist as e: self.commons.logs(level=2, message=str(e), name=__name__) self.error_msg = _('The user does not exists.') except Exception as e: self.commons.logs(level=3, message=str(e), name=__name__) else: self.error_msg = serializer.errors return self.commons.response( _status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg )
class Login(ViewSet): """ API Login """ authentication_classes = [] permission_classes = [ partial(APIAccessPermission, API().get_api_name('auth', 'login')) ] renderer_classes = [JSONRenderer] serializer_class = LoginSerializer def __init__(self, **kwargs): super().__init__(**kwargs) self.token = '' self.status = Status() self.commons = Commons() self.message = _('You are successfully logged in.') self.error_msg = _('Something wrong. Please try again.') 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(): obj_user = authenticate(username=serializer.data['username'], password=serializer.data['password']) if obj_user is not None: data = {'token': self.commons.init_token(obj_user)} login(request, obj_user) self.commons.logs(level=1, message=str(obj_user) + ' has successfully logged in.', name=__name__) return self.commons.response(_status=self.status.HTTP_2000_OK, data=data, message=self.message) self.error_msg = _('User does not exists.') else: self.error_msg = serializer.errors self.commons.logs(level=2, message=self.error_msg, name=__name__) return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg)
class CityList(ViewSet): """ Get city list """ authentication_classes = [TokenAuthentication] permission_classes = [ IsAuthenticated & partial(APIAccessPermission, API().get_api_name('city', 'list')) ] renderer_classes = [JSONRenderer] serializer_class = CityListSerializer 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=self.request.data) if serializer.is_valid(): page = serializer.data['page'] obj_city_list = City.objects.distinct().filter(status=True)\ .values('id', 'name')\ .order_by('order', 'name') paginator = Paginator(obj_city_list, 30) try: data_each_page = paginator.page(page) except PageNotAnInteger: data_each_page = paginator.page(1) except EmptyPage: data_each_page = paginator.page(paginator.num_pages) data = { 'city_list': data_each_page.object_list, 'page': data_each_page.number } return self.commons.response(_status=self.status.HTTP_2000_OK, data=data) else: self.error_msg = serializer.errors return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg)
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)
class ProfileUpdate(ViewSet): """ Update profile """ authentication_classes = [TokenAuthentication] permission_classes = [ IsAuthenticated & partial(APIAccessPermission, API().get_api_name('profile', 'update')) ] renderer_classes = [JSONRenderer] serializer_class = ProfileUpdateSerializer def __init__(self, **kwargs): super().__init__(**kwargs) self.status = Status() self.commons = Commons() self.message = '' self.error_msg = _('Something wrong. Please try again.') @transaction.atomic() def create(self, request): self.commons.active_language(language=request.META.get( 'HTTP_LANGUAGE', getattr(settings, 'LANGUAGE_CODE'))) serializer = self.serializer_class(data=self.request.data) if serializer.is_valid(): self.error_msg = '' obj_user = self.request.user obj_user.first_name = serializer.data['first_name'] obj_user.last_name = serializer.data['last_name'] obj_user.sex = serializer.data['sex'] obj_user.email = serializer.data['email'] obj_user.relationship_status = serializer.data[ 'relationship_status'] obj_user.city_id = serializer.data['city_id'] obj_user.is_update = True obj_user.save() return self.commons.response(_status=self.status.HTTP_2000_OK, message=_('Update successful.'), error_msg=self.error_msg) else: self.error_msg = serializer.errors return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg)
class ProfileDetail(ViewSet): """ Get profile detail """ authentication_classes = [TokenAuthentication] permission_classes = [ IsAuthenticated & partial(APIAccessPermission, API().get_api_name('profile', 'detail')) ] renderer_classes = [JSONRenderer] def __init__(self, **kwargs): super().__init__(**kwargs) self.commons = Commons() self.status = Status() self.message = '' self.error_msg = _('Something wrong. Please try again.') def create(self, request): self.commons.active_language(language=request.META.get( 'HTTP_LANGUAGE', getattr(settings, 'LANGUAGE_CODE'))) obj_user = self.request.user data = { 'first_name': obj_user.first_name, 'last_name': obj_user.last_name, 'email': obj_user.email, 'sex': obj_user.sex, 'relationship_status': obj_user.relationship_status, 'last_login': obj_user.last_login if obj_user.last_login else '', 'date_joined': obj_user.date_joined, 'is_update': obj_user.is_update, 'city': obj_user.city_id, 'facebook': obj_user.social_network.facebook if obj_user.social_network else '', 'twitter': obj_user.social_network.twitter if obj_user.social_network else '', 'instagram': obj_user.social_network.instagram if obj_user.social_network else '', 'github': obj_user.social_network.github if obj_user.social_network else '', } return self.commons.response(_status=self.status.HTTP_2000_OK, data=data)
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)
class Register(ViewSet): """ Register your account. """ authentication_classes = [] permission_classes = [partial(APIAccessPermission, API().get_api_name('auth', 'register'))] renderer_classes = [JSONRenderer] serializer_class = RegisterSerializer def __init__(self, **kwargs): super().__init__(**kwargs) self.status = Status() self.commons = Commons() self.message = _('Congratulations. You have successfully registered.') self.error_msg = _('Something wrong. Please try again.') @transaction.atomic() def create(self, request): self.commons.active_language(language=request.META.get('HTTP_LANGUAGE', getattr(settings, 'LANGUAGE_CODE'))) serializer = self.serializer_class(data=self.request.data) if serializer.is_valid(): if not serializer.compare_pwd(data=request.data): obj_user, created = Users.objects.get_or_create(username=serializer.data['username'], defaults={ 'password': make_password(serializer.data['password']) }) if created: data = { 'token': self.commons.init_token(obj_user) } self.commons.logs(level=1, message=str(obj_user) + ' has successfully registered.', name=__name__) return self.commons.response(_status=self.status.HTTP_2000_OK, data=data, message=self.message) self.error_msg = _('User always exists.') else: self.error_msg = _('Password does not match.') else: self.error_msg = serializer.errors self.commons.logs(level=2, message=self.error_msg, name=__name__) return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg)
class RemoveFriend(ViewSet): """ Remove a friend """ authentication_classes = [TokenAuthentication] permission_classes = [ IsAuthenticated & partial(APIAccessPermission, API().get_api_name('friend', 'remove')) ] renderer_classes = [JSONRenderer] serializer_class = RemoveFriendSerializer def __init__(self, **kwargs): super().__init__(**kwargs) self.commons = Commons() self.status = Status() self.error_msg = _('Something wrong. Please try again.') def create(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): try: with transaction.atomic(): obj_friend = Friend.objects.get( id=serializer.data['friend_id']) request.user.friend.remove(obj_friend) return self.commons.response( _status=self.status.HTTP_2000_OK, message=_('Successfully deleted a friend.')) except Exception as e: self.commons.logs(level=2, message=str(e), name=__name__) self.error_msg = _('Friend does not exists.') else: self.error_msg = serializer.errors return self.commons.response(_status=self.status.HTTP_4000_BAD_REQUEST, error_msg=self.error_msg)