Пример #1
0
def album_upload():
    form = AlbumUploadForm()
    albums = Album.query.filter_by(user_id=session.get("user_id")).all()
    form.album_title.choices = [(item.id, item.title) for item in albums]
    if len(form.album_title.choices) < 1:
        flash(message="请先创建一个相册!再上传照片", category='err')
        return redirect(url_for("album_create"))
    if request.method == "POST":
        # 通过 files.getlist() 获得上传的 FileStorage 文件对象列表
        fses = request.files.getlist("album_upload")
        # 检查文件扩展名,将合格的文件过滤出来
        valid_fses = check_filestorages_extension(fses, ALLOWED_IMAGE_EXTENSIONS)
        if len(valid_fses) < 1:
            flash(message="只允许上传文件类型:" + str(ALLOWED_IMAGE_EXTENSIONS), category='err')
            return redirect(url_for("album_upload"))
        else:
            files_url = []
            album_cover = ''
            # 开始遍历保存每一个合格文件
            for fs in valid_fses:
                album_title = ''
                for id, title in form.album_title.choices:
                    if id == form.album_title.data:
                        album_title = title
                folder = session.get("user_name") + '/' + album_title
                name_orig = secure_filename_with_uuid(fs.filename)
                fname = photosSet.save(fs, folder=folder, name=name_orig)
                ts_path = photosSet.config.destination + '/' + folder
                # 创建并保存缩略图
                name_thumb = create_thumbnail(path=ts_path, filename=name_orig, base_width=300)
                # 创建并保存展示图
                name_show = create_show(path=ts_path, filename=name_orig, base_width=800)
                # 把产生的Photo对象保存到数据库
                photo = Photo(origname=name_orig, showname=name_show, thumbname=name_thumb,
                              album_id=form.album_title.data)
                db.session.add(photo)
                db.session.commit()
                # 设置封面文件
                album_cover = photo.thumbname
                # 获取刚才保存的缩略图文件的url
                furl = photosSet.url(folder + '/' + name_thumb)
                files_url.append(furl)

            album = Album.query.filter_by(id=form.album_title.data).first()
            album.photonum += len(valid_fses)
            album.cover = album_cover
            db.session.add(album)
            db.session.commit()
            message = "成功保存:" + str(len(valid_fses)) + "张图像; "
            message += "当前相册共有:" + str(album.photonum) + "张图像"
            flash(message=message, category='ok')
            return render_template("album_upload.html", form=form, files_url=files_url)
    return render_template("album_upload.html", form=form)
Пример #2
0
def user_album_mine_photo_add(id):
    album = Album.query.get_or_404(id)
    if request.method == 'GET':
        folder = album.user.name + '/' + album.title
        for photo in album.photos:
            photo.url = photosSet.url(folder + '/' + photo.thumbname)
    if request.method == "POST":
        # 通过 files.getlist() 获得上传的 FileStorage 文件对象列表
        fses = request.files.getlist("album_upload")
        # 检查文件扩展名,将合格的文件过滤出来
        valid_fses = check_filestorages_extension(fses,
                                                  ALLOWED_IMAGE_EXTENSIONS)
        if len(valid_fses) < 1:
            flash(message="只允许上传文件类型:" + str(ALLOWED_IMAGE_EXTENSIONS),
                  category='err')
            return redirect(url_for("user_album_mine_adddel", id=id))
        else:
            # 开始遍历保存每一个合格文件
            for fs in valid_fses:
                folder = session.get("user_name") + '/' + album.title
                name_orig = secure_filename_with_uuid(fs.filename)
                fname = photosSet.save(fs, folder=folder, name=name_orig)
                ts_path = photosSet.config.destination + '/' + folder
                # 创建并保存缩略图
                name_thumb = create_thumbnail(path=ts_path,
                                              filename=name_orig,
                                              base_width=300)
                # 创建并保存展示图
                name_show = create_show(path=ts_path,
                                        filename=name_orig,
                                        base_width=800)
                # 把产生的Photo对象保存到数据库
                photo = Photo(origname=name_orig,
                              showname=name_show,
                              thumbname=name_thumb,
                              album_id=album.id)
                db.session.add(photo)
                db.session.commit()
            album.photonum += len(valid_fses)
            db.session.add(album)
            db.session.commit()
            message = "成功保存:" + str(len(valid_fses)) + "张图像; "
            message += "当前相册共有:" + str(album.photonum) + "张图像"
            flash(message=message, category='ok')
        return redirect(url_for("user_album_mine_photo_add", id=id))
    return render_template('user_album_mine_adddel.html',
                           album=album,
                           form=PhotoAddForm())
Пример #3
0
def album_upload():  # 相册首页
    form = AlbumUploadForm()
    albums = Album.query.filter_by(
        user_id=session["user_id"]).all()  # 获取全部相册标签
    # 动态构造form表单数据: 相册的id作为form.album_title.data即 form.album_title.data为当前相册的id
    form.album_title.choices = [(item.id, item.title)
                                for item in albums]  # 获取到数据库中存储的全部相册标签然后动态填写

    if len(albums) < 1:
        flash(message="请先创建一个相册,再上传照片!", category="err")
        return redirect(url_for("album_create"))

    if request.method == "POST":
        fses = request.files.getlist("album_upload")  # 获取上传的多个文件
        # 检查文件扩展名,将合格的文件过滤出来
        valid_fses = check_filestorages_extension(
            fses, allowed_extensions=ALLOWED_IMAGEEXTENSIONS)
        if len(valid_fses) < 1:
            flash(message="只允许上传文件类型:" + str(ALLOWED_IMAGEEXTENSIONS),
                  category="err")
            return redirect(url_for("album_upload"))
        else:
            # 获取当前相册的名称
            files_url = []
            album_title = ""
            for id, title in form.album_title.choices:
                if id == form.album_title.data:
                    album_title = title
            # 开始遍历保存每一个合格文件
            for fs in valid_fses:
                name_origin = secure_filename_with_uuid(fs.filename)
                folder = session.get("user_name") + "/" + album_title
                fname = photosSet.save(fs, folder=folder, name=name_origin)
                ts_path = photosSet.config.destination + "/" + folder
                # 创建并保存缩略图  photosSet.config.destination=app.config['UPLOADED_PHOTOS_DEST']   绝对路径 uploads文件夹
                name_thumb = create_thumbnail(path=ts_path,
                                              filename=name_origin,
                                              base_width=300)
                # 获取保存缩略图文件的url
                # furl = photosSet.url(fname)  #原图url
                furl = photosSet.url(folder + "/" + name_thumb)
                files_url.append(furl)
                # 创建并保存大图
                name_show = create_show(path=ts_path,
                                        filename=name_origin,
                                        base_width=800)

                # 将产生的Photo对象保存到数据库中去
                photo = Photo(origname=name_origin,
                              showname=name_show,
                              thumbname=name_thumb,
                              album_id=form.album_title.data)
                # 更新数据库
                db.session.add(photo)
                db.session.commit()

                furl = photosSet.url(folder + "/" + name_thumb)
                # files_url.append(furl)

            # 当前上传图片的相册
            print("当前相册id: ", form.album_title.data)
            # 获取当前相册 form构造的时候 相册的id作为form.album_title.data即 form.album_title.data为当前相册的id
            album = Album.query.filter_by(id=form.album_title.data).first()
            album.photonum += len(valid_fses)
            # 更新数据库
            db.session.add(album)
            db.session.commit()
            message = "成功上传 " + str(
                len(valid_fses)) + " 张图片!!" + "当前相册共有 " + str(
                    album.photonum) + " 张图片!!"
            flash(message=message, category="ok")

            return render_template("album_upload.html",
                                   form=form,
                                   files_url=files_url)

    return render_template("album_upload.html", form=form)