def verify(user_id, code): session = SESSION() try: if not code.isdigit(): return None nmc = session.query(NewMemberCaptcha).filter_by( user_id=user_id, is_verify=0, verify_code=int(code)).first() if not nmc: # sql = ''' # SELECT id FROM new_member_captcha WHERE verify_code={} # '''.format(code) # res = session.execute(sql) # message = "" # for r in res: # message += str(r) return None # if code != str(nmc.verify_code): # return "验证码错误" nmc.is_verify = 1 session.commit() return nmc.group_id except Exception as e: return None finally: session.close()
def find_lib(lib_name): session = SESSION() try: return session.query(PythonLibs).filter_by(name=lib_name).first() except Exception as e: return None finally: session.close()
def find_out_date(): session = SESSION() try: return session, session.query(NewMemberCaptcha).filter( time.time() - NewMemberCaptcha.insert_time > 300, NewMemberCaptcha.is_verify == 0).all() except Exception as e: return session, []
def search_captcha(**kwargs): session = SESSION() try: return session.query(NewMemberCaptcha).filter_by(**kwargs).first() except Exception as e: return None finally: session.close()
def insert_point(**kwargs): session = SESSION() try: roll = RollHistory(**kwargs) session.add(roll) session.commit() except Exception as e: return e finally: session.close()
def get_need_increase(): session = SESSION() try: days = session.query(Days).all() for d in days: yield d except Exception as e: pass finally: session.close()
def count_my_roll(group_id, user_id): session = SESSION() try: res = session.query(func.sum(RollHistory.point).label('s')).filter_by( group_id=group_id, user_id=user_id).first() if res: return res[0] return 0 except Exception as e: return 0 finally: session.close()
def find_lang_ref(many=False, **kwargs): session = SESSION() try: res = session.query(PythonLangRef).filter_by(**kwargs) if not many: return res.first() else: return res.all() except Exception as e: return None finally: session.close()
def count_toady_roll(**kwargs): session = SESSION() try: today_start = time.mktime(datetime.today().date().timetuple()) today_end = today_start + 86400 res = session.query(RollHistory).filter_by(**kwargs).filter( today_start < RollHistory.roll_time, today_end > RollHistory.roll_time) return res.count() except Exception as e: return 0 finally: session.close()
def increase_one(user_id): session = SESSION() try: d = session.query(Days).filter_by(user_id=user_id).first() if not d: return d.days += 1 session.commit() except Exception as e: print(e) session.rollback() finally: session.close()
def count_most_times(group_id): session = SESSION() try: res = session.query( RollHistory.user_id, func.count(RollHistory.user_id).label('t')).filter_by( group_id=group_id).group_by(RollHistory.user_id).order_by( desc('t')).first() if res: return res return 0, 0 except Exception as e: return 0, 0 finally: session.close()
def insert_lang_ref(**kwargs): session = SESSION() try: lang = session.query(PythonLangRef).filter_by( title1=kwargs.get('title1'), title2=kwargs.get('title2')).first() if lang: lang.update(**kwargs) else: lang = PythonLangRef(**kwargs) session.add(lang) session.commit() except Exception as e: pass finally: session.close()
def insert_new_captcha(**kwargs): ''' :param kwargs: group_id, user_id, verify_code :return: ''' nmc = NewMemberCaptcha(**kwargs) session = SESSION() try: session.add(nmc) session.commit() except Exception as e: pass finally: session.close()
def insert_lib(**kwargs): session = SESSION() try: lib = session.query(PythonLibs).filter_by( name=kwargs.get('lib_name')).first() if lib: lib.update(**kwargs) else: lib = PythonLibs(**kwargs) session.add(lib) session.commit() except Exception as e: pass finally: session.close()
def to_zero(user_id): session = SESSION() try: d = session.query(Days).filter_by(user_id=user_id).first() if not d: d = Days(user_id=user_id, days=0) session.add(d) session.commit() return d.days = 0 session.commit() return True except Exception as e: session.rollback() finally: session.close()