Exemplo n.º 1
0
def update_avatar(request):
    """
    Update avatar from gallery.
    Specific method instead using settings_profile() to avoid to handle all required fields.
    """
    profile = request.user.profile
    form = ImageAsAvatarForm(request.POST)
    if form.is_valid():
        profile.avatar_url = form.data["avatar_url"]
        try:
            profile.save()
        except:
            messages.error(request, "Une erreur est survenue.")
            return redirect(reverse("zds.member.views.settings_profile"))
        messages.success(request, "L'avatar a correctement été mis à jour.")

    return redirect(reverse("zds.member.views.details",
                            args=[profile.user.username]))
Exemplo n.º 2
0
def update_avatar(request):
    """
    Update avatar from gallery.
    Specific method instead using settings_profile() to avoid to handle all required fields.
    """
    profile = request.user.profile
    form = ImageAsAvatarForm(request.POST)
    if form.is_valid():
        profile.avatar_url = form.data["avatar_url"]
        try:
            profile.save()
        except:
            messages.error(request, "Une erreur est survenue.")
            return redirect(reverse("zds.member.views.settings_profile"))
        messages.success(request, "L'avatar a correctement été mis à jour.")

    return redirect(
        reverse("zds.member.views.details", args=[profile.user.username]))
Exemplo n.º 3
0
    def test_image_as_avatar_form(self):
        form = ImageAsAvatarForm()

        self.assertFalse(form.is_bound)
Exemplo n.º 4
0
    def get_context_data(self, **kwargs):
        context = super(EditImage, self).get_context_data(**kwargs)

        context['as_avatar_form'] = ImageAsAvatarForm()

        return context
Exemplo n.º 5
0
def edit_image(request, gal_pk, img_pk):
    """Edit an existing image."""

    gal = get_object_or_404(Gallery, pk=gal_pk)
    img = get_object_or_404(Image, pk=img_pk)

    # Check if user can edit image
    try:
        permission = UserGallery.objects.get(user=request.user, gallery=gal)
        if permission.mode != 'W':
            raise PermissionDenied
    except:
        raise PermissionDenied

    # Check if the image belong to the gallery
    if img.gallery != gal:
        raise PermissionDenied

    if request.method == "POST":
        form = UpdateImageForm(request.POST, request.FILES)
        if form.is_valid():
            if "physical" in request.FILES:
                if request.FILES["physical"].size > settings.IMAGE_MAX_SIZE:
                    messages.error(
                        request,
                        "Votre image est beaucoup trop lourde, réduisez sa taille à moins de {} \
                    <abbr title=\"kibioctet\">Kio</abbr> avant de l'envoyer".
                        format(str(settings.IMAGE_MAX_SIZE / 1024)))
                else:
                    img.title = request.POST["title"]
                    img.legend = request.POST["legend"]
                    img.physical = request.FILES["physical"]
                    img.slug = slugify(request.FILES["physical"])
                    img.update = datetime.now()
                    img.save()
                    # Redirect to the newly uploaded image edit page after POST
                    return redirect(
                        reverse("zds.gallery.views.edit_image",
                                args=[gal.pk, img.pk]))
            else:
                img.title = request.POST["title"]
                img.legend = request.POST["legend"]
                img.update = datetime.now()
                img.save()
                # Redirect to the newly uploaded image edit page after POST
                return redirect(
                    reverse("zds.gallery.views.edit_image",
                            args=[gal.pk, img.pk]))

    else:
        form = UpdateImageForm(
            initial={
                "title": img.title,
                "legend": img.legend,
                "physical": img.physical,
                "new_image": False,
            })

    as_avatar_form = ImageAsAvatarForm()
    return render_template(
        "gallery/image/edit.html", {
            "form": form,
            "as_avatar_form": as_avatar_form,
            "gallery": gal,
            "image": img
        })
Exemplo n.º 6
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        context["as_avatar_form"] = ImageAsAvatarForm()
        context["image"] = self.image
        return context