def log(self, message, *args): """Log to client console.""" ts = u.current_timestamp() if args: message = message % args print("%s %s: %s" % (ts, self.name, message))
def update_child( db: Session, user_id: int, child_id: int, id_card_no: str = None, realname: str = None, nickname: str = None, birth_ts: int = None, gender: int = None) -> int: m_child = ChildModel(id=child_id) if id_card_no: m_child.id_card_no = id_card_no, if realname: m_child.realname = realname if nickname: m_child.nickname = nickname if birth_ts is not None: m_child.birth_ts = birth_ts if gender is not None: m_child.gender = gender m_child.update_ts = current_timestamp() db.merge(m_child) db.commit() return m_child
def create_time_sharing(db: Session, user_id: int, carer_info: UserCarerInfoModel, start_ts: int, end_ts: int, price: int, activity_tags: List[str], description: str, accompany_required: bool) -> int: m_time_sharing = TimeSharingModel( # data copy from publisher user_id=user_id, child_age_min=carer_info.child_age_min, child_age_max=carer_info.child_age_max, child_count_max=carer_info.child_count_max, address_id=carer_info.address_id, # data new for this instance start_ts=start_ts, end_ts=end_ts, price=price, activity=','.join([str(i) for i in activity_tags]), description=description, accompany_required=accompany_required, child_count=0, # data for common create_ts=current_timestamp(), status=0) db.add(m_time_sharing) db.commit() return m_time_sharing.id
def upsert_user_info( db: Session, user_id: int, id_card_no: str = None, realname: str = None, mobile: str = None, child_relation: int = None, avatar_oss: str = None, nickname: str = None, born: bool = None): m_user_info = UserInfoModel(user_id=user_id) if id_card_no: m_user_info.id_card_no = id_card_no if realname: m_user_info.realname = realname if mobile: m_user_info.mobile = mobile if child_relation: m_user_info.child_relation = child_relation if avatar_oss: m_user_info.avatar_oss = avatar_oss if nickname is not None: m_user_info.nickname = nickname if born is not None: m_user_info.born = born m_user_info.update_ts = current_timestamp() db.merge(m_user_info) db.commit() return user_id
def create_child_transaction( db: Session, user_id: str, id_card_no: str = None, realname: str = None, nickname: str = None, birth_ts: int = None, gender: int = None) -> int: try: m_child = ChildModel( id_card_no=id_card_no, realname=realname, nickname=nickname, birth_ts=birth_ts, gender=gender, status=0, create_ts=current_timestamp() ) db.add(m_child) db.flush() m_user_child = UserChildModel( user_id=user_id, child_id=m_child.id, create_ts=current_timestamp() ) db.add(m_user_child) db.commit() except Exception as e: # TODO: LOG exception db.rollback() print(e) child_id = m_child.id if m_child else None raise ORMCreateChildFailed(user_id, child_id) return m_child.id
def approve_carer_application_transaction(db: Session, user_id: int): application = db.query(CarerApplicationModel).filter( CarerApplicationModel.user_id == user_id).first() if not application: raise exception.NonExistentCarerApplication(user_id, None) try: # 1. Copy carer info from `carer_application` to `user_carer_info` carer_info = UserCarerInfoModel( user_id=user_id, intro_video_id=application.intro_video_id, playground_video_id=application.playground_video_id, extra_video_ids=application.extra_video_ids, address_id=application.address_id, birth_certificate_oss=application.birth_certificate_oss, degree=application.degree, care_exp=application.care_exp, child_count_max=application.child_count_max, child_age_min=application.child_age_min, child_age_max=application.child_age_max, update_ts=current_timestamp()) db.merge(carer_info) # 2. Set `carer_application` record status to `Approved` application.status = CarerApplicationStatus.Approved.value application.update_ts = current_timestamp() db.commit() except Exception as e: # TODO: LOG print('approve_carer_application_transaction', str(e)) db.rollback() raise
def add_crypto_position(symbol, price, quantity): with _DbContext() as ctx: pos_db = ctx.positions().read() id = pos_db['id_counter'] pos_db['id_counter'] += 1 timestamp = util.current_timestamp() data = { "id": id, "symbol": symbol, "timestamp": timestamp, "unit_price": str(Decimal(price)), "total_price": str(util.safe_mult(price, quantity)), "quantity": str(Decimal(quantity)), } pos_db['crypto'].append(data) ctx.positions().write(pos_db) return data
def create_carer_application_transaction( db: Session, user_id: int, intro_video_id: int, playground_video_id: int, extra_video_ids: List[int], address_id: int, birth_certificate_oss: str, degree: int, care_exp: int, child_count_max: int, child_age_min: int, child_age_max: int) -> CarerApplicationModel: try: """ # 1. Set Reviewing applications to Disabled reviewing_applications = db.query(CarerApplicationModel).filter( CarerApplicationModel.user_id == user_id, CarerApplicationModel.status==CarerApplicationStatus.Reviewing.value ).all() for r in reviewing_applications: r.status = CarerApplicationStatus.Disabled.value """ application = CarerApplicationModel( user_id=user_id, intro_video_id=intro_video_id, playground_video_id=playground_video_id, extra_video_ids=','.join([str(i) for i in extra_video_ids]), address_id=address_id, birth_certificate_oss=birth_certificate_oss, degree=degree, care_exp=care_exp, child_count_max=child_count_max, child_age_min=child_age_min, child_age_max=child_age_max, create_ts=current_timestamp(), status=CarerApplicationStatus.Reviewing.value) db.merge(application) db.commit() return application except Exception as e: # TODO: LOG print('create_carer_application_transaction', str(e)) db.rollback() raise
def create_user_guardian(db: Session, user_id: int, id_card_no: str, realname: str, mobile: str) -> int: try: m_guardian = UserGuardianModel( user_id=user_id, id_card_no=id_card_no, realname=realname, mobile=mobile, create_ts=current_timestamp(), status=0 ) db.add(m_guardian) db.commit() return m_guardian except IntegrityError as exc: db.rollback() if is_duplicate_entry_exception(exc): return m_guardian else: raise
def create_user_address(db: Session, user_id: int, lat: Decimal, lng: Decimal, province: str = None, city: str = None, city_id: int = None, district: str = None, address: str = None, name: str = None, room: str = None, poi: str = None) -> int: """ Create `user_address` record if not existed. Igonre `Duplicate Entry` error. """ try: m_address = UserAddressModel(user_id=user_id, lat=lat, lng=lng, province=province, city=city, city_id=city_id, district=district, address=address, name=name, room=room, status=0, poi=poi, create_ts=current_timestamp()) db.add(m_address) db.commit() return m_address except IntegrityError as exc: db.rollback() if is_duplicate_entry_exception(exc): return m_address else: raise
def create_video(db: Session, cloud: str, bucket: str, key: str, etag: str = None, mime_type: str = None, size: int = None, duration: int = None, width: int = None, height: int = None, persistent_id: str = None) -> VideoModel: """ Create `video` record if not existed. Igonre `Duplicate Entry` error. """ try: video = VideoModel(cloud=cloud, bucket=bucket, key=key, etag=etag, mime_type=mime_type, size=size, duration=duration, width=width, height=height, persistent_id=persistent_id, status=0, create_ts=current_timestamp()) db.add(video) db.commit() return video except IntegrityError as exc: db.rollback() if is_duplicate_entry_exception(exc): return video else: raise
os.dup2(self.wfile.fileno(), sys.stdout.fileno()) os.execve(interp, args, env) except: self.server.handle_error(self.request, self.client_address) os._exit(127) return if __name__ == '__main__': if sys.argv[1:]: port = int(sys.argv[1]) else: print "need port" sys.exit(1) httpd = BaseHTTPServer.HTTPServer(("", port), CGIHandler) # add fd_cloexec flag to socket util.set_cloexec_flag(httpd.fileno()) print "serving at port", port sdir = sys.path[0].rstrip('/') pidfile = sdir + '/server.pid' logdir = sdir + '/log' if not os.path.isdir(logdir): os.mkdir(logdir) logfile = logdir + '/server.log' util.daemon(pidfile, stdout = logfile, stderr = logfile) sys.stdout.write('server start %s\n' % util.current_timestamp()) httpd.serve_forever()
def log(message, *args): """Log to client console.""" ts = u.current_timestamp() if args: message = message % args print(message)
cgitb.enable() print "Content-Type: text/html;charset=UTF-8" # HTML is following print # blank line, end of headers form = cgi.FieldStorage() if 'upfile' not in form: util.errquit('need upfile') if 'target' not in form: util.errquit('need target') target = form.getvalue('target') if os.path.isdir(target): util.errquit('target path error') dirname = os.path.dirname(target) if len(dirname.strip()) == 0: util.errquit('target miss dir') if not os.path.isdir(dirname): util.errquit('target dir ' + dirname + ' is not exist') fileitem = form['upfile'] if not fileitem.file: util.errquit('can not receive upfile') now = datetime.datetime.now() tmpfile = target + '.' + util.current_timestamp() fwriter = open(tmpfile, 'w') while True: tmp = fileitem.file.read(1024) if tmp: fwriter.write(tmp) else: break; fwriter.close() util.syscmd("mv " + tmpfile + " " + target)