예제 #1
0
    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())
예제 #2
0
    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())
예제 #3
0
    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())
예제 #4
0
    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())
예제 #5
0
파일: models.py 프로젝트: zeus911/MyJobs
    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
예제 #6
0
파일: models.py 프로젝트: kepinae/MyJobs
    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
예제 #7
0
    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)
예제 #8
0
    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
예제 #9
0
    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)
예제 #10
0
    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')
예제 #11
0
    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')
예제 #12
0
    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")
예제 #13
0
    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())
예제 #14
0
    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())
예제 #15
0
    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])
예제 #16
0
    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])
예제 #17
0
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.")
예제 #18
0
    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')
예제 #19
0
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.")
예제 #20
0
파일: views.py 프로젝트: chbasel/MyJobs
    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')
예제 #21
0
파일: views.py 프로젝트: wejhink/MyJobs
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
예제 #22
0
    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')
예제 #23
0
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
예제 #24
0
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
예제 #25
0
    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)
예제 #26
0
    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)