profile change photo
This commit is contained in:
SDE
2023-10-22 14:18:22 +03:00
parent 51598dc022
commit cc377963e0
4 changed files with 65 additions and 4 deletions

View File

@@ -23,4 +23,6 @@ urlpatterns = [
path('change_profile/', change_profile_ajax, name='change_profile_ajax'),
path('change_profile_confirm/', change_profile_confirm_ajax, name='change_profile_confirm_ajax'),
path('change_avatar_confirm/', change_avatar_confirm_ajax, name='change_avatar_confirm_ajax'),
]

View File

@@ -12,6 +12,10 @@ from datetime import datetime
from django.template.loader import render_to_string
from django.urls import reverse
from .funcs import *
from django.core.exceptions import ValidationError
import json
from django.core.files import File
import base64
# @login_required(login_url='/profile/login/')
@@ -64,6 +68,64 @@ def support_tickets_ajax(request):
return JsonResponse({'html': html}, status=200)
@login_required(login_url='/profile/login/')
def change_avatar_confirm_ajax(request):
from django.core.files.base import ContentFile
if request.method != 'POST':
raise Http404
data = json.loads(request.body)
file_data = json.loads(data[0])
head, content = file_data['file'].split(',')
content = base64.b64decode(content)
file = ContentFile(content)
request.user.user_profile.avatar.save(file_data['file_name'], file)
request.user.user_profile.save(update_fields=['avatar'])
return JsonResponse({'url': request.user.user_profile.avatar.url})
@login_required(login_url='/profile/login/')
def change_profile_confirm_ajax(request):
if request.method != 'POST':
raise Http404
data = request.POST
from .forms import RegistrationForm
form = RegistrationForm(data)
if not form.is_valid():
raise ValidationError(form.errors)
data_for_save = {}
if 'firstname' in data:
data_for_save.update({'first_name': data['firstname']})
if 'lastname' in data:
data_for_save.update({'last_name': data['lastname']})
if 'email' in data:
data_for_save.update({'email': data['email']})
data_for_save.update({'username': data['email']})
request.user.update(**data_for_save)
data_for_save = {}
if 'password' in data:
data_for_save.update({'password': data['password']})
if 'confirm_password' in data:
data_for_save.update({'confirm_password': data['confirm_password']})
request.user.u.set_password(data['password'])
data_for_save = {}
if 'country' in data:
data_for_save.update({'country': data['country']})
if 'city' in data:
data_for_save.update({'city': data['city']})
if 'tel' in data:
data_for_save.update({'phone': data['tel']})
request.user.user_profile.update(**data_for_save)
html = get_profile_change_page_content_html(request)
return JsonResponse({'html': html}, status=200)
@login_required(login_url='/profile/login/')
def change_profile_ajax(request):