Ejemplo n.º 1
0
def new_total(request):
    """
    This view lets accountants set the amount collected
    to be displayed on the front page
    """
    def sort_totals_reversed(objects):
        """
        show the objects in reverse order (highest id first)
        """
        return sorted(objects, key=lambda obj: obj.id, reverse=True)

    try:
        last_total = TheTotal.get_total()
        last_sum = last_total.amount_actual
        last_promised = last_total.amount_promised
        last_shirts = last_total.num_shirts
    except:
        last_sum = 0
        last_promised = 0
        last_shirts = 0

    class NewTotal(colander.MappingSchema):
        """
        colander schema for setting the collected sum
        """
        amount_collected = colander.SchemaNode(
            colander.Int(),
            title=_(
                u"Sum collected (Euro, Integer) "
                "(This amount will be deducted from 70000 and "
                "the result be displayed on the landing page)"),
            validator=colander.Range(0, 200000),
            default=last_sum,
            oid="sum",
        )
        amount_promised = colander.SchemaNode(
            colander.Int(),
            title=_(u"Sum promised (Euro, Integer)"),
            default=last_promised,
            oid="promised",
        )
        num_shirts = colander.SchemaNode(
            colander.Int(),
            title=_(u"Number of shirts (Integer)"),
            default=last_shirts,
            oid="shirts",
        )

    schema = NewTotal()

    form = deform.Form(
        schema,
        buttons=[
            deform.Button('submit', _(u'Submit')),
            deform.Button('reset', _(u'Reset'))
        ],
        #use_ajax=True,
        #renderer=zpt_renderer
    )

    # get and show the former totals
    _totals = TheTotal.get_listing(
        TheTotal.id.desc())

    # if the form has been used and SUBMITTED, check contents
    if 'submit' in request.POST:
        print("the form was submitted")
        controls = request.POST.items()
        try:
            appstruct = form.validate(controls)
            print("the appstruct: %s" % appstruct)

            # time to save the data to the DB
            #{
            #    'num_shirts': 123,
            #    'amount_collected': 12345,
            #    'amount_promised': 23456
            #}

            _new_total = TheTotal(
                amount_actual=appstruct['amount_collected'],
                amount_promised=appstruct['amount_promised'],
                num_shirts=appstruct['num_shirts'],
            )
            try:
                DBSession.add(_new_total)
                DBSession.flush()
                _totals = TheTotal.get_listing(
                    TheTotal.id.desc())

            except:
                print("could not write to DB. Error: ")

        except ValidationFailure, e:
            print(e)

            request.session.flash(
                _(u"Please note: There were errors, "
                  "please check the form below."),
                'message_above_form',
                allow_duplicate=False)
            return{'form': e.render(),
                   'totals': {}}
Ejemplo n.º 2
0
def new_total(request):
    """
    This view lets accountants set the amount collected
    to be displayed on the front page
    """

    class NewTotal(colander.MappingSchema):
        """
        colander schema for setting the collected sum
        """
        amount_collected = colander.SchemaNode(
            colander.Int(),
            title=_(u"sum collected"),
            validator=colander.Range(0, 200000),
            oid="sum",
        )
        amount_promised = colander.SchemaNode(
            colander.Int(),
            title=_(u"promised"),
            oid="promised",
        )
        num_shirts = colander.SchemaNode(
            colander.Int(),
            title=_(u"shirts"),
            oid="shirts",
        )

    schema = NewTotal()

    form = deform.Form(
        schema,
        buttons=[
            deform.Button('submit', _(u'Submit')),
            deform.Button('reset', _(u'Reset'))
        ],
        #use_ajax=True,
        #renderer=zpt_renderer
    )

    # get and show the former totals
    _totals = TheTotal.get_listing(
        TheTotal.id.asc())

    # if the form has been used and SUBMITTED, check contents
    if 'submit' in request.POST:
        print("the form was submitted")
        controls = request.POST.items()
        try:
            appstruct = form.validate(controls)
            print("the appstruct: %s" % appstruct)

            # time to save the data to the DB
            #{
            #    'num_shirts': 123,
            #    'amount_collected': 12345,
            #    'amount_promised': 23456
            #}

            _new_total = TheTotal(
                amount_actual=appstruct['amount_collected'],
                amount_promised=appstruct['amount_promised'],
                num_shirts=appstruct['num_shirts'],
            )
            try:
                DBSession.add(_new_total)
                DBSession.flush()
            except:
                print("could not write to DB. Error: ")

        except ValidationFailure, e:
            print(e)

            request.session.flash(
                _(u"Please note: There were errors, "
                  "please check the form below."),
                'message_above_form',
                allow_duplicate=False)
            return{'form': e.render(),
                   'totals': {}}