diff --git a/polls/urls.py b/polls/urls.py index bf199ab..ffe2097 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -73,7 +73,7 @@ urlpatterns = [ path('app/intro.html', views.app_intro, name='polls_app_intro'), path('app/download/', views.download, name='polls_app_download'), path('app/has/update/', views.has_update, name='polls_app_has_update'), - path('app/profile/image/upload/', views.upload_image, name='polls_profile_upload'), + path('app/profile/image/upload/', views.upload_profile, name='polls_profile_upload'), ] tasks.process_notify_task(repeat=5) \ No newline at end of file diff --git a/polls/utils.py b/polls/utils.py index 84107f6..5733698 100644 --- a/polls/utils.py +++ b/polls/utils.py @@ -11,7 +11,6 @@ from aliyunsdkcore.client import AcsClient import uuid from aliyunsdkcore.profile import region_provider - def sent_sms_code(phone, code): clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53') param = {YC.MOBILE: phone, @@ -131,7 +130,6 @@ def queryset_to_list(q, fields): l.append(r) return l - if __name__ == '__main__': # r = sent_sms_code('13993199566', 4321) # print(r.code(), type(r.code())) diff --git a/polls/views/__pycache__/__init__.cpython-38.pyc b/polls/views/__pycache__/__init__.cpython-38.pyc index 552b9a2..08a71cf 100644 Binary files a/polls/views/__pycache__/__init__.cpython-38.pyc and b/polls/views/__pycache__/__init__.cpython-38.pyc differ diff --git a/polls/views/__pycache__/user.cpython-38.pyc b/polls/views/__pycache__/user.cpython-38.pyc index 5f2f14c..746cda5 100644 Binary files a/polls/views/__pycache__/user.cpython-38.pyc and b/polls/views/__pycache__/user.cpython-38.pyc differ diff --git a/polls/views/user.py b/polls/views/user.py index cbd4921..52b9393 100644 --- a/polls/views/user.py +++ b/polls/views/user.py @@ -6,9 +6,11 @@ from django.views.decorators.csrf import csrf_exempt from django_token.models import Token import datetime from django.conf import settings -import tempfile import shutil import uuid +import os +from pathlib import Path + from polls.models import Notice, VerifyCode @@ -225,14 +227,19 @@ def reset_password(request): @csrf_exempt @polls_login_required def upload_profile(request): + u = request.user source = request.FILES['photo'] - ext = source.split('.')[-1] + ext = source.name.split('.')[-1] if ext not in ['jpg', 'png', 'jpeg', 'gif']: return JsonResponse({'status': 'error', 'message': '上传的文件不是图片'}) - + filename = "%s.%s" % (uuid.uuid4(), ext) - # fd, filepath = tempfile.mkstemp( - # prefix=source.name, dir=settings.MEDIA_ROOT) - # with open(filepath, 'wb') as dest: - # shutil.copyfileobj(source, dest) - return JsonResponse({'status': 'success', 'message': filename}) + foldpath = os.path.join(settings.MEDIA_ROOT, 'profile') + Path(foldpath).mkdir(parents=True, exist_ok=True) + filepath = os.path.join(foldpath, filename) + with open(filepath, 'wb') as dest: + shutil.copyfileobj(source, dest) + profile = Userprofile.objects.filter(user=u).first() + profile.image = '/profile/%s' % filename + profile.save() + return JsonResponse({'status': 'success', 'message': request.build_absolute_uri('/media/profile/%s' % filename)})