def do_import(annotations, gallery, commenter):
    sys.stderr.write("Importing {} images to gallery '{}'\n"
                     .format(len(annotations), gallery.title))

    ndigits = len(str(len(annotations)-1))

    comment_timestamp = timezone.make_aware(datetime(2015, 10, 4, 20, 0),
                                            timezone.get_fixed_timezone(-240))

    for seq, ann in enumerate(annotations):
        sys.stderr.write(ann.fname + "\n")
        img = GalleryImage(gallery      = gallery,
                           sort_order   = seq,
                           date         = ann.date,
                           notes        = ann.desc)
        img.save()

        if ann.photog: img.photographer.set(ann.photog)
        if ann.loc:    img.location.set(ann.loc)
        if ann.people: img.people.set(*ann.people)
        if ann.tags:   img.tags.set(*ann.tags)

        ifile = ImageFile(open(ann.fname, 'rb'))
        img.image.save("I{1:0{0}}.jpg".format(ndigits, seq),
                       ifile)
        img.save()

        if ann.comment:
            comment = GalleryComment(image   = img,
                                     user    = commenter,
                                     date    = comment_timestamp,
                                     comment = ann.comment)
            comment.save()
示例#2
0
    def test_gallery_item_ajax_galleriffic(self):
        # create objects
        gallery = Gallery()
        gallery.save()
        gi_obj = GalleryImage(gallery=gallery, state='published')
        gi_obj.save()
        gi_obj.sites.add(Site.objects.get_current())
        ve_obj = VideoEmbed(gallery=gallery, state='published')
        ve_obj.save()
        ve_obj.sites.add(Site.objects.get_current())
        vf_obj = VideoFile(gallery=gallery, state='published', file='test.flv')
        vf_obj.save()
        vf_obj.sites.add(Site.objects.get_current())

        # raise 404 on invalid slug
        self.assertRaises(Http404, views.gallery_item_ajax_galleriffic, request=None, slug='invalid_slug')

        # use galleryimage template for gallery image object
        client = Client()
        response = client.get(reverse('gallery_item_ajax_galleriffic', kwargs={'slug': gi_obj.slug}))
        self.assertTemplateUsed(response, 'gallery/ajax/galleriffic_galleryimage.html')
        
        # use videoembed template for video embed object
        response = client.get(reverse('gallery_item_ajax_galleriffic', kwargs={'slug': ve_obj.slug}))
        self.assertTemplateUsed(response, 'gallery/ajax/galleriffic_videoembed.html')
        
        # use videofile template for video file object
        response = client.get(reverse('gallery_item_ajax_galleriffic', kwargs={'slug': vf_obj.slug}))
        self.assertTemplateUsed(response, 'gallery/ajax/galleriffic_videofile.html')
示例#3
0
    def save(self, commit=True):
        images = []
        if self.cleaned_data['files']:
            for item in self.cleaned_data['files']:
                item.seek(0)
                to_add = []

                # Zip file?
                itemfp = StringIO(item.read())
                item.seek(0)
                try:
                    zfp = zipfile.ZipFile(itemfp, 'r')
                except:
                    # zipfile does not raise a specific exception
                    to_add.append(item)
                else:    
                    if not zfp.testzip():
                        # Running into issues using streams, so use temp files
                        tmpdir = mkdtemp()
                        for filename in sorted(zfp.namelist()):
                            tmpfile = os.path.join(tmpdir, filename)
                            data = zfp.read(filename)
                            fp = open(tmpfile, 'wb')
                            fp.write(data)
                            fp.close()
                            afile = File(open(tmpfile), 'rb')
                            afile.name = filename
                            to_add.append(afile)
                    else:                    
                        to_add.append(item)

                for afile in to_add:                    
                    obj = GalleryImage(title=afile.name, gallery=self.gallery)
                    obj.image = afile
                    obj.save()
                    obj.sites = list(self.gallery.sites.all())
                    obj.save()
                    images.append(obj)

        return images