Exemplo n.º 1
0
def user_profile(request):
    if request.method == 'POST':
        user = request.user
        form = EditUserModelForm(request.POST, request.FILES, instance=user)

        if form.is_valid():
            print request.POST
            if not request.POST.get('organization'):
                response_data = {'error': True, "response": {"organization": 'Please provide organization name'}}
                return HttpResponse(json.dumps(response_data))
            organization = Organization.objects.get(id=request.user.organization.id)
            organization.name = request.POST.get('organization')
            organization.domain = request.POST.get('domain').lower()
            organization.save()

            if 'profile_pic' in request.FILES:

                user.profile_pic = request.FILES['profile_pic']
                try:
                    shutil.rmtree(settings.MEDIA_ROOT + 'profile/' + user.username)
                except:
                    pass

            form.save()
            response_data = {'error': False, "response": 'Successfully updated'}
            messages.success(request, 'Your profile updated successfully !')
        else:
            response_data = {'error': True, 'response': form.errors}

        return HttpResponse(json.dumps(response_data))
    return render(request, 'user/user_profile.html', {'notification_list': get_notification_list(request.user)})
Exemplo n.º 2
0
def user_profile(request):
    user = request.user

    if request.method == 'POST':

        form = EditUserModelForm(request.POST, request.FILES, instance=user)

        if form.is_valid():
            if 'profile_pic' in request.FILES:

                user.profile_pic = request.FILES['profile_pic']
                try:
                    os.remove(settings.MEDIA_ROOT+'profile/'+user.username+'/'+user.username+'.jpg')
                except:
                    pass

            form.save()
            response_data = {'error': False, "response": 'Successfully updated'}
            return HttpResponse(json.dumps(response_data), content_type='application/json')
        else:
            response_data = {'error': True, 'response': form.errors}

            return HttpResponse(json.dumps(response_data), content_type="application/json")
    form = EditUserModelForm(instance=user)

    context = {'form': form}
    return render(request, 'user_profile.html', context)