def get_queryset(self): """ Method which returns queryset after filter. :param self: :return qs: """ required_values = ['id', self.obj_alias] qs = super(Select2Mixin, self).get_queryset() org_id = self.request.GET.get('org', '0') sSearch = self.request.GET.get('sSearch', None) tech_name = self.request.GET.get('tech_name', None) if str(org_id) == "0": # Create instance of 'NocoutUtilsGateway' class nocout_utils = NocoutUtilsGateway() organizations = nocout_utils.logged_in_user_organizations(self) qs = qs.filter(organization__id__in=organizations) else: qs = qs.filter(organization_id=org_id) if str(qs.model.__name__).strip().lower() == 'sector': sector_required_list = [ 'id', self.obj_alias, 'name', 'sector_configured_on__ip_address', 'sector_id' ] required_values = sector_required_list elif str(qs.model.__name__).strip().lower() == 'device': required_values = required_values = [ 'id', self.obj_alias, 'ip_address' ] if sSearch: #specific cases to handle #ask Anoop how to make it more generic if str(qs.model.__name__).strip().lower() == 'sector': #we have a search for sector #search for sector can happen on #sector id, sector configured on qs = qs.filter( Q(**{"%s__icontains" % self.obj_alias: sSearch}) | Q(**{ "sector_configured_on__ip_address__icontains": sSearch }) | Q(**{"sector_id__icontains": sSearch})) else: qs = qs.filter( Q(**{"%s__icontains" % self.obj_alias: sSearch})) if tech_name: try: tech_id = DeviceTechnology.objects.get( name__iexact=tech_name).id qs = qs.filter(device_technology=tech_id) except Exception, e: pass
def get_initial_queryset(self): """ Preparing Initial Queryset for the for rendering the data table. """ if not self.model: raise NotImplementedError("Need to provide a model.") qs = self.model.objects.all() if not self.request.user.is_superuser: # Create instance of 'NocoutUtilsGateway' class nocout_utils = NocoutUtilsGateway() qs = qs.filter( **{ self.organization_field + "__in": nocout_utils.logged_in_user_organizations(self) }) if self.values_queryset: qs = qs.values(*self.columns + ['id']) if self.extra_qs_kwargs: qs = qs.filter(**self.extra_qs_kwargs) return qs
""" Signals associated with download_center app. """ # Import nocout utils gateway class from nocout.utils.util import NocoutUtilsGateway # Create instance of 'NocoutUtilsGateway' class nocout_utils = NocoutUtilsGateway() from download_center.tasks import scheduled_email_report @nocout_utils.disable_for_loaddata def send_mail_on_report_generation(sender, instance=None, created=False, **kwargs): if instance: report = instance.report_name scheduled_email_report.delay(report)
query_set_length = len(rows_req_obj.get_initial_queryset()) rows_req.REQUEST['iDisplayLength'] = query_set_length rows_req.REQUEST['length'] = query_set_length if payload['max_rows']: rows_req_obj.max_display_length = int(payload['max_rows']) else: rows_req_obj.max_display_length = query_set_length except Exception, e: logger.error(e.message) titles_list = ['Sheet 1'] resultset.append(rows_req_obj.get_context_data()) # Create instance of 'NocoutUtilsGateway' class nocout_utils = NocoutUtilsGateway() counter = 0 is_created = False # excel workbook # wb = xlwt.Workbook() # file path file_name = payload['username'] + '_' + payload['fulltime'] + '.xlsx' file_path = 'download_excels/{0}'.format(file_name) wb = xlsxwriter.Workbook(MEDIA_ROOT + file_path) for result in resultset: # error rows list
def prepare_results(self, qs): """ Preparing the final result after fetching from the data base to render on the data table. :param qs: :return qs """ if qs: qs = [{key: val if val else "" for key, val in dct.items()} for dct in qs] # Create instance of 'NocoutUtilsGateway' class nocout_utils = NocoutUtilsGateway() for dct in qs: # icon for excel file excel_green = static("img/ms-office-icons/excel_2013_green.png") # full path of report report_path = "" try: report_path = dct['path'] splitter = settings.REPORT_RELATIVE_PATH report_link = report_path.split(splitter)[1] report_path = report_link except Exception as e: logger.info(e.message) # 'created on' field timezone conversion from 'utc' to 'local' try: dct['created_on'] = nocout_utils.convert_utc_to_local_timezone( dct['created_on']) except Exception as e: logger.error("Timezone conversion not possible. Exception: ", e.message) # 'report date' field timezone conversion from 'utc' to 'local' try: dct['report_date'] = nocout_utils.convert_utc_to_local_timezone( dct['report_date']) except Exception as e: logger.error("Timezone conversion not possible. Exception: ", e.message) dct.update( path= '<a href="{}"><img src="{}" style="float:left; display:block; height:25px; width:25px;">' .format(report_path, excel_green)) actions_data = '<a href="/download_center/delete/{0}"><i class="fa fa-trash-o text-danger"></i></a>'.format( dct.get('id')) try: if SINGLE_REPORT_EMAIL: report_email_perm = json.loads(REPORT_EMAIL_PERM) else: report_email_perm = {} except Exception as e: logger.exception(e) page_type = self.request.GET.get('page_type') if report_email_perm.get(page_type): actions_data += ' <span style="cursor: pointer;" class="send_report_btn" title="Email Report" report_id="{0}"> \ <i class="fa fa-envelope text-primary"></i></span>'.format( dct.pop('id')) dct.update(actions=actions_data) return qs
class ActionListingTable(PermissionsRequiredMixin, BaseDatatableView, AdvanceFilteringMixin): """ View to show list of user log activity in datatable. URL - 'http://127.0.0.1:8000/logs/actions/' """ model = UserAction required_permissions = ('activity_stream.view_useraction', ) columns = ['user_id', 'action', 'module', 'logged_at'] order_columns = columns # Create instance of 'NocoutUtilsGateway' class nocout_utils = NocoutUtilsGateway() def filter_queryset(self, qs): """ The filtering of the queryset with respect to the search keyword entered. """ # get global search value sSearch = self.request.GET.get('search[value]', None) if sSearch and not self.pre_camel_case_notation: q = Q() for col in self.columns: if col == 'logged_at': continue q |= Q(**{'%s__icontains' % col: sSearch}) qs = qs.filter(q) return self.advance_filter_queryset(qs) def get_initial_queryset(self): """ Preparing initial queryset for rendering the data table. """ if not self.model: raise NotImplementedError( "Need to provide a model or implement get_initial_queryset!") # Get all the user ids of logged in user's organization. user_id_list = UserProfile.objects.filter( organization__in=self.nocout_utils.logged_in_user_organizations( self)).values_list('id') # Show logs from start time to end time. start_time = datetime.today() - timedelta(days=30) end_time = datetime.today() # Get user logs of last 30 days for all fetched user id's. user_logs_resultset = UserAction.objects.filter( user_id__in=user_id_list, logged_at__gt=start_time, logged_at__lte=end_time, ).values(*self.columns).order_by('-logged_at') return user_logs_resultset def prepare_results(self, qs): """ Preparing the final result after fetching from the data base to render on the data table. """ if qs: for dct in qs: dct['logged_at'] = self.nocout_utils.convert_utc_to_local_timezone( dct['logged_at']) user_id = dct['user_id'] try: dct['user_id'] = unicode( UserProfile.objects.get(id=user_id)) except Exception, e: dct['user_id'] = 'User Unknown/Deleted' pass return list(qs) return []