Ejemplo n.º 1
0
 def create_thumbnail(self, size, quality=None):
     # invalidate the cache of the thumbnail with the given size first
     invalidate_cache(self.user, size)
     try:
         orig = self.avatar.storage.open(self.avatar.name, 'rb')
         image = Image.open(orig)
         image = self.transpose_image(image)
         quality = quality or settings.AVATAR_THUMB_QUALITY
         w, h = image.size
         if w != size or h != size:
             if w > h:
                 diff = int((w - h) / 2)
                 image = image.crop((diff, 0, w - diff, h))
             else:
                 diff = int((h - w) / 2)
                 image = image.crop((0, diff, w, h - diff))
             if image.mode not in ("RGB", "RGBA"):
                 image = image.convert("RGB")
             image = image.resize((size, size),
                                  settings.AVATAR_RESIZE_METHOD)
             thumb = six.BytesIO()
             image.save(thumb,
                        settings.AVATAR_THUMB_FORMAT,
                        quality=quality)
             thumb_file = ContentFile(thumb.getvalue())
         else:
             thumb_file = File(orig)
         thumb = self.avatar.storage.save(self.avatar_name(size),
                                          thumb_file)
     except IOError:
         return  # What should we do here?  Render a "sorry, didn't work" img?
Ejemplo n.º 2
0
 def create_thumbnail(self, size, quality=None):
     # invalidate the cache of the thumbnail with the given size first
     invalidate_cache(self.user, size)
     try:
         orig = self.avatar.storage.open(self.avatar.name, 'rb')
         image = Image.open(orig)
         quality = quality or settings.AVATAR_THUMB_QUALITY
         w, h = image.size
         if w != size or h != size:
             if w > h:
                 diff = int((w - h) / 2)
                 image = image.crop((diff, 0, w - diff, h))
             else:
                 diff = int((h - w) / 2)
                 image = image.crop((0, diff, w, h - diff))
             if image.mode not in ("RGB", "RGBA"):
                 image = image.convert("RGB")
             image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD)
             thumb = six.BytesIO()
             image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality)
             thumb_file = ContentFile(thumb.getvalue())
         else:
             thumb_file = File(orig)
         thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
     except IOError:
         return  # What should we do here?  Render a "sorry, didn't work" img?
Ejemplo n.º 3
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.html')
    context = {
        'avatar': avatar,
        'avatars': avatars,
        'upload_avatar_form': upload_avatar_form,
        'next': next_override,
    }
    context.update(extra_context)
    template_name = 'auth/avatar_add.html'
    return render(request, template_name, context)
Ejemplo n.º 4
0
 def create_thumbnail(self, size, quality=None):
     # invalidate the cache of the thumbnail with the given size first
     invalidate_cache(self.user, size)
     try:
         orig = self.avatar.storage.open(self.avatar.name, 'rb')
         image = Image.open(orig)
         image = self.transpose_image(image)
     except IOError:
         import traceback
         traceback.print_exc()
         # TODO: What should we do here?  Render a "sorry, didn't work" img?
         return
     quality = quality or settings.AVATAR_THUMB_QUALITY
     (w, h) = image.size
     if w != size or h != size:
         if w > h:
             diff = (w - h) / 2
             image = image.crop((diff, 0, w - diff, h))
         else:
             diff = (h - w) / 2
             image = image.crop((0, diff, w, h - diff))
         if image.mode != "RGB":
             image = image.convert("RGB")
         image = image.resize((size, size), settings.AVATAR_RESIZE_METHOD)
         thumb = io.BytesIO()
         image.save(thumb, settings.AVATAR_THUMB_FORMAT, quality=quality)
         thumb_file = ContentFile(thumb.getvalue())
     else:
         if hasattr(orig, 'read'):
             orig = orig.read()
         thumb_file = ContentFile(orig)
     thumb = self.avatar.storage.save(self.avatar_name(size), thumb_file)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
    def create_thumbnail(self, size, quality=None):
        # invalidate the cache of the thumbnail with the given size first
        invalidate_cache(self.user, size)
        try:
            orig = self.avatar.storage.open(self.avatar.name, 'rb')
            image = Image.open(orig)
            image = self.transpose_image(image)
            quality = quality or settings.AVATAR_THUMB_QUALITY
            w, h = image.size
            if w != size or h != size:
                if w > h:
                    diff = int((w - h) / 2)
                    image = image.crop((diff, 0, w - diff, h))
                else:
                    diff = int((h - w) / 2)
                    image = image.crop((0, diff, w, h - diff))
                if image.mode in ('RGBA', 'LA'):
                    old_mode = image.mode

                    alpha = image.getchannel('A')
                    image = image.convert(old_mode[:-1]) \
                        .convert('P', palette=Image.ADAPTIVE, colors=255)
                    mask = Image.eval(alpha, lambda px: 255 if px < 128 else 0)
                    image.paste(255, mask)
                    image.info['transparency'] = 255

                if image.mode not in ("RGB", "RGBA"):
                    image = image.convert("RGB")
                image = image.resize((size, size),
                                     settings.AVATAR_RESIZE_METHOD)
                thumb = six.BytesIO()
                image.save(thumb,
                           settings.AVATAR_THUMB_FORMAT,
                           quality=quality)
                thumb_file = ContentFile(thumb.getvalue())
            else:
                thumb_file = File(orig)
            thumb = self.avatar.storage.save(self.avatar_name(size),
                                             thumb_file)
        except IOError as error:
            print("ERROR: ", error)
Ejemplo n.º 9
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, _("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)
Ejemplo n.º 10
0
def invalidate_avatar_cache(sender, instance, **kwargs):
    invalidate_cache(instance.user)
Ejemplo n.º 11
0
def invalidate_avatar_cache(sender, instance, **kwargs):
    invalidate_cache(instance.user)