Add avatar upload api
This commit is contained in:
@@ -3,12 +3,13 @@
|
|||||||
|
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from ..views.user import (UserInfoAPI, UserProfileAPI,
|
from ..views.user import (UserInfoAPI, UserProfileAPI, AvatarUploadAPI,
|
||||||
SSOAPI, TwoFactorAuthAPI)
|
SSOAPI, TwoFactorAuthAPI)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r"^user$", UserInfoAPI.as_view(), name="user_info_api"),
|
url(r"^user$", UserInfoAPI.as_view(), name="user_info_api"),
|
||||||
url(r"^profile$", UserProfileAPI.as_view(), name="user_profile_api"),
|
url(r"^profile$", UserProfileAPI.as_view(), name="user_profile_api"),
|
||||||
|
url(r"^avatar/upload$", AvatarUploadAPI.as_view(), name="avatar_upload_api"),
|
||||||
url(r"^sso$", SSOAPI.as_view(), name="sso_api"),
|
url(r"^sso$", SSOAPI.as_view(), name="sso_api"),
|
||||||
url(r"^two_factor_auth$", TwoFactorAuthAPI.as_view(), name="two_factor_auth_api")
|
url(r"^two_factor_auth$", TwoFactorAuthAPI.as_view(), name="two_factor_auth_api")
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
import qrcode
|
import qrcode
|
||||||
|
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
@@ -56,6 +57,24 @@ class UserProfileAPI(APIView):
|
|||||||
return self.success(_("Succeeded"))
|
return self.success(_("Succeeded"))
|
||||||
|
|
||||||
|
|
||||||
|
class AvatarUploadAPI(APIView):
|
||||||
|
def post(self, request):
|
||||||
|
if "file" not in request.FILES:
|
||||||
|
return self.error(_("Upload failed"))
|
||||||
|
|
||||||
|
f = request.FILES["file"]
|
||||||
|
if f.size > 1024 * 1024:
|
||||||
|
return self.error(_("Picture too large"))
|
||||||
|
if os.path.splitext(f.name)[-1].lower() not in [".gif", ".jpg", ".jpeg", ".bmp", ".png"]:
|
||||||
|
return self.error(_("Unsupported file format"))
|
||||||
|
|
||||||
|
name = "avatar_" + rand_str(5) + os.path.splitext(f.name)[-1]
|
||||||
|
with open(os.path.join(settings.IMAGE_UPLOAD_DIR, name), "wb") as img:
|
||||||
|
for chunk in request.FILES["file"]:
|
||||||
|
img.write(chunk)
|
||||||
|
return self.success({"path": "/static/upload/" + name})
|
||||||
|
|
||||||
|
|
||||||
class SSOAPI(APIView):
|
class SSOAPI(APIView):
|
||||||
@login_required
|
@login_required
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
|
|||||||
Reference in New Issue
Block a user