def test_serialize_csv(self): """Test that serializing to CSV creates the correct number of rows.""" data = StringIO(helpers.serialize('csv', self.records)) reader = csv.reader(data, delimiter=',') self.assertEqual(len(list(reader)) - 1, self.records.count())
def test_serialize_csv(self): """Test that serializing to CSV creates the correct number of rows.""" data = StringIO(helpers.serialize("csv", self.records)) reader = csv.reader(data, delimiter=",") self.assertEqual(len(list(reader)) - 1, self.records.count())
def test_serialize_python(self): """ Test that serializing a `QuerySet` into a Python object creates the correct number of `dict`s. """ data = helpers.serialize('python', self.records) self.assertEqual(len(data), self.records.count())
def test_serialize_python(self): """ Test that serializing a `QuerySet` into a Python object creates the correct number of `dict`s. """ data = helpers.serialize("python", self.records) self.assertEqual(len(data), self.records.count())
def regenerate(self): """Regenerate the report file if it doesn't already exist on disk.""" if not self.results: values = json.loads(self.values) contents = serialize('json', self.queryset, values=values) results = ContentFile(contents) self.results.save('%s-%s.json' % (self.name, self.pk), results) self._results = contents
def regenerate(self): """Regenerate the report file if it doesn't already exist on disk.""" contents = serialize('json', self.queryset) results = ContentFile(contents) if self.results: self.results.delete() self.results.save('%s-%s.json' % (self.name, self.pk), results) self._results = contents
def test_serialize_values(self): """ Test that if the `values` parameter is specified, column inclusion/exclusion as well as column ordering is respected. """ # by default, all fields except pk should be inclued, and columns # should be sorted alphabetically data = helpers.serialize('python', self.records) values = data[-1].keys() self.assertEqual(data[0].keys(), sorted(values)) # when values are specified, output records should respec the nubmer # and order of fields specified values = ['contact', 'contact_email', 'tags'] data = helpers.serialize('python', self.records, values=values) self.assertEqual(data[0].keys(), values)
def test_serialize_values(self): """ Test that if the `values` parameter is specified, column inclusion/exclusion as well as column ordering is respected. """ # by default, all fields except pk should be inclued, and columns # should be sorted alphabetically data = helpers.serialize("python", self.records) values = data[-1].keys() self.assertEqual(data[0].keys(), sorted(values)) # when values are specified, output records should respec the nubmer # and order of fields specified values = ["contact", "contact_email", "tags"] data = helpers.serialize("python", self.records, values=values) self.assertEqual(data[0].keys(), values)
def test_humanize(self): """Test that fields get converted to human-readable equivalents.""" data = helpers.humanize(helpers.serialize('python', self.records)) for record in data: # ensure tags were converted self.assertEqual(record['tags'], 'test, stuff, working') # ensure communication type was converted self.assertTrue(record['contact_type'] == 'Email')
def test_humanize(self): """Test that fields get converted to human-readable equivalents.""" data = helpers.humanize(helpers.serialize("python", self.records)) for record in data: # ensure tags were converted self.assertEqual(record["tags"], "test, stuff, working") # ensure contact type was converted self.assertTrue(record["contact_type"] == "Email")
def test_serialize_json(self): """ Test that serializing to JSON creates the correct number of objects. """ # JSON is returned as a string, but deserializing it after serializing # it should create a list of dicts comparable to the number of records # that actually exist. data = json.loads(helpers.serialize('json', self.records)) self.assertEqual(len(data), self.records.count())
def test_serialize_json(self): """ Test that serializing to JSON creates the correct number of objects. """ # JSON is returned as a string, but deserializing it after serializing # it should create a list of dicts comparable to the number of records # that actually exist. data = json.loads(helpers.serialize("json", self.records)) self.assertEqual(len(data), self.records.count())
def test_serialize_order_by(self): """ Test that if the `order_by` parameter is specified, records are ordered by that parameter's value. """ data = helpers.serialize('python', self.records, order_by='-date_time') datetimes = [record['date_time'] for record in data] # make sure the earliest time is first self.assertTrue(max(datetimes) == datetimes[0]) # make sure that the latest time is last self.assertTrue(min(datetimes) == datetimes[-1])
def test_serialize_order_by(self): """ Test that if the `order_by` parameter is specified, records are ordered by that parameter's value. """ data = helpers.serialize("python", self.records, order_by="-date_time") datetimes = [record["date_time"] for record in data] # make sure the earliest time is first self.assertTrue(max(datetimes) == datetimes[0]) # make sure that the latest time is last self.assertTrue(min(datetimes) == datetimes[-1])
def view_records(request, app="mypartners", model="contactrecord"): """ Returns records as JSON. Inputs: :request: Request object to inspect for search parameters. :app: Application to query. :model: Model to query. Query String Parameters: :values: The fields to include in the output. :order_by: The field to order the results by. Prefix with a '-' to indiciate descending order. Output: A JSON response containing the records queried for. """ if request.is_ajax() and request.method == 'GET': company = get_company_or_404(request) # parse request into dict, converting singleton lists into single items params = parse_params(request.GET) # remove non-query related params values = params.pop('values', None) order_by = params.pop('order_by', None) records = get_model(app, model).objects.from_search( company, params) if values: if not hasattr(values, '__iter__'): values = [values] records = records.values(*values) if order_by: if not hasattr(order_by, '__iter__'): order_by = [order_by] records = records.order_by(*order_by) ctx = serialize('json', records, values=values) response = HttpResponse( ctx, content_type='application/json; charset=utf-8') return response else: raise Http404("This view is only reachable via an AJAX GET request.")
def post(self, request, app='mypartners', model='contactrecords'): """ Create a report by querying on a specific model. The request's POST data is parsed for parameters to pass to the model's `from_search` method. Inputs: :app: The app to which the model belongs. :model: The model to query on Query String Parameters: :csrfmiddlewaretoken: Used to prevent Cross Site Request Forgery. :report_name: What to name the report. Spaces are converted to underscores. :values: Fields to include in report output. Outputs: An HttpResponse indicating success or failure of report creation. """ company = get_company_or_404(request) params = parse_params(request.POST) params.pop('csrfmiddlewaretoken', None) name = params.pop('report_name', str(datetime.now())) values = params.pop('values', None) records = get_model(app, model).objects.from_search( company, params) if values: if not hasattr(values, '__iter__'): values = [values] records = records.values(*values) contents = serialize('json', records, values=values) results = ContentFile(contents) report, created = Report.objects.get_or_create( name=name, created_by=request.user, owner=company, app=app, model=model, values=json.dumps(values), params=json.dumps(params)) report.results.save('%s-%s.json' % (name, report.pk), results) return HttpResponse(name, content_type='text/plain')
def view_records(request, app="mypartners", model="contactrecord"): """ Returns records as JSON. Inputs: :request: Request object to inspect for search parameters. :app: Application to query. :model: Model to query. Query String Parameters: :filters: A JSON string representing th exact query to be run. :values: The fields to include in the output. :order_by: The field to order the results by. Prefix with a '-' to indiciate descending order. Output: A JSON response containing the records queried for. """ if request.is_ajax() and request.method == 'POST': company = get_company_or_404(request) filters = request.POST.get("filters") values = request.POST.getlist("values") order_by = request.POST.get("order_by", None) records = get_model(app, model).objects.from_search( company, filters) if values: if not hasattr(values, '__iter__'): values = [values] records = records.values(*values) if order_by: if not hasattr(order_by, '__iter__'): order_by = [order_by] records = records.order_by(*order_by) ctx = serialize('json', records, values=values) response = HttpResponse( ctx, content_type='application/json; charset=utf-8') return response else: raise Http404("This view is only reachable via an AJAX GET request.")
def post(self, request, app='mypartners', model='contactrecords'): """ Create a report by querying on a specific model. The request's POST data is parsed for parameters to pass to the model's `from_search` method. Inputs: :app: The app to which the model belongs. :model: The model to query on Query String Parameters: :csrfmiddlewaretoken: Used to prevent Cross Site Request Forgery. :report_name: What to name the report. Spaces are converted to underscores. :filters: A JSON string representing th exact query to be run. :values: Fields to include in report output. Outputs: An HttpResponse indicating success or failure of report creation. """ company = get_company_or_404(request) name = request.POST.get('report_name', str(datetime.now())) filters = request.POST.get('filters', "{}") records = get_model(app, model).objects.from_search(company, filters) contents = serialize('json', records) results = ContentFile(contents) report, _ = Report.objects.get_or_create(name=name, created_by=request.user, owner=company, app=app, model=model, filters=filters) report.results.save('%s-%s.json' % (name, report.pk), results) return HttpResponse(name, content_type='text/plain')
def download_dynamic_report(request): """ Download dynamic report as CSV. Query String Parameters: :id: ID of the report to download :values: Fields to include in the resulting CSV, as well as the order in which to include them. :order_by: The sort order for the resulting CSV. Outputs: The report with the specified options rendered as a CSV file. """ report_id = request.GET.get('id', 0) values = request.GET.getlist('values', None) order_by = request.GET.get('order_by', None) report = DynamicReport.objects.get(id=report_id) if order_by: report.order_by = order_by report.save() configuration = report.report_presentation.configuration columns = Column.objects.active_for_configuration(configuration) values = [c.column_name for c in columns] records = (dict((v, unicode(rec.get(v))) for v in values) for rec in report.python) response = HttpResponse(content_type='text/csv') content_disposition = "attachment; filename=%s-%s.csv" response['Content-Disposition'] = content_disposition % ( report.name.replace(' ', '_'), report.pk) response.write(serialize('csv', records, values=values, order_by=order_by)) return response
def post(self, request, app='mypartners', model='contactrecords'): """ Create a report by querying on a specific model. The request's POST data is parsed for parameters to pass to the model's `from_search` method. Inputs: :app: The app to which the model belongs. :model: The model to query on Query String Parameters: :csrfmiddlewaretoken: Used to prevent Cross Site Request Forgery. :report_name: What to name the report. Spaces are converted to underscores. :filters: A JSON string representing th exact query to be run. :values: Fields to include in report output. Outputs: An HttpResponse indicating success or failure of report creation. """ company = get_company_or_404(request) name = request.POST.get('report_name', str(datetime.now())) filters = request.POST.get('filters', "{}") records = get_model(app, model).objects.from_search( company, filters) contents = serialize('json', records) results = ContentFile(contents) report, _ = Report.objects.get_or_create( name=name, created_by=request.user, owner=company, app=app, model=model, filters=filters) report.results.save('%s-%s.json' % (name, report.pk), results) return HttpResponse(name, content_type='text/plain')
def download_report(request): """ Download report as CSV. Query String Parameters: :id: ID of the report to download :values: Fields to include in the resulting CSV, as well as the order in which to include them. :order_by: The sort order for the resulting CSV. Outputs: The report with the specified options rendered as a CSV file. """ report_id = request.GET.get('id', 0) values = request.GET.getlist('values', None) order_by = request.GET.get('order_by', None) report = get_object_or_404( get_model('myreports', 'report'), pk=report_id) if order_by: report.order_by = order_by report.save() if values: report.values = json.dumps(values) report.save() records = humanize(report.python) response = HttpResponse(content_type='text/csv') content_disposition = "attachment; filename=%s-%s.csv" response['Content-Disposition'] = content_disposition % ( report.name.replace(' ', '_'), report.pk) response.write(serialize('csv', records, values=values, order_by=order_by)) return response
def test_serialize_strip_html(self): """ Test that HTML is properly stripped from fields when being serialized. """ # Only adding notes to one recod so that we can test that empty notes # are parsed correctly as well record = self.records[0] record.notes = """ <div class="tip-content"> Saved Search Notification<br /> <a href="https://secure.my.jobs">My.jobs</a> <p>Saved search was created on your behalf</p> </div> """ record.save() data = helpers.serialize('python', self.records) for record in data: text = ''.join(str(value) for value in record.values()) self.assertTrue('<' not in text, text) self.assertTrue('>' not in text, text)
def test_serialize_strip_html(self): """ Test that HTML is properly stripped from fields when being serialized. """ # Only adding notes to one recod so that we can test that empty notes # are parsed correctly as well record = self.records[0] record.notes = """ <div class="tip-content"> Saved Search Notification<br /> <a href="https://secure.my.jobs">My.jobs</a> <p>Saved search was created on your behalf</p> </div> """ record.save() data = helpers.serialize("python", self.records) for record in data: text = "".join(str(value) for value in record.values()) self.assertTrue("<" not in text, text) self.assertTrue(">" not in text, text)