Пример #1
0
def create_export(app_label):
    participant_content_type = ContentType.objects.get(app_label=app_label, model='participant')

    model_names_as_attributes = {
        "Participant": None,
        'Match': 'match',
        'Treatment': 'treatment',
        'Experiment': 'experiment',
        'SessionParticipant': 'session_participant',
        'Session': 'session',
    }


    format_name = "CSV"
    export_name = '{} participants'.format(participant_content_type.app_label)

    # delete if it already exists
    Export.objects.filter(name = export_name).delete()

    csv_format = Format.objects.get(name=format_name)
    export = Export(name = export_name,
                    slug = slugify(export_name),
                    model = participant_content_type,
                    export_format = csv_format)
    export.save()

    export_fields = ptree.adminlib.get_data_export_fields(app_label)
    export_field_tuples = []
    for model_name in ptree.adminlib.MODEL_NAMES:
        if model_name == "Participant":
            export_field_tuples.extend((field, field) for field in export_fields[model_name])
        else:
            for field in export_fields[model_name]:
                dot_path = '{}.{}'.format(model_names_as_attributes[model_name], field)
                export_field_tuples.append((dot_path, dot_path))
    for i, tup in enumerate(export_field_tuples):
        path, path = tup
        column = Column(export = export,
                        column = path,
                        label = path,
                        order = i)
        column.save()
Пример #2
0
def create_export(content_type, export_name, fields, format_name="CSV"):

    model_name = '{}: {} ({})'.format(content_type.app_label,
                                      export_name,
                                      format_name)
    # delete if it already exists
    Export.objects.filter(name = model_name).delete()

    csv_format = Format.objects.get(name=format_name)
    export = Export(name = model_name,
                    slug = slugify(model_name),
                    model = content_type,
                    export_format = csv_format)
    export.save()

    for i, field in enumerate(fields):
        column = Column(export = export,
                        column = field,
                        label = field,
                        order = i)
        column.save()