示例#1
0
def new_image(request, gal_pk):
    '''
    Creates a new image
    '''
    gal = get_object_or_404(Gallery, pk=gal_pk)

    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid() \
           and request.FILES['physical'].size < settings.IMAGE_MAX_SIZE:
            img = Image()
            img.physical = request.FILES['physical']
            img.gallery = gal
            img.title = request.POST['title']
            img.slug = slugify(request.FILES['physical'])
            img.legend = request.POST['legend']
            img.pubdate = datetime.now()

            img.save()

            # Redirect to the document list after POST
            return redirect(gal.get_absolute_url())
        else:
            # TODO: add errors to the form and return it
            raise Http404
    else:
        form = ImageForm()  # A empty, unbound form
        return render_template('gallery/new_image.html', {
            'form': form,
            'gallery': gal
        })
示例#2
0
def edit_image(request, gal_pk, img_pk):
    '''
    Creates a new image
    '''
    gal = get_object_or_404(Gallery, pk=gal_pk)
    img = get_object_or_404(Image, pk=img_pk)

    if request.method == 'POST':
        form = ImageForm(request.POST)
        if form.is_valid():
            img.title = request.POST['title']
            img.legend = request.POST['legend']
            img.update = datetime.now()

            img.save()

            # Redirect to the document list after POST
            return redirect(gal.get_absolute_url())
        else:
            # TODO: add errors to the form and return it
            raise Http404
    else:
        form = ImageForm()  # A empty, unbound form
        return render_template('gallery/edit_image.html', {
            'form': form,
            'gallery': gal,
            'image': img
        })
示例#3
0
文件: views.py 项目: Zikkee/zds-site
def new_image(request, gal_pk):
    """Creates a new image."""

    gal = get_object_or_404(Gallery, pk=gal_pk)

    # check if the user can upload new image in this gallery
    try:
        gal_mode = UserGallery.objects.get(gallery=gal, user=request.user)
        if gal_mode.mode != 'W':
            raise PermissionDenied
    except:
        raise PermissionDenied

    if request.method == "POST":
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid() and request.FILES["physical"].size < settings.IMAGE_MAX_SIZE:
            img = Image()
            img.physical = request.FILES["physical"]
            img.gallery = gal
            img.title = request.POST["title"]
            img.slug = slugify(request.FILES["physical"])
            img.legend = request.POST["legend"]
            img.pubdate = 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:
            return render_template("gallery/image/new.html", {"form": form,
                                                              "gallery": gal})
    else:
        form = ImageForm(initial={"new_image": True})  # A empty, unbound form
        return render_template("gallery/image/new.html", {"form": form,
                                                          "gallery": gal})
示例#4
0
    def test_empty_pic_image_form(self):

        data = {
            'title': 'Test Title',
            'legend': 'Test Legend',
        }

        files = {'physical': ''}
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
示例#5
0
    def test_empty_pic_image_form(self):

        data = {
            "title": "Test Title",
            "legend": "Test Legend",
        }

        files = {"physical": ""}
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
示例#6
0
文件: views.py 项目: Aer-o/zds-site
def edit_image(request, gal_pk, img_pk):
    """Creates a new 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 = ImageForm(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éduidez sa taille à moins de {} avant de l'envoyer".format(str(settings.IMAGE_MAX_SIZE/1024*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 document list after POST
                    return redirect(gal.get_absolute_url())
            else:
                img.title = request.POST["title"]
                img.legend = request.POST["legend"]
                img.update = datetime.now()
                img.save()

                # Redirect to the document list after POST
                return redirect(gal.get_absolute_url())
    else:
        form = ImageForm(initial={"title": img.title, "legend": img.legend, "physical": img.physical})
    
    as_avatar_form = ImageAsAvatarForm()
    return render_template(
        "gallery/image/edit.html", {
            "form": form,
            "as_avatar_form": as_avatar_form,
            "gallery": gal,
            "image": img
        })
示例#7
0
    def test_empty_title_image_form(self):
        upload_file = (settings.BASE_DIR / "fixtures" / "logo.png").open("rb")

        data = {
            "title": "",
            "legend": "Test Legend",
        }

        files = {"physical": SimpleUploadedFile(upload_file.name, upload_file.read())}
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
        upload_file.close()
示例#8
0
    def test_empty_pic_image_form(self):

        data = {
            'title': 'Test Title',
            'legend': 'Test Legend',
        }

        files = {
            'physical': ''
        }
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
示例#9
0
    def test_empty_title_image_form(self):
        upload_file = open(os.path.join(settings.SITE_ROOT, 'fixtures', 'logo.png'), 'r')

        data = {
            'title': '',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
        upload_file.close()
示例#10
0
    def test_empty_title_image_form(self):
        upload_file = open(os.path.join(settings.BASE_DIR, 'fixtures', 'logo.png'), 'rb')

        data = {
            'title': '',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
        upload_file.close()
示例#11
0
    def test_valid_image_form(self):
        upload_file = open(os.path.join(settings.BASE_DIR, 'fixtures', 'logo.png'), 'rb')

        data = {
            'title': 'Test Image',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        form = ImageForm(data, files)

        self.assertTrue(form.is_valid())
        upload_file.close()
示例#12
0
    def test_valid_image_form(self):
        upload_file = open(os.path.join(settings.SITE_ROOT, 'fixtures', 'logo.png'), 'r')

        data = {
            'title': 'Test Image',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        form = ImageForm(data, files)

        self.assertTrue(form.is_valid())
        upload_file.close()
示例#13
0
    def test_empty_title_image_form(self):
        upload_file = (settings.BASE_DIR / 'fixtures' / 'logo.png').open('rb')

        data = {
            'title': '',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
        upload_file.close()
示例#14
0
    def test_bot_a_pic_image_form(self):
        upload_file = open(
            os.path.join(settings.BASE_DIR, 'fixtures', 'forums.yaml'), 'r')

        data = {
            'title': 'Test Title',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name,
                                           upload_file.read())
        }
        form = ImageForm(data, files)

        self.assertFalse(form.is_valid())
        upload_file.close()
示例#15
0
    def test_too_long_legend_image_form(self):
        upload_file = (settings.BASE_DIR / "fixtures" / "logo.png").open("rb")

        data = {
            "title": "Test Title",
            "legend": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam condimentum enim amet.",
        }

        files = {"physical": SimpleUploadedFile(upload_file.name, upload_file.read())}
        ImageForm(data, files)
        upload_file.close()
示例#16
0
    def test_too_long_title_image_form(self):
        upload_file = open(os.path.join(settings.SITE_ROOT, 'fixtures', 'logo.png'), 'r')

        data = {
            'title': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam condimentum enim amet.',
            'legend': 'Test Legend',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        ImageForm(data, files)
        upload_file.close()
示例#17
0
    def test_too_long_legend_image_form(self):
        upload_file = (settings.BASE_DIR / 'fixtures' / 'logo.png').open('rb')

        data = {
            'title': 'Test Title',
            'legend': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam condimentum enim amet.',
        }

        files = {
            'physical': SimpleUploadedFile(upload_file.name, upload_file.read())
        }
        ImageForm(data, files)
        upload_file.close()
示例#18
0
def new_image(request, gal_pk):
    """Creates a new image."""

    gal = get_object_or_404(Gallery, pk=gal_pk)

    # check if the user can upload new image in this gallery
    try:
        gal_mode = UserGallery.objects.get(gallery=gal, user=request.user)
        if gal_mode.mode != 'W':
            raise PermissionDenied
    except:
        raise PermissionDenied

    if request.method == "POST":
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            img = Image()
            img.physical = request.FILES["physical"]
            img.gallery = gal
            img.title = request.POST["title"]
            img.slug = slugify(request.FILES["physical"])
            img.legend = request.POST["legend"]
            img.pubdate = 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:
            return render_template("gallery/image/new.html", {
                "form": form,
                "gallery": gal
            })
    else:
        form = ImageForm(initial={"new_image": True})  # A empty, unbound form
        return render_template("gallery/image/new.html", {
            "form": form,
            "gallery": gal
        })