def render(self, *args, **kwargs): logger.info('Init footer') all_cats = MCategory.query_all() kwd = { 'cats': all_cats, } return self.render_string('modules/post/menu.html', kwd=kwd)
def render_user(self, *args, **kwargs): ''' render, with userinfo ''' kind = args[0] num = args[1] with_tag = kwargs['with_tag'] if 'with_tag' in kwargs else False user_id = kwargs['user_id'] if 'user_id' in kwargs else '' glyph = kwargs['glyph'] if 'glyph' in kwargs else '' logger.info('Infor user recent, username: {user_name}, kind: {kind}, num: {num}'.format( user_name=user_id, kind=kind, num=num )) all_cats = MUsage.query_recent(user_id, kind, num).naive() kwd = { 'with_tag': with_tag, 'router': router_post[kind], 'glyph': glyph } return self.render_string('modules/info/list_user_equation.html', recs=all_cats, kwd=kwd)
def add_or_update(user_id, post_id, kind): ''' Create the record if new, else update it. ''' rec = MUsage.query_by_signature(user_id, post_id) cate_rec = MInfor2Catalog.get_first_category(post_id) if cate_rec: cat_id = cate_rec.tag_id else: return False if rec.count() > 0: logger.info('Usage update: {uid}'.format(uid=post_id)) rec = rec.get() query = TabUsage.update(kind=kind).where(TabUsage.uid == rec.uid) query.execute() MUsage.count_increate(rec.uid, cat_id, rec.count) else: logger.info('Usage create: {uid}'.format(uid=post_id)) TabUsage.create( uid=tools.get_uuid(), post_id=post_id, user_id=user_id, count=1, tag_id=cat_id, timestamp=int(time.time()), kind=kind, )
def get_id_by_name(tag_name, kind='z'): ''' Get ID by tag_name of the label. ''' recs = TabTag.select().where((TabTag.name == tag_name) & (TabTag.kind == kind)) logger.info('tag count of {0}: {1} '.format(tag_name, recs.count())) # the_id = '' if recs.count() == 1: the_id = recs.get().uid elif recs.count() > 1: rec0 = None for rec in recs: # Only keep one. if rec0: TabPost2Tag.delete().where( TabPost2Tag.tag_id == rec.uid).execute() TabTag.delete().where(TabTag.uid == rec.uid).execute() else: rec0 = rec the_id = rec0.uid else: the_id = MLabel.create_tag(tag_name) return the_id
def json_register(self): ''' The first char of 'code' stands for the different field. '1' for user_name '2' for user_email '3' for user_pass '4' for user_role The seconde char of 'code' stands for different status. '1' for invalide '2' for already exists. ''' user_create_status = {'success': False, 'code': '00'} post_data = self.get_post_data() user_create_status = self.__check_valid(post_data) if not user_create_status['success']: return json.dump(user_create_status, self) form = SumForm(self.request.arguments) if form.validate(): user_create_status = MUser.create_user(post_data) logger.info('user_register_status: {0}'.format(user_create_status)) return json.dump(user_create_status, self) else: return json.dump(user_create_status, self)
def update(self, uid): if self.__could_edit(uid): pass else: return False postinfo = self.mpost.get_by_id(uid) if postinfo.kind == self.kind: pass else: return False post_data = self.get_post_data() post_data['user_name'] = self.get_current_user() post_data['kind'] = self.kind is_update_time = True if post_data['is_update_time'][ 0] == '1' else False cnt_old = tornado.escape.xhtml_unescape(postinfo.cnt_md).strip() cnt_new = post_data['cnt_md'].strip() if cnt_old == cnt_new: pass else: self.mpost_hist.insert_data(postinfo) logger.info('upadte: {0}'.format(uid)) logger.info('Update post_data: {0}'.format(post_data)) self.mpost.update(uid, post_data, update_time=is_update_time) self.update_category(uid) self.update_tag(uid) self.redirect('/{0}/{1}'.format(router_post[postinfo.kind], uid))
def post(self, *args, **kwargs): url_str = args[0] logger.info('Post url: {0}'.format(url_str)) url_arr = self.parse_url(url_str) if url_arr[0] in ['_edit']: self.update(url_arr[1]) elif url_arr[0] in ['_add']: if len(url_arr) == 2: self.add(uid=url_arr[1]) else: self.add() elif url_arr[0] == '_edit_kind': self._change_kind(url_arr[1]) elif url_arr[0] in ['_cat_add']: self.add(catid=url_arr[1]) elif len(url_arr) == 1: # Todo: should not exists. if len(url_str) in [4, 5]: self.add(uid=url_str) elif url_arr[0] == 'rel' and len(url_arr) == 3: self._add_relation(url_arr[1], url_arr[2]) else: self.show404()
def __get_tmpl_view(self, rec): ''' According to the application, each info of it's classification could has different temaplate. :param rec: the App record. :return: the temaplte path. ''' # post2catinfo = MPost2Catalog.query_by_post( rec.uid ) if 'gcat0' in rec.extinfo and rec.extinfo['gcat0'] != '': cat_id = rec.extinfo['gcat0'] elif 'def_cat_uid' in rec.extinfo and rec.extinfo['def_cat_uid'] != '': cat_id = rec.extinfo['def_cat_uid'] else: cat_id = None logger.info('For templates: catid: {0}, filter_view: {1}'.format( cat_id, self.filter_view)) if cat_id and self.filter_view: tmpl = 'autogen/view/view_{0}.html'.format(cat_id) else: tmpl = 'post_{0}/post_view.html'.format(self.kind) return tmpl
def update_category(self, uid, **kwargs): ''' Update the category of the post. :param uid: The ID of the post. Extra info would get by requests. :return: ''' catid = kwargs['catid'] if ('catid' in kwargs and MCategory.get_by_uid( kwargs['catid'])) else None post_data = self.get_post_data() current_infos = MPost2Catalog.query_by_entity_uid(uid, kind='').naive() new_category_arr = [] # Used to update post2category, to keep order. def_cate_arr = ['gcat{0}'.format(x) for x in range(10)] # for old page. def_cate_arr.append('def_cat_uid') # Used to update post extinfo. cat_dic = {} for key in def_cate_arr: if key not in post_data: continue if post_data[key] == '' or post_data[key] == '0': continue # 有可能选重复了。保留前面的 if post_data[key] in new_category_arr: continue new_category_arr.append(post_data[key] + ' ' * (4 - len(post_data[key]))) cat_dic[key] = post_data[key] + ' ' * (4 - len(post_data[key])) if catid: def_cat_id = catid elif new_category_arr: def_cat_id = new_category_arr[0] else: def_cat_id = None if def_cat_id: cat_dic['def_cat_uid'] = def_cat_id cat_dic['def_cat_pid'] = MCategory.get_by_uid(def_cat_id).pid # Add the category logger.info('Update category: {0}'.format(new_category_arr)) logger.info('Update category: {0}'.format(cat_dic)) MPost.update_jsonb(uid, cat_dic) for index, catid in enumerate(new_category_arr): MPost2Catalog.add_record(uid, catid, index) # Delete the old category if not in post requests. for cur_info in current_infos: if cur_info.tag_id not in new_category_arr: MPost2Catalog.remove_relation(uid, cur_info.tag_id)
def viewinfo(self, postinfo): ''' 查看 Post. ''' self.redirect_kind(postinfo) __ext_catid = postinfo.extinfo.get('def_cat_uid', '') cat_enum1 = MCategory.get_qian2(__ext_catid[:2]) if __ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self._chuli_cookie_relation(postinfo.uid) catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) MAcces.add(postinfo.uid) if self.get_current_user() and self.userinfo: MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', kwd['cookie_str']) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6).objects()[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render( tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, author=postinfo.user_name, # Todo: remove the key `author`. catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, subcats=MCategory.query_sub_cat(p_catinfo.uid) if p_catinfo else '', ad_switch=random.randint(1, 18), tag_info=filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(postinfo.uid).objects()), recent_apps=recent_apps, cat_enum=cat_enum1)
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' self.redirect_kind(postinfo) ext_catid = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] rand_recs, rel_recs = self.fetch_additional_posts(postinfo.uid) self._chuli_cookie_relation(postinfo.uid) catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) self.set_cookie('user_pass', kwd['cookie_str']) tmpl = self.ext_tmpl_view(postinfo) if self.userinfo: recent_apps = MUsage.query_recent(self.userinfo.uid, postinfo.kind, 6).naive()[1:] else: recent_apps = [] logger.info('The Info Template: {0}'.format(tmpl)) self.render( tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, author=postinfo.user_name, # Todo: remove the key `author`. catinfo=catinfo, pcatinfo=p_catinfo, relations=rel_recs, rand_recs=rand_recs, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), # tag_info=MPost2Label.get_by_uid(postinfo.uid).naive(), tag_info=filter(lambda x: not x.tag_name.startswith('_'), MPost2Label.get_by_uid(postinfo.uid).naive()), recent_apps=recent_apps, cat_enum=cat_enum1)
def post(self, *args): url_str = args[0] logger.info('Post url: {0}'.format(url_str)) url_arr = self.parse_url(url_str) if url_arr[0] in ['_edit', 'edit', 'modify']: self.update(url_arr[1]) elif url_arr[0] in ['_add', 'add', 'add_document', ]: if len(url_arr) == 2: self.add(uid=url_arr[1]) else: self.add() elif url_arr[0] == '_edit_kind': self.__change_kind(url_arr[1]) elif url_arr[0] in ['_cat_add', 'cat_add']: self.add(catid=url_arr[1]) elif len(url_arr) == 1: # Todo: should not exists. if len(url_str) in [4, 5]: self.add(uid=url_str) elif url_arr[0] == 'rel' and len(url_arr) == 3: self.add_relation(url_arr[1], url_arr[2]) else: kwd = { 'title': '', 'info': '404. No such action!', } self.set_status(404) self.render('html/404.html', kwd=kwd, userinfo=self.userinfo, )
def render_user(self, *args, **kwargs): ''' render, with userinfo fun(kind, num) fun(kind, num, with_tag = val1) fun(kind, num, with_tag = val1, user_id = val2) fun(kind, num, with_tag = val1, user_id = val2, glyph = val3) ''' kind = kwargs.get('kind', args[0]) num = kwargs.get('num', args[1] if len(args) > 1 else 6) with_tag = kwargs.get('with_tag', False) user_id = kwargs.get('user_id', '') glyph = kwargs.get('glyph', '') logger.info( 'Infor user recent, username: {user_name}, kind: {kind}, num: {num}' .format(user_name=user_id, kind=kind, num=num)) all_cats = MUsage.query_recent(user_id, kind, num).objects() kwd = { 'with_tag': with_tag, 'router': router_post[kind], 'glyph': glyph } return self.render_string('modules/info/list_user_equation.html', recs=all_cats, kwd=kwd)
def _to_edit(self, infoid): ''' render the HTML page for post editing. ''' postinfo = MPost.get_by_uid(infoid) if postinfo: pass else: return self.show404() if 'def_cat_uid' in postinfo.extinfo: catid = postinfo.extinfo['def_cat_uid'] else: catid = '' if len(catid) == 4: pass else: catid = '' catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) if post2catinfo: catid = post2catinfo.tag_id catinfo = MCategory.get_by_uid(catid) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) kwd = { 'def_cat_uid': catid, 'parentname': '', 'catname': '', 'parentlist': MCategory.get_parent_list(), 'userip': self.request.remote_ip, 'extinfo': json.dumps(postinfo.extinfo, indent=2, ensure_ascii=False), } if self.filter_view: tmpl = 'autogen/edit/edit_{0}.html'.format(catid) else: tmpl = 'post_{0}/post_edit.html'.format(self.kind) logger.info('Meta template: {0}'.format(tmpl)) self.render(tmpl, kwd=kwd, postinfo=postinfo, catinfo=catinfo, pcatinfo=p_catinfo, userinfo=self.userinfo, cat_enum=MCategory.get_qian2(catid[:2]), tag_infos=MCategory.query_all(by_order=True, kind=self.kind), tag_infos2=MCategory.query_all(by_order=True, kind=self.kind), app2tag_info=MPost2Catalog.query_by_entity_uid(infoid, kind=self.kind).objects(), app2label_info=MPost2Label.get_by_uid(infoid).objects())
def remove_collect(self, post_id): ''' Add or update the category. ''' logger.info('Collect info: user-{0}, uid-{1}'.format( self.userinfo.uid, post_id)) MCollect.remove_collect(self.userinfo.uid, post_id) out_dic = {'success': True} return json.dump(out_dic, self)
def add_or_update(self, app_id): ''' Add or update the category. ''' logger.info('Collect info: user-{0}, uid-{1}'.format( self.userinfo.uid, app_id)) MCollect.add_or_update(self.userinfo.uid, app_id) out_dic = {'success': True} return json.dump(out_dic, self)
def viewinfo(self, postinfo): ''' In infor. :param postinfo: :return: ''' self.redirect_kind(postinfo) ###################################################### if DB_CFG['kind'] == 's': cat_enum1 = [] else: ext_catid = postinfo.extinfo['def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else '' ext_catid2 = postinfo.extinfo[ 'def_cat_uid'] if 'def_cat_uid' in postinfo.extinfo else None cat_enum1 = MCategory.get_qian2(ext_catid2[:2]) if ext_catid else [] ###################################################### catinfo = None p_catinfo = None post2catinfo = MPost2Catalog.get_first_category(postinfo.uid) catalog_infors = None if post2catinfo: catinfo = MCategory.get_by_uid(post2catinfo.tag_id) if catinfo: p_catinfo = MCategory.get_by_uid(catinfo.pid) catalog_infors = MPost2Catalog.query_pager_by_slug(catinfo.slug, current_page_num=1, order=True) kwd = self._the_view_kwd(postinfo) MPost.update_misc(postinfo.uid, count=True) if self.get_current_user(): MUsage.add_or_update(self.userinfo.uid, postinfo.uid, postinfo.kind) tmpl = 'post_{0}/leaf_view.html'.format(self.kind) logger.info('The Info Template: {0}'.format(tmpl)) self.render(tmpl, kwd=dict(kwd, **self.ext_view_kwd(postinfo)), postinfo=postinfo, userinfo=self.userinfo, catinfo=catinfo, pcatinfo=p_catinfo, unescape=tornado.escape.xhtml_unescape, ad_switch=random.randint(1, 18), tag_info=MPost2Label.get_by_uid(postinfo.uid), catalog_infos=catalog_infors, cat_enum=cat_enum1)
def get(self, url_str=''): url_arr = self.parse_url(url_str) logger.info('infocat get url_str: {0}'.format(url_str)) if len(url_str) == 4: self.list(url_str) elif len(url_str) > 4: self.echo_html(url_str) else: self.render('html/404.html', kwd={})
def get(self, *args, **kwargs): url_str = args[0] logger.info('infocat get url_str: {0}'.format(url_str)) if len(url_str) == 4: self.list(url_str) elif len(url_str) > 4: self.echo_html(url_str) else: self.render('misc/html/404.html', kwd={})
def add(self, post_id): post_data = self.get_post_data() post_data['user_name'] = self.userinfo.user_name post_data['user_id'] = self.userinfo.uid post_data['post_id'] = post_id replyid = MReply.create_reply(post_data) if replyid: out_dic = {'pinglun': post_data['cnt_reply'], 'uid': replyid} logger.info('add reply result dic: {0}'.format(out_dic)) return json.dump(out_dic, self)
def get_id_by_impath(path): logger.info('Get Entiry, Path: {0}'.format(path)) uu = g_Entity.select().where(g_Entity.path == path) if uu.count() == 1: return uu.get().uid elif uu.count() > 1: for rec in uu: MEntity.delete(rec.uid) return False else: return False
def _change_kind(self, post_uid): ''' To modify the category of the post, and kind. ''' post_data = self.get_post_data() logger.info('admin post update: {0}'.format(post_data)) MPost.update_misc(post_uid, kind=post_data['kcat']) self.update_category(post_uid) self.redirect('/{0}/{1}'.format(router_post[post_data['kcat']], post_uid))
def create_page(slug, post_data): ''' The page would be created with slug. ''' logger.info('Call create Page') if MWiki.get_by_uid(slug): return False title = post_data['title'].strip() if len(title) < 2: return False return MWiki.__create_rec(slug, '2', post_data=post_data)
def echo_html(self, url_str): ''' Show the HTML ''' logger.info('info echo html: {0}'.format(url_str)) condition = self.gen_redis_kw() url_arr = self.parse_url(url_str) sig = url_arr[0] post_data = self.get_post_data() sort_option = post_data.get('sort', '') num = (len(url_arr) - 2) // 2 catinfo = MCategory.get_by_uid(sig) if catinfo.pid == '0000': condition['def_cat_pid'] = sig else: condition['def_cat_uid'] = sig fenye_num = 1 for idx in range(num): ckey = url_arr[idx * 2 + 2] tval = url_arr[idx * 2 + 3] if tval == '0': continue if ckey == 'fenye': # 分页参数。单独处理。 fenye_num = int(tval) continue else: cval = tval ckey = 'tag_' + ckey condition[ckey] = cval if url_arr[1] == 'con': infos = MPost.query_list_pager(condition, fenye_num, kind=catinfo.kind, sort_option=sort_option) self.echo_html_list_str(sig, infos, catinfo) elif url_arr[1] == 'num': allinfos = MPost.query_under_condition(condition, kind=catinfo.kind, sort_option=sort_option) self.write( tornado.escape.xhtml_unescape( echo_html_fenye_str(allinfos.count(), fenye_num)))
def get_id_by_impath(path): logger.info('Get Entiry, Path: {0}'.format(path)) entity_list = TabEntity.select().where(TabEntity.path == path) if entity_list.count() == 1: return entity_list.get().uid elif entity_list.count() > 1: for rec in entity_list: MEntity.delete(rec.uid) return False else: return False
def update_post(self, postid): ''' The rating of Post should be updaed if the count is greater than 10 ''' voted_recs = MRating.query_by_post(postid) if voted_recs.count() > 10: rating = MRating.query_average_rating(postid) else: rating = 5 logger.info('Get post rating: {rating}'.format(rating=rating)) # MPost.__update_rating(postid, rating) MPost.update_misc(postid, rating=rating)
def post(self, *args, **kwargs): url_str = args[0] logger.info('Post url: {0}'.format(url_str)) url_arr = self.parse_url(url_str) if url_arr[0] in ['_add']: print("*" * 50) print(len(url_arr)) self.add() else: self.show404()
def post(self, url_str=''): logger.info('post url: {0}'.format(url_str)) url_arr = self.parse_url(url_str) if url_arr[0] in ['modify', 'edit', '_edit']: self.update(url_arr[1]) elif url_arr[0] in ['add_document', '_add']: self.add() elif len(url_arr) == 1: if len(url_str) in [4, 5]: self.add(url_str) else: self.redirect('html/404.html')
def get_by_id(self, reply_id): reply = MReply.get_by_uid(reply_id) logger.info('get_reply: {0}'.format(reply_id)) self.render('misc/reply/show_reply.html', reply=reply, username=reply.user_name, date=reply.date, vote=reply.vote, uid=reply.uid, userinfo=self.userinfo, unescape=tornado.escape.xhtml_unescape)
def reset_password(self): ''' Do reset password :return: ''' post_data = self.get_post_data() if 'email' in post_data: userinfo = MUser.get_by_email(post_data['email']) if tools.timestamp() - userinfo.time_reset_passwd < 70: self.set_status(400) kwd = { 'info': '两次重置密码时间应该大于1分钟', 'link': '/user/reset-password', } self.render('misc/html/404.html', kwd=kwd, userinfo=self.userinfo) return False if userinfo: timestamp = tools.timestamp() passwd = userinfo.user_pass username = userinfo.user_name hash_str = tools.md5(username + str(timestamp) + passwd) url_reset = '{0}/user/reset-passwd?u={1}&t={2}&p={3}'.format( config.SITE_CFG['site_url'], username, timestamp, hash_str) email_cnt = '''<div>请查看下面的信息,并<span style="color:red">谨慎操作</span>:</div> <div>您在"{0}"网站({1})申请了密码重置,如果确定要进行密码重置,请打开下面链接:</div> <div><a href={2}>{2}</a></div> <div>如果无法确定本信息的有效性,请忽略本邮件。</div>'''.format(config.SMTP_CFG['name'], config.SITE_CFG['site_url'], url_reset) if send_mail([userinfo.user_email], "{0}|密码重置".format(config.SMTP_CFG['name']), email_cnt): MUser.update_time_reset_passwd(username, timestamp) self.set_status(200) logger.info('password has been reset.') return True else: self.set_status(400) return False else: self.set_status(400) return False else: self.set_status(400) return False