Example #1
0
def show(report_id):
    """
        Get report
    """
    try:
        report = Report.objects.filter(id=report_id).values(*REPORT_FIELDS)[0]
    except (IndexError, ValueError):
        return 404, {'status': 'Not Found', 'code': 404}

    # Convert date
    for key, val in report.iteritems():
        if isinstance(val, datetime):
            report[key] = time.mktime(val.timetuple())

    # Add related infos
    if report.get('plaintiff'):
        report['plaintiff'] = model_to_dict(Plaintiff.objects.get(id=report['plaintiff']))
    if report.get('service'):
        report['service'] = model_to_dict(Service.objects.get(id=report['service']))
    if report.get('defendant'):
        report['defendant'] = DefendantsController.show(report['defendant'])[1]
    if report.get('provider'):
        report['provider'] = ProvidersController.show(report['provider'])[1]

    tags = Report.objects.get(id=report['id']).tags.all()
    report['tags'] = [model_to_dict(tag) for tag in tags]

    return 200, report
Example #2
0
def __format_report_response(reports):
    """ Convert datetime object and add flat foreign key
    """
    for rep in reports:
        for key, val in rep.iteritems():
            if isinstance(val, datetime):
                rep[key] = time.mktime(val.timetuple())

        # Flat foreign models
        if rep.get('defendant'):
            defendant = Defendant.objects.get(id=rep['defendant'])
            rep['defendant'] = model_to_dict(defendant)
            rep['defendant']['email'] = defendant.details.email
        if rep.get('plaintiff'):
            rep['plaintiff'] = model_to_dict(Plaintiff.objects.get(id=rep['plaintiff']))
        if rep.get('service'):
            rep['service'] = model_to_dict(Service.objects.get(id=rep['service']))
        if rep.get('provider'):
            rep['provider'] = ProvidersController.show(rep['provider'])[1]
        if rep.get('tags'):
            tags = Report.objects.get(id=rep['id']).tags.all()
            rep['tags'] = [model_to_dict(tag) for tag in tags]