def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: form = AvatarCropForm(request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) image = Image.open(avatar.image.path) box = [ left, top, right, bottom ] image = image.crop(box) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.save(avatar.image.path) avatar.valid = True avatar.save() return HttpResponseRedirect(reverse("profile_avatar_crop_done")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, } return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width-height) / 2 left = diff right = width-diff bottom = height else: diff = (height-width) / 2 top = diff right = width bottom = height-diff box = [ left, top, right, bottom ] image = image.crop(box) if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, image.format) f.seek(0) avatar.image.delete() file_ext = image.content_type.split("/")[1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() messages.success(request, _("Your new avatar has been saved successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, \ 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get("top")) left = int(form.cleaned_data.get("left")) right = int(form.cleaned_data.get("right")) bottom = int(form.cleaned_data.get("bottom")) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width - height) / 2 left = diff right = width - diff bottom = height else: diff = (height - width) / 2 top = diff right = width bottom = height - diff box = [left, top, right, bottom] image = image.crop(box) if image.mode not in ("L", "RGB"): image = image.convert("RGB") if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, "JPEG") f.seek(0) avatar.image.delete() avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() request.user.message_set.create(message=_("Your new avatar has been saved successfully.")) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = {"section": "avatar", "avatar": avatar, "form": form} signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width-height) / 2 left = diff right = width-diff bottom = height else: diff = (height-width) / 2 top = diff right = width bottom = height-diff box = [ left, top, right, bottom ] image = image.crop(box) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, "JPEG") f.seek(0) avatar.image.delete() avatar.image.save("%s.jpg" % request.user.username, ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() request.user.message_set.create(message=_("Your new avatar has been saved successfully.")) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: form = AvatarCropForm(request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) image = Image.open(avatar.image.path) box = [left, top, right, bottom] image = image.crop(box) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.save(avatar.image.path) avatar.valid = True avatar.save() messages.add_message(request, messages.SUCCESS, 'Avatar set') return HttpResponseRedirect(reverse("profile_avatar_crop_done")) else: messages.add_message(request, messages.ERROR, 'Avatar not set. Form is invalid.') template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, } return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get('top')) left = int(form.cleaned_data.get('left')) right = int(form.cleaned_data.get('right')) bottom = int(form.cleaned_data.get('bottom')) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width - height) / 2 left = diff right = width - diff bottom = height else: diff = (height - width) / 2 top = diff right = width bottom = height - diff box = [left, top, right, bottom] image = image.crop(box) if hasattr(settings, "AWS_SECRET_ACCESS_KEY"): f = StringIO() image.save(f, image.format) f.seek(0) avatar.image.delete() file_ext = image.content_type.split("/")[ 1] # "image/gif" => "gif" if file_ext == 'pjpeg': file_ext = 'jpeg' avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) else: image.save(avatar.image.path) avatar.valid = True avatar.save() messages.success(request, _("Your new avatar has been saved successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response( signal_responses) or HttpResponseRedirect( reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { 'section': 'avatar', 'avatar': avatar, 'form': form, \ 'DEFAULT_AVATAR_SIZE': DEFAULT_AVATAR_SIZE, 'MIN_AVATAR_SIZE': MIN_AVATAR_SIZE } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))
def avatarcrop(request): """ Avatar management """ avatar = get_object_or_404(Avatar, user=request.user, valid=False) if not request.method == "POST": form = AvatarCropForm() else: image = Image.open(ContentFile(avatar.image.read())) if image.mode != "RGB": image = image.convert("RGB") form = AvatarCropForm(image, request.POST) if form.is_valid(): top = int(form.cleaned_data.get("top")) left = int(form.cleaned_data.get("left")) right = int(form.cleaned_data.get("right")) bottom = int(form.cleaned_data.get("bottom")) if not (top or left or right or bottom): (width, height) = image.size if width > height: diff = (width - height) / 2 left = diff right = width - diff bottom = height else: diff = (height - width) / 2 top = diff right = width bottom = height - diff box = [left, top, right, bottom] image = image.crop(box) for a in Avatar.objects.filter(user=request.user).exclude(id=avatar.id): a.delete() f = File(StringIO(), name=avatar.image.name) # need if format is empty image.save(f, image.format) f.seek(0) if hasattr(image, "content_type"): file_ext = image.content_type.split("/")[1] # "image/gif" => "gif" else: file_ext = os.path.splitext(avatar.image.name)[1][1:] if file_ext == "pjpeg": file_ext = "jpeg" avatar.image.delete() avatar.image.save("%s.%s" % (request.user.username, file_ext), ContentFile(f.read())) avatar.valid = True avatar.save() messages.success(request, _("Your new avatar has been saved successfully."), fail_silently=True) signal_responses = signals.post_signal.send(sender=avatarcrop, request=request, form=form) return signals.last_response(signal_responses) or HttpResponseRedirect(reverse("profile_edit_avatar")) template = "userprofile/avatar/crop.html" data = { "section": "avatar", "avatar": avatar, "form": form, "DEFAULT_AVATAR_SIZE": DEFAULT_AVATAR_SIZE, "MIN_AVATAR_SIZE": MIN_AVATAR_SIZE, } signals.context_signal.send(sender=avatarcrop, request=request, context=data) return render_to_response(template, data, context_instance=RequestContext(request))