def get(self, id=None): if id is None: sql = ''' select * from rich_text order by created_date desc limit 100 ''' biographys = list(pg.query(sql)) self.render('../dist/biography.html', biographys=biographys) else: sql = ''' select * from rich_text where id=%s ''' % id biographys = list(pg.query(sql)) if biographys: biography = biographys[0] # 让img能响应式 soup = BeautifulSoup(biography.text) for img in soup.find_all('img'): img['class'] = 'ui image' del img['width'] del img['height'] biography.text = str(soup) god = {'img': ''} self.render('../dist/biography_detail.html', biography=biography, god=god)
def sync(type, main, god_name=None, wait=None, must_followed=None): ''' create by bigzhu at 16/03/26 18:58:06 同步的公用 create by bigzhu at 16/05/01 09:04:36 只查有人关注的出来 modify by bigzhu at 16/05/27 10:58:00 所有都查,因为要先查了再follow modify by bigzhu at 16/05/27 10:59:24 改为查 god 表 modify by bigzhu at 16/05/27 14:13:11 有的时候不用wait remain modify by bigzhu at 16/05/30 14:33:59 要有人关注的再sync modify by bigzhu at 16/05/30 17:12:36 加入参数 ''' sql = ''' select * from god where %s is not null and %s!='' ''' % (type, type) if must_followed: sql += " and id in (select god_id from follow_who) " if god_name: sql += " and name='%s'" % god_name sql = filter_bz.filterNotBlackGod(sql) users = pg.query(sql) for user in users: # print 'checking %s %s' % (type, user[type]) main(user, wait)
def put(self): self.set_header("Content-Type", "application/json") data = json.loads(self.request.body) social_name = data['social_name'] if social_name == '' or social_name is None: raise Exception('必须有god名字才能修改') type = data['type'] if type == '' or type is None: raise Exception('必须有type才能修改') count = public_db.delNoName(type, social_name) # if count != 1: # raise Exception("修改失败 type:%s social_name: %s count: %s" % (type, social_name, count)) sql = ''' update apply_del set stat=1 where social_name='%s' and type='%s' and stat is null''' % ( social_name, type) count = pg.query(sql) if count != 1: raise Exception("修改失败" + count) self.write( json.dumps({ 'error': '0', 'count': count }, cls=json_bz.ExtEncoder))
def getMyGods(user_id, limit, before=None, cat=None): ''' get my gods >>> getMyGods('1', 10) <utils.IterBetter instance at ...> ''' sql = ''' select g.id as god_id, g.stat_date as u_stat_date, * from god g ''' sql = filter_bz.filterHaveSocialGod(sql) sql = add_bz.addGodFollowedCount(sql) sql = add_bz.godAdminRemark(sql) sql = addGodfolloweInfoByUserId(sql) sql = filterFollowed(sql) sql = filter_bz.godNotBlock(sql, user_id) sql = add_bz.godUserRemark(sql, user_id) if before: sql = filterBeforeCreatedDate(sql) if cat: sql = filterCat(sql) sql += " order by followed_at desc " if limit: sql += ' limit $limit ' return pg.query(sql, vars=locals())
def getMidAndCsrfTokenHolder(user_id, reset_cookie=False): sql = ''' select * from anki where user_id='%s' ''' % user_id datas = pg.query(sql).list() if (len(datas) == 0): raise Exception('你还没有配置Anki信息') print(datas) data = datas[0] if data.mid is not None and not reset_cookie: return data.mid, data.csrf_token, data.cookie mid, csrf_token, cookie = getMidAndCsrfToken(data.user_name, data.password) sql = ''' update anki set mid='%s', csrf_token='%s', cookie='%s' where user_id='%s' ''' % (mid, csrf_token, cookie, user_id) pg.query(sql) return mid, csrf_token, cookie
def queryUnreadCount(after, user_id=None): ''' create by bigzhu at 16/12/14 17:14:49 取未读数 ''' sql = ' select * from all_message s ' sql = filter_bz.filterFollowedMessages(sql, user_id) sql = filter_bz.filterAfterMessages(sql, after) sql = wrapCount(sql) return pg.query(sql)[0].count
def get(self): self.set_header("Content-Type", "application/json") sql = ''' select * from apply_del where stat is null order by created_date desc ''' apply_dels = pg.query(sql) self.write( json.dumps({ 'error': '0', 'apply_dels': apply_dels }, cls=json_bz.ExtEncoder))
def get(self): self.set_header("Content-Type", "application/json") sql = 'select user_name, password from anki where user_id=$user_id' datas = pg.query(sql, vars={'user_id': self.current_user}) if datas: data = datas[0] else: data = None self.write( json.dumps({ 'error': '0', 'anki': data }, cls=json_bz.ExtEncoder))
def get(self, parm): self.set_header("Content-Type", "application/json") parm = json.loads(parm) count = parm.get('count', None) # is_public = parm.get('is_public', None) user_id = self.current_user if count: # 只查总数 sql = ''' select count(id) from block where user_id='%s' ''' % user_id self.data.count = pg.query(sql)[0].count self.write(json.dumps(self.data, cls=json_bz.ExtEncoder))
def getSocialGods(type, god_name=None): ''' create by bigzhu at 17/05/21 18:11:06 获取有某个社交数据的 gods >>> getSocialGods('twitter') <utils.IterBetter instance at ...> ''' sql = ''' select * from god where %s is not null and %s->'name' <> to_jsonb(''::text) ''' % (type, type) if god_name: sql += " and name='%s'" % god_name return pg.query(sql)
def getTheGodInfo(god_id, user_id): ''' >>> getTheGodInfo(1, '1') <Storage...> ''' sql = ''' select id as god_id, * from god where id=$god_id ''' sql = addGodfolloweInfoByUserId(sql) sql = add_bz.godAdminRemark(sql) sql = add_bz.godUserRemark(sql, user_id) result = pg.query(sql, vars=locals()) if (not result): raise Exception('未找到这个 god ' + god_id) return result[0]
def makeSureSocialUnique(type, name): ''' create by bigzhu at 17/05/20 08:35:04 确保 Social 不重复 >>> makeSureSocialUnique('twitter', 'bigzhu') >>> makeSureSocialUnique('twitter', 'bigzhu1') '{"name": "bigzhu1"}' ''' sql = ''' select * from god where %s ->>'name'::text = $name ''' % type if (pg.query(sql, vars=locals())): return None else: return json.dumps({'name': name})
def getCollectMessages(user_id): ''' create by bigzhu at 16/05/29 07:30:11 just collect message ''' sql = ''' select * from all_message m ''' sql = add_bz.messagesCollect(sql, user_id) sql = add_bz.messagesAnkiSave(sql, user_id) sql = ''' select * from (%s) s where collect is not null ''' % sql # order by sql += ' order by collect_date desc ' return pg.query(sql)
def getMessage(id, user_id=None): ''' create by bigzhu at 16/05/29 07:49:51 查看具体 ''' sql = ''' select * from all_message m where id = %s ''' % id if user_id: sql = add_bz.messagesCollect(sql, user_id) sql = add_bz.messagesAnkiSave(sql, user_id) else: # 不给看18+ sql = ''' select m.*, null as collect, null as collect_date from (%s) m ''' % sql return pg.query(sql)
def getNewMessages(user_id=None, after=None, limit=None, god_name=None, search_key=None, m_type=None): ''' create by bigzhu at 16/05/28 22:01:58 查new的,根据mind图重构 ''' sql = ''' select * from all_message m ''' if user_id: sql = add_bz.messagesCollect(sql, user_id) sql = add_bz.messagesAnkiSave(sql, user_id) else: # 不给看18+ sql = filter_bz.messageNot18(sql) # 只能看 public god 的 message sql = filter_bz.filterPublicGodMessages(sql) # 封住,以直接加where sql = ''' select * from (%s) s ''' % sql # 查比这个时间新的 if after: sql += " where created_at > '%s' " % after # 再封 sql = ''' select * from (%s) s ''' % sql # 互斥的filter_bz.filter if god_name: sql = filter_bz.messageThisGod(sql, god_name) elif search_key: # sql += " where upper(s.text) like '%%%s%%' or upper(s.content::text) like '%%%s%%' " % (search_key.upper(), search_key.upper()) sql = filter_bz.filterSearchKey(sql, search_key) else: sql = filter_bz.filterFollowedMessages(sql, user_id) # sql = filter_bz.messagesEffSocial(sql) if m_type: sql += " and s.m_type='%s' " % m_type # order by sql += ' order by created_at ' # limit if limit is None and after is None: limit = 99 sql += ' limit %s ' % limit return pg.query(sql)
def checkOtherNameSocialNameUnique(god_name, social_name, type): ''' create by bigzhu at 17/05/21 16:39:02 检查其他的god下是否已有相同名字的 social >>> checkOtherNameSocialNameUnique('bigzhu', 'bigzhu', 'twitter') >>> checkOtherNameSocialNameUnique('bigzhu2', 'bigzhu', 'twitter') Traceback (most recent call last): ... Exception: 已有别人绑定了 twitter bigzhu, 修改失败 ''' if social_name == '': # 要删除的, 不用管 return sql = ''' select * from god where name <> $god_name and %s ->>'name'::text = $social_name ''' % type if (pg.query(sql, vars=locals())): raise Exception('已有别人绑定了 %s %s, 修改失败' % (type, social_name))
def getOldMessages(before, user_id=None, limit=None, god_name=None, search_key=None, m_type=None): ''' create by bigzhu at 16/05/29 08:06:28 查老的 ''' sql = ''' select * from all_message ''' if user_id: sql = add_bz.messagesCollect(sql, user_id) sql = add_bz.messagesAnkiSave(sql, user_id) else: # 不给看18+ sql = filter_bz.messageNot18(sql) # 封住,以直接加where sql = ''' select * from (%s) s ''' % sql # 互斥的filter_bz.filter if god_name: sql = filter_bz.messageThisGod(sql, god_name) elif search_key: sql = filter_bz.filterSearchKey(sql, search_key) else: sql = filter_bz.filterFollowedMessages(sql, user_id) sql = filter_bz.filterMTYpe(sql, m_type) sql = filter_bz.filterBefore(sql, before) # order by if search_key is None: sql += ' order by created_at desc ' # limit if limit is None: limit = 10 sql += ' limit %s ' % limit return pg.query(sql)
def followedWho(user_id): sql = ''' select god_id from follow_who where user_id='%s' ''' % user_id return pg.query(sql)
def getGodInfoFollow(user_id=None, god_name=None, recommand=False, is_my=None, cat=None, is_public=None, limit=None, before=None, blocked=None): ''' modify by bigzhu at 15/08/06 17:05:22 可以根据god_name来取 modify by bigzhu at 15/08/28 17:09:31 推荐模式就是只查随机5个 modify by bigzhu at 15/08/28 17:30:38 没有社交帐号的不要查出来 modify by bigzhu at 16/03/24 21:46:07 可以只查我关注的 modify by bigzhu at 16/05/24 23:21:36 关联到god表,可以只查某种类别 modify by bigzhu at 16/05/27 22:16:30 没人关注的也查出来 modify by bigzhu at 16/06/21 12:15:24 add xmind, 整理了sql modify by bigzhu at 17/01/13 10:51:33 juest select blocked god ''' sql = ''' select g.id as god_id, g.stat_date as u_stat_date, * from god g ''' sql = filter_bz.filterHaveSocialGod(sql) sql = add_bz.addGodFollowedCount(sql) sql = add_bz.godAdminRemark(sql) # 只查有人关注的 # sql = ''' # select * from (%s) s where s.id in (select god_id from follow_who) # ''' % sql if user_id: # followed info sql = god_oper.addGodfolloweInfoByUserId(sql) if (blocked): sql = filter_bz.godBlock(sql, user_id) else: sql = filter_bz.godNotBlock(sql, user_id) sql = add_bz.godUserRemark(sql, user_id) if recommand: sql = ''' select * from (%s) s where s.followed=0 or s.followed is null ''' % sql if not cat: sql = filter_bz.messageNot18(sql) if is_my: sql = ''' select * from (%s) s where s.followed=1 ''' % sql else: if recommand: sql = filter_bz.messageNot18(sql) if cat: sql = ''' select * from (%s) s where cat='%s' ''' % (sql, cat) if god_name: sql = ''' select * from (%s) s where name='%s' ''' % (sql, god_name) if is_public: sql = filter_bz.filterPublicGod(sql) if before: sql = ''' select * from (%s) s where created_date < '%s' ''' % (sql, before) sql += " order by created_date desc " if limit: sql += ' limit %s ' % limit return pg.query(sql, vars=locals())
def getFollowedGodCount(user_id): sql = ''' select count(god_id) from follow_who where user_id=$user_id ''' count = pg.query(sql, vars=locals())[0].count return count