Example #1
0
 def list(self, request, *args, **kwargs):
     locales = [{
         "code": c,
         "name": n,
         "bidi": c in settings.LANGUAGES_BIDI
     } for c, n in settings.LANGUAGES]
     return response.Ok(locales)
Example #2
0
    def change_avatar(self, request):
        """
        Change avatar to current logged user.
        """
        avatar = request.FILES.get('avatar', None)
        id = request.data.get('id', None)
        if not avatar:
            raise exc.WrongArguments(_("Incomplete arguments"))

        try:
            pil_image(avatar)
        except Exception:
            raise exc.WrongArguments(_("Invalid image format"))

        if id is None:
            request.user.photo = avatar
            request.user.save(update_fields=["photo"])
            user_data = self.admin_serializer_class(request.user).data
        else:
            user = get_object_or_404(models.User, id=id)
            user.photo = avatar
            user.save(update_fields=["photo"])
            user_data = self.admin_serializer_class(user).data

        return response.Ok(user_data)
Example #3
0
 def remove_avatar(self, request):
     """
     Remove the avatar of current logged user.
     """
     request.user.photo = None
     request.user.save(update_fields=["photo"])
     user_data = self.admin_serializer_class(request.user).data
     return response.Ok(user_data)
Example #4
0
 def list(self, request, *args, **kwargs):
     self.object_list = filters.UserFilterBackend().filter_queryset(
         request, self.get_queryset(), self)
     page = self.paginate_queryset(self.object_list)
     if page is not None:
         serializer = self.get_pagination_serializer(page)
     else:
         serializer = self.get_serializer(self.object_list, many=True)
     return response.Ok(serializer.data)
Example #5
0
    def create(self, request, **kwargs):
        auth_plugins = get_auth_plugins()

        login_type = request.data.get("type", None)

        if login_type in auth_plugins:
            data = auth_plugins[login_type]['login_func'](request)
            return response.Ok(data)

        raise exc.BadRequest(_("invalid login type"))
Example #6
0
    def password_recovery(self, request, pk=None):
        username_or_email = request.data.get('username', None)

        if not username_or_email:
            raise exc.WrongArguments(_("Invalid username or email"))

        user = get_user_by_username_or_email(username_or_email)
        user.token = str(uuid.uuid1())
        user.save(update_fields=["token"])

        email = mail_builder.password_recovery(user, {"user": user})
        email.send()

        return response.Ok(
            {"detail": _("A Mail has been sent to you successful!")})
Example #7
0
 def me(self, request, pk=None):
     """
     Get me.
     """
     user_data = self.admin_serializer_class(request.user).data
     return response.Ok(user_data)
Example #8
0
 def retrieve(self, request, *args, **kwargs):
     self.object_list = filters.UserFilterBackend().filter_queryset(
         request, self.get_queryset(), self)
     self.object = get_object_or_404(self.object_list, **kwargs)
     serializer = self.get_serializer(self.object)
     return response.Ok(serializer.data)