def recreate_photodb(self): self._recreate_thumbs() self._recreate_default_tags() self._delete_photos_from_db() self._fill_photo_list() for photo in self._photos: # Save path of photo and thumbnail without 'static' prefix full_path = photo[len('static/'):] thumbnail = path.join(path.dirname(photo), self._thumbs_dirname, path.basename(photo))[len('static/'):] name = path.basename(photo) create_date = timezone.now() # TODO: add exception (if tag not found) default_tag = Tag.objects.get(name=path.basename(path.dirname(photo))) photo_item = Photo() photo_item.name = name photo_item.full_path = full_path photo_item.thumbnail = thumbnail photo_item.create_date = create_date photo_item.is_album_orientation = self.is_album_orientation(photo) photo_item.save() photo_item.tags.add(default_tag)
def restore_album(d, s): adict = yaml.load(open(os.path.join(d, "info.yaml"))) a = Album() a.id = adict["id"] a.display_name = adict["display_name"] a.created = adict["created"] a.preview_id = adict["preview_id"] a.descr = adict["descr"] a.hidden = adict["hidden"] a.sort_by = adict["sort_by"] s.add(a) s.commit() for pdict in adict["photos"]: pfile = pdict["name"] if pfile in ["previews", "info.yaml"]: continue ppath = os.path.join(d, pfile) if os.path.isdir(ppath): continue p = Photo() p.id = pdict["id"] p.album_id = a.id p.name = pdict["name"] p.display_name = pdict["display_name"] p.created = pdict["created"] p.width = pdict["width"] p.height = pdict["height"] p.hidden = pdict["hidden"] photo_name = os.path.basename(p.get_path()) preview_name = os.path.basename(p.get_preview_path()) shutil.copy(os.path.join(d, photo_name), p.get_path()) shutil.copy(os.path.join(d, "previews", preview_name), p.get_preview_path()) s.add(p) for afile in os.listdir(d): if afile == "previews": continue apath = os.path.join(d, afile) if not os.path.isdir(apath): continue restore_album(apath, s)
src = os.path.join(path, f) fh = open(src) h = hashlib.sha1() h.update(fh.read()) image_hash = h.hexdigest() fh.close() try: photo = Photo.objects.get(image_hash=image_hash) except Photo.DoesNotExist: photo = Photo(image_hash=image_hash) if not photo.image: # new photo dst_relative = os.path.join(photo.image.field.upload_to, f) dst = os.path.join(photo.image.storage.location, dst_relative) shutil.copy2(src, dst) fn, fe = os.path.splitext(os.path.basename(dst)) photo.image = dst_relative photo.gallery = gallery photo.published_at = datetime.now() photo.name = fn photo.save() if gallery_created: # new gallery gallery.name = gallery_name gallery.cover = photo gallery.save() order += 1