Beispiel #1
0
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 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)
Beispiel #3
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)
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)
	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.')