def _write_data(self, response): pk = self.kwargs['pk'] # If we came from an organisation page: if '/organisation' in self.request.build_absolute_uri(): linkevents = LinkEvent.objects.filter( url__collection__organisation__pk=pk).distinct() else: program = Program.objects.get(pk=pk) linkevents = program.get_linkevents() linkevents = filter_queryset(linkevents.select_related('username'), self.request.GET) writer = csv.writer(response) writer.writerow([ 'Link', 'User', 'Bot user', 'Page title', 'Project', 'Timestamp', 'Revision ID', 'Change' ]) for link in linkevents.order_by('-timestamp'): writer.writerow([ link.link, link.username, link.user_is_bot, link.page_title, link.domain, link.timestamp, link.rev_id, link.change ])
def _write_data(self, response): program_pk = self.kwargs['pk'] this_program_orgs = Organisation.objects.filter(program__pk=program_pk) program = Program.objects.get(pk=program_pk) this_program_linkevents = program.get_linkevents() this_program_linkevents = filter_queryset(this_program_linkevents, self.request.GET) top_orgs = top_organisations(this_program_orgs, this_program_linkevents) writer = csv.writer(response) writer.writerow(['Organisation', 'Links added', 'Links removed']) for org in top_orgs: writer.writerow([org.name, org.links_added, org.links_removed])
def _write_data(self, response): pk = self.kwargs['pk'] # If we came from an organisation page: if '/organisation' in self.request.build_absolute_uri(): linkevents = LinkEvent.objects.filter( url__collection__organisation__pk=pk).distinct() else: program = Program.objects.get(pk=pk) linkevents = program.get_linkevents() linkevents = filter_queryset(linkevents, self.request.GET) top_projects = annotate_top(linkevents, '-links_added', ['domain']) writer = csv.writer(response) writer.writerow(['Project', 'Links added', 'Links removed']) for project in top_projects: writer.writerow([ project['domain'], project['links_added'], project['links_removed'] ])
def _write_data(self, response): org_pk = self.kwargs['pk'] linkevents = LinkEvent.objects.filter( url__collection__organisation__pk=org_pk).distinct() linkevents = filter_queryset(linkevents, self.request.GET) top_pages = annotate_top( linkevents, '-links_added', ['page_title', 'domain'], ) writer = csv.writer(response) writer.writerow( ['Page title', 'Project', 'Links added', 'Links removed']) for page in top_pages: writer.writerow([ page['page_title'], page['domain'], page['links_added'], page['links_removed'] ])
def get_context_data(self, **kwargs): context = super(ProgramDetailView, self).get_context_data(**kwargs) this_program_organisations = Organisation.objects.filter( program=self.object) context['organisations'] = this_program_organisations form = self.form_class(self.request.GET) context['form'] = form this_program_linkevents = self.get_object().get_linkevents() # Filter queryset based on form, if used if form.is_valid(): form_data = form.cleaned_data this_program_linkevents = filter_queryset(this_program_linkevents, form_data) context = get_linkevent_context(context, this_program_linkevents) context['top_organisations'] = top_organisations( this_program_organisations, this_program_linkevents, num_results=5) context['query_string'] = self.request.META['QUERY_STRING'] return context
def get_context_data(self, **kwargs): context = super(OrganisationDetailView, self).get_context_data(**kwargs) form = self.form_class(self.request.GET) context['form'] = form organisation_collections = Collection.objects.filter( organisation=self.object ) # Here we have a slightly more complex context dictionary setup, where # each collection has its own dictionary of data. context['collections'] = {} for collection in organisation_collections: this_collection_linkevents = collection.get_linkevents() this_collection_linksearchtotals = LinkSearchTotal.objects.filter( url__collection=collection ) if form.is_valid(): form_data = form.cleaned_data this_collection_linkevents = filter_queryset( this_collection_linkevents, form_data) this_collection_linksearchtotals = filter_linksearchtotals( this_collection_linksearchtotals, form_data ) # Replace all special characters that might confuse JS with an # underscore. collection_key = re.sub('[^0-9a-zA-Z]+', '_', collection.name) context['collections'][collection_key] = {} context['collections'][collection_key]['object'] = collection context['collections'][collection_key]['urls'] = URLPattern.objects.filter( collection=collection ) context['collections'][collection_key] = get_linkevent_context( context['collections'][collection_key], this_collection_linkevents) context['collections'][collection_key]['top_pages'] = annotate_top( this_collection_linkevents, '-links_added', ['page_title', 'domain'], num_results=5, ) # LinkSearchTotal chart data dates, linksearch_data = get_linksearchtotal_data_by_time( this_collection_linksearchtotals) context['collections'][collection_key]['linksearch_dates'] = dates context['collections'][collection_key]['linksearch_data'] = linksearch_data # Statistics if linksearch_data: total_start = linksearch_data[0] total_current = linksearch_data[-1] total_diff = total_current - total_start start_date_object = datetime.strptime(dates[0], '%Y-%m-%d') start_date = start_date_object.strftime('%B %Y') # If we haven't collected any LinkSearchTotals yet, then set # these variables to None so we don't show them in the statistics # box else: total_start = None total_current = None total_diff = None start_date = None context['collections'][collection_key]['linksearch_total_start'] = total_start context['collections'][collection_key]['linksearch_total_current'] = total_current context['collections'][collection_key]['linksearch_total_diff'] = total_diff context['collections'][collection_key]['linksearch_start_date'] = start_date context['query_string'] = self.request.META['QUERY_STRING'] return context