def list(self, request, *args, **kwargs): cache_date = gregorian_to_persian_chart(datetime.datetime.now(), str_type="%Y/%m/%d %H:%M") # تاریخ ساخت گزارش start_date = request.query_params.get('start_date') end_date = request.query_params.get('end_date') province = request.query_params.get('province') county = request.query_params.get('county') q_object = {} if end_date: end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d') q_object["requested_at"] = {"$lte": end_date} if start_date: start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d') q_object["requested_at"] = {"$gte": start_date} query_set = list(cursor.rest_log.distinct('user_id', q_object)) query_set_sql = BaseUser.objects.filter(id__in=query_set) female, male = self.get_student_visit(county, province, query_set_sql) teachers = self.get_teachers_visit_count(county, province, query_set_sql) dict_report = { "male": male, "female": female, "teachers": teachers, "all_user": male + female + teachers, "cache_date": cache_date, } return Response(dict_report, status=status.HTTP_200_OK)
def created_sms_persian_date(self): from apps.common.utils import gregorian_to_persian_chart if self.created_at: return gregorian_to_persian_chart(self.created_at, str_type="%Y/%m/%d %H:%M:%S") else: return ""
def date_accept_activity(self, request): report_type = self.request.query_params.get("report_type", None) results = [] if report_type not in ['hour', 'week', 'month', 'day']: return Response(status=status.HTTP_400_BAD_REQUEST) queryset = self.set_activity_report_queryset( self.request.query_params, filter_start_date='accepted_at__gte', filter_end_date='accepted_at__lte' ) queryset = queryset.filter(~Q(accepted_at=None)) if report_type == "hour": results = queryset.annotate( axis_x=ExtractHour('accepted_at') ).values('axis_x').annotate(count=Count('id')).order_by("axis_x") results = list(results) elif report_type == "week": results = queryset.annotate( axis_x=ExtractWeekDay('accepted_at') ).values('axis_x').annotate(count=Count('id')).order_by("axis_x") week_days = WEEK_DAYS.copy() # map data for result for report_item in results: day_value = (report_item['axis_x'] % 7) + 1 report_item['axis_x'] = day_value report_item['sort_key'] = day_value report_item['axis_x'] = week_days[str(report_item['axis_x'])] results = sorted(results, key=lambda k: k['sort_key']) elif report_type == "day": results = list(queryset.annotate( axis_x=TruncDay('accepted_at') ).values('axis_x').annotate(count=Count('id')).order_by("axis_x")) for report_item in results: report_item['axis_x'] = gregorian_to_persian_chart(report_item['axis_x']) return Response(results, status=status.HTTP_200_OK)
def accepted_persian_date(self): if self.accepted_at: return gregorian_to_persian_chart(self.accepted_at, str_type="%Y/%m/%d %H:%M:%S") else: return ""
def last_login_persian_date(self): from apps.common.utils import gregorian_to_persian_chart if self.last_login: return gregorian_to_persian_chart(self.last_login, str_type="%Y/%m/%d %H:%M:%S") else: return ""
def get_created_at(obj): return gregorian_to_persian_chart(obj.created_at, str_type="%Y/%m/%d %H:%M")