def get_articles(): # 获取请求参数 page_num = int(request.args.get('page', 1)) # 页码 page_size = int(request.args.get('size', 20)) # 每页数量 dbms = request.args.get('dbms') # 请求数据库 sort_by = request.args.get('sort_by') if sort_by is not None and sort_by not in ArticleService.field_names: if sort_by[1:] not in ArticleService.field_names: sort_by = None try: # 检查数据库地址正确性 check_alias(db_alias=dbms) except DbmsAliasError: return Result.gen_failed('404', 'dbms error') # 获取查询参数 title = request.args.get('title') authors = request.args.get('authors') category = request.args.get('category') articleTags = request.args.get('articleTags') language = request.args.get('language') detail = request.args.get('detail', '1') # print(request.data) # print(exclude) cons = { 'title__contains': title, 'authors': authors, 'category': category, 'articleTags': articleTags, 'language': language } # 去除为空的查询参数 kwargs = {} for key, value in cons.items(): if value is not None and value != '': kwargs[key] = value if detail != '1': kwargs['exclude'] = ['text', 'image', 'video'] # 尝试从dbms对应的Redis中获取该数据 _REDIS_KEY_ = f"{ARTICLES_LIST}:{dbms}:{page_num}:{page_size}:{sort_by}:{kwargs}" data = RedisService().get_dict(dbms, _REDIS_KEY_) # 判断数据是否存在 if data is None or data == {}: # 不存在 arts = ArticleService().get_articles(page_num=page_num, page_size=page_size, db_alias=dbms, sort_by=sort_by, **kwargs) arts = list(art.to_dict() for art in arts) total = ArticleService().count(db_alias=dbms, **kwargs) data = { 'total': total, 'list': arts } # 将该数据存入Redis中 RedisService().set_dict(dbms, _REDIS_KEY_, data) return Result.gen_success(data) pass
def update_an_article(role, user): # TODO 管理员或者用户更新文章信息 aid = input("请输入要更新的文章的id:") if aid is None or aid == '': print("无输入,返回上一级") return try: article = ArticleService().get_one_by_aid(aid=aid) except Exception: print("id不存在") return if article is None: print("文章不存在。") return if (role == 'user' and article.authors == user.name) or role == 'admin': print_an_article(article) update_info = input("请输入要更改的信息(json格式:)") if update_info == '': return try: con = json.loads(update_info) if ArticleService().update_one(article, con) is not None: print("更新成功") else: print("更新失败") except json.JSONDecodeError: print("输入不是json格式") return None else: print("无权限对该文章进行操作") pass
def test_get_articles_by_aids(self): aids = [0, 1, 2, 3] articles = ArticleService().get_articles_by_aids(aids, db_alias=DBMS.DBMS2, only=['title']) ArticleService.pretty_articles( list(articles[aid] for aid in aids if aid in articles.keys()))
def articles(): i = 0 with open(data_path + article_file_name, 'r') as f: for line in f: data = json.loads(line) ArticleService.add_an_article(title=data['title'], authors=data['authors'], category=data['category'], abstract=data['abstract'], articleTags=data['articleTags'], language=data['language'], text=data['text'], image=data['image'], video=data['video']) i += 1 print_bar(i, ARTICLES_NUM)
def query_all(total, page_num=1, page_size=20, db_alias=None, **kwargs): articles = ArticleService().get_articles(page_num, page_size, db_alias=db_alias, **kwargs) if len(articles) == 0: print("未找到文章信息") return ArticleService().pretty_articles(articles) show_next(page_num, page_size, total, db_alias=db_alias, next_func=query_all, **kwargs) pass
def dashboard(): from config import DBMS from service.user_service import UserService from service.article_service import ArticleService from service.read_service import ReadService from service.popular_service import PopularService dbms = request.args.get('dbms') nums = { 'users': UserService().count(db_alias=dbms), 'articles': ArticleService().count(db_alias=dbms), 'reads': ReadService().count(db_alias=dbms), 'populars': PopularService().count(temporalGranularity='daily', db_alias=dbms) } charts = {'users': [], 'articles': [], 'reads': []} for dbms in DBMS().get_all_dbms_by_region(): charts['users'].append({ 'name': dbms, 'value': UserService().count(db_alias=dbms) }) charts['articles'].append({ 'name': dbms, 'value': ArticleService().count(db_alias=dbms) }) charts['reads'].append({ 'name': dbms, 'value': ReadService().count(db_alias=dbms) }) data = {'nums': nums, 'charts': charts} # if dbms == 'Beijing': # data = { # 'users': 12354, # 'articles': 533366, # 'reads': 23424, # 'populars': 90372 # } # else: # data = { # 'users': 63234, # 'articles': 1284, # 'reads': 724933, # 'populars': 8422 # } return Result.gen_success(data=data) pass
def articles_query_all(page_num=1, page_size=20, **kwargs): print("1. Beijing") print("2. Hong Kong (所有数据)") print("输入其他内容返回") mode = input("请选择操作: ") if mode == '1': total = ArticleService().count(db_alias=DBMS.DBMS1, **kwargs) query_all(total, page_num, page_size, db_alias=DBMS.DBMS1, **kwargs) elif mode == '2': total = ArticleService().count(db_alias=DBMS.DBMS2, **kwargs) query_all(total, page_num, page_size, db_alias=DBMS.DBMS2, **kwargs) return
def gen_articles(): print() for i in range(ARTICLES_NUM): print_bar(i, ARTICLES_NUM) data = gen_an_article(i) ArticleService().import_from_dict(data) if (i + 1) % 50000 == 0: logger.info(f"saving articles, {i}") ArticleService().update_many() # logger.info(f"saving article's be read, {i}") # BeReadService().update_many() ArticleService().update_many()
def choices_article(user): # from prettytable import PrettyTable print("1. 根据aid查询") print("2. 根据title查询") print("3. 返回") mode = input("请选择:") if mode == '1': aid = input("请输入要阅读的aid: ") article = ArticleService().get_one_by_aid(aid=aid) if article is not None: print_an_article(article) mode = input("是否阅读该文章:(y or n)") if mode == 'y' or mode == 'Y' or mode == 'yes' or mode == 'YES': read_an_article(user, article) else: return else: print("未找到相关文章") return pass elif mode == '2': keyword = input("请输入要查看的文章部分标题: ") if keyword == '': return articles = ArticleService().get_articles_by_title(keyword, db_alias=DBMS.DBMS2) # TODO 文章数量过多时分页显示 if len(articles) == 0: print("未找到相关文章") return x = PrettyTable() x.field_names = ("num", "title", "authors", "abstract") for index, article in enumerate(articles): x.add_row([index, article.title, article.authors, article.abstract]) print(x) while True: try: index = int(input("请选择要阅读的序号( -1 或者不输入任何内容退出): ")) except ValueError: print("输入错误") return if 0 <= index < len(articles): read_an_article(user, articles[index]) break elif index == -1: break else: print("输入错误,请重新输入") pass else: return
def upload_doc(): args = request.json doc = args.get('doc') id = args.get('id') title = args.get('name') assert doc is not None and len(doc) > 0 return Response.success(ArticleService.save(g.user.name, id, title, doc))
def add_one(self, aid, uid, readOrNot, commentOrNot, agreeOrNot, shareOrNot, timestamp=None, is_multi=False): article = ArticleService().get_one_by_aid(aid, only=['category']) if article is None: return None # be_read = self.get_by_aid(aid) be_read = None # bid = self.get_bid() if be_read is None else be_read.bid for dbms in get_dbms_by_category(article.category): # logger.info("save be read to : {} uid: {}".format(dbms, uid)) be_read = self.new_record(aid, uid, readOrNot, commentOrNot, agreeOrNot, shareOrNot, timestamp, db_alias=dbms, is_multi=is_multi) return be_read
def test_save_read(self, aid=None, uid=None): aid = aid or ArticleService().get_articles_by_title('title4')[0].aid uid = uid or UserService().get_user_by_name('user4').uid read = self.readService.add_one(aid, uid, 1, 34, 2, 0, 'sdf', 1, 1) assert read is not None self.readService.pretty_reads([read]) return read
def test_get_by_uid_and_aid(self): aid = ArticleService().get_articles_by_title('title')[0].aid uid = UserService().get_user_by_name('user5').uid read1 = self.test_save_read(aid, uid) read = self.readService.get_by_uid_and_aid(uid=uid, aid=aid) print(read) assert read1.aid in read.item_frequencies( 'aid').keys() and read1.uid in read.item_frequencies('uid').keys()
def test_read_twice(self): aid = ArticleService().get_articles_by_title('title2')[0].aid uid = UserService().get_user_by_name('user2').uid read1 = self.readService.add_one(aid, uid, 1, 34, 2, 0, 'sdf', 0, 1) ReadService().pretty_reads([read1]) read2 = self.readService.add_one(aid, uid, 1, 34, 2, 1, 'asdaff', 1, 1) ReadService().pretty_reads([read2]) read1.reload() assert read1.readSequence == read2.readSequence
def get_one_by_aid(self, aid: str, db_alias=None, **kwargs) -> BeRead: if db_alias is None: article = ArticleService().get_one_by_aid(aid, only=['category']) return self.get_one_by_aid(aid, db_alias=get_best_dbms_by_category( article.category)) else: check_alias(db_alias) return self.get_model(db_alias).get_one(aid=aid, **kwargs)
def get_article(aid): if aid is None: return Result.gen_failed('404', 'aid not found') # 获取查询参数 category = request.args.get('category', None) # article = {} # 尝试从Redis中获取该数据 _REDIS_KEY_ = f"{ARTICLES_ITEM}:{aid}" if category is not None: if category not in DBMS().category['values']: return Result.gen_failed('404', '类别不存在') # 从category对应的dbms的redis中取数据 dbms = get_best_dbms_by_category(category) article = RedisService().get_dict(dbms, _REDIS_KEY_) if article == {}: # 缓存中无数据,则去数据库中查询 article = ArticleService().get_one_by_aid(aid=aid, db_alias=get_best_dbms_by_category(category)) if article is not None: # article存在,存入redis中 article = article.to_dict() RedisService().set_dict(dbms, _REDIS_KEY_, article) else: # 尝试从所有redis中取该数据 article = RedisService().get_dict_from_all(_REDIS_KEY_) if article == {}: # 缓存中没有,去数据库中查询 article = ArticleService().get_one_by_aid(aid=aid) if article is not None: article = article.to_dict() try: for dbms in DBMS().get_dbms_by_category(article['category']): RedisService().set_dict(dbms, _REDIS_KEY_, article) except Exception: # 避免出现类别不存在的情况 logger.info(f"aid: {aid} 存入缓存失败, data: {article}") if article is None or article == {}: return Result.gen_failed(404, '文章不存在') return Result.gen_success(article)
def del_an_article(user, role): aid = input("请输入要删除的文章aid: ") if aid == '': return if role == 'user' and user is not None: article = ArticleService().get_an_article(id=aid, authors=user.name) if article is None: print("您无权限删除该文章或者该文章不存在") return ArticleService().del_article(article) elif role == 'admin': ArticleService().del_by_aid(_id=aid) else: print("用户不存在或者权限错误") return print("删除成功") return None pass
def __update_rank(self, rank, articles, db_alias): rank.articleAidDict = {} for aid, count in articles: # print("{}, {}".format(aid, ArticleService().has_article(aid, db_alias))) if ArticleService().has_article(aid, db_alias): rank.articleAidDict[str(aid)] = count rank.update_time = get_timestamp() # print(rank.articleAidDict) rank.save()
def __get_articles_by_rank(self, rank, db_alias): populars = list() if rank is None or rank.articleAidDict == []: return populars aids = list(int(aid) for aid in rank.articleAidDict.keys()) articles = ArticleService().get_articles_by_aids( aids, only=['title', 'category'], db_alias=db_alias) for aid, count in rank.articleAidDict.items(): articles[int(aid)].count = count populars.append(articles[int(aid)]) return populars
def test_update_article_by_aid(self): a = ArticleService().get_articles_by_title('title5')[0] article = ArticleService().get_one_by_aid(a.aid) ArticleService.pretty_articles([article]) ArticleService().update_one(article, { "title": "update24", "language": 'zh' }) article = ArticleService().get_one_by_aid(a.aid) ArticleService.pretty_articles([article]) assert article.language == 'zh'
def publish_article(): args = request.json id = args.get('id') title = args.get('title') tags = args.get('tags') is_published = args.get('is_published') assert id is not None assert title is not None assert tags is not None return Response.success( ArticleService.publish_article(g.user.name, id, title, tags, is_published))
def get_user_read_history(uid): """ 获取指定用户的所有read记录 :return: """ region = request.args.get('region') if region not in DBMS().region['values']: return Result.gen_failed('404', 'region error') else: dbms = DBMS().get_best_dbms_by_region(region) kwargs = {'uid': uid} # kwargs = {} data = None # _REDIS_KEY_ = f"READ_LIST:{dbms}:{page_num}:{page_size}:{kwargs}" # data = RedisService().get_redis(dbms).get_dict(_REDIS_KEY_) if data is None or data == {}: res = ReadService().get_reads(sort_by='-timestamp', db_alias=dbms, **kwargs) records = {} for read in res: if read.aid not in records.keys(): article = ArticleService().get_one_by_aid(aid=read.aid, only=['title']) if article is not None: # record = read.to_dict() read.title = article.title records[read.aid] = read else: records[read.aid].readOrNot = records[ read.aid].readOrNot or read.readOrNot records[read.aid].agreeOrNot = records[ read.aid].agreeOrNot or read.agreeOrNot records[read.aid].shareOrNot = records[ read.aid].shareOrNot or read.shareOrNot records[read.aid].commentOrNot = records[ read.aid].commentOrNot or read.commentOrNot records[read.aid].readTimeLength += read.readTimeLength records[read.aid].readSequence += read.readSequence records[read.aid].timestamp = max(records[read.aid].timestamp, read.timestamp) records = list( read.to_dict(other=['title']) for (key, read) in records.items()) total = ReadService().count(db_alias=dbms, **kwargs) data = {'total': total, 'list': records} return Result.gen_success(data)
async def prepare(self): # await self.init_session() if not self.session: self.session = Session(self) # 如果已登录 session_id = self.session.get_session_id() if session_id: uinfo = await self.session.get() self.current_user = LoginUser(uinfo['login_user']) # if session_keys['login_user'] in self.session: # self.current_user = LoginUser(self.session[session_keys['login_user']]) # 添加pv和uv await self.add_pv_uv() # 获取博客信息 if self.site_info.navbar is None: # 博客概况 blog = BlogInfoService.get_blog_info(self.db) self.site_info.title = blog.title self.site_info.signature = blog.signature self.site_info.navbar = blog.navbar # 插件列表 plugin = PluginService.list_plugins(self.db) self.site_info.plugins = plugin # 今日uv和pv view = BlogViewService.get_blog_view(self.db) self.site_info.todaypv = view.pv self.site_info.todayuv = view.uv # 文章来源 self.site_info.article_sources = ArticleService.get_article_sources( self.db) # 文章数量 self.site_info.article_count = ArticleService.get_article_count( self.db) # 评论数 self.site_info.comment_count = CommentService.get_comment_count( self.db)
def new_articles(): data = request.json for key in list(data.keys())[::-1]: if key not in ArticleService.field_names: data.pop(key) logger.info(f'添加文章: {data}') # 先删除对应的缓存,然后在添加文章 for dbms in DBMS().get_dbms_by_category(data['category']): RedisService().delete_key_by_pattern(dbms, f'{ARTICLES_LIST}:*') aid = ArticleService().add_an_article(**data) return Result.gen_success(data={'aid': aid}) pass
def test_del_article(self): ArticleService().add_an_article('test_article', 'gahon', 'science', 'asdf', 'asdfas', 'asdf', 'asdf') articles = ArticleService().get_articles_by_title('test_article') _aid = articles[0].aid re1 = ArticleService().del_by_aid(_aid) assert re1 # article = ArticleService().get_one_by_aid(_aid) assert article is None re = ArticleService().add_an_article('test_article', 'gahon', 'science', 'asdf', 'asdfas', 'asdf', 'asdf') assert re articles = ArticleService().get_articles_by_title('test_article') ArticleService().pretty_articles(articles) aid = articles[0].aid re = ArticleService().del_article(articles[0]) assert re article = ArticleService().get_one_by_aid(aid) assert article is None
def delete_article(aid): if aid is None: return Result.gen_failed('404', 'aid not found') _REDIS_KEY_ = f"{ARTICLES_ITEM}:{aid}" category = request.args.get('category') if category is None: return Result.gen_failed('5000', '请带上category参数') # 先从缓存中删除该键值以及所有list对应的键值 for dbms in DBMS().get_dbms_by_category(category): RedisService().delete_key(dbms, _REDIS_KEY_) RedisService().delete_key_by_pattern(dbms, f"{ARTICLES_LIST}:*") ArticleService().del_by_aid(aid) return Result.gen_success('删除成功')
def create_article(user): # TODO 输入内容为空判断 # 根据Article表的项内容创建文章,并且保存 title = input("请输入title: ") while title == '': title = input("输入不能为空,请重新输入: ") category = input("请输入category: ") while category != 'science' and category != 'technology': print('category 输入错误,应该为science or technology') category = input("请输入category: ") abstract = input("请输入abstract: ") articleTags = input("请输入articleTags: ") language = input("请输入language: ") text = input("请输入text: ") image = input("请输入image: ") video = input("请输入video: ") ArticleService().add_an_article(title, user.name, category, abstract, articleTags, language, text, image, video) pass
async def delete_post(self, article_id): article_deleted, comments_deleted = await self.loop_current.run_in_executor( self.thread_executor, ArticleService.delete_article, self.db, article_id) if article_deleted: # yield self.flush_article_cache(Constants.FLUSH_ARTICLE_ACTION_REMOVE, article_deleted) # yield self.flush_comments_cache(Constants.FLUSH_COMMENT_ACTION_REMOVE, comments_deleted) self.site_info.article_count = self.site_info.article_count - 1 self.site_info.comment_count = self.site_info.comment_count - len( comments_deleted) self.site_info.article_sources = ArticleService.get_article_sources( self.db) self.add_message('success', '删除成功,并删除{}条评论!'.format(len(comments_deleted))) # js脚本自动重载,不需要跳转 # self.redirect(self.reverse_url('admin.articles')) else: self.add_message('danger', '删除失败!')
def test_article_record(self): uid = 0 aid = 0 region = UserService().get_user_by_uid(uid, only=['region']).region category = ArticleService().get_one_by_aid(aid, only=['category']).category data = { 'uid': uid, 'aid': aid, 'region': region, 'category': category, 'readTimeLength': 80, 'commentOrNot': 1, 'commentDetail': '第二次评论', 'agreeOrNot': 1 } response = self.client.post(f'/api/reads', json=data) print_json(response.json)
def test_add_an_article(self): re = ArticleService().add_an_article('test', 'gahon', 'science', 'asdf', 'asdfas', 'asdf', 'asdf') assert re re = ArticleService().add_an_article('test', 'gahon', 'technology', 'asdf', 'asdfas', 'asdf', 'asdf') assert re articles = ArticleService().get_articles_by_title('test', db_alias=DBMS.DBMS1) ArticleService().pretty_articles(articles) articles = ArticleService().get_articles_by_title('test', db_alias=DBMS.DBMS2) ArticleService().pretty_articles(articles)