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

Forced cast to string type #8

Open
TimurKady opened this issue Apr 6, 2021 · 4 comments · May be fixed by #17
Open

Forced cast to string type #8

TimurKady opened this issue Apr 6, 2021 · 4 comments · May be fixed by #17

Comments

@TimurKady
Copy link

TimurKady commented Apr 6, 2021

Friends, this widget works with the JSON type. If you do not explicitly specify a quoted string, then you do not need to interpret the value as a string.
Here is an example that actually causes an error in the application due to forced casting of a value to a string type.
Input: key field max_length, value 255. Result
{"max_length": "255"}

@nemesifier
Copy link
Member

This widget implements what we need in @openwisp, so it's good enough for us, but if you can find a way to retain the right type let us know, I'm open for improvements if they're not too complex.

@TimurKady
Copy link
Author

TimurKady commented Apr 8, 2021

it's not very difficult. You need to override the format_value method of the widget. It takes the value of the fields as a string. It needs to be processed. Here is some sample code I sketched for my project. This is the "first pass" (raw code without trying to do it nicely)

import json
from distutils.util import strtobool
from django.utils import dateparse

    def format_value(self, field_value):
        if field_value == '[]':
            return field_value
        data = json.loads(field_value)
        for key, value in data.items():
            if not isinstance(value, str):
                continue

            try:
                data[key] = strtobool(value)
                continue
            except:
                pass

            date_time = dateparse.parse_datetime(value)
            if date_time:
                data[key] = dateparse.parse_datetime(value)
                continue

            try:
                data[key] = int(value)
                continue
            except:
                pass

            try:
                data[key] = float(value)
                continue
            except:
                pass

        return json.dumps(data)

@atb00ker
Copy link
Member

atb00ker commented Apr 9, 2021

Thanks @Timurkhan for sharing your work, if you working on it for your project, could you please create a PR for upstream as well? 😄

@TimurKady
Copy link
Author

Certainly. At a minimum, we always provide links to all code elements that were not written by us, indicating the appropriate licenses and indicating the copyright holder / author.
Well, and on my own behalf, I always recommend tested code to my colleagues. If you have other ideas, I'm ready to do something useful :-)

@XF-FW XF-FW linked a pull request Mar 25, 2022 that will close this issue
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

Successfully merging a pull request may close this issue.

3 participants