def page_export_pdf(request, config_id):
    config = get_object_or_404(Configuration, pk=config_id)
    if request.method == "GET":
        return {'config': config}

    log_filename = request.POST.get("key")
    assert (log_filename and os.sep not in log_filename
            and log_filename.isalnum()) # lazy security checking
    log_filename = os.path.join(tempfile.gettempdir(),
                                log_filename + ".log")

    data_source = request.POST.get("data_source")

    logger = open(log_filename, 'w')
    print >> logger, "Fetching data from source.."
    logger.flush()

    from zope.dottedname.resolve import resolve
    queryset = resolve(settings.PDFBUILDER_DATA_SOURCE)(request, data_source)

    entry_count_upper_bound = queryset.count()

    order_by = registry.get_ordering(config.order_by())
    if order_by is not None:
        queryset = queryset.order_by(*order_by)

    group_by = registry.get_grouping(config.group_by())
    
    #filter_by = config.filter_by()
    ## FIXME: this is ugly
    #def fixer(queryset, filter_by):
    #    for item in queryset:
    #        omit = False
    #        for field in filter_by:
    #            if getattr(item, field)() is None:
    #                omit = True
    #                break
    #        if omit is True:
    #            continue
    #        yield item
    #queryset = fixer(queryset, filter_by)

    template = registry.get_template(config.template())

    def log_callback(prepared_flowables):
        count = 0
        for name, bucket in prepared_flowables.items():
            count += len(bucket)
        if count % 500:
            return
        print >> logger, "%s / %s" % (count, entry_count_upper_bound)
        logger.flush()

    try:
        grouped_elements = template.generate_flowables(
            queryset,
            number_entries=config.number_entries(),
            bucket_selector=group_by,
            log_callback=log_callback)
    except Exception, e:
        print >> logger, pprint.pformat(sys.exc_info())
        logger.flush()
        raise
 def load_template(self):
     from pdfbuilder import registry
     return registry.get_template(self.template())