def get_hackathon_list(self, args): # get values from request's QueryString page = int(args.get("page", 1)) per_page = int(args.get("per_page", 20)) order_by = args.get("order_by", "create_time") status = args.get("status") name = args.get("name") # build query by search conditions and order_by status_filter = Q() name_filter = Q() condition_filter = Q() order_by_condition = '-id' if status: status_filter = Q(status=status) if name: name_filter = Q(name__contains=name) if order_by == 'create_time': # 最新发布 order_by_condition = '-create_time' elif order_by == 'event_start_time': # 即将开始 order_by_condition = '-event_start_time' elif order_by == 'registered_users_num': # 人气热点 # hackathons with zero registered users would not be shown. hot_hackathon_stat = HackathonStat.objects( type=HACKATHON_STAT.REGISTER, count__gt=0).order_by('-count') hot_hackathon_list = [ stat.hackathon.id for stat in hot_hackathon_stat ] condition_filter = Q(id__in=hot_hackathon_list) else: order_by_condition = '-id' # perform db query with pagination pagination = Hackathon.objects(status_filter & name_filter & condition_filter).order_by( order_by_condition).paginate( page, per_page) hackathon_list = pagination.items hackathon_stat = HackathonStat.objects(hackathon__in=hackathon_list) user = None user_hackathon = [] team = [] if self.user_manager.validate_login(): user = g.user user_hackathon = UserHackathon.objects( user=user, hackathon__in=hackathon_list) team = Team.objects(members__user=user, hackathon__in=hackathon_list) def func(hackathon): return self.__fill_hackathon_detail(hackathon, user, hackathon_stat, user_hackathon, team) # return serializable items as well as total count return self.util.paginate(pagination, func)
def get_hackathon_list(self, args): # get values from request's QueryString page = int(args.get("page", 1)) per_page = int(args.get("per_page", 20)) order_by = args.get("order_by", "create_time") status = args.get("status") name = args.get("name") # build query by search conditions and order_by status_filter = Q() name_filter = Q() condition_filter = Q() order_by_condition = '-id' if status: status_filter = Q(status=status) if name: name_filter = Q(name__contains=name) if order_by == 'create_time': # 最新发布 order_by_condition = '-create_time' elif order_by == 'event_start_time': # 即将开始 order_by_condition = '-event_start_time' elif order_by == 'registered_users_num': # 人气热点 # hackathons with zero registered users would not be shown. hot_hackathon_stat = HackathonStat.objects(type=HACKATHON_STAT.REGISTER, count__gt=0).order_by('-count') hot_hackathon_list = [stat.hackathon.id for stat in hot_hackathon_stat] condition_filter = Q(id__in=hot_hackathon_list) else: order_by_condition = '-id' # perform db query with pagination pagination = Hackathon.objects(status_filter & name_filter & condition_filter).order_by( order_by_condition).paginate(page, per_page) hackathon_list = pagination.items hackathon_stat = HackathonStat.objects(hackathon__in=hackathon_list) user = None user_hackathon = [] team = [] if self.user_manager.validate_login(): user = g.user user_hackathon = UserHackathon.objects(user=user, hackathon__in=hackathon_list) team = Team.objects(members__user=user, hackathon__in=hackathon_list) def func(hackathon): return self.__fill_hackathon_detail(hackathon, user, hackathon_stat, user_hackathon, team) # return serializable items as well as total count return self.util.paginate(pagination, func)
def __get_hackathon_detail(self, hackathon, user=None): """Return hackathon info as well as its details including configs, stat, organizers, like if user logon""" detail = hackathon.dic() detail["stat"] = { "register": 0, "like": 0} for stat in HackathonStat.objects(hackathon=hackathon): if stat.type == HACKATHON_STAT.REGISTER: detail["stat"]["register"] = stat.count elif stat.type == HACKATHON_STAT.LIKE: detail["stat"]["like"] = stat.count if user: user_hackathon = UserHackathon.objects(hackathon=hackathon, user=user).first() if user_hackathon and user_hackathon.like: detail['like'] = user_hackathon.like detail["user"] = self.user_manager.user_display_info(user) detail["user"]["is_admin"] = user.is_super or ( user_hackathon and user_hackathon.role == HACK_USER_TYPE.ADMIN) # TODO: we need to review those items one by one to decide the API output # asset = self.db.find_all_objects_by(UserHackathonAsset, user_id=user.id, hackathon_id=hackathon.id) # if asset: # detail["asset"] = [o.dic() for o in asset] if user_hackathon and user_hackathon.role == HACK_USER_TYPE.COMPETITOR: detail["registration"] = user_hackathon.dic() team = Team.objects(hackathon=hackathon, members__user=user).first() if team: detail["team"] = team.dic() return detail
def __get_hackathon_detail(self, hackathon, user=None): """Return hackathon info as well as its details including configs, stat, organizers, like if user logon""" detail = hackathon.dic() detail["stat"] = {"register": 0, "like": 0} for stat in HackathonStat.objects(hackathon=hackathon): if stat.type == HACKATHON_STAT.REGISTER: detail["stat"]["register"] = stat.count elif stat.type == HACKATHON_STAT.LIKE: detail["stat"]["like"] = stat.count if user: user_hackathon = UserHackathon.objects(hackathon=hackathon, user=user).first() if user_hackathon and user_hackathon.like: detail['like'] = user_hackathon.like detail["user"] = self.user_manager.user_display_info(user) detail["user"]["is_admin"] = user.is_super or ( user_hackathon and user_hackathon.role == HACK_USER_TYPE.ADMIN) # TODO: we need to review those items one by one to decide the API output # asset = self.db.find_all_objects_by(UserHackathonAsset, user_id=user.id, hackathon_id=hackathon.id) # if asset: # detail["asset"] = [o.dic() for o in asset] if user_hackathon and user_hackathon.role == HACK_USER_TYPE.COMPETITOR: detail["registration"] = user_hackathon.dic() team = Team.objects(hackathon=hackathon, members__user=user).first() if team: detail["team"] = team.dic() return detail
def increase_hackathon_stat(self, hackathon, stat_type, increase): """Increase or descrease the count for certain hackathon stat :type hackathon: Hackathon :param hackathon: instance of Hackathon to be counted :type stat_type: str|unicode :param stat_type: type of stat that defined in constants.py#HACKATHON_STAT :type increase: int :param increase: increase of the count. Can be positive or negative """ stat = HackathonStat.objects(hackathon=hackathon, type=stat_type).first() if stat: stat.count += increase else: stat = HackathonStat(hackathon=hackathon, type=stat_type, count=increase) if stat.count < 0: stat.count = 0 stat.update_time = self.util.get_now() stat.save()
def update_hackathon_stat(self, hackathon, stat_type, count): """Increase or descrease the count for certain hackathon stat :type hackathon: Hackathon :param hackathon: instance of Hackathon to be counted :type stat_type: str|unicode :param stat_type: type of stat that defined in constants.py#HACKATHON_STAT :type count: int :param count: the new count for this stat item """ stat = HackathonStat.objects(hackathon=hackathon, type=stat_type).first() if stat: stat.count = count stat.update_time = self.util.get_now() else: stat = HackathonStat(hackathon=hackathon, type=stat_type, count=count) if stat.count < 0: stat.count = 0 stat.save()
def __get_hackathon_stat(self, hackathon): stats = HackathonStat.objects(hackathon=hackathon).all() result = {"hackathon_id": str(hackathon.id), "online": 0, "offline": 0} for item in stats: result[item.type] = item.count reg_list = UserHackathon.objects( hackathon=hackathon, role=HACK_USER_TYPE.COMPETITOR, deleted=False, status__in=[ HACK_USER_STATUS.AUTO_PASSED, HACK_USER_STATUS.AUDIT_PASSED ]).only("user").no_dereference().all() reg_list = [uh.user.id for uh in reg_list] reg_count = len(reg_list) if reg_count > 0: online_count = User.objects(id__in=reg_list, online=True).count() result["online"] = online_count result["offline"] = reg_count - online_count return result
def __get_hackathon_stat(self, hackathon): stats = HackathonStat.objects(hackathon=hackathon).all() result = { "hackathon_id": str(hackathon.id), "online": 0, "offline": 0 } for item in stats: result[item.type] = item.count reg_list = UserHackathon.objects(hackathon=hackathon, role=HACK_USER_TYPE.COMPETITOR, deleted=False, status__in=[HACK_USER_STATUS.AUTO_PASSED, HACK_USER_STATUS.AUDIT_PASSED] ).only("user").no_dereference().all() reg_list = [uh.user.id for uh in reg_list] reg_count = len(reg_list) if reg_count > 0: online_count = User.objects(id__in=reg_list, online=True).count() result["online"] = online_count result["offline"] = reg_count - online_count return result