def set_language(request): logger.debug('SET LANGAUGE') url = '/' + '/'.join(request.META['HTTP_REFERER'].split('/')[3:]) match = resolve(url) logger.debug('MATCH: {0}'.format(match)) if match: url = '{0}:{1}'.format(match.namespace, match.url_name) else: url = 'people:choose_language' if request.method == 'POST': if request.POST.get('language', '') != '': user_language = request.POST.get('language', '') person = get_or_create_person(request) set_current_language_for_person(person, user_language) translation.activate(user_language) request.session[translation.LANGUAGE_SESSION_KEY] = user_language response = redirect( reverse(url)) # render(request, 'people/set_language.html') response = set_language_cookie(response, user_language) logger.debug('RESPONSE: {0}'.format(response)) return response else: return redirect(reverse(url))
def choose_language(request): person = get_or_create_person(request) if not person: return redirect(reverse('account_login')) current_language = get_current_language(request) if current_language: set_current_language_for_person(person, current_language) next_page = request.GET.get('next', None) known_languages = KnownLanguage.objects.filter(person=person).count() if known_languages > 0: extra = known_languages else: extra = 1 unknown = get_unknown_languages(person) KnownLanguageFormset = inlineformset_factory( Person, KnownLanguage, form=KnownLanguageFormWithPerson, fields=('language', 'level_of_proficiency', 'person', 'accent', 'dialect'), max_num=get_num_supported_languages(), extra=extra, ) # formset = KnownLanguageFormset(form_kwargs={'person':person}) # KnownLanguageFormsetWithPerson = inlineformset_factory(Person, KnownLanguage, form=form, fields=('language','level_of_proficiency','person'), max_num=get_num_supported_languages(), extra=known_languages+1) formset = KnownLanguageFormset(instance=person, form_kwargs={ 'person': person, 'require_proficiency': True }) if request.method == 'POST': # Upon first post to the person choosing their language # We can ensure that the user just signed up person.just_signed_up = False person.save() formset = KnownLanguageFormset(request.POST, request.FILES, instance=person, form_kwargs={ 'person': person, 'require_proficiency': True }) if formset.has_changed(): if formset.is_valid(): instances = formset.save() current_language = get_current_language(request) if not current_language: for instance in instances: if instance.active: current_language = obj.language if not current_language: current_language = translation.get_language() try: set_current_language_for_person(person, current_language) except: logger.debug( "We may be trying to set a language when knownlanguage doens't exist" ) if next_page: response = redirect(reverse(next_page)) else: response = redirect(reverse('people:choose_language')) response = set_language_cookie(response, current_language) return response else: if formset.is_valid(): if next_page: return redirect(reverse(next_page)) # else: # return redirect(reverse('people:choose_language')) # formset = KnownLanguageFormsetWithPerson(instance=person) response = render( request, 'people/choose_language.html', { 'known_language_form': formset, 'known_languages': known_languages, 'unknown_languages': unknown, 'just_signup_track': person.just_signed_up, }) current_language = get_current_language(request) if current_language: set_current_language_for_person(person, current_language) response = set_language_cookie(response, current_language) else: logger.debug('no current language') return response