def met(self, user_id, last_id, limit, base_url): met_list = self.meet.get_meet(user_id, last_id) blacks = self.friend.get_black_by_user(user_id) black_list = [b.friend_account_id for b in blacks] if blacks else list() data = list() count = 0 for met in met_list: another_user_id = met.user_id if met.another_user_id == int( user_id) else met.another_user_id if another_user_id in black_list: continue temp = self.user.get_friend_information(user_id, another_user_id, met.apply_id, base_url) if temp: temp["last_id"] = met.id temp["apply_id"] = met.apply_id temp["created_at"] = OHHODatetime.clock2string( OHHODatetime.utc2beijing(met.created_at)) data.append(temp) count += 1 if limit and int(limit) > 0: if count >= int(limit): break result = Result.result_success() result["data"] = data return result
def _execute_time(self): start_time = OHHODatetime.get_current_timestamp() result = func(self) end_time = OHHODatetime.get_current_timestamp() OHHOLog.print_log(end_time) OHHOLog.print_log(start_time) OHHOLog.print_log(end_time - start_time) return result
def get_meet_in24hour_user_ids(self, user_id): current = OHHODatetime.get_now() current_utc = OHHODatetime.beijing2utc(current) current_utc_before24hour = OHHODatetime.get_some_hour_after( current_utc, -24) query = self.met.get_query() query = self.met.get_great_than_equal_created_at( query, current_utc_before24hour) query1 = self.met.get_by_user_id(query, user_id) user1_id_list = [m.another_user_id for m in query1] query2 = self.met.get_by_another_user_id(query, user_id) user2_id_list = [m.user_id for m in query2] return list(set(user2_id_list + user1_id_list))
def print_log(information=None): try: raise Exception except: f = sys.exc_info()[2].tb_frame.f_back now = OHHODatetime.get_now() now_string = OHHODatetime.clock2string(now) file_name = f.f_code.co_filename name = f.f_code.co_name line_no = f.f_lineno content = now_string + "\t" + file_name + "\t" + name + "\t" + str( line_no) + "\t" + str(information) logger.info(content)
def add_without_commit(model, obj_dict): try: instance = model() obj_dict["created_at"] = OHHODatetime.get_utc_now() obj_dict["changed_at"] = OHHODatetime.get_utc_now() if not obj_dict.get("timestamp", None): obj_dict["timestamp"] = OHHODatetime.get_current_timestamp() for key, value in obj_dict.items(): Operation.set_attribute(instance, key, value) session.add(instance) return instance except Exception as ex: OHHOLog.print_log(ex) return None
def get(self): user = DBOHHOUser() query = user.get_query() total_count = user.get_count(query) today_start = OHHODatetime.get_today_start() utc_today_start = OHHODatetime.beijing2utc(today_start) today_query = user.get_great_than_equal_created_at( query, utc_today_start) today_count = user.get_count(today_query) return self.render( STATISTICS_USER_HTML, total_count=total_count, today_count=today_count, )
def get_near_map(self, user_id, delta_time=5 * 1000): timestamp = OHHODatetime.get_current_timestamp() timestamp = timestamp - delta_time query = self.map.filter_by_user(user_id) query = self.map.get_great_than_equal_timestamp(query, timestamp) query = self.map.order_by_id_desc(query) return query
def send(self, user_id, friend_user_id, content, type): result = Result.result_success() data = dict() data["account_id"] = user_id data["another_account_id"] = friend_user_id data["message"] = content success = self.message.add(data) if success: OHHOLog.print_log("add message to database successfully!") else: OHHOLog.print_log("add message to database failed!") result["add2db"] = success message = dict() msg = dict() msg["content"] = content msg["user_id"] = user_id msg["type"] = type msg["current_timestamp"] = OHHODatetime.get_current_timestamp() message["msg"] = msg success = self.send_message.send_attach_message( user_id, friend_user_id, OHHOOperation.dict2json(message)) OHHOLog.print_log(success) result["send2im"] = success return result
def post(self): instance = User() the_post = Post() country_code = the_post.get_cellphone_country_code(self) username = the_post.get_username(self) password = the_post.get_password(self) if not username or not password or not country_code: message = "username or password or country_code is empty" return self.redirect("/backstage/login/?data=%s" % message) else: success, the_username = instance.check_user4backstage( username, password, country_code) if success: OHHOLog.print_log("login success") user_instance = instance.get_by_username(the_username) if user_instance: instance.token.add(user_instance.id) update_success = instance.update_user( user_instance, {"last_login": OHHODatetime.get_utc_now()}) OHHOLog.print_log(update_success) else: OHHOLog.print_log("no such user") self.set_secure_cookie("username", the_username) return self.redirect("/backstage/home/") if the_username: message = "password is incorrect" else: message = "user does not exist" return self.redirect("/backstage/login/?data=%s" % message)
def get(self, one_user_id, another_user_id): """ 根据发起人和接收人获取最新的配对请求, 并且这个请求只在30分钟内有效 :param data_dict: :return: """ if one_user_id and another_user_id: apply = self.apply query = apply.get_query() query = apply.get_by_one_user(query, one_user_id) query = apply.get_by_another_user(query, another_user_id) query = apply.order_by_id_desc(query) query = apply.first(query) if query: # 因为配对请求只能添加不能更改(没有地方会更改), # 所以这里的timestamp是添加时的timestamp if query.timestamp + VALID_INTERVAL_MILLISECOND > OHHODatetime.get_current_timestamp(): return query else: OHHOLog.print_log(VALID_MATCH_APPLY_NOT_EXIST) else: OHHOLog.print_log(MATCH_APPLY_NOT_EXIST) else: OHHOLog.print_log(PARAMETERS_ARE_INVALID) return None
def return_map_information(self, friend_user_id): result = dict() result["is_timeout"] = 0 user = self.user.get_by_id(friend_user_id) if user: map_information = self.user.get_user_map_information_from_redis(user.username) map_information_dict = RedisDB.data2dict(map_information) if map_information_dict: map_information_dict["longitude"] = float(map_information_dict.get("longitude", 0)) map_information_dict["latitude"] = float(map_information_dict["latitude"]) map_information_dict["altitude"] = float(map_information_dict["altitude"]) map_information_dict["accuracy"] = float(map_information_dict["accuracy"]) map_information_dict["angle"] = float(map_information_dict["angle"]) map_information_dict["satellite_number"] = int(map_information_dict["satellite_number"]) now = OHHODatetime.get_current_timestamp() if map_information_dict["timestamp"] > 0 and now - map_information_dict["timestamp"] > 30000: result["is_timeout"] = 1 result["timestamp"] = now result["last_timestamp"] = map_information_dict["timestamp"] else: information = dict() information["user_id"] = friend_user_id information["latitude"] = float(map_information_dict["latitude"]) information["longitude"] = float(map_information_dict["longitude"]) information["accuracy"] = float(map_information_dict["accuracy"]) information["supplier"] = map_information_dict["supplier"] if map_information_dict.get("floor", None): information["floor"] = map_information_dict["floor"] information["address"] = map_information_dict["address"] information["timestamp"] = now result["information"] = map_information_dict return result
def get(self, apply_id): """ 根据请求ID获取最新的配对请求, 并且这个请求只在30分钟内有效 :param data_dict: :return: """ if apply_id: agree = self.agree query = agree.filter_by_apply(apply_id) query = agree.order_by_id_desc(query) query = agree.first(query) if query: # 因为配对同意只能添加不能更改(没有地方会更改), # 所以这里的timestamp是添加时的timestamp if query.timestamp + VALID_INTERVAL_MILLISECOND > OHHODatetime.get_current_timestamp( ): return query else: OHHOLog.print_log(VALID_FRIEND_AGREE_NOT_EXIST) else: OHHOLog.print_log(FRIEND_AGREE_NOT_EXIST) else: OHHOLog.print_log(PARAMETERS_ARE_INVALID) return None
def _backstage_authenticate(self): OHHOLog.print_log("backstage authentication start") if self.current_user: OHHOLog.print_log("backstage authentication current user:"******"backstage authentication user") user_id = user.id token_from_db = token_instance.get_by_user_id(user_id) if token_from_db: current_timestamp = OHHODatetime.get_current_timestamp() if token_from_db.timestamp and current_timestamp - token_from_db.timestamp > 20 * 60 * 1000: OHHOLog.print_log("backstage authentication timeout") OHHOLog.print_log(current_timestamp) OHHOLog.print_log(token_from_db.timestamp) token_instance.delete(token_from_db) self.clear_cookie("username") return self.redirect("/backstage/login/") else: OHHOLog.print_log("backstage authentication success") token_instance.update(token_from_db, dict()) result = func(self) else: self.clear_cookie("username") return self.redirect("/backstage/login/") else: self.clear_cookie("username") return self.redirect("/backstage/login/") else: OHHOLog.print_log("backstage authentication no username") return self.redirect("/backstage/login/") return result
def is_valid_instance(self, instance): if instance: timestamp = OHHODatetime.get_current_timestamp() if instance.timestamp + VALID_INTERVAL_MILLISECOND < timestamp: return False return True else: return False
def add_icon(self, user_id, icon_id, icon, is_head_sculpture, base_url): # result = Result.result_failed("default failed") # OHHOLog.print_log(icon) if icon: now = OHHODatetime.get_now() path = "static/user/icon/%d/%d/%d/" % (now.year, now.month, now.day) os.makedirs(path, exist_ok=True) img = icon[0] timestamp = OHHODatetime.get_current_timestamp() name = str(user_id) + "_" + str(timestamp) + ".png" thumbnail_name = str(user_id) + "_" + str( timestamp) + "_thumbnail.png" path_name = path + name thumbnail_path_name = path + thumbnail_name with open(path_name, 'wb') as f: f.write(img["body"]) self.compress_icon(path_name, thumbnail_path_name) icon_object = self.user.user_icon.get_by_id(icon_id) data = dict() data["icon"] = path_name data["thumbnail"] = thumbnail_path_name data["is_head_sculpture"] = is_head_sculpture if icon_object: self.user.user_icon.update_without_commit(icon_object, data) else: data["user_id"] = user_id self.user.user_icon.add_without_commit(data) result = Result.result_success() result["url"] = base_url + path_name result["thumbnail_url"] = base_url + thumbnail_path_name else: result = Result.result_failed("no image file") result["url"] = "" icon_object = self.user.user_icon.get_by_id(icon_id) if icon_object: data = dict() data["icon"] = "" data["thumbnail"] = "" data["is_head_sculpture"] = 0 self.user.user_icon.update_without_commit(icon_object, data) OHHOLog.print_log(result)
def delete_outdated(self): query = self.get_query() query = self.get_by_user(query, None) query = self.get_by_device_from_query(query, None) twenty_four_hours_before = OHHODatetime.several_hours_before_from_now() query = self.imei.get_less_than_created_at(query, twenty_four_hours_before) OHHOLog.print_log(self.imei.get_count(query)) return self.delete_some(query)
def get_apply_friend_list(self, user_id): timestamp = int(OHHODatetime.get_current_timestamp()) query = self.apply.get_query() query = self.apply.get_by_another_user(query, user_id) # query = self.friend_get_apply_list(query) query = self.apply.get_great_than_equal_timestamp( query, timestamp - ONE_DAY_MICRO_SECONDS) # query = self.get_valid_relation(query) return query
def get_exclude_user_list(self, user_id): created_at = OHHODatetime.get_today_start() created_at = OHHODatetime.beijing2utc(created_at) one_day_before = OHHODatetime.get_some_hour_after(created_at, -24) query = self.exclude.get_query() query = self.exclude.get_great_than_equal_created_at( query, one_day_before) query1 = self.exclude.get_by_user(query, user_id) query2 = self.exclude.get_by_exclude_user(query, user_id) if self.exclude.is_empty(query1): user1_list = list() else: user1_list = [exclude.exclude_user_id for exclude in query1] if self.exclude.is_empty(query1): user2_list = list() else: user2_list = [exclude.user_id for exclude in query2] return list(set(user1_list + user2_list))
def get_countdown(self, apply_id, total=TOTAL_TIME): result = dict() result["current_stamp"] = OHHODatetime.get_current_timestamp() duplex_agree = self.duplex_agree.get_by_apply(apply_id) current_timestamp = OHHODatetime.get_current_timestamp() if duplex_agree and duplex_agree.timestamp: end_timestamp = total + duplex_agree.timestamp passed = current_timestamp - duplex_agree.timestamp result["start_timestamp"] = duplex_agree.timestamp result["end_timestamp"] = end_timestamp result["countdown"] = TOTAL_TIME - passed result["is_both_agree"] = True else: result["start_timestamp"] = 0 result["end_timestamp"] = 0 result["countdown"] = 0 result["is_both_agree"] = False return result
def get_user_extension_birthday(self, obj): try: birthday = self.get_the_argument(obj, USER_EXTENSION_BIRTHDAY, DEFAULT) if not birthday: return DEFAULT else: return OHHODatetime.string2date(birthday) except Exception as ex: return DEFAULT
def match_by_condition_version2(self, user_id, user_id_list): # 根据user_id获取配对条件 # 根据user_id_list获取配对用户列表 # 根据条件从用户列表中配对 # result = user_id_list # return result result = list() condition = self.condition_relation.get_nearest_match_relation_by_user( user_id) if condition: match_condition = self.condition.get_by_id( condition.match_condition_id) if match_condition: query = self.user_extension.get_by_user_list(user_id_list) sex = match_condition.sex if sex and sex != 3: query = self.user_extension.get_by_sex(query, sex) small_age = match_condition.small_age big_age = match_condition.big_age if small_age and big_age: this_year = OHHODatetime.get_now().year small_age_year = this_year - small_age big_age_year = this_year - big_age big_age_year_start = str(big_age_year) + "-1-1" date_big_age_year_start = OHHODatetime.string2date( big_age_year_start) small_age_year_end = str(small_age_year) + "-1-1" date_small_age_year_end = OHHODatetime.string2date( small_age_year_end) query = self.user_extension.find_by_birthday( query, date_big_age_year_start, date_small_age_year_end) if self.user_extension.is_empty(query): pass else: result = [ue.user_id for ue in query] return result
def is_valid_instance(self, instance): # OHHOLog.print_log("start") if instance: # OHHOLog.print_log("start") # OHHOLog.print_log(instance.id) timestamp = OHHODatetime.get_current_timestamp() if instance.timestamp + VALID_INTERVAL_MILLISECOND < timestamp: return False return True else: return False
def add_user_icon(user_id, image_file, base_url, image_sequence): image_sequence = int(image_sequence) now = OHHODatetime.get_now() path = "static/user/icon/%d/%d/%d/" % (now.year, now.month, now.day) os.makedirs(path, exist_ok=True) if image_file: img = image_file[0] timestamp = OHHODatetime.get_current_timestamp() name = str(user_id) + "_" + str(timestamp) + ".png" path_name = path + name with open(path_name, 'wb') as f: f.write(img["body"]) result = Result.result_success() instance = DBOHHOUserIcon() user_icon = instance.get_by_user(user_id) if user_icon: if image_sequence == 0: instance.update(user_icon, {"first": path_name}) elif image_sequence == 1: instance.update(user_icon, {"second": path_name}) elif image_sequence == 2: instance.update(user_icon, {"third": path_name}) else: instance.update(user_icon, {"fourth": path_name}) else: if image_sequence == 0: instance.add({"first": path_name, "user_id": user_id}) elif image_sequence == 1: instance.add({"second": path_name, "user_id": user_id}) elif image_sequence == 2: instance.add({"third": path_name, "user_id": user_id}) else: instance.add({"fourth": path_name, "user_id": user_id}) result["url"] = base_url + path_name if image_sequence == 0: UpdateUserInfo.update_user_info(user_id, icon=base_url + path_name) else: result = Result.result_failed("no image file") result["url"] = "" return result
def get_by_apply(self, apply_id_list): result = list() if apply_id_list: query = self.refuse.find_by_apply(apply_id_list) query = self.refuse.order_by_id_desc(query) query = self.refuse.first(query) if not self.refuse.is_empty(query): for q in query: if q.timestamp + VALID_INTERVAL_MILLISECOND > OHHODatetime.get_current_timestamp( ): result.append(q) return result
def get_header(): header = dict() nonce = OHHORandom.get_nonce() current_time = str(OHHODatetime.get_current_timestamp_second()) app_secret = APP_SECRET app_key = APP_KEY check_sum = Header.get_check_sum(app_secret, nonce, current_time) header[HEADER_NAME_NONCE] = nonce header[HEADER_NAME_CURRENT_TIME] = current_time header[HEADER_NAME_APP_KEY] = app_key header[HEADER_NAME_CHECK_SUM] = check_sum return header
def get(self): device = DBOHHODevice() query = device.get_query() total_count = device.get_count(query) today_start = OHHODatetime.get_today_start() utc_today_start = OHHODatetime.beijing2utc(today_start) today_query = device.get_great_than_equal_changed_at(query, utc_today_start) today_count = device.get_count(today_query) relation = DBOHHOUserAndDeviceRelation() relation_query = relation.get_query() relation_query = relation.get_valid(relation_query) total_bind_count = relation.get_count(relation_query) today_bind_query = relation.get_great_than_equal_changed_at(relation_query, utc_today_start) today_bind_count = relation.get_count(today_bind_query) return self.render(STATISTICS_DEVICE_HTML, total_count=total_count, today_count=today_count, today_bind_count=today_bind_count, total_bind_count=total_bind_count, )
def clear_outdated_user(self): timestamp = OHHODatetime.get_current_timestamp() timestamp = timestamp - 1 * 60 * 1000 query = self.match_online.get_query() delete_query = self.match_online.get_less_than_timestamp( query, timestamp) success = self.match_online.delete_some(delete_query) if success: OHHOLog.print_log( Result.result_success("clear online outdated user")) else: OHHOLog.print_log( Result.result_failed("clear online outdated user"))
def find_device_position(self, identity_id): result = dict() data = list() circle = dict() circle["longitude"] = -1 circle["latitude"] = -1 circle["radius"] = -1 self.device.set_identity(identity_id) device = self.device.get_by_identity() if device: relation = self.relation.get_by_device(device.id) if relation: query = self.map.get_by_user(relation.user_id) query = self.map.order_by_id_desc(query) first = self.map.first(query) if first: current_timestamp = OHHODatetime.get_current_timestamp() if current_timestamp < first.timestamp + FIND_DEVICE_TIMESTAMP_DELTA: circle["longitude"] = first.longitude circle["latitude"] = first.latitude sensor_query = self.sensor.get_query() sensor_query = self.sensor.get_by_device( sensor_query, device.id) sensor_query = self.sensor.get_by_user( sensor_query, relation.user_id) sensor_query = self.sensor.order_by_id_desc( sensor_query) first = self.sensor.first(sensor_query) if first: if current_timestamp < first.timestamp + FIND_DEVICE_TIMESTAMP_DELTA: result = Result.result_success() circle["radius"] = first.distance else: result = Result.result_failed( "device sensor is outdated!") else: result = Result.result_failed("no device sensor!") else: result = Result.result_failed( "map position is outdated!") else: result = Result.result_failed("no map position!") else: result = Result.result_failed("no user binds the device!") else: result = Result.result_failed("no such device!") data.append(circle) result["data"] = data return result
def get(self, user_id, base_url): data = list() result = Result.result_success() user = self.user.get_by_id(user_id) current_timestamp = OHHODatetime.get_current_timestamp() timestamp = RedisDB.hash_get(REDIS_APPLY_TIMESTAMP_NAME, user_id) if not timestamp: timestamp = current_timestamp - 5 * 60 * 1000 OHHOLog.print_log(timestamp) RedisDB.hash_set(REDIS_APPLY_TIMESTAMP_NAME, user_id, current_timestamp) # OHHOLog.print_log("start") # OHHOLog.print_log(user_id) if user: # OHHOLog.print_log("user") apply_you = self.apply.polling_get_by_another( user, int(timestamp), current_timestamp) OHHOLog.print_log(current_timestamp) OHHOLog.print_log(current_timestamp) # OHHOLog.print_log(int(timestamp)) # OHHOLog.print_log(current_timestamp) # OHHOLog.print_log(self.apply.apply.is_empty(apply_you)) if not self.apply.apply.is_empty(apply_you): OHHOLog.print_log("has valid apply!") for apply in apply_you: OHHOLog.print_log(apply.id) if self.meet.has_agree_apply( apply.one_user_id) or self.meet.has_agree_apply( apply.another_user_id): # OHHOLog.print_log("has agree apply!") continue if self.meet.has_valid_apply(apply.one_user_id, apply.another_user_id): # OHHOLog.print_log("has valid apply 2!") OHHOLog.print_log(apply.id) user_dict = self.user.get_user_basic_information( apply.one_user_id, base_url) if user_dict: data.append(user_dict) if data: # OHHOLog.print_log("has data") # OHHOLog.print_log(data) result["data"] = data else: pass OHHOLog.print_log("not data") return result
def set_match_switch(self, user_id, is_match): published = self.published.get_the_last_by_user(user_id) if published: current_timestamp = OHHODatetime.get_current_timestamp() if published.timestamp + PUBLISHED_TIMESTAMP >= current_timestamp: result = Result.result_published() result["rest"] = PUBLISHED_TIMESTAMP - (current_timestamp - published.timestamp) return result else: self.published.delete(published) configuration = self.configuration.get_by_user(user_id) if configuration: if is_match: success = self.configuration.open_match(configuration) if success: result = Result.result_success("open match successfully!") else: result = Result.result_failed("open match failed!") else: success = self.configuration.close_match(configuration) if success: result = Result.result_success("close match successfully!") else: result = Result.result_failed("close match failed!") else: data_dict = dict() data_dict["user_id"] = user_id data_dict["is_switch"] = is_match success = self.configuration.add(data_dict) if is_match: if success: result = Result.result_success( "add open match successfully!") else: result = Result.result_failed("add open match failed!") else: if success: result = Result.result_success( "add close match successfully!") else: result = Result.result_failed("add close match failed!") return result