def response(**kwargs): """Model maker for feedback.models.Response.""" ua = kwargs.pop('ua', USER_AGENT) parsed = browsers.parse_ua(ua) defaults = { 'prodchan': 'firefox.desktop.stable', 'happy': True, 'url': u'', 'description': u'So awesome!', 'user_agent': ua, 'browser': parsed.browser, 'browser_version': parsed.browser_version, 'platform': parsed.platform, 'product': Response.infer_product(parsed.platform), 'channel': u'stable', 'version': parsed.browser_version, 'locale': u'en-US', } defaults.update(kwargs) return Response(**defaults)
def _analytics_search_export(request, opinions_s): """Handles CSV export for analytics search This only exports MAX_OPINIONS amount. It adds a note to the top about that if the results are truncated. """ MAX_OPINIONS = 1000 # Create the HttpResponse object with the appropriate CSV header. response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="{0}"'.format( datetime.now().strftime('%Y%m%d_%H%M_search_export.csv')) keys = Response.get_export_keys(confidential=True) total_opinions = opinions_s.count() opinions_s = opinions_s.values_list('id')[:MAX_OPINIONS] # We convert what we get back from ES to what's in the db so we # can get all the information. opinions = Response.objects.filter( id__in=[mem[0][0] for mem in opinions_s]) writer = csv.writer(response) # Specify what this search is writer.writerow(['URL: {0}'.format(request.get_full_path())]) writer.writerow([ 'Params: ' + ' '.join( ['{0}: {1}'.format(key, val) for key, val in request.GET.items()]) ]) # Add note if we truncated. if total_opinions > MAX_OPINIONS: writer.writerow( ['Truncated {0} rows.'.format(total_opinions - MAX_OPINIONS)]) # Write headers row. writer.writerow(keys) # Write opinion rows. for op in opinions: writer.writerow([force_bytes(getattr(op, key)) for key in keys]) return response
class ResponseFactory(factory.DjangoModelFactory): class Meta: model = Response happy = True url = u'' description = u'So awesome!' user_agent = USER_AGENT browser = factory.LazyAttribute( lambda a: unicode(browsers.parse_ua(a.user_agent).browser)) browser_version = factory.LazyAttribute( lambda a: unicode(browsers.parse_ua(a.user_agent).browser_version)) platform = factory.LazyAttribute( lambda a: unicode(browsers.parse_ua(a.user_agent).platform)) product = factory.LazyAttribute(lambda a: unicode( Response.infer_product(browsers.parse_ua(a.user_agent)))) channel = u'stable' version = factory.LazyAttribute( lambda a: unicode(browsers.parse_ua(a.user_agent).browser_version)) locale = u'en-US' api = None