Git Product home page Git Product logo

flask-admin-modal's People

Contributors

chimeworld avatar ernstki avatar pjcunningham avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

flask-admin-modal's Issues

Python 3 encode/decode not working

The process of encoding/decoding the id list doesn't work for me in Python 3. I get errors due to encoding str instead of byte objects and vice versa on the decode side. However, I don't want to try to make it work for both Python 3 and 2 at the same time so I'll just include my solution here for future reference.

Someone else could work this into the example or if I'm over complicating things show my error. But hopefully this helps someone.

@expose('/', methods=['POST'])
def index(self):
    if request.method == 'POST':
        # get the original checked ids
        url = get_redirect_target() or self.get_url('.index_view')
        ids = request.form.getlist('rowid')
        joined_ids = ','.join(ids)
        # could not encode as a `str` so it must be encoded
        encoded_ids = base64.b64encode(joined_ids.encode('utf-8'))
        change_form = ReconcileForm()
        # now the `byte` object needs to be encoded into a `str` for the form
        change_form.ids.data = str(encoded_ids, 'utf-8')
        self._template_args['url'] = url
        self._template_args['change_form'] = change_form
        self._template_args['change_modal'] = True
        return self.index_view()

@expose('/update/', methods=['POST'])
def update_view(self):
    if request.method == 'POST':
        url = get_redirect_target() or self.get_url('.index_view')
        change_form = ReconcileForm(request.form)
        if change_form.validate():
            decoded_ids = base64.b64decode(change_form.ids.data)
            # again `byte` object needs to be made into a `str`
            ids = str(decoded_ids, 'utf-8').split(',')
            cost = change_form.cost.data
            _update_mappings = [{'id': rowid, 'cost': cost} for rowid in ids]
            db.session.bulk_update_mappings(Project, _update_mappings)
            db.session.commit()
            flash("Set cost for {} record{} to {}."
                  .format(len(ids), 's' if len(ids) > 1 else '', cost),
                  category='info')
            return redirect(url)
        else:
            # Form failed validation
            self._template_args['url'] = url
            self._template_args['change_form'] = change_form
            self._template_args['change_modal'] = True
            return self.index_view()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.