def article_read(id): article = Article.query.get_or_404(id) article.clicknum += 1 db.session.add(article) db.session.commit() userface_url = photosSet.url(filename=article.user.name + '/' + article.user.face) # 读取文章收藏列表(使用ORM方式或联合查表方式) favor_articles = [] if 'user_id' in session: # user = User.query.get_or_404(int(session.get('user_id'))) # for favor in user.articlefavors: # item = favor.article # item.coverimgurl = photosSet.url(filename=item.cover) # favor_articles.append(item) favor_articles = Article.query.join(ArticleFavor). \ filter(ArticleFavor.user_id == session.get('user_id')).all() for item in favor_articles: item.coverimgurl = photosSet.url(filename=item.cover) # 读取文章推荐列表 recommed__articles = Article.query.filter(Article.tag_id == article.tag_id, Article.id != article.id, Article.privacy != 'private').all() for item in recommed__articles: item.coverimgurl = photosSet.url(filename=item.cover) return render_template('home/article_read.html', article=article, userface_url=userface_url, favor_articles=favor_articles, recommed__articles=recommed__articles)
def article_browse_file(): # 获取CKeditor的JS回调函数reference CKEditorFuncNum = request.args.get('CKEditorFuncNum') if request.args.get('type') == 'images': albums = Album.query.filter(Album.privacy != 'private').all() albumId = request.args.get('aid') selected_photos = [] if albumId: for album in albums: if int(albumId) == album.id: for phitem in album.photos: subfolder = album.user.name + '/' + album.title + '/' phitem.url = photosSet.url(filename=subfolder + phitem.thumbname) selected_photos.append(phitem) photoId = request.args.get('phid') if photoId: photo = Photo.query.get_or_404(int(photoId)) subfolder = photo.album.user.name + '/' + photo.album.title + '/' photo_url = photosSet.url(filename=subfolder + photo.origname) # 把文件的url返回给CKEditor ret_js = "<script type='text/javascript'> " \ "window.opener.CKEDITOR.tools.callFunction(%s, '%s');" \ "window.close();</script>" % (CKEditorFuncNum, photo_url) return ret_js return render_template('home/article_browse_file.html', type=request.args.get('type'), CKEditorFuncNum=CKEditorFuncNum, albums=albums, selected_photos=selected_photos)
def set_cover_url(albums, isRand=True): for item in albums: if isRand: item.cover = item.photos[randint(0, len(item.photos) - 1)].thumbname folder = item.user.name + '/' + item.title coverimgurl = photosSet.url(filename=folder + '/' + item.cover) item.coverimgurl = coverimgurl
def album_browse(id): album = Album.query.get_or_404(id) photos_url = [] for photo in album.photos: photo_folder = album.user.name + '/' + album.title + '/' photo_url = photosSet.url(filename=photo_folder + photo.showname) photos_url.append(photo_url) return render_template("admin/album_browse.html", album=album, photos_url=photos_url)
def user_article_mine(page): articletags = ArticleTag.query.all() tagid = request.args.get('tag', 'all') if tagid == 'all': articles = Article.query.filter(Article.user_id == int(session.get('user_id'))). \ order_by(Article.addtime.desc()).paginate(page=page, per_page=6) else: articles = Article.query.filter(Article.user_id == int(session.get('user_id')), Article.tag_id == int(tagid)). \ order_by(Article.addtime.desc()).paginate(page=page, per_page=6) for article in articles.items: article.coverimgurl = photosSet.url(filename=article.cover) return render_template("home/user_article_mine.html", articletags=articletags, articles=articles)
def album_browse(id): # 取出相册的基本信息 album = Album.query.get_or_404(int(id)) # 增加对应相册的浏览量 album.clicknum += 1 db.session.add(album) db.session.commit() # 查询推荐相册 recommed_albums = Album.query.filter(Album.tag_id == album.tag_id, Album.id != album.id, Album.privacy != 'private').all() # 给每个推荐相册随机挑选一个封面图像 for recmm in recommed_albums: recmm.cover = recmm.photos[randint(0, len(recmm.photos) - 1)].thumbname folder = recmm.user.name + '/' + recmm.title coverimgurl = photosSet.url(filename=folder + '/' + recmm.cover) recmm.coverimgurl = coverimgurl # 准备我的收藏列表 favor_albums = [] if 'user_id' in session: user = User.query.get_or_404(int(session.get('user_id'))) for favor in user.favors: favor_albums.append(favor.album) for falbum in favor_albums: falbum.cover = falbum.photos[randint(0, len(falbum.photos) - 1)].thumbname folder = falbum.user.name + '/' + falbum.title coverimgurl = photosSet.url(filename=folder + '/' + falbum.cover) falbum.coverimgurl = coverimgurl # 取出作者头像的url userface_url = photosSet.url(filename=album.user.name + '/' + album.user.face) # 取出该相册下面的所有图像 for photo in album.photos: photo_folder = album.user.name + '/' + album.title + '/' photo.url = photosSet.url(filename=photo_folder + photo.showname) # 用查询到的数据填充渲染页面 return render_template("home/album_browse.html", album=album, userface_url=userface_url, recommed_albums=recommed_albums, favor_albums=favor_albums)
def album_list(page): albumtags = AlbumTag.query.all() tagid = request.args.get('tag', 'all') if tagid == 'all': albums = Album.query.filter(Album.privacy != 'private'). \ order_by(Album.addtime.desc()).paginate(page=page, per_page=8) else: albums = Album.query.filter(Album.privacy != 'private', Album.tag_id == int(tagid)). \ order_by(Album.addtime.desc()).paginate(page=page, per_page=8) for album in albums.items: album.cover = album.photos[randint(0, len(album.photos) - 1)].thumbname folder = album.user.name + '/' + album.title coverimgurl = photosSet.url(filename=folder + '/' + album.cover) album.coverimgurl = coverimgurl return render_template("home/album_list.html", albumtags=albumtags, albums=albums)
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("home.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("home.user_album_mine_photo_add", id=id)) return render_template('home/user_album_mine_adddel.html', album=album, form=PhotoAddForm())
def set_article_cover_url(articles): for item in articles: coverimgurl = photosSet.url(filename=item.cover) item.coverimgurl = coverimgurl userface_url = photosSet.url(filename=item.user.name + '/' + item.user.face) item.userface_url = userface_url
def user_detail(): user = User.query.get_or_404(int(session.get("user_id"))) face_url = photosSet.url(user.name + '/' + user.face) return render_template("home/user_detail.html", user=user, face_url=face_url)
def music_listen(id): # 获取当前音乐对象 music = Music.query.get_or_404(id) # 更新播放量 music.clicknum += 1 db.session.add(music) db.session.commit() # 获取当前音乐文件的url music.audio_url = musicAudiosSet.url(music.audiofile) music.cover_url = musicAudiosSet.url(music.coverfile) # 读取歌词文件 lrc_path = musicLrcsSet.path(filename=music.lrcfile) lrcf = open(file=lrc_path, mode='r', encoding='utf8') music.lrc = [] for lrcline in lrcf.readlines(): lrcline = lrcline.strip('\n') # print(lrcline) idx = lrcline.find(']') + 1 # 分割时间戳和歌词内容 timestamp = lrcline[0:idx] lrccontent = lrcline[idx:] tidx = timestamp.find(':') # 分割时间戳内的分钟数和秒数 midum = timestamp[tidx - 2:tidx] second = timestamp[tidx + 1:tidx + 3] if midum == 'ti' or midum == 'ar' or midum == 'al' or midum == 'by': continue else: timestamp = int(midum) * 60 + int(second) # 把时间戳转换成秒数 # 把处理好的一行歌词添加到列表 music.lrc.append((timestamp, lrccontent)) # 获取推荐音乐和url recmm_lang_musics = Music.query.filter(Music.lang_id == music.lang_id, Music.id != music.id).all()[0:6] for recmm_lang in recmm_lang_musics: recmm_lang.cover_url = musicCoversSet.url(filename=recmm_lang.coverfile) # 按音乐流派推荐 recmm_style_musics = Music.query.filter(Music.style_id == music.style_id, Music.id != music.id).all()[0:6] for recmm_style in recmm_style_musics: recmm_style.cover_url = musicCoversSet.url(filename=recmm_style.coverfile) # 按音乐主题推荐 recmm_theme_musics = Music.query.filter(Music.theme_id == music.theme_id, Music.id != music.id).all()[0:6] for recmm_theme in recmm_theme_musics: recmm_theme.cover_url = musicCoversSet.url(filename=recmm_theme.coverfile) # 按音乐心情推荐 recmm_emotion_musics = Music.query.filter(Music.emotion_id == music.emotion_id, Music.id != music.id).all()[0:6] for recmm_emotion in recmm_emotion_musics: recmm_emotion.cover_url = musicCoversSet.url(filename=recmm_emotion.coverfile) # 按音乐场景推荐 recmm_scene_musics = Music.query.filter(Music.scene_id == music.scene_id, Music.id != music.id).all()[0:6] for recmm_scene in recmm_scene_musics: recmm_scene.cover_url = musicCoversSet.url(filename=recmm_scene.coverfile) # 按音乐歌手推荐 recmm_singer_musics = Music.query.filter(Music.singer == music.singer, Music.id != music.id).all()[0:6] for recmm_singer in recmm_singer_musics: recmm_singer.cover_url = musicCoversSet.url(filename=recmm_singer.coverfile) # 获取评论列表,以及用户头像 music_comments = MusicComment.query.filter_by(music_id=music.id). \ order_by(MusicComment.addtime.desc()).all() for cmmt in music_comments: cmmt.userface_url = photosSet.url(filename=cmmt.user.name + '/' + cmmt.user.face) return render_template('home/music_listen.html', form=MusicCommentForm(), music=music, recmm_lang_musics=recmm_lang_musics, recmm_style_musics=recmm_style_musics, recmm_theme_musics=recmm_theme_musics, recmm_emotion_musics=recmm_emotion_musics, recmm_scene_musics=recmm_scene_musics, recmm_singer_musics=recmm_singer_musics, music_comments=music_comments)
def user_profile(id): user = User.query.get_or_404(id) user.faceurl = photosSet.url(filename=user.name + '/' + user.face) return render_template("admin/user_profile.html", user=user)