Beispiel #1
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['DISCOURSE_URL'] = settings.DISCOURSE_URL.rstrip('/')
        context['rating_form'] = AppRatingForm(
            initial={'language_code': get_language()})

        ratings = AppRating.objects.filter(app=context['app'])
        rating_languages = flatmap(
            lambda r: r.get_available_languages(), ratings)

        # make sure current session language is in the list even if there are
        # no comments.
        rating_languages = list(rating_languages)
        if get_language() not in rating_languages:
            rating_languages.append(get_language())

        context['languages'] = set(sorted(rating_languages))
        context['fallbackLang'] = 'en' if 'en' in context['languages'] else ''
        context['user_has_rated_app'] = False
        if self.request.user.is_authenticated:
            try:
                app_rating = AppRating.objects.get(user=self.request.user,
                                                   app=context['app'])

                # if parler fallsback to a fallback language
                # it doesn't set the language as current language
                # and we can't select the correct language in the
                # frontend. So we try and find a languge that is
                # available
                language_code = app_rating.get_current_language()
                if not app_rating.has_translation(language_code):
                    for fallback in app_rating.get_fallback_languages():
                        if app_rating.has_translation(fallback):
                            app_rating.set_current_language(fallback)

                # when accessing an empty comment django-parler tries to
                # fall back to the default language. However for comments
                # the default (English) does not always exist. Unfortunately
                # it throws the same exception as non existing models,
                # so we need to access it beforehand
                try:
                    comment = app_rating.comment
                except AppRating.DoesNotExist:
                    comment = ''

                context['rating_form'] = AppRatingForm({
                    'rating': app_rating.rating,
                    'comment': comment,
                    'language_code': app_rating.get_current_language(),
                })
                context['user_has_rated_app'] = True
            except AppRating.DoesNotExist:
                pass
        context['categories'] = Category.objects.prefetch_related(
            'translations').all()
        context['latest_releases_by_platform_v'] = \
            self.object.latest_releases_by_platform_v()
        return context
Beispiel #2
0
 def post(self, request, id):
     form = AppRatingForm(request.POST, id=id, user=request.user)
     # there is no way that a rating can be invalid by default
     if form.is_valid() and request.user.is_authenticated:
         form.save()
     return redirect('app-detail', id=id)
Beispiel #3
0
 def post(self, request, id):
     form = AppRatingForm(request.POST, id=id, user=request.user)
     # there is no way that a rating can be invalid by default
     if form.is_valid() and request.user.is_authenticated:
         form.save()
     return redirect('app-detail', id=id)