Ejemplo n.º 1
0
def _label(model, value_list, datakeys=None):
    """Make labels for the table head.
    Returns a list of tuples. Each tuple contains the verbose label and a key
    that can be used for sort parameters in the URL.
    """
    attrs = []
    labels = []
    for value in value_list:
        if value in datakeys:
            labels.extend(datakeys[value])
            attrs.extend(datakeys[value])
        else:
            attrs.append(value)
            try:
                labels.append(get_verbose_name(model, value))
            except FieldDoesNotExist:
                labels.append(value)
    return zip(labels, attrs)
Ejemplo n.º 2
0
def _label(model, value_list, datakeys=None):
    """Make labels for the table head.
    Returns a list of tuples. Each tuple contains the verbose label and a key
    that can be used for sort parameters in the URL.
    """
    attrs = []
    labels = []
    for value in value_list:
        if value in datakeys:
            labels.extend(datakeys[value])
            attrs.extend(datakeys[value])
        else:
            attrs.append(value)
            try:
                labels.append(get_verbose_name(model, value))
            except FieldDoesNotExist:
                labels.append(value)
    return zip(labels, attrs)
Ejemplo n.º 3
0
def move(request,
         model,
         form_model,
         redirect,
         title_attr='id',
         extra_context=None):

    if not extra_context:
        extra_context = {}

    # If no post or no objects selected, start over
    if request.method != 'POST':
        return HttpResponseRedirect(reverse(redirect))
    if not request.POST.getlist('object'):
        new_message(request, "You need to select at least one object to edit",
                    Messages.ERROR)
        return HttpResponseRedirect(reverse(redirect))

    data = None
    confirm = False
    verbose_name = model._meta.verbose_name
    objects = model.objects.filter(id__in=request.POST.getlist('object'))

    # Find out what step we are on, or default to start of wizard
    try:
        step = int(request.POST.get('step', '0'))
    except ValueError:
        step = STEP_CHANGEVALUES

    # Choose new values to foreign keys
    if step == STEP_CHANGEVALUES:
        form = form_model()
        step = STEP_CONFIRM

    # Confirm the changes
    elif step == STEP_CONFIRM:
        form = form_model(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            confirm = True
            step = STEP_SAVE

    # Update the objects
    elif step == STEP_SAVE:
        form = form_model(request.POST)
        if form.is_valid():
            foreign_keys = form.cleaned_data.keys()
            data = dict()

            # If nothing is selected, don't update
            for key in foreign_keys:
                if not form.cleaned_data[key] is None:
                    data[key] = form.cleaned_data[key]

            # Update
            objects.update(**data)

            # Generate message based on what was changed and redirect back
            foreign_key_string = ", ".join(
                [get_verbose_name(model, key) for key in data])

            if foreign_key_string == "":
                foreign_key_string = "nothing"

            new_message(
                request, "Changed %s on %i %s models" %
                (foreign_key_string, len(objects), verbose_name),
                Messages.SUCCESS)

            return HttpResponseRedirect(reverse(redirect))

    # Keep values from the form and pass them as context
    if form:
        fields = form.fields.keys()
    else:
        fields = form_model().fields.keys()

    values = objects.values('pk', title_attr, *fields)
    object_list = _parse_value_differences(values, data, title_attr, fields)

    context = {
        'form': form or '',
        'objects': objects,
        'values': object_list,
        'data': data,
        'confirm': confirm,
        'sub_active': {
            'list': True
        },
        'title': 'Move %s' % verbose_name,
        'step': step,
    }

    extra_context.update(context)

    return render(request, 'seeddb/move.html', extra_context)
Ejemplo n.º 4
0
def test_verbose_name():
    """Test that get_verbose_name() works on all supported Django versions"""
    from nav.models.manage import Netbox
    name = get_verbose_name(Netbox, 'type__name')
    assert name == 'type name'