Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 encode/decode not working #2

Closed
michaeltoohig opened this issue Mar 7, 2019 · 4 comments
Closed

Python 3 encode/decode not working #2

michaeltoohig opened this issue Mar 7, 2019 · 4 comments

Comments

@michaeltoohig
Copy link

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()

@pjcunningham
Copy link
Owner

Hello Michael,

Thanks for pointing out the problem. I will look into this.

Cheers, Paul.

@Alek-dr
Copy link

Alek-dr commented Jun 19, 2020

If it's still actual:

    @expose('/', methods=['POST'])
    def index(self):
        if request.method == 'POST':
            url = get_redirect_target() or self.get_url('.index_view')
            ids = request.form.getlist('rowid')
            joined_ids = ','.join(ids)
            change_form = ChangeForm()
            change_form.ids.data = joined_ids
            self._template_args['url'] = url
            self._template_args['change_form'] = change_form
            self._template_args['change_modal'] = True
            return self.index_view()

@pjcunningham
Copy link
Owner

Hello Alexandr,

Thanks - will try and incorporate this fix over the weekend.

Cheers, Paul.

@pjcunningham
Copy link
Owner

Finally fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants