Example #1
0
def tap_list(request):
    context = RequestContext(request)
    taps = [{'tap': tap} for tap in request.kbsite.taps.all()]
    for tinfo in taps:
        tap = tinfo['tap']
        prefix = 'tap-%s' % tap.seqn
        tinfo['prefix'] = prefix

        # Demux multiple forms using submit button name.
        submit_name = "%s-submit" % prefix
        edit_form = None
        if request.method == 'POST' and submit_name in request.POST:
            edit_form = forms.TapForm(request.POST,
                                      site=request.kbsite,
                                      instance=tap,
                                      prefix=prefix)
            if edit_form.is_valid():
                tap = edit_form.save(commit=False)
                val = edit_form.cleaned_data['ml_per_tick']
                ml = val[0]
                if ml == '0':
                    ml = val[1]
                tap.ml_per_tick = float(ml)
                tap.save()
                messages.success(request, 'Tap "%s" was updated.' % tap.name)
            else:
                messages.error(request,
                               'Your form had errors, please correct them.')
        if not edit_form:
            edit_form = forms.TapForm(site=request.kbsite,
                                      instance=tap,
                                      prefix=prefix,
                                      initial={
                                          'ml_per_tick': (str(tap.ml_per_tick),
                                                          str(tap.ml_per_tick))
                                      })

        for field in edit_form.fields.values():
            widget = field.widget
            if widget.is_hidden:
                continue
            if isinstance(widget, widgets.Textarea) or isinstance(
                    widget, widgets.TextInput):
                new_class = widget.attrs.get('class', '') + ' span10'
                #widget.attrs['class'] = new_class.strip()

        tinfo['edit_form'] = edit_form
        if tap.current_keg and tap.current_keg.is_active():
            tinfo['end_form'] = forms.KegHiddenSelectForm(
                initial={'keg': tap.current_keg})
        else:
            tinfo['keg_form'] = forms.ChangeKegForm()

    context['all_taps'] = taps
    context['create_tap_form'] = forms.TapForm(site=request.kbsite)
    return render_to_response('kegadmin/tap-edit.html', context)
Example #2
0
def do_end_keg(request):
    form = forms.KegHiddenSelectForm(request.POST)
    if form.is_valid():
        keg = form.cleaned_data['keg']
        if keg.site == request.kbsite:
            keg.status = "offline"
            keg.enddate = datetime.datetime.now()
            keg.save()
            if keg.current_tap:
                keg.current_tap.current_keg = None
                keg.current_tap.save()
            messages.success(request, 'The keg was deactivated.')
    result_url = reverse('kegadmin-taps', args=[request.kbsite.url()])
    return redirect_to(request, result_url)
Example #3
0
def tap_list(request):
    context = RequestContext(request)

    # Create a new tap if needed.
    context['create_tap_form'] = forms.TapForm(site=request.kbsite)
    if request.method == 'POST' and 'new-tap-submit' in request.POST:
        success, form = _create_or_update_tap(None, request)
        if not success:
            context['create_tap_form'] = form

    for tap in request.kbsite.taps.all():
        submit_name = 'tap-%s-delete' % tap.seqn
        if request.method == 'POST' and submit_name in request.POST:
            tap_name = tap.name
            tap.delete()
            messages.success(request, 'Tap "%s" was deleted.' % tap_name)

    # Build/process forms for all taps.
    taps = [{'tap': tap} for tap in request.kbsite.taps.all()]
    for tinfo in taps:
        tap = tinfo['tap']
        prefix = 'tap-%s' % tap.seqn
        tinfo['prefix'] = prefix

        # Demux multiple forms using submit button name.
        submit_name = "%s-submit" % prefix
        if request.method == 'POST' and submit_name in request.POST:
            success, form = _create_or_update_tap(tap, request)
            if not success:
                tinfo['edit_form'] = form

        if 'edit_form' not in tinfo:
            tinfo['edit_form'] = forms.TapForm(site=request.kbsite,
                                               instance=tap,
                                               prefix=prefix,
                                               initial={
                                                   'ml_per_tick':
                                                   (str(tap.ml_per_tick),
                                                    str(tap.ml_per_tick))
                                               })

        if tap.current_keg and tap.current_keg.is_active():
            tinfo['end_form'] = forms.KegHiddenSelectForm(
                initial={'keg': tap.current_keg})
        else:
            tinfo['keg_form'] = forms.ChangeKegForm()

    context['all_taps'] = taps
    return render_to_response('kegadmin/tap-edit.html', context)