Esempio n. 1
0
def comment_detail(request, slug):
    object = get_object_or_404(Entry, slug=slug)

    # Paginated comments
    comments = Comment.objects.for_model(object).filter(is_removed=False, is_public=True)
    comments = paginate(request, comments, default_to_last_page=True)

    return  { 'object': object, 'comments': comments }
Esempio n. 2
0
def entries(request):
    """
    A list of the entries.
    """

    return {
        'object_list': paginate(request, Entry.objects.live()),
        'site_url': Site.objects.get_current().domain,
    }
Esempio n. 3
0
    def inner(request, slug, ids):
        object_list, extra_context = advreport.get_object_list(request)

        paginated = paginate(request, object_list, num_per_page=advreport.items_per_page, use_get_parameters=True)

        report = {
            'header': advreport.column_headers,
            'extra': extra_context,
            'items': [_item_values(o, advreport) for o in paginated.object_list[:]],
            'items_per_page': advreport.items_per_page,
            'item_count': len(object_list),
            'searchable_columns': advreport.searchable_columns,
            'search_fields': advreport.search_fields
        }
        return HttpResponse(simplejson.dumps(report, indent=2, default=_json_object_encoder))
Esempio n. 4
0
def entry(request, slug, archive=False):
    """
    Single blog entry, detail view
    """
    # News entry
    object = get_object_or_404(Entry, slug=slug, site=Site.objects.get_current())
    if object.status == DRAFT_STATUS and not request.user.is_staff or object.status == HIDDEN_STATUS:
        raise Http404

    # Comment form
    context = get_form_context(request, object)
    context.update(get_blog_context())

    # Paginated comments
    comments = Comment.objects.for_model(object).filter(is_removed=False, is_public=True)
    comments = paginate(request, comments, default_to_last_page=True)

    # Context
    context.update({'object':object, 'archive':archive, 'comments': comments })
    return context
Esempio n. 5
0
def list(request, slug, ids=None, internal_mode=False, report_header_visible=True):
    advreport = get_report_or_404(slug)
    advreport.set_request(request)
    advreport.internal_mode = internal_mode
    advreport.report_header_visible = report_header_visible

    def inner(request, slug, ids):
        context = {}

        # Handle POST
        if request.method == 'POST':
            sorted_keys = [k for k in request.POST.keys()]
            sorted_keys.sort()
            selected_object_ids = [k.split('_')[2] for k in sorted_keys if 'checkbox_' in k and request.POST[k] == 'true']
            method = request.POST['method']

            if not method:
                messages.warning(request, _(u'You did not select any action.'))
                if not advreport.internal_mode:
                    return _get_redirect(advreport)

            if len(selected_object_ids) == 0:
                messages.warning(request, _(u'You did not select any %(object)s.') % {'object': advreport.verbose_name})
                if not advreport.internal_mode:
                    return _get_redirect(advreport)

            try:
                response, count = advreport.handle_multiple_actions(method, selected_object_ids, request)
                if response:
                    return response

                if count > 0:
                    messages.success(request, _(u'Successfully executed action on %(count)d %(objects)s')
                                                    % {'count': count,
                                                       'objects': advreport.verbose_name_plural if count != 1 else advreport.verbose_name})
                else:
                    messages.error(request, _(u'No selected %(object)s is applicable for this action.') % {'object': advreport.verbose_name})
                if not advreport.internal_mode:
                    return _get_redirect(advreport, querystring=request.META['QUERY_STRING'])
            except ActionException, e:
                context.update({'error': e.msg})


        object_list, extra_context = advreport.get_object_list(request, ids=ids)
        context.update(extra_context)

        # CSV?
        if 'csv' in request.GET:
            from cStringIO import StringIO
            csv = StringIO()
            header = u'%s\n' % u';'.join(c['verbose_name'] for c in advreport.column_headers)
            lines = (u'%s\n' % u';'.join((c['html'] for c in o.advreport_column_values)) for o in object_list[:])
            lines = (line.replace(u' ', u' ') for line in lines)
            lines = (line.replace(u'€', u'€') for line in lines)
            lines = (line.replace(u'<br/>', u' ') for line in lines)
            lines = (strip_entities(line) for line in lines)
            lines = (strip_tags(line).encode('utf-8') for line in lines)
            csv.write(header)
            csv.writelines(lines)
            response = HttpResponse(csv.getvalue(), 'text/csv')
            response['Content-Disposition'] = 'attachment; filename="%s.csv"' % advreport.slug
            return response

        # Paginate
        paginated = paginate(request, object_list, num_per_page=advreport.items_per_page, use_get_parameters=True)

        # Extra context?
        context.update(advreport._extra_context(request))

        # Render
        context.update({'advreport': advreport,
                        'paginated': paginated,
                        'object_list': object_list})

        func = render_to_string if advreport.internal_mode else render_to_response
        return func(advreport.get_template(), context, context_instance=RequestContext(request))
Esempio n. 6
0
def report(request, content_type='', report_id='-1', order_column='-1', order_direction='A'):
    """
    Shows a StateReport
    """
    query_string = '&'.join(['%s=%s' % i for i in request.GET.items() if i[0].startswith('ITEM_')])

    def get_report_and_states(report_id):
        if content_type == '':
            raise Http404
        if '.' in content_type:
            app,model = content_type.split('.')
            ct = get_object_or_404(ContentType, app_label=app, model=model)
        else:
            ct = get_object_or_404(ContentType, pk=int(content_type))

        is_custom = False

        if report_id == '-1':
            default = StateReport.objects.default(ct)
            report_id = default.id
            is_custom = default.menu_order == -1

        if is_custom:
            machine = get_machine(ct)
            checkboxes = [p[0][5:] for p in request.GET.items() if p[0][:5] == 'ITEM_']
            report = default

            all_states = [{'obj': state,
                           'enabled': state[1] in checkboxes,
                           'count': State.objects.filter(state_id=state[0]).count()} for state in machine.states]
            states = State.objects.none()
            for checkbox in checkboxes:
                state_id = machine.states_dict_code[checkbox][0]
                states = states | State.objects.filter(state_id=state_id)
        else:
            all_states = []
            report = get_object_or_404(StateReport, pk=report_id)
            states = report.states

        return report, states, all_states, ct, is_custom

    def get_columns(report, order_column):
        columns = list(report.columns.all())
        column_names = [_(c.column_name) for c in columns]
        order_column = int(order_column)

        # determine which column to sort
        if order_column == -1:
            sorting_column = report.order_column
            sorting_desc = not report.ascending
            order_column = columns.index(report.order_column) if report.order_column in columns else -1
        else:
            sorting_column = report.columns.all()[order_column]
            sorting_desc = not order_direction == 'A'

        return columns, column_names, order_column, sorting_column, sorting_desc

    def sort_states(states, sorting_column, sorting_desc):
        if sorting_column:
            states = State.objects.sort_by_column(states, sorting_column, sorting_desc)
        return states

    def download(states, column_names, templates):
        csv = [ ','.join(column_names) ]

        renderers = [ c.create_template_renderer() for c in columns ]
        for state in states:
            csv.append(','.join([ unicode(r(state)) for r in renderers ]))

        response = HttpResponse(u'\r\n'.join(csv).encode('latin-1', 'ignore'), mimetype='text/csv')
        response['Content-Disposition'] = 'attachment; filename=%s.csv' % defaultfilters.slugify(report.name)
        return response

    # Get report and columns
    report, states, all_states, ct, is_custom = get_report_and_states(report_id)
    columns, column_names, order_column, sorting_column, sorting_desc = get_columns(report, order_column)

    # Call ordering, but only for xhr request or downloading request
    if 'xhr' in request.GET or 'download' in request.GET:
        states = sort_states(states, sorting_column, sorting_desc)
    else:
        states = State.objects.none()

    templates = [Template(c.expression) for c in columns]

    if 'download' in request.GET:
        return download(states, column_names, templates)

    # Paginate
    paginated_objects = paginate(request, states, num_per_page=14, use_get_parameters=True)

    # Render content
    renderers = [c.create_template_renderer() for c in columns]

    paged_states = [{'obj': s, 'columns': [r(s) for r in renderers]} for s in paginated_objects.object_list]

    return render(request, 'states/report.html', {
        'all_states': all_states,
        'states': paged_states,
        'column_names': column_names,
        'order_ascending': order_direction == 'A',
        'ascending': 'A',
        'descending': 'D',
        'report_id': report_id,
        'paginator': paginated_objects,
        'reports': get_state_report_context(ct),
        'report': report,
        'report_name': report.trans_name(),  # Avoid multiple datatrans lookups on this field.
        'order_column': order_column,
        'order_direction': order_direction,
        'count': len(states),
        'is_custom': is_custom,
        'query_string': query_string,
    })