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
Improve phone handling #1433
Improve phone handling #1433
Conversation
saleor/userprofile/forms.py
Outdated
@@ -15,13 +16,18 @@ def get_address_form(data, country_code, initial=None, instance=None, **kwargs): | |||
country_code = country_form.cleaned_data['country'] | |||
preview = country_form.cleaned_data['preview'] | |||
|
|||
if data: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I am struggling right now is to transfer user input to the form, so it's handled properly (by 'properly' I mean to glue phoneprefix
and phonenumber
and doing a reverse - after providing address data, user can go back and select another address - proper fields should be repopulated. It could be done with a lot of ifs
(checking if instance exists, if it has phone
attribute, etc), but it seems to be very non-elegant and messy solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll find it rather challenging to handle this as two separate fields. I think it would be much easier if it was a single field (as it's going to be stored as such) with a MultiWidget.
In fact there is already one provided by the library you're using.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Such a great tip, thank you, solved many issues I had!
saleor/userprofile/i18n.py
Outdated
exclude = ['phone'] | ||
|
||
phoneprefix = ChoiceField(choices=phone_prefixes) | ||
phone = forms.CharField() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried use this one:
https://github.com/stefanfoulis/django-phonenumber-field/blob/master/phonenumber_field/formfields.py
but with not much success, as it wants "correct" number (with phone country code included), while here we want to separate things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we don't necessarily want to separate them as fields, we want separate widgets.
saleor/userprofile/i18n.py
Outdated
super(AddressForm, self).__init__(*args, **kwargs) | ||
if self.instance.phone: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why, but form does not see phone
and phoneprefix
provided in the way form(instance=instance, dtaa, phone=phone
...), without those lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a workaround and not a solution.
saleor/userprofile/i18n.py
Outdated
@@ -37,6 +48,12 @@ | |||
'zip': pgettext_lazy('Address field', 'ZIP code')} | |||
|
|||
|
|||
class PhonePrefixWidget(PhoneNumberPrefixWidget): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to overwrite this one as there are issues with default widget value:
stefanfoulis/django-phonenumber-field#82
Quick example: choosing "United Kingdom +44 -> continue -> select new address makes field being populated by Guernsey as it has the same phone code, but it's alphabetically before UK.
Codecov Report
@@ Coverage Diff @@
## master #1433 +/- ##
=========================================
Coverage ? 83.27%
=========================================
Files ? 130
Lines ? 5882
Branches ? 678
=========================================
Hits ? 4898
Misses ? 821
Partials ? 163
Continue to review full report at Codecov.
|
a722964
to
0623a62
Compare
@dominik-zeglen Address form requires a few UI improvements:
|
saleor/userprofile/i18n.py
Outdated
|
||
|
||
PhoneNumberField.default_validators = [validate_possible_number] | ||
PhoneNumberField.to_python = lambda self, value: value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this kind of patching, we should rather extend the original class and override the methods or fields that we want to customize. This would be also a good place to include relevant docstrings, explaining how the customization is different from the original implementation.
saleor/userprofile/i18n.py
Outdated
@@ -37,6 +50,22 @@ | |||
'zip': pgettext_lazy('Address field', 'ZIP code')} | |||
|
|||
|
|||
class CustomPhonePrefix(PhonePrefixSelect): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good practice to include the widget type in the class name so in this case, CustomPhonePrefixSelect
would be a better name. Also, the word "custom" doesn't say much. What makes this widget custom? It would be great if we could reflect the purpose of this class in the name.
saleor/userprofile/i18n.py
Outdated
class CustomPhonePrefix(PhonePrefixSelect): | ||
|
||
def __init__(self, initial=None): | ||
choices = phone_prefixes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's no need to assign phone_prefixes
to a new variable here.
saleor/userprofile/i18n.py
Outdated
choices = phone_prefixes | ||
if initial: | ||
self.initial = phone_prefixes[initial] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty blank lines in simple functions aren't necessary, it doesn't add much to readability.
saleor/userprofile/i18n.py
Outdated
super(PhonePrefixSelect, self).__init__(choices=choices) | ||
|
||
|
||
class PhonePrefixWidget(PhoneNumberPrefixWidget): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docstring.
I want to merge this change because...
Fixes #1375
Pull Request Checklist
pycodestyle
,pydocstyle
,pylint
.eslint
.