Exemplo n.º 1
0
def add(request, extra_context=None, next_override=None,
        upload_form=UploadAvatarForm, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
        request.FILES or None, user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user = request.user,
                primary = True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            request.user.message_set.create(
                message=_("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
            'avatar/add.html',
            extra_context,
            context_instance = RequestContext(
                request,
                { 'avatar': avatar, 
                  'avatars': avatars, 
                  'upload_avatar_form': upload_avatar_form,
                  'next': next_override or _get_next(request), }
            )
        )
Exemplo n.º 2
0
def upload_avatar_js(request):
    '''
    Upload avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':

        avatar, avatars = avatarviews._get_avatars(request.user)
        upload_avatar_form = UploadAvatarForm(None,
                                              request.FILES,
                                              user=request.user)
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user=request.user,
                primary=True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
            response['avatar'] = accountutil.get_user_avatar_url(request.user)
            status = 200
    return HttpResponse(json.dumps(response),
                        content_type='application/json; charset=utf8',
                        status=status)
Exemplo n.º 3
0
def upload(request, user_id=None):
    user = get_object_or_404(User, pk=user_id)
    if request.method == 'POST':
        if 'avatar' in request.FILES:
            form = UploadAvatarForm(
                            request.POST,
                            request.FILES,
                            user=user)
            if form.is_valid():
                avatar = Avatar(user=user, primary=True)
                image_file = request.FILES['avatar']
                avatar.avatar.save(image_file.name, image_file)
                avatar.save()
                sizes = avatar_settings.AVATAR_AUTO_GENERATE_SIZES
                for size in sizes:
                    avatar.create_thumbnail(size)
                avatar_updated.send(sender=Avatar, user=user, avatar=avatar)
                user.avatar_type = 'a'
                user.clear_avatar_urls()
                user.save()
                message = _('Avatar uploaded and set as primary')
            else:
                errors = get_error_list(form)
                message = ', '.join(map(lambda v: force_str(v), errors))
        else:
            message = _('Please choose file to upload')

        request.session['askbot_avatar_status_message'] = message

    return redirect_to_show_list(user_id)
Exemplo n.º 4
0
def change(request, extra_context=None, next_override=None,
           upload_form=UploadAvatarForm, primary_form=PrimaryAvatarForm,
           *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
        user=request.user, avatars=avatars, **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            messages.success(request, _("Successfully updated your avatar."))
        if updated:
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        return redirect(next_override or _get_next(request))

    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'primary_avatar_form': primary_avatar_form,
        'next': next_override or _get_next(request)
    }
    context.update(extra_context)
    return render(request, 'avatar/change.html', context)
Exemplo n.º 5
0
def add(request,
        extra_context=None,
        next_override=None,
        upload_form=UploadAvatarForm,
        *args,
        **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
                                     request.FILES or None,
                                     user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(user=request.user, primary=True)
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            messages.success(request, _("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
            return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add.html', context)
Exemplo n.º 6
0
def upload(request, user_id=None):
    user = get_object_or_404(User, pk=user_id)
    if request.method == 'POST':
        if 'avatar' in request.FILES:
            form = UploadAvatarForm(
                            request.POST,
                            request.FILES,
                            user=user)
            if form.is_valid():
                avatar = Avatar(user=user, primary=True)
                image_file = request.FILES['avatar']
                avatar.avatar.save(image_file.name, image_file)
                avatar.save()
                sizes = avatar_settings.AVATAR_AUTO_GENERATE_SIZES
                for size in sizes:
                    avatar.create_thumbnail(size)
                avatar_updated.send(sender=Avatar, user=user, avatar=avatar)
                user.avatar_type = 'a'
                user.clear_avatar_urls()
                user.save()
                message = _('Avatar uploaded and set as primary')
            else:
                errors = get_error_list(form)
                message = ', '.join(map(lambda v: force_str(v), errors))
        else:
            message = _('Please choose file to upload')

        request.session['askbot_avatar_status_message'] = message

    return redirect_to_show_list(user_id)
Exemplo n.º 7
0
def delete_avatar_js(request):
    '''
    Delete avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        from avatar.models import Avatar
        from avatar import views as avatarviews
        from avatar.forms import DeleteAvatarForm
        from avatar.signals import avatar_updated
        avatar, avatars = avatarviews._get_avatars(request.user)
        if avatar is None :
            response = 'No avatar exists'
        else:
            aid = avatar.id
            for a in avatars:
                if a.id == aid :
                    a.primary = True
                    a.save()
                    avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
                    break
            Avatar.objects.filter(pk=aid).delete()
            response['avatar'] = accountutil.get_user_avatar_url(request.user)
            status = 200
    return HttpResponse(
        simplejson.dumps(response),
        content_type = 'application/json; charset=utf8',
        status=status
    )
Exemplo n.º 8
0
def upload_avatar_js(request):
    '''
    Upload avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        from avatar.models import Avatar
        from avatar import views as avatarviews
        from avatar.forms import UploadAvatarForm
        from avatar.signals import avatar_updated
        avatar, avatars = avatarviews._get_avatars(request.user)
        upload_avatar_form = UploadAvatarForm(None, request.FILES, user=request.user)
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user = request.user,
                primary = True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            response['avatar'] = accountutil.get_user_avatar_url(request.user)
            status = 200
    return HttpResponse(
        simplejson.dumps(response),
        content_type = 'application/json; charset=utf8',
        status=status
    )
Exemplo n.º 9
0
def delete_avatar_js(request):
    '''
    Delete avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        avatar, avatars = avatarviews._get_avatars(request.user)
        if avatar is None:
            response = 'No avatar exists'
        else:
            aid = avatar.id
            for a in avatars:
                if a.id == aid:
                    a.primary = True
                    a.save()
                    avatar_updated.send(
                        sender=Avatar,
                        user=request.user,
                        avatar=avatar
                    )
                    break
            Avatar.objects.filter(pk=aid).delete()
            response['avatar'] = userutil.get_user_avatar_url(request.user)
            status = 200
    return JsonResponse(
        response,
        status=status
    )
Exemplo n.º 10
0
def delete(request, extra_context=None, next_override=None, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None,
        user=request.user, avatars=avatars)
    if request.method == 'POST':
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data['choices']
            if unicode(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if unicode(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            request.user.message_set.create(
                message=_("Successfully deleted the requested avatars."))
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/confirm_delete.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar': avatar, 
              'avatars': avatars,
              'delete_avatar_form': delete_avatar_form,
              'next': next_override or _get_next(request), }
        )
    )
Exemplo n.º 11
0
def delete_avatar_js(request):
    '''
    Delete avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        avatar, avatars = avatarviews._get_avatars(request.user)
        if avatar is None:
            response = 'No avatar exists'
        else:
            aid = avatar.id
            for a in avatars:
                if a.id == aid:
                    a.primary = True
                    a.save()
                    avatar_updated.send(
                        sender=Avatar,
                        user=request.user,
                        avatar=avatar
                    )
                    break
            Avatar.objects.filter(pk=aid).delete()
            response['avatar'] = userutil.get_user_avatar_url(request.user)
            status = 200
    return JsonResponse(
        response,
        status=status
    )
Exemplo n.º 12
0
def delete(request, extra_context=None, next_override=None, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None,
                                          user=request.user,
                                          avatars=avatars)
    if request.method == 'POST':
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data['choices']
            if unicode(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if unicode(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar,
                                            user=request.user,
                                            avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            request.user.message_set.create(
                message=_("Successfully deleted the requested avatars."))
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/confirm_delete.html',
        extra_context,
        context_instance=RequestContext(
            request, {
                'avatar': avatar,
                'avatars': avatars,
                'delete_avatar_form': delete_avatar_form,
                'next': next_override or _get_next(request),
            }))
Exemplo n.º 13
0
def add(request, extra_context=None, next_override=None,
        upload_form=UploadAvatarForm, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
        request.FILES or None, user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                emailuser = request.user.username,
                primary = True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            messages.success(request, _("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return HttpResponseRedirect(next_override or _get_next(request))
        else:
            messages.error(request, upload_avatar_form.errors['avatar'])

        return HttpResponseRedirect(_get_next(request))
    else:
        # Only allow post request to change avatar.
        raise Http404
Exemplo n.º 14
0
def delete_avatar_for_user(request, for_user=None, extra_context=None,
        next_override=None, *args, **kwargs):
    target_user = get_object_or_404(User, username=for_user, is_active=True)
    if not AVATAR_ADMIN_TEST(request, target_user):
        raise PermissionDenied
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(target_user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None,
        user=target_user, avatars=avatars)
    if request.method == 'POST':
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data['choices']
            if unicode(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if unicode(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar, user=target_user, avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            messages.add_message(request, messages.INFO, _("Successfully deleted the requested avatars"))
            return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'target_user': target_user,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add_for_user.html', context)
Exemplo n.º 15
0
def add_avatar_for_user(request, for_user=None, extra_context=None,
        next_override=None,
        upload_form=UploadAvatarForm, *args, **kwargs):
    target_user = get_object_or_404(User, username=for_user, is_active=True)
    if not AVATAR_ADMIN_TEST(request, target_user):
        raise PermissionDenied
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(target_user)
    upload_avatar_form = upload_form(request.POST or None,
        request.FILES or None, user=target_user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user = target_user,
                primary = True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            messages.add_message(request, messages.INFO, _("Successfully uploaded a new avatar"))
            avatar_updated.send(sender=Avatar, user=target_user, avatar=avatar)
            return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'target_user': target_user,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add_for_user.html', context)
Exemplo n.º 16
0
def add_qbox(request, extra_context=None, next_override=None,
    upload_form=UploadAvatarForm, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar = Avatar.objects.get(user=request.user)
    upload_avatar_form = upload_form(request.POST or None,
        request.FILES or None, user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            image_file = request.FILES['avatar']
            # storage的功能就体现在这里,当你save的时候,FileField自动调用storage的
            # save方法,当调用FileField.delete()的时候,自动调用storage的delete方法
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            request.user.message_set.create(
                message=_("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
            'avatar/add.html',
            extra_context,
            context_instance=RequestContext(
                request,
                {'avatar': avatar,
                 'upload_avatar_form': upload_avatar_form,
                 'next': next_override or _get_next(request), }
            )
        )
Exemplo n.º 17
0
def delete(request, extra_context=None, next_override=None, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None,
                                          user=request.user,
                                          avatars=avatars)
    if request.method == 'POST':
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data['choices']
            if six.text_type(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if six.text_type(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar, user=request.user,
                                            avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            messages.success(request,
                             _("Successfully deleted the requested avatars."))
            return redirect(next_override or _get_next(request))

    context = {
        'avatar': avatar,
        'avatars': avatars,
        'delete_avatar_form': delete_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    template_name = settings.AVATAR_DELETE_TEMPLATE or 'avatar/confirm_delete.html'
    return render(request, template_name, context)
Exemplo n.º 18
0
def delete_avatar_js(request):
    '''
    Delete avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        avatar, avatars = avatarviews._get_avatars(request.user)
        if avatar is None:
            response = 'No avatar exists'
        else:
            aid = avatar.id
            for a in avatars:
                if a.id == aid:
                    a.primary = True
                    a.save()
                    avatar_updated.send(sender=Avatar,
                                        user=request.user,
                                        avatar=avatar)
                    break
            Avatar.objects.filter(pk=aid).delete()
            response['avatar'] = accountutil.get_user_avatar_url(request.user)
            status = 200
    return HttpResponse(json.dumps(response),
                        content_type='application/json; charset=utf8',
                        status=status)
Exemplo n.º 19
0
def delete(request, extra_context=None, next_override=None, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None, user=request.user, avatars=avatars)
    if request.method == "POST":
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data["choices"]
            if unicode(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if unicode(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            messages.add_message(request, messages.INFO, _("Successfully deleted the requested avatars"))
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        "avatar/confirm_delete.html",
        extra_context,
        context_instance=RequestContext(
            request,
            {
                "avatar": avatar,
                "avatars": avatars,
                "delete_avatar_form": delete_avatar_form,
                "next": next_override or _get_next(request),
            },
        ),
    )
Exemplo n.º 20
0
def add(request, extra_context=None, next_override=None,
        upload_form=UploadAvatarForm, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
                                     request.FILES or None,
                                     user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(user=request.user, primary=True)
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            # messages.success(request, _("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add.html', context)
Exemplo n.º 21
0
def upload_avatar_js(request):
    '''
    Upload avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':

        avatar, avatars = avatarviews._get_avatars(request.user)
        upload_avatar_form = UploadAvatarForm(
            None,
            request.FILES,
            user=request.user
        )
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user=request.user,
                primary=True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(
                sender=Avatar,
                user=request.user,
                avatar=avatar
            )
            response['avatar'] = userutil.get_user_avatar_url(request.user)
            status = 200
    return JsonResponse(
        response,
        status=status
    )
Exemplo n.º 22
0
def upload_avatar(request):
    '''
    Upload avatar image
    '''
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':

        avatar, avatars = avatarviews._get_avatars(request.user)
        upload_avatar_form = UploadAvatarForm(None,
                                              request.FILES,
                                              user=request.user)
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user=request.user,
                primary=True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
            response['avatar'] = userutil.get_user_avatar_url(
                request.user)['url']
            status = 200
    return JsonResponse(response, status=status)
Exemplo n.º 23
0
def add(request, extra_context=None, next_override=None, upload_form=UploadAvatarForm, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None, request.FILES or None, user=request.user)
    if request.method == "POST" and "avatar" in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(user=request.user, primary=True)
            image_file = request.FILES["avatar"]
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            messages.add_message(request, messages.INFO, _("Successfully uploaded a new avatar"))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        "avatar/add.html",
        extra_context,
        context_instance=RequestContext(
            request,
            {
                "avatar": avatar,
                "avatars": avatars,
                "upload_avatar_form": upload_avatar_form,
                "next": next_override or _get_next(request),
            },
        ),
    )
Exemplo n.º 24
0
def AvatarAdd(request,extra_context=None,next_override=None,upload_form=UploadAvatarForm,*args,**kwargs):
    if extra_context is None:
        extra_context = {}
    avatar,avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
                                     request.FILES or None,
                                     user = request.user)
    if request.method == 'POST' and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(user=request.user, primary=True)
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name,image_file)
            avatar.save()
            invalidate_cache(request.user)
            messages.success(request, _("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return render(request,'auth/avatar_change_middleware.html')
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override,
    }
    context.update(extra_context)
    template_name = 'auth/avatar_add_middleware.html'
    return render(request, template_name, context)    
Exemplo n.º 25
0
 def save(self, *args, **kwargs):
     request = self.context.get('request')
     user = request.user
     if 'avatar' in request.FILES:
         avatar = Avatar(user=user, primary=True)
         image_file = request.FILES['avatar']
         avatar.avatar.save(image_file.name, image_file)
         avatar.save()
         avatar_updated.send(sender=Avatar, user=user, avatar=avatar)
     return user
Exemplo n.º 26
0
def change(request, extra_context=None, next_override=None,
        upload_form=UploadAvatarForm, primary_form=PrimaryAvatarForm,
        *args, **kwargs):

    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    result = "width"

    kwargs = {}
    if avatar:
        try:
            if avatar.avatar.width <= avatar.avatar.height:
                result = "height"
            else:
                result = "width"
            kwargs = {'initial': {'choice': avatar.id}}
        except IOError:
            avatar.delete()
            avatar = None

    upload_avatar_form = upload_form(request.POST or None,
            request.FILES or None, user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user = request.user,
                primary = True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name.encode('utf8'), image_file)
            avatar.save()
            if avatar.avatar.width <= avatar.avatar.height:
                result = "height"
            else:
                result = "width"
            messages.success(request, _("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            return HttpResponse(json.dumps({'avatar_url': avatar.avatar.url, 'dim':result }), mimetype='text/html', content_type="text/plain")

    return render_to_response(
        'avatar/change.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar': avatar,
              'avatars': avatars,
              'dim': result,
              'AVATAR_CROP_MAX_SIZE': AVATAR_CROP_MAX_SIZE,
              #'upload_avatar_form': upload_avatar_form,
              'crop_form':AvatarCropForm,
              'next': next_override or _get_next(request), }
        )
    )
Exemplo n.º 27
0
 def form_valid(self, form):
     new_avatar = self.create_avatar(self.target, self.request.FILES['avatar'])
     if new_avatar:
         self.request.user.message_set.create(
             message=_("Successfully uploaded a new avatar."))
         avatar_updated.send(sender=Avatar, target=self.target,
                             avatar=new_avatar)
     if self.request.is_ajax() or self.request.REQUEST.get('async', None) == 'true':
         return self.ajax_form_valid(new_avatar)
     else:
         return FormMixin.form_valid(self, form)
Exemplo n.º 28
0
def disable_gravatar(request, user_id=None):
    if request.method == 'POST':
        user = get_object_or_404(User, pk=user_id)
        user.avatar_type = 'a'
        if user.avatar_set.count():
            avatar = user.avatar_set.all()[0]
            avatar.primary = True
            avatar.save()
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        user.clear_avatar_urls()
        user.save()
    return redirect_to_show_list(user_id)
Exemplo n.º 29
0
def disable_gravatar(request, user_id=None):
    if request.method == 'POST':
        user = get_object_or_404(User, pk=user_id)
        user.avatar_type = 'a'
        if user.avatar_set.count():
            avatar = user.avatar_set.all()[0]
            avatar.primary = True
            avatar.save()
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        user.clear_avatar_urls()
        user.save()
    return redirect_to_show_list(user_id)
Exemplo n.º 30
0
 def save(self):
     if 'avatar' in self.files:
         avatar = Avatar(user=self.user, primary=True)
         image_file = self.files['avatar']
         avatar.avatar.save(image_file.name, image_file)
         avatar.save()
         avatar_updated.send(sender=Avatar, user=self.user, avatar=avatar)
     self.user.first_name = self.cleaned_data['first_name']
     self.user.last_name = self.cleaned_data['last_name']
     self.user.profile.summary = self.cleaned_data['summary']
     self.user.profile.save()
     self.user.save()
Exemplo n.º 31
0
    def form_valid(self, form):
        new_avatar = Avatar.objects.get(id=form.cleaned_data['choice'])
        new_avatar.primary = True
        new_avatar.save()
        self.request.user.message_set.create(
            message=_("Avatar successfully updated."))
        avatar_updated.send(sender=Avatar, target=self.target, avatar=new_avatar)

        if self.request.is_ajax() or self.request.REQUEST.get('async', None) == 'true':
            return self.ajax_form_valid(new_avatar)
        else:
            return FormMixin.form_valid(self, form)
Exemplo n.º 32
0
def change(request,
           extra_context=None,
           next_override=None,
           upload_form=UploadAvatarForm,
           primary_form=PrimaryAvatarForm,
           *args,
           **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
                                       user=request.user,
                                       avatars=avatars,
                                       **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            request.user.message_set.create(
                message=_("Successfully updated your avatar."))
        if updated:
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
            if notification:
                notification.send(
                    [request.user], "Picture_updated", {
                        'message':
                        "You have successfully updated your profile picture.",
                    })
        return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/change.html',
        extra_context,
        context_instance=RequestContext(
            request, {
                'avatar': avatar,
                'avatars': avatars,
                'upload_avatar_form': upload_avatar_form,
                'primary_avatar_form': primary_avatar_form,
                'next': next_override or _get_next(request),
            }))
Exemplo n.º 33
0
def change_avatar(request):
    '''Customised version of the default add avatar view from the django-avatar
    app. Changed to allow the form to be embedded on the main DWAD profile
    page.'''
    upload_avatar_form = UploadAvatarForm(request.POST or None,
                                          request.FILES or None,
                                          user=request.user)
    if 'avatar' in request.FILES and upload_avatar_form.is_valid():
        avatar = Avatar(user=request.user, primary=True)
        image_file = request.FILES['avatar']
        avatar.avatar.save(image_file.name, image_file)
        avatar.save()
        avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        return redirect(reverse('profile'))
    context = {'upload_avatar_form': upload_avatar_form}
    return profile(request, extra_context=context)
Exemplo n.º 34
0
def AvatarChange(request,
                 extra_context=None,
                 next_override=None,
                 upload_form=UploadAvatarForm,
                 primary_form=PrimaryAvatarForm,
                 *args,
                 **kwargs):
    if extra_context is None:
        extra_context = {}

    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
                                       user=request.user,
                                       avatars=avatars,
                                       **kwargs)

    if request.method == 'POST':
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            invalidate_cache(request.user)
            messages.success(request, _("Successfully updated your avatar."))
        if updated:
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
        return render(request, 'auth/avatar_change.html')

    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'primary_avatar_form': primary_avatar_form,
        'next': next_override
    }
    context.update(extra_context)
    template_name = 'auth/avatar_change.html'
    return render(request, template_name, context)
Exemplo n.º 35
0
    def update(self, instance, validated_data):
        request = self.context.get('request')

        profile = validated_data.get('profile', None)
        instance.username = validated_data.get('username', instance.username)
        instance.first_name = validated_data.get('first_name',
                                                 instance.first_name)

        if profile:
            bio = profile.get("bio")
            location = profile.get("location")
            birth_date = profile.get("birth_date")
            first_name = profile.get("first_name")
            last_name = profile.get("last_name")
            if bio and bio != instance.profile.bio:
                instance.profile.bio = bio
            if location and location != instance.profile.location:
                instance.profile.location = location
            if birth_date and birth_date != instance.profile.birth_date:
                instance.profile.birth_date = birth_date
            if first_name and first_name != instance.profile.first_name:
                instance.profile.first_name = first_name
            if last_name and last_name != instance.profile.last_name:
                instance.profile.last_name = last_name

        email = validated_data.get('email', None)
        if email and email != instance.email:
            adapter = get_adapter()
            adapter.send_mail('account/email/email_change', instance.email, {})
            email_address = EmailAddress.objects.get(user=instance,
                                                     verified=True)
            email_address.change(request, email, True)
            instance.email = email

        if 'avatar' in request.FILES:
            avatar = Avatar(user=instance, primary=True)
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(sender=Avatar, user=instance, avatar=avatar)

        instance.save()

        # sync_sso(instance)

        return instance
Exemplo n.º 36
0
def change(request,
           extra_context=None,
           next_override=None,
           upload_form=UploadAvatarForm,
           primary_form=PrimaryAvatarForm,
           *args,
           **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
                                       user=request.user,
                                       avatars=avatars,
                                       **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.create_thumbnail(settings.AVATAR_DEFAULT_SIZE)
            avatar.save()
            updated = True
            invalidate_cache(request.user)
            messages.success(request, _("Successfully updated your avatar."))
        if updated:
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
        return redirect(next_override or _get_next(request))

    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'primary_avatar_form': primary_avatar_form,
        'next': next_override or _get_next(request)
    }
    context.update(extra_context)
    template_name = settings.AVATAR_CHANGE_TEMPLATE or 'avatar/change.html'
    return render(request, template_name, context)
Exemplo n.º 37
0
def change_avatar_for_user(request,
                           for_user=None,
                           extra_context=None,
                           next_override=None,
                           upload_form=UploadAvatarForm,
                           primary_form=PrimaryAvatarForm,
                           *args,
                           **kwargs):
    target_user = get_object_or_404(User, username=for_user, is_active=True)
    if not AVATAR_ADMIN_TEST(request, target_user):
        raise PermissionDenied
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(target_user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=target_user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
                                       user=target_user,
                                       avatars=avatars,
                                       **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.create_thumbnail(settings.AVATAR_DEFAULT_SIZE)
            avatar.save()
            updated = True
            messages.add_message(request, messages.INFO,
                                 _("Successfully updated this user's avatars"))
        if updated:
            avatar_updated.send(sender=Avatar, user=target_user, avatar=avatar)
        return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'target_user': target_user,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add_for_user.html', context)
Exemplo n.º 38
0
def set_primary(request, user_id=None, extra_context=None, avatar_size=128):
    """changes default uploaded avatar"""
    user = get_object_or_404(User, pk=user_id)

    if request.method == "POST":
        updated = False
        form = PrimaryAvatarForm(
                        request.POST,
                        user=user,
                        avatars=user.avatar_set.all()
                    )
        if 'choice' in request.POST and form.is_valid():
            avatar = Avatar.objects.get(id=form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            user.clear_avatar_cache()
    return redirect_to_show_list(user_id)
Exemplo n.º 39
0
def change(
    request,
    extra_context=None,
    next_override=None,
    upload_form=UploadAvatarForm,
    primary_form=PrimaryAvatarForm,
    *args,
    **kwargs
):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {"initial": {"choice": avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None, user=request.user, avatars=avatars, **kwargs)
    if request.method == "POST":
        updated = False
        if "choice" in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(id=primary_avatar_form.cleaned_data["choice"])
            avatar.primary = True
            avatar.save()
            updated = True
            messages.add_message(request, messages.INFO, "Successfully updated your avatar.")
        if updated:
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        "avatar/change.html",
        extra_context,
        context_instance=RequestContext(
            request,
            {
                "avatar": avatar,
                "avatars": avatars,
                "show_avatars": False if AVATAR_SINGLE_AVATAR else True,
                "upload_avatar_form": upload_avatar_form,
                "primary_avatar_form": primary_avatar_form,
                "next": next_override or _get_next(request),
            },
        ),
    )
Exemplo n.º 40
0
def change(request,
           extra_context=None,
           next_override=None,
           upload_form=UploadAvatarForm,
           primary_form=PrimaryAvatarForm,
           *args,
           **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
                                       user=request.user,
                                       avatars=avatars,
                                       **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(
                id=primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            invalidate_cache(request.user)
            messages.success(request, temp("成功更新头像"))
        if updated:
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
        return redirect(next_override or _get_next(request))

    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'primary_avatar_form': primary_avatar_form,
        'next': next_override or _get_next(request)
    }
    context.update(extra_context)
    return render(request, 'account/avatar/change.html', context)
Exemplo n.º 41
0
def add(request,
        extra_context=None,
        next_override=None,
        upload_form=UploadAvatarForm,
        *args,
        **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
                                     request.FILES or None,
                                     user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(user=request.user, primary=True)
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            messages.success(request, _("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)

            files_dict = {
                "files": [{
                    "name": image_file.name,
                    "size": len(image_file),
                    "url": avatar.avatar.url,
                    "thumbnailUrl": avatar.avatar.url,
                    "deleteUrl": "",
                    "deleteType": "DELETE"
                }]
            }
            return HttpResponse(json.dumps(files_dict),
                                content_type='application/json')
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add.html', context)
Exemplo n.º 42
0
def add(request,
        extra_context=None,
        next_override=None,
        upload_form=UploadAvatarForm,
        *args,
        **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    upload_avatar_form = upload_form(request.POST or None,
                                     request.FILES or None,
                                     user=request.user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user=request.user,
                primary=True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            request.user.message_set.create(
                message=_("Successfully uploaded a new avatar."))
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
            if notification:
                notification.send(
                    [request.user], "Picture_added", {
                        'message':
                        "You have successfully added new profile picture.",
                    })
            return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/add.html',
        extra_context,
        context_instance=RequestContext(
            request, {
                'avatar': avatar,
                'avatars': avatars,
                'upload_avatar_form': upload_avatar_form,
                'next': next_override or _get_next(request),
            }))
Exemplo n.º 43
0
def delete_avatar_for_user(request,
                           for_user=None,
                           extra_context=None,
                           next_override=None,
                           *args,
                           **kwargs):
    target_user = get_object_or_404(User, username=for_user, is_active=True)
    if not AVATAR_ADMIN_TEST(request, target_user):
        raise PermissionDenied
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(target_user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None,
                                          user=target_user,
                                          avatars=avatars)
    if request.method == 'POST':
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data['choices']
            if unicode(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if unicode(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar,
                                            user=target_user,
                                            avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            messages.add_message(
                request, messages.INFO,
                _("Successfully deleted the requested avatars"))
            return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'target_user': target_user,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add_for_user.html', context)
Exemplo n.º 44
0
 def form_valid(self, form):
     ids = form.cleaned_data['choices']
     new_avatar = None
     if unicode(self.avatar.id) in ids and self.avatars.count() > len(ids):
         # Find the next best avatar, and set it as the new primary
         for a in self.avatars:
             if unicode(a.id) not in ids:
                 a.primary = True
                 a.save()
                 new_avatar = a
                 avatar_updated.send(sender=Avatar, target=self.target,
                                     avatar=a)
                 break
     Avatar.objects.filter(id__in=ids).delete()
     self.request.user.message_set.create(
         message=_("Successfully deleted the requested avatars."))
     if self.request.is_ajax() or self.request.REQUEST.get('async', None) == 'true':
         return self.ajax_form_valid(new_avatar)
     else:
         return FormMixin.form_valid(self, form)
Exemplo n.º 45
0
def set_primary(request, user_id=None, extra_context=None, avatar_size=128):
    """changes default uploaded avatar"""
    user = get_object_or_404(User, pk=user_id)

    if request.method == "POST":
        updated = False
        form = PrimaryAvatarForm(request.POST,
                                 user=user,
                                 avatars=user.avatar_set.all())
        if 'choice' in request.POST and form.is_valid():
            avatar = Avatar.objects.get(id=form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            avatar_updated.send(sender=Avatar,
                                user=request.user,
                                avatar=avatar)
            user.avatar_type = 'a'
            user.clear_avatar_urls()
            user.save()
    return redirect_to_show_list(user_id)
Exemplo n.º 46
0
def change_avatar_for_user(request, for_user=None, extra_context=None,
        next_override=None,
        upload_form=UploadAvatarForm, primary_form=PrimaryAvatarForm,
        *args, **kwargs):
    target_user = get_object_or_404(User, username=for_user, is_active=True)
    if not AVATAR_ADMIN_TEST(request, target_user):
        raise PermissionDenied
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(target_user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=target_user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
        user=target_user, avatars=avatars, **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(id=
                primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            messages.add_message(request, messages.INFO, _("Successfully updated this user's avatars"))
        if updated:
            avatar_updated.send(sender=Avatar, user=target_user, avatar=avatar)
        return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/change_for_user.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar': avatar,
              'avatars': avatars,
              'target_user': target_user,
              'upload_avatar_form': upload_avatar_form,
              'primary_avatar_form': primary_avatar_form, }
        )
    )
Exemplo n.º 47
0
def change(request, extra_context=None, next_override=None,
        upload_form=UploadAvatarForm, primary_form=PrimaryAvatarForm,
        *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
        user=request.user, avatars=avatars, **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(id=
                primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            request.user.message_set.create(
                message=_("Successfully updated your avatar."))
        if updated:
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
            if notification:
                notification.send([request.user], "Picture_updated", {'message': "You have successfully updated your profile picture.",})
        return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/change.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar': avatar, 
              'avatars': avatars,
              'upload_avatar_form': upload_avatar_form,
              'primary_avatar_form': primary_avatar_form,
              'next': next_override or _get_next(request), }
        )
    )
Exemplo n.º 48
0
def delete(request, extra_context=None, next_override=None, *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    delete_avatar_form = DeleteAvatarForm(request.POST or None,
                                          user=request.user,
                                          avatars=avatars)
    if request.method == 'POST':
        if delete_avatar_form.is_valid():
            ids = delete_avatar_form.cleaned_data['choices']
            for a in avatars:
                if six.text_type(a.id) in ids:
                    avatar_deleted.send(sender=Avatar,
                                        user=request.user,
                                        avatar=a)
            if six.text_type(avatar.id) in ids and avatars.count() > len(ids):
                # Find the next best avatar, and set it as the new primary
                for a in avatars:
                    if six.text_type(a.id) not in ids:
                        a.primary = True
                        a.save()
                        avatar_updated.send(sender=Avatar,
                                            user=request.user,
                                            avatar=avatar)
                        break
            Avatar.objects.filter(id__in=ids).delete()
            messages.success(request,
                             _("Successfully deleted the requested avatars."))
            return redirect(next_override or _get_next(request))

    context = {
        'avatar': avatar,
        'avatars': avatars,
        'delete_avatar_form': delete_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    template_name = \
        settings.AVATAR_DELETE_TEMPLATE or 'avatar/confirm_delete.html'
    return render(request, template_name, context)
Exemplo n.º 49
0
    def update(self, instance, validated_data):
        request = self.context.get('request')

        profile = validated_data.get('profile', None)
        instance.username = validated_data.get('username', instance.username)
        instance.first_name = validated_data.get(
            'first_name', instance.first_name)
        if profile:
            new_track = profile.get('track')
            track = Track.objects.get(slug=new_track.slug)
            is_track_updated = instance.profile.track != new_track
            instance.profile.track = track
            instance.profile.last_opened_lesson = profile.get(
                'last_opened_lesson', instance.profile.last_opened_lesson)
            if is_track_updated:
                instance.profile.last_opened_lesson = None

        email = validated_data.get('email', None)
        if email and email != instance.email:
            adapter = get_adapter()
            adapter.send_mail('account/email/email_change', instance.email, {})
            email_address = EmailAddress.objects.get(
                user=instance, verified=True)
            email_address.change(request, email, True)
            instance.email = email

        if 'avatar' in request.FILES:
            avatar = Avatar(user=instance, primary=True)
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(sender=Avatar, user=instance, avatar=avatar)

        instance.save()

        # sync_sso(instance)

        return instance
Exemplo n.º 50
0
def add_avatar_for_user(request,
                        for_user=None,
                        extra_context=None,
                        next_override=None,
                        upload_form=UploadAvatarForm,
                        *args,
                        **kwargs):
    target_user = get_object_or_404(User, username=for_user, is_active=True)
    if not AVATAR_ADMIN_TEST(request, target_user):
        raise PermissionDenied
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(target_user)
    upload_avatar_form = upload_form(request.POST or None,
                                     request.FILES or None,
                                     user=target_user)
    if request.method == "POST" and 'avatar' in request.FILES:
        if upload_avatar_form.is_valid():
            avatar = Avatar(
                user=target_user,
                primary=True,
            )
            image_file = request.FILES['avatar']
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            messages.add_message(request, messages.INFO,
                                 _("Successfully uploaded a new avatar"))
            avatar_updated.send(sender=Avatar, user=target_user, avatar=avatar)
            return redirect(next_override or _get_next(request))
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'target_user': target_user,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override or _get_next(request),
    }
    context.update(extra_context)
    return render(request, 'avatar/add_for_user.html', context)
Exemplo n.º 51
0
def delete_avatar(request):
    '''
    Delete avatar image
    '''
    response = {}
    status = 405
    avatar, avatars = avatarviews._get_avatars(request.user)
    if avatar is None:
        response['error'] = 'User has no avatar'
    else:
        aid = avatar.id
        for a in avatars:
            if a.id == aid:
                a.primary = True
                a.save()
                avatar_updated.send(sender=Avatar,
                                    user=request.user,
                                    avatar=avatar)
                break
        Avatar.objects.filter(pk=aid).delete()
        response['avatar'] = userutil.get_user_avatar_url(request.user)['url']
        status = 200
    return JsonResponse(response, status=status)
Exemplo n.º 52
0
def upload_avatar(request):
    """
    Upload avatar image
    """
    response = {}
    status = 405

    avatar, avatars = avatarviews._get_avatars(request.user)
    upload_avatar_form = UploadAvatarForm(None,
                                          request.FILES,
                                          user=request.user)
    if upload_avatar_form.is_valid():
        avatar = Avatar(
            user=request.user,
            primary=True,
        )
        image_file = request.FILES["avatar"]
        avatar.avatar.save(image_file.name, image_file)
        avatar.save()
        avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        response["avatar"] = request.user.avatar_url["url"]
        status = 200
    return JsonResponse(response, status=status)
Exemplo n.º 53
0
def delete_avatar(request):
    """
    Delete avatar image
    """
    response = {}
    status = 405
    avatar, avatars = avatarviews._get_avatars(request.user)
    if avatar is None:
        response["error"] = "User has no avatar"
    else:
        aid = avatar.id
        for a in avatars:
            if a.id == aid:
                a.primary = True
                a.save()
                avatar_updated.send(sender=Avatar,
                                    user=request.user,
                                    avatar=avatar)
                break
        Avatar.objects.filter(pk=aid).delete()
        response["avatar"] = request.user.avatar_url["url"]
        status = 200
    return JsonResponse(response, status=status)
Exemplo n.º 54
0
    def save_avatar(self, instance):
        keys = self.initial_data.keys()
        if 'avatar' in keys and \
            (self.avatar is not None or self.initial_data.get('avatar') is None):
            avatar = instance.user.avatar_set.filter(primary=True).first()
            if avatar is not None:
                avatar.primary = False
                avatar.save()
                return avatar_updated.send(sender=Avatar,
                                           user=instance.user,
                                           avatar=avatar)
        elif self.avatar is None:
            return None

        if self.avatar:
            avatar = Avatar(user=instance.user, primary=True)
            image_file = self.avatar
            avatar.avatar.save(image_file.name, image_file)
            avatar.save()
            avatar_updated.send(sender=Avatar,
                                user=instance.user,
                                avatar=avatar)
            return avatar
Exemplo n.º 55
0
def change(request, extra_context=None, next_override=None,
        upload_form=UploadAvatarForm, primary_form=PrimaryAvatarForm,
        *args, **kwargs):
    if extra_context is None:
        extra_context = {}
    avatar, avatars = _get_avatars(request.user)
    if avatar:
        kwargs = {'initial': {'choice': avatar.id}}
    else:
        kwargs = {}
    upload_avatar_form = upload_form(user=request.user, **kwargs)
    primary_avatar_form = primary_form(request.POST or None,
        user=request.user, avatars=avatars, **kwargs)
    if request.method == "POST":
        updated = False
        if 'choice' in request.POST and primary_avatar_form.is_valid():
            avatar = Avatar.objects.get(id=
                primary_avatar_form.cleaned_data['choice'])
            avatar.primary = True
            avatar.save()
            updated = True
            messages.add_message(request, messages.INFO, "Votre photo a été mise à jour avec succès")
        if updated:
            avatar_updated.send(sender=Avatar, user=request.user, avatar=avatar)
        return HttpResponseRedirect(next_override or _get_next(request))
    return render_to_response(
        'avatar/change.html',
        extra_context,
        context_instance = RequestContext(
            request,
            { 'avatar': avatar, 
              'avatars': avatars,
              'upload_avatar_form': upload_avatar_form,
              'primary_avatar_form': primary_avatar_form,
              'next': next_override or _get_next(request), }
        )
    )
Exemplo n.º 56
0
 def save_model(self, request, obj, form, change):
     super(AvatarAdmin, self).save_model(request, obj, form, change)
     avatar_updated.send(sender=Avatar, user=request.user, avatar=obj)
Exemplo n.º 57
0
 def update_picture(self, filename, file_content):
     remove_avatar_thumbnails(instance=self)
     self.avatar.save(filename, file_content)
     self.save()
     avatar_updated.send(sender=Avatar, user=self.user, avatar=self)