def list(self, request, *args, **kwargs): data = request.query_params user_id = int(data.get('user_id', 17)) queryset_ = [{ 'batch_id': wb.id, 'batch_name': wb.name, 'no_of_response': wb.no_of_response, 'partner': wb.partner.name, 'partner_id': wb.partner.id, 'survey_id': wb.current_status.survey.id, 'survey_name': wb.current_status.survey.name, 'status': wb.get_completion_status(), 'users': wb.get_users_all_states() } for wb in self.get_queryset()] queryset = queryset_ if user_id: queryset = [] partner_list = self.get_user_partner_list(user_id) for u_id in queryset_: if user_id in u_id.get('users') and u_id.get( 'partner_id') in partner_list: queryset.append(u_id) if request.query_params.get('key', 1): get_page = ceil( float(self.get_queryset().count()) / float(pg_size)) paginator = CustomPagination() result_page = paginator.paginate_queryset(queryset, request) status, message = (2, 'Successfully Retreieved') if queryset else ( 0, 'No Data') return paginator.get_paginated_response(result_page, status, message, 30, get_page) return Response(queryset)
def get(self, request): dict_table = {"NE":"never_enrolled_dropped_out", "DO":"dropped_out", "NEA":"ece_non_enrollment", "IC":"irregular_child_quarter", "EC":"child_involved_economic_activity", "BR":"birth_reg_yet_to_be_done", "CE":"child_engaged_or_married", "MC":"list_of_missing_child", "MGC":"list_of_migrated_child", "HPW":"health_pregnant_women","HCS":"health_centre_survey_1","SLI":"school_level_information_2","ACI":"anganwadi_centre_information_3","LM":"lactating_mothers_60","GM":"growth_monitoring_47","CVI":"child_vaccination_and_immuniza_49","PW":"pregnant_women_57"} model = apps.get_model('MutantApp', dict_table.get(request.GET.get('table'))) headers = [f.name for f in model._meta.get_fields()] display_headers = [f.verbose_name for f in model._meta.get_fields()] #response = [{j:getattr(f, j) for j in headers} for f in model.objects.all()] ## from fields_dict = {} [fields_dict.update({field.name:field.verbose_name}) for field in model._meta.get_fields()] partner = filter(None,str(request.GET.get('part')).split(',')) if len(partner) != 0: qmnth = request.GET.get('qid') qyr = request.GET.get('yid') fcn_yr = get_fincal_yr(qmnth,qyr) data = model.objects.filter(partner_id__in=partner).values(*fields_dict.keys())#,financial_month_year=fcn_yr) else: data = model.objects.all().values(*fields_dict.keys()) for d in data: for k in d.keys(): try: if int(k): choice = Choice.objects.get_or_none(id=d.get(k)) if choice: d[k]=choice.text except Exception as e: pass ## to get_page = ceil(float(len(data)) / float(10)) paginator = CustomPagination() result_page = paginator.paginate_queryset(data, request) return paginator.get_paginated_response(result_page, '200', 'Successfully Retreieved', 37, get_page,\ headers=display_headers, download=headers, table=request.GET.get('table'))
def list(self, request, *args, **kwargs): queryset = [self.update_list_dict(wtf) for wtf in self.get_queryset()] if request.query_params.get('key', 1): get_page = ceil( float(self.get_queryset().count()) / float(pg_size)) paginator = CustomPagination() result_page = paginator.paginate_queryset(queryset, request) status, message = (2, 'Successfully Retreieved') if queryset else ( 0, 'No Data') return paginator.get_paginated_response(result_page, status, message, 28, get_page) return Response(queryset)
def list(self, request, *args, **kwargs): get_level_loc = [] response = {'status': 0, 'message': 'Something went wrong', 'data': []} data = request.query_params queryset = self.get_queryset() get_user_based = list( SurveyRestore.objects.filter(active=2, survey_user__id=data.get('user_id'))) get_level_loc = list( set( queryset.filter(boundary__id__in=self.get_location_access( data.get('user_id'))).values_list('survey_restore', flat=True))) if get_level_loc or get_user_based: rest = SurveyRestore.objects.filter(active=2) fina_ = map( lambda x: rest.filter(id=x)[0] if rest.filter(id=x) else None, get_level_loc) combined_data = get_user_based + list(fina_) data_ = [{ 'id': cd.id, 'name': cd.name, 'download': HOST_URL + '/' + cd.restore_file.url if cd.restore_file else '', 'level': cd.level, 'level_type': cd.location_name() } for cd in list(set(combined_data)) if cd != None] data_.sort(key=lambda x: x.get('id'), reverse=True) response = { 'status': 2, 'message': 'Successfully retrieved the data' } response['data'] = data_ if data.get('key') == '1': get_page = ceil(float(len(combined_data)) / float(pg_size)) paginator = CustomPagination() result_page = paginator.paginate_queryset(data_, request) return paginator.get_paginated_response( result_page, response.get('status'), response.get('message'), 31, get_page) return Response(response)
def post(self, request, *args, **kwargs): data = request.data queryset = self.get_queryset() response = {'status': 0, 'count': 0, 'message': 'No Data to display.', 'data': [], 'display_headers': display_headers} get_data, query_data = self.get_partner_data(data, queryset) if get_data: pdr = PartnerDownloadReport() data_response = pdr.list(request, query_data) response = {'status': 2, 'message': 'Successfully data retrieved', 'display_headers': display_headers} response['data'] = get_data get_page = ceil(float(len(get_data)) / float(pg_size)) paginator = CustomPagination() result_page = paginator.paginate_queryset(get_data, request) return paginator.get_paginated_response(result_page, '200', 'Successfully Retreieved', 33, get_page, display_headers, DATA, data_response.data.get('download')) return Response(response)
def list(self, request, *args, **kwargs): queryset = [{ "id": wfsr.id, "work_name": wfsr.workflow.name if wfsr.workflow else '', "survey_name": wfsr.survey.name if wfsr.survey else '', "start_date": wfsr.start_date.strftime("%Y-%m-%d") if wfsr.start_date else '', "end_date": wfsr.end_date.strftime("%Y-%m-%d") if wfsr.end_date else '', } for wfsr in self.get_queryset()] if request.query_params.get('key', 1): get_page = ceil( float(self.get_queryset().count()) / float(pg_size)) paginator = CustomPagination() result_page = paginator.paginate_queryset(queryset, request) status, message = (2, 'Successfully Retreieved') if queryset else ( 0, 'No Data') return paginator.get_paginated_response(result_page, status, message, 29, get_page) return Response(queryset)
def post(self, request): response = {} all_fields = [i for i in CodeConfig._meta.get_all_field_names()] object_list = CodeConfig.objects.filter(active=2).order_by('-id') data = [] for obj in object_list: objdict = {'content_object':obj.content_object.name if obj.content_object else ''} for field in all_fields: try: if self.get_fk_model(CodeConfig, field): g = getattr(obj, field) objdict.update({field:g.model.capitalize()}) except: pass get_updated_objdict(obj, field, objdict) data.append(objdict) response.update({'data':data, 'status':2, 'message':'Successfully Retrieved'}) data.append(dict(pages=ceil(float(object_list.count()) / float(pg_size)))) get_page = int(data[-1].get('pages')) paginator = CustomPagination() del data[-1] result_page = paginator.paginate_queryset(data, request) return paginator.get_paginated_response(result_page, response.get('status'), response.get('message'),21, get_page)
def list(self, request, *args, **kwargs): get_wsu = lambda x: WorkStateUserRelation.objects.filter(workstate=x) queryset = [{ 'id': ws.id, 'name': ws.name, 'order': ws.order, 'users': ','.join( get_wsu(ws)[0].users.filter(is_active=True).values_list( 'username', flat=True)) if get_wsu(ws) else '' } for ws in self.get_queryset().order_by('-id')] if request.query_params.get('key', 1): get_page = ceil( float(self.get_queryset().count()) / float(pg_size)) paginator = CustomPagination() result_page = paginator.paginate_queryset(queryset, request) status, message = (2, 'Successfully Retreieved') if queryset else ( 0, 'No Data') return paginator.get_paginated_response(result_page, status, message, 27, get_page) return Response(queryset)
def post(self, request, key): """ Generic Listing Option --- parameters: - name: partner_id description: Enter Id type: Integer required: false """ response = {} model = self.get_model_class() page_no = self.get_pagination_id() try: partner_id = request.data['partner_id'] except: partner_id = None all_fields = [i for i in model._meta.get_all_field_names()] if key == 'beneficiarytype': nm = request.POST.get('name') object_list = model.objects.filter(active=2).exclude( id__in=[1]).order_by('-id') elif key == 'facility' and partner_id: object_list = model.objects.filter( active=2, partner_id=partner_id).order_by('-id') else: object_list = model.objects.filter(active=2).order_by('-id') if request.POST.get('name'): object_list = object_list.filter( name__icontains=request.POST.get('name')) if request.POST.get('btype'): object_list = object_list.filter( facility_type_id=request.POST.get('btype')) data = [] for obj in object_list: objdict = {} for field in all_fields: if field == 'jsondata' and key == 'facility': boundary_id = '' try: boundary_id = int(obj.jsondata.get('boundary_id')[0]) except: pass if not boundary_id: try: boundary_id = int( eval(obj.jsondata).get('boundary_id')[0]) except: boundary_id = int( eval(eval(obj.jsondata)).get('boundary_id')[0]) boundary_name = str( Boundary.objects.get(id=boundary_id).name.encode( 'ascii', 'replace')) objdict.update({ 'boundary_id': boundary_id, 'boundary_name': boundary_name }) else: try: if self.get_fk_model(model, field): g = getattr(obj, field) objdict.update({field: g.name}) except: pass try: objdict.update({field: obj.__dict__[field]}) except: pass data.append(objdict) response.update({ 'data': data, 'status': 2, 'message': 'Successfully Retrieved' }) data.append( dict(pages=ceil(float(object_list.count()) / float(pg_size)))) get_page = int(data[-1].get('pages')) paginator = CustomPagination() del data[-1] result_page = paginator.paginate_queryset(data, request) return paginator.get_paginated_response(result_page, response.get('status'), response.get('message'), page_no, get_page)