Skip to content

preferred language #810

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

Merged
merged 3 commits into from
Apr 25, 2018
Merged

preferred language #810

merged 3 commits into from
Apr 25, 2018

Conversation

kziemianek
Copy link
Contributor

Description

Allow the user to set different language for the Gitcoin app

Checklist
  • linter status: 100% pass
  • changes don't break existing behavior
  • commit message follows [commit guidelines]
Affected core subsystem(s)

backend, frontend

Testing

locally

Refers/Fixes

Refs: #802

@kziemianek
Copy link
Contributor Author

kziemianek commented Apr 5, 2018

I leave Polish locales (translated only navbar) to allow You check how it works locally. I'll remove them before final push. User can change preferred language in email settings, I hope it will be moved to profile settings in #795.
Emails translation to be done. Did i miss anything?

Copy link
Contributor

@mbeacom mbeacom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider

@@ -420,6 +421,15 @@ def email_settings(request, key):
else:
raise Http404
es = es.first()
profile_id = request.session.get('profile_id')
profile = Profile.objects.get(pk=profile_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to check if profile_id is None here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if it's possible... can user without profile change preferences ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An anonymous user can still hit the https://gitcoin.co/email/settings/ endpoint at the moment. We'll want to either restrict the view from being accessed without auth or handle when profile_id = request.session.get('profile_id') is None 😢

Copy link
Contributor Author

@kziemianek kziemianek Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferred_language selection will be available only if profile_id is present in session data, is it fine ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kziemianek 👍 Works for me! Realistically, we'll restrict this view from being accessed by users that haven't authenticated, so that should work!

@@ -684,6 +684,7 @@ class Profile(SuperModel):
last_sync_date = models.DateTimeField(null=True)
email = models.CharField(max_length=255, blank=True, db_index=True)
github_access_token = models.CharField(max_length=255, blank=True, db_index=True)
pref_lang_code = models.CharField(max_length=10)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should limit the choices to LANGUAGES and we can probably set the max_length to 2.

@@ -420,6 +421,15 @@ def email_settings(request, key):
else:
raise Http404
es = es.first()
profile_id = request.session.get('profile_id')
profile = Profile.objects.get(pk=profile_id)
if request.POST.get('preferred_language', False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the False here. We could probably just do:

preferred_language = request.POST.get('preferred_language')
if preferred_language:
    profile.pref_lang_code = preferred_language
    profile.save()

Also should we assert preferred_language is in LANGUAGES?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should we assert preferred_language is in LANGUAGES?

Yes !! Better safe than sorry

@@ -420,6 +421,15 @@ def email_settings(request, key):
else:
raise Http404
es = es.first()
profile_id = request.session.get('profile_id')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note... This is changing and will soon be: profile = request.user.profile

session_data = {
'handle': user_profile.handle,
'email': user_profile.email,
'access_token': user_profile.github_access_token,
'profile_id': user_profile.pk,
'name': user_profile.data.get('name', None),
'access_token_last_validated': timezone.now().isoformat(),
}
for k, v in session_data.items():
LANGUAGE_SESSION_KEY: user_profile.pref_lang_code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user doesn't have pref_lang_code set, we shouldn't manipulate LANGUAGE_SESSION_KEY.

<label class="form__label" for="level">{% trans "Preferred language" %}</label>
<div class="form__select">
<select id="preferred_language" name="preferred_language" required>
{% get_language_info_list for available_languages as langs %}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to replace this with:

{% get_available_languages as global_languages %}
{% get_language_info_list for global_languages as langs %}

Then you can eliminate the 'available_languages': ['en', 'pl'], part all together.

@@ -464,6 +474,8 @@ def email_settings(request, key):
'msg': msg,
'autocomplete_keywords': json.dumps(
[str(key) for key in Keyword.objects.all().values_list('keyword', flat=True)]),
'available_languages': ['en', 'pl'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed per the comment below.

@owocki owocki mentioned this pull request Apr 5, 2018
@codecov
Copy link

codecov bot commented Apr 6, 2018

Codecov Report

Merging #810 into master will decrease coverage by 0.4%.
The diff coverage is 7.65%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #810      +/-   ##
==========================================
- Coverage   33.08%   32.68%   -0.41%     
==========================================
  Files         100      100              
  Lines        6435     6539     +104     
  Branches      780      783       +3     
==========================================
+ Hits         2129     2137       +8     
- Misses       4208     4304      +96     
  Partials       98       98
Impacted Files Coverage Δ
app/app/pipeline.py 0% <0%> (ø) ⬆️
app/marketing/mails.py 10.5% <1.38%> (-3.29%) ⬇️
app/app/settings.py 80.66% <100%> (-1.65%) ⬇️
app/marketing/views.py 11.72% <18.75%> (+0.07%) ⬆️
app/tdi/views.py 19.1% <20%> (+0.02%) ⬆️
app/app/utils.py 22.92% <50%> (+0.7%) ⬆️
app/dashboard/models.py 64.39% <66.66%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b4d80a...621282c. Read the comment docs.

session_data = {
'handle': user_profile.handle,
'email': user_profile.email,
'access_token': user_profile.github_access_token,
'profile_id': user_profile.pk,
'name': user_profile.data.get('name', None),
'access_token_last_validated': timezone.now().isoformat(),
LANGUAGE_SESSION_KEY: user_profile.pref_lang_code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove this in favor of the if check below?

@@ -47,7 +47,7 @@ <h5>{% trans "Basic Preferences" %}</h5>
</div>
<div class="form-group">
<label class="form__label" for="level">{% trans "Send me" %}</label>
<div class="form__select">
<div class="form__select">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the trailing whitespace here?

@@ -420,6 +421,15 @@ def email_settings(request, key):
else:
raise Http404
es = es.first()
profile_id = request.session.get('profile_id')
profile = Profile.objects.get(pk=profile_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An anonymous user can still hit the https://gitcoin.co/email/settings/ endpoint at the moment. We'll want to either restrict the view from being accessed without auth or handle when profile_id = request.session.get('profile_id') is None 😢

@mbeacom mbeacom added frontend This needs frontend expertise. waiting on contributor Gitcoin Emails Gitcoin Emails labels Apr 9, 2018
owocki added a commit that referenced this pull request Apr 9, 2018
@kziemianek kziemianek changed the title WIP: preferred language preferred language Apr 11, 2018
@kziemianek
Copy link
Contributor Author

Tests fix in #860

@mbeacom
Copy link
Contributor

mbeacom commented Apr 17, 2018

@kziemianek Mind resolving the conflicts here and we'll get it merged?

@kziemianek kziemianek force-pushed the pref_language branch 2 times, most recently from 534525c to 41a5706 Compare April 17, 2018 19:31
'locale',
)

LANGUAGES = [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

)

LANGUAGES = [
('en', _('English'))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

@@ -405,3 +415,4 @@
}]
SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT = env.int(
'SILKY_MAX_RECORDED_REQUESTS_CHECK_PERCENT', default=10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W391 blank line at end of file
W293 blank line contains whitespace

app/app/utils.py Outdated
@@ -55,6 +56,11 @@ def add_contributors(repo_data):
return repo_data


def setup_lang(handle, request):
profile = Profile.objects.get(handle=handle)
request.session[LANGUAGE_SESSION_KEY] = profile.get_profile_preferred_language()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

app/app/utils.py Outdated
@@ -90,7 +96,7 @@ def sync_profile(handle, user=None):
except Exception as e:
logger.error(e)
return None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W293 blank line contains whitespace

@@ -575,23 +577,34 @@ def email_settings(request, key):
suppress_leaderboard = False
email = ''
level = ''
msg = ''

msg = ''

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

if request.POST and request.POST.get('submit'):
email = request.POST.get('email')
level = request.POST.get('level')
profile = Profile.objects.get(pk=request.user.profile.id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

if request.POST and request.POST.get('submit'):
email = request.POST.get('email')
level = request.POST.get('level')
profile = Profile.objects.get(pk=request.user.profile.id)
if profile:
pref_lang = profile.get_profile_preferred_language();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E703 statement ends with a semicolon

@@ -603,14 +616,17 @@ def email_settings(request, key):
else:
es.metadata['ip'].append(ip)
es.save()
msg = "Updated your preferences. "
msg = "Updated your preferences. "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

context = {
'nav': 'internal',
'active': '/settings/email',
'title': _('Email Settings'),
'es': es,
'msg': msg,
'navs': settings_navs,
'available_languages': ['en', 'pl'],
'preferred_language': pref_lang,
'logged_in' : False if request.session.get('profile_id') is None else True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E203 whitespace before ':'

@kziemianek kziemianek force-pushed the pref_language branch 3 times, most recently from 1701c4b to 0047d19 Compare April 19, 2018 17:55
@@ -827,7 +827,8 @@ class Profile(SuperModel):
handle = models.CharField(max_length=255, db_index=True)
last_sync_date = models.DateTimeField(null=True)
email = models.CharField(max_length=255, blank=True, db_index=True)
github_access_token = models.CharField(max_length=255, blank=True, db_index=True)
github_access_token = models.CharField(max_length=255, blank=True, db_index=True)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

@kziemianek kziemianek force-pushed the pref_language branch 2 times, most recently from cf7b16d to 6d439a7 Compare April 19, 2018 18:12

@receiver(user_logged_out)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E303 too many blank lines (2)

@@ -0,0 +1,19 @@
# Generated by Django 2.0.3 on 2018-04-19 17:47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrations will need rebuilt here.
sorry

@@ -1043,19 +1044,20 @@ def get_access_token(self, save=True):
return ''
return access_token

@receiver(user_logged_in)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shouldn't be on Profile . Can you indent them back?

"""Handle actions to take on user logout."""
from dashboard.utils import create_user_action
create_user_action(user, 'Logout', request)
def get_profile_preferred_language(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to move this back up to Profile after you move the signals out.

preferred_language = profile.get_profile_preferred_language()
translation.activate(preferred_language)
except Profile.DoesNotExist:
print(f'-- Could not determine recipient preferred email, using default.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for literal string interpolation here since we're not using any vars.

@@ -611,6 +624,8 @@ def email_settings(request, key):
'es': es,
'msg': msg,
'navs': settings_navs,
'available_languages': ['en', 'pl'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop this now that you're doing: {% get_available_languages as LANGUAGES %} and {% get_language_info_list for LANGUAGES as langs %}

Copy link
Contributor

@mbeacom mbeacom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🙌 :shipit:

@mbeacom mbeacom merged commit d01574a into gitcoinco:master Apr 25, 2018
@owocki
Copy link
Contributor

owocki commented Apr 25, 2018

its live!

@owocki
Copy link
Contributor

owocki commented Apr 25, 2018

#979

do we need to do more regression testing here? this is just a random feature i tried

@mbeacom mbeacom mentioned this pull request Apr 25, 2018
3 tasks
@kziemianek kziemianek deleted the pref_language branch December 4, 2018 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend This needs frontend expertise. Gitcoin Emails Gitcoin Emails
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants