Пример #1
0
def add_folder(folder, gallery=None):
    """
    Scan media upload folder and add any missing gallery items.
    """

    if not os.path.isdir(folder):
        return _("Provided folder is not a valid folder.")

    if gallery:
        try:
            gallery = Gallery.objects.get(title=gallery)
        except Gallery.DoesNotExist:
            return _("Provided gallery title does not exists.")

    def read_file(filename):
        try:
            full = os.path.join(folder, filename)
            with open(full, 'rb') as f:
                return f.read()
        except:
            return None
        
    for filename in os.listdir(folder):
        full = os.path.join(folder, filename)
        if not os.path.isfile(full):
            continue
        file_mtime = datetime.fromtimestamp(os.path.getmtime(full))
        if not is_aware(file_mtime):
            file_mtime = make_aware(file_mtime, get_current_timezone())
        with open(full, 'rb') as f:
            upload_file(filename, filename, f, file_mtime, read_file, None, gallery)
    return _('Done')
Пример #2
0
    def process_zipfile(self):
        from photologue.utils.upload import upload_file

        if path.isfile(self.zip_file.path):
            try:
                zip = zipfile.ZipFile(self.zip_file.path)
                bad_file = zip.testzip()
            except:
                bad_file = self.zip_file.path
            if bad_file:
                raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)

            parsed_tags = parse_tags(self.tags)
            if self.gallery:
                gallery = self.gallery
                self.title = gallery.title
            else:
                gallery = Gallery.objects.create(
                    title=self.title,
                    title_slug=slugify(self.title),
                    description=self.description,
                    is_public=self.is_public,
                )
                gallery.save()
            gallery.tags.add(*parsed_tags)
            count = 0

            def read_file(fn):
                try:
                    return zip.read(fn)
                except:
                    return None

            for filename in sorted(zip.namelist()):
                data = zip.read(filename)
                file_mtime = zip.getinfo(filename).date_time
                file_mtime = make_aware(datetime(*file_mtime), get_current_timezone())
                if self.use_title:
                    name = os.path.basename(filename)
                    namebase, ext = os.path.splitext(name)
                    name = "".join([self.title, ext])
                else:
                    name = filename
                count = upload_file(
                    name,
                    filename,
                    data,
                    file_mtime,
                    read_file,
                    parsed_tags,
                    gallery,
                    self.caption,
                    self.is_public,
                    count,
                )
                if not self.use_title:
                    count = 0
            zip.close()