コード例 #1
0
ファイル: viewbase.py プロジェクト: javagrails/a-plus
    def get_resource_objects(self):
        super().get_resource_objects()
        user = self.request.user
        instance = self.get_course_instance_object()
        if instance is not None:
            self.instance = instance
            self.course = self.instance.course
            self.content = CachedContent(self.instance)
            self.user_course_data = None
            is_real_user = user.is_authenticated and not user.is_anonymous
            if is_real_user:
                self.user_course_data = self.instance.get_enrollment_for(user)
            self.is_student = self.instance.is_student(user)
            self.is_assistant = self.instance.is_assistant(user)
            self.is_teacher = self.course.is_teacher(user)
            self.is_course_staff = self.is_teacher or self.is_assistant
            self.get_taggings = lambda: CachedStudent(instance, user.id).data['tag_slugs']
            self.url_without_language = remove_query_param_from_url(self.request.get_full_path(), 'hl')
            self.query_language = None
            self.user_language = None

            self.note(
                "course", "instance", "content", "user_course_data", "is_student", "is_assistant",
                "is_teacher", "is_course_staff", "get_taggings", "url_without_language",
                "query_language", "user_language"
            )

            # Try to find a language that is defined for this course instance
            # and apply it
            if self.instance.language:
                instance_languages = self.instance.language.strip('|').split('|')
                instance_def_language = instance_languages[0]
                instance_languages = set(instance_languages)

                languages = []
                if self.user_course_data and self.user_course_data.language:
                    languages.append(self.user_course_data.language)
                if is_real_user and user.userprofile.language:
                    languages.append(user.userprofile.language)
                languages.append(get_language())

                query_language = self.request.GET.get('hl')
                if query_language:
                    if query_language[:2] in instance_languages:
                        language = query_language
                        if languages:
                            self.user_language = languages[0]
                            if self.user_language[:2] != query_language[:2]:
                                self.query_language = query_language
                    else:
                        raise TranslationNotFound
                else:
                    for lang in languages:
                        if lang[:2] in instance_languages:
                            language = lang
                            break
                    else:
                        language = instance_def_language

                translation.activate(language)
コード例 #2
0
ファイル: viewbase.py プロジェクト: javagrails/a-plus
 def handle_exception(self, exc):
     if isinstance(exc, TranslationNotFound):
         instance_languages = self.instance.language.strip("|").split("|")
         url = remove_query_param_from_url(self.request.get_full_path(), 'hl')
         for i, lang in enumerate(instance_languages):
             instance_languages[i] = {"name": get_language_info(lang)['name'], "url": update_url_params(url, {'hl' : lang})}
         return render_to_response('404.html', {'error_msg': str(exc), 'languages': instance_languages}, status=404)
     return super().handle_exception(exc)
コード例 #3
0
ファイル: views.py プロジェクト: javagrails/a-plus
    def post(self, request, *args, **kwargs):
        LANGUAGE_PARAMETER = 'language'

        next = remove_query_param_from_url(
            request.POST.get('next', request.GET.get('next')), 'hl')
        if ((next or not request.is_ajax())
                and not is_safe_url(url=next,
                                    allowed_hosts={request.get_host()},
                                    require_https=request.is_secure())):
            next = remove_query_param_from_url(
                request.META.get('HTTP_REFERER'), 'hl')
            next = next and unquote(next)  # HTTP_REFERER may be encoded.
            if not is_safe_url(url=next,
                               allowed_hosts={request.get_host()},
                               require_https=request.is_secure()):
                next = '/'
        response = HttpResponseRedirect(next) if next else HttpResponse(
            status=204)
        if request.method == 'POST':
            lang_code = request.POST.get(LANGUAGE_PARAMETER)
            if lang_code and check_for_language(lang_code):
                if next:
                    next_trans = translate_url(next, lang_code)
                    if next_trans != next:
                        response = HttpResponseRedirect(next_trans)
                if request.user.is_authenticated:
                    enrollment = self.user_course_data
                    if enrollment:
                        enrollment.language = lang_code
                        enrollment.save()
                    else:
                        userprofile = request.user.userprofile
                        userprofile.language = lang_code
                        userprofile.save()
                else:
                    if hasattr(request, 'session'):
                        request.session[LANGUAGE_SESSION_KEY] = lang_code
                    response.set_cookie(
                        settings.LANGUAGE_COOKIE_NAME,
                        lang_code,
                        max_age=settings.LANGUAGE_COOKIE_AGE,
                        path=settings.LANGUAGE_COOKIE_PATH,
                        domain=settings.LANGUAGE_COOKIE_DOMAIN,
                    )
                request.REQUEST_LANG = lang_code
        return response
コード例 #4
0
ファイル: base.py プロジェクト: etanttila/a-plus
def localized_url(path, language=None):
    base_url = settings.BASE_URL
    if base_url.endswith('/'):
        base_url = base_url[:-1]
    path = remove_query_param_from_url(path, 'hl')
    if not language:
        language = settings.LANGUAGE_CODE.split('-')[0]
    path = update_url_params(path, {'hl': language})
    return base_url + path
コード例 #5
0
 def process_request(self, request):
     query_language = request.GET.get('hl')
     if query_language:
         language = query_language
         lang_codes = set(lang[0][:2] for lang in settings.LANGUAGES)
         if language[:2] not in lang_codes:
             url = remove_query_param_from_url(request.get_full_path(), 'hl')
             return render(request, '404.html', {'url_without_language': url}, status=404)
     elif hasattr(request, 'user') and request.user.is_authenticated and not request.user.is_anonymous:
         language = request.user.userprofile.language
     else:
         language = translation.get_language_from_request(request)
     language = language[:2]
     translation.activate(language)
     request.LANGUAGE_CODE = translation.get_language()