def get_orders(req: Request, order_status: str = None, start_time: str = None, end_time: str = None, limit_num: str = None): if order_status is not None and order_status != 'in_progress' and order_status != 'delivering' \ and order_status != 'cancelled' and order_status != 'finished': raise NotFound() if order_status == 'in_progress': order_status = models.ORDER_IN_PROGRESS elif order_status == 'delivering': order_status = models.ORDER_DELIVERING elif order_status == 'cancelled': order_status = models.ORDER_CANCELLED elif order_status == 'finished': order_status = models.ORDER_FINISHED user = token_check(req=req) order_list = [] for e in models.Order.objects.filter(buyer=user).filter( view_utils.general_query_time_limit(end_time=end_time, start_time=start_time, status=order_status))[ :int(limit_num)]: order = view_utils.get_model_dict(e, excluded_fields=['buyer', 'delivery_address', 'delivery']) if e.delivery_address: order['delivery_address'] = json.loads(e.delivery_address) if e.delivery: order['delivery'] = json.loads(e.delivery) order_list.append(order) if len(order_list) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MR_ORDER_NOT_FOUND, message=_('Order not found')) return view_utils.get_json_response(order_list=order_list)
def get_recycle_record(req: Request, limit_num: str, start_time: str=None, end_time: str=None): user = account.token_check(req=req, permission_limit=models.RECYCLE_ACCOUNT_GARBAGE_COLLECTOR) result_list = [] for r in models.RecycleCleaningRecord.objects.filter(view_utils.general_query_time_limit(start_time=start_time, end_time=end_time, user=user))[:int(limit_num)]: result_list.append(view_utils.get_model_dict(r, excluded_fields=['user'])) if len(result_list) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MR_RECYCLE_RECORD_NOT_FOUND, message=_('No record')) return view_utils.get_json_response(recycle_record_list=result_list)
def get_commodities(req: Request, keyword: str = None, start_time: str = None, end_time: str = None, limit_num: str = None): commodity_list = [] for e in models.Commodity.objects.filter( view_utils.general_query_time_limit(end_time=end_time, start_time=start_time, title__icontains=keyword))[ :int(limit_num)]: commodity_list.append( view_utils.get_model_dict(e, excluded_fields=['description', 'stock', 'quantity_limit', 'type'])) if len(commodity_list) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MR_COMMODITY_NOT_FOUND, message=_('Commodity not found')) return view_utils.get_json_response(commodity_list=commodity_list)
def bulletin(req: Request, group_id: str, limit_num: str, start_time: str = None, end_time: str = None): user = account.token_check(req) if not check_utils.check_group_member(user_id=user.user_id, group_id=int(group_id)): raise CheckException(result_code=result_code.MC_USER_NOT_GROUP_MEMBER, message=_('User does not belong to this group')) bulletins = [] for b in CleaningGroupBulletin.objects.filter( view_utils.general_query_time_limit(start_time=start_time, end_time=end_time, group_id=int(group_id)))[:int(limit_num)]: bulletins.append(view_utils.get_bulletin_dict(b)) if len(bulletins) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MC_GROUP_BULLETIN_NOT_FOUND, message=_('Bulletin not found')) return view_utils.get_json_response(bulletin_list=bulletins)
def get_feedback(req: Request, limit_num: str, start_time: str = None, end_time: str = None): feedback_list = [] for fb in models.Feedback.objects.filter( view_utils.general_query_time_limit( end_time=end_time, start_time=start_time))[:int(limit_num)]: feedback_list.append(view_utils.get_feedback_dict(fb)) if len(feedback_list) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MP_FEEDBACK_NOT_FOUND, message=_('No feedback')) return view_utils.get_json_response(feedback_list=feedback_list)
def get_events(req: Request, start_time: str = None, end_time: str = None, limit_num: str = None): event_list = [] for e in models.Event.objects.filter( view_utils.general_query_time_limit( end_time=end_time, start_time=start_time))[:int(limit_num)]: event_list.append(view_utils.get_model_dict(e)) if len(event_list) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MR_EVENT_NOT_FOUND, message=_('Event not found')) return view_utils.get_json_response(event_list=event_list)
def get_work_record(req: Request, limit_num: str, user_id: str = None, trash_id: str = None, start_time: str = None, end_time: str = None): account.token_check(req) work_record_list = [] q = CleaningWorkRecord.objects.filter( view_utils.general_query_time_limit(start_time=start_time, end_time=end_time)) if user_id is not None: q.filter(user_id=int(user_id)) if trash_id is not None: q.filter(trash_id=int(trash_id)) for wr in q[:int(limit_num)]: work_record_list.append(view_utils.get_work_record_dict(wr)) if len(work_record_list) == 0: raise CheckException(status=status.HTTP_404_NOT_FOUND, result_code=result_code.MC_WORK_RECORD_NOT_FOUND, message=_('Work record not found')) return view_utils.get_json_response(work_record_list=work_record_list)
def get_credit_records(req: Request, start_time: str = None, end_time: str = None, limit_num: str = None): user = account.token_check(req) credit_record_list = [] for cr in models.RecycleCreditRecord.objects.filter( view_utils.general_query_time_limit(end_time=end_time, start_time=start_time, user=user))[:int(limit_num)]: credit_record_list.append( view_utils.get_model_dict(cr, excluded_fields=['user'])) if len(credit_record_list) == 0: raise CheckException( status=status.HTTP_404_NOT_FOUND, result_code=result_code.MR_CREDIT_RECORD_NOT_FOUND, message=_('Credit record not found')) return view_utils.get_json_response(credit_record_list=credit_record_list)
def update_rank_list(rank_list_type: str = RANK_LIST_TYPE_DAILY): now = datetime.datetime.now() if rank_list_type == RANK_LIST_TYPE_DAILY: global daily_credit_rank_list global daily_credit_rank_list_update_time rank_list = daily_credit_rank_list daily_credit_rank_list_update_time = datetime.datetime.now() start_time = int( now.replace(hour=0, minute=0, second=0, microsecond=0).timestamp()) end_time = int( now.replace(hour=23, minute=59, second=59, microsecond=10**6 - 1).timestamp()) elif rank_list_type == RANK_LIST_TYPE_WEEKLY: global weekly_credit_rank_list global weekly_credit_rank_list_update_time rank_list = weekly_credit_rank_list weekly_credit_rank_list_update_time = datetime.datetime.now() start_time = int( (now - datetime.timedelta(days=now.weekday())).replace( hour=0, minute=0, second=0, microsecond=0).timestamp()) end_time = int( (now + datetime.timedelta(days=6 - now.weekday())).replace( hour=23, minute=59, second=59, microsecond=10**6 - 1).timestamp()) else: return user_credit_dict = {} models.RecycleCreditRecord.objects.filter() for cr in models.RecycleCreditRecord.objects.filter(credit__gt=0).filter( view_utils.general_query_time_limit(start_time=start_time, end_time=end_time)): user_name = cr.user.user_name user_credit = user_credit_dict.get(user_name) if user_credit is None: user_credit = cr.credit else: user_credit = user_credit + cr.credit user_credit_dict[user_name] = user_credit rank_list.clear() for key in user_credit_dict: rank_list.append({'user_name': key, 'credit': user_credit_dict[key]}) rank_list.sort(key=lambda x: x['credit'], reverse=True) logger.info('Finish updating %s credit rank list' % rank_list_type)