def frontend_test_view(request): context = get_default_context(request) discussion = context["discussion"] target_locale = Locale.get_or_create('en', discussion.db) locale_labels = json.dumps( DummyGoogleTranslationService.target_locale_labels_cls(target_locale)) context['translation_locale_names_json'] = locale_labels context['translation_service_data_json'] = '{}' context['preferences_json'] = json.dumps(dict(discussion.preferences)) return context
def resolve_locales(self, args, context, info): locales = DummyGoogleTranslationService.target_localesC() asPosixLocale = DummyGoogleTranslationService.asPosixLocale target_locale = strip_country( models.Locale.get_or_create(args.get('lang'))) labels = models.LocaleLabel.names_of_locales_in_locale( [strip_country(asPosixLocale(loc)) for loc in locales], target_locale) return [ Locale(locale_code=locale_code, label=label) for locale_code, label in sorted(labels.items(), key=lambda entry: entry[1]) ]
def frontend_test_view(request): context = get_default_context(request) discussion = context["discussion"] target_locale = 'en' locale_labels = json.dumps( DummyGoogleTranslationService.target_locale_labels_cls(target_locale)) context['translation_locale_names_json'] = locale_labels context['translation_service_data_json'] = '{}' context['preferences_json'] = json.dumps(dict(discussion.preferences)) static_url = context['STATIC_URL'] context['js_links'] = get_js_links(static_url, True) context['css_links'] = get_css_links(static_url, True) return context
def base_admin_view(request): """The Base admin view, for frontend urls""" user_id = authenticated_userid(request) or Everyone if user_id == Everyone: raise HTTPUnauthorized() context = get_default_context(request) session = Discussion.default_db preferences = Preferences.get_default_preferences(session) user = User.get(user_id) if '_LOCALE_' in request.cookies: locale = request.cookies['_LOCALE_'] process_locale(locale, user, session, LanguagePreferenceOrder.Cookie) elif '_LOCALE_' in request.params: locale = request.params['_LOCALE_'] process_locale(locale, user, session, LanguagePreferenceOrder.Parameter) else: locale = request.locale_name process_locale(locale, user, session, LanguagePreferenceOrder.OS_Default) target_locale = strip_country(locale) locale_labels = json.dumps( DummyGoogleTranslationService.target_locale_labels_cls(target_locale)) context['translation_locale_names_json'] = locale_labels role_names = [x for (x, ) in session.query(Role.name).all()] permission_names = [x for (x, ) in session.query(Permission.name).all()] context['role_names'] = json.dumps(role_names) context['permission_names'] = json.dumps(permission_names) context['discussion'] = PseudoDiscussion() response = render_to_response('../../templates/adminIndex.jinja2', context, request=request) # Prevent caching the home, especially for proper login/logout response.cache_control.max_age = 0 response.cache_control.prevent_auto = True return response
def resolve_locales(self, args, context, info): locales = DummyGoogleTranslationService.target_localesC() asPosixLocale = DummyGoogleTranslationService.asPosixLocale lang = args.get('lang') target_locale = strip_country(models.Locale.get_or_create(lang)) locales_without_country = [ strip_country(asPosixLocale(loc)) for loc in locales ] labels_en = models.LocaleLabel.names_of_locales_in_locale( locales_without_country, models.Locale.get_or_create('en')) if lang == 'en': labels = labels_en else: # labels can be a subset of labels_en or completely empty # if there is no translation of the locales for target_locale labels = models.LocaleLabel.names_of_locales_in_locale( locales_without_country, target_locale) result = [] for locale_code, label_en in labels_en.items(): label = labels.get(locale_code, label_en) result.append(Locale(locale_code=locale_code, label=label)) return sorted(result, key=lambda locale: locale.label)
def resolve_locales(self, args, context, info): locales = DummyGoogleTranslationService.target_localesC() asPosixLocale = DummyGoogleTranslationService.asPosixLocale lang = args.get('lang') target_locale = strip_country( models.Locale.get_or_create(lang)) locales_without_country = [strip_country(asPosixLocale(loc)) for loc in locales] labels_en = models.LocaleLabel.names_of_locales_in_locale( locales_without_country, models.Locale.get_or_create('en')) if lang == 'en': labels = labels_en else: # labels can be a subset of labels_en or completely empty # if there is no translation of the locales for target_locale labels = models.LocaleLabel.names_of_locales_in_locale( locales_without_country, target_locale) result = [] for locale_code, label_en in labels_en.items(): label = labels.get(locale_code, label_en) result.append(Locale(locale_code=locale_code, label=label)) return sorted(result, key=lambda locale: locale.label)
def home_view(request): """The main view on a discussion""" user_id = authenticated_userid(request) or Everyone context = get_default_context(request) discussion = context["discussion"] canRead = user_has_permission(discussion.id, user_id, P_READ) if not canRead and user_id == Everyone: # User isn't logged-in and discussion isn't public: # redirect to login page # need to pass the route to go to *after* login as well # With regards to a next_view, if explicitly stated, then # that is the next view. If not stated, the referer takes # precedence. In case of failure, login redirects to the # discussion which is its context. next_view = request.params.get('next', None) if not next_view and discussion: # If referred here from a post url, want to be able to # send the user back. Usually, Assembl will send the user # here to login on private discussions. referrer = request.url next_view = path_qs(referrer) if discussion.preferences['authorization_server_backend']: login_url = request.route_url( "contextual_social_auth", discussion_slug=discussion.slug, backend=discussion.preferences['authorization_server_backend'], _query={"next": next_view}) elif next_view: login_url = request.route_url("contextual_login", discussion_slug=discussion.slug, _query={"next": next_view}) else: login_url = request.route_url('contextual_login', discussion_slug=discussion.slug) return HTTPTemporaryRedirect(login_url) elif not canRead: # User is logged-in but doesn't have access to the discussion # Would use render_to_response, except for the 401 from pyramid_jinja2 import IJinja2Environment jinja_env = request.registry.queryUtility(IJinja2Environment, name='.jinja2') template = jinja_env.get_template('cannot_read_discussion.jinja2') body = template.render(get_default_context(request)) return Response(body, 401) # if the route asks for a post, get post content (because this is needed for meta tags) route_name = request.matched_route.name if route_name == "purl_posts": post_id = FrontendUrls.getRequestedPostId(request) if not post_id: return HTTPSeeOther( request.route_url('home', discussion_slug=discussion.slug)) post = Post.get_instance(post_id) if not post or post.discussion_id != discussion.id: return HTTPSeeOther( request.route_url('home', discussion_slug=discussion.slug)) context['post'] = post elif route_name == "purl_idea": idea_id = FrontendUrls.getRequestedIdeaId(request) if not idea_id: return HTTPSeeOther( request.route_url('home', discussion_slug=discussion.slug)) idea = Idea.get_instance(idea_id) if not idea or idea.discussion_id != discussion.id: return HTTPSeeOther( request.route_url('home', discussion_slug=discussion.slug)) context['idea'] = idea canAddExtract = user_has_permission(discussion.id, user_id, P_ADD_EXTRACT) context['canAddExtract'] = canAddExtract context['canDisplayTabs'] = True preferences = discussion.preferences if user_id != Everyone: from assembl.models import UserPreferenceCollection user = User.get(user_id) preferences = UserPreferenceCollection(user_id, discussion) # TODO: user may not exist. Case of session with BD change. user.is_visiting_discussion(discussion.id) session = Discussion.default_db if '_LOCALE_' in request.cookies: locale = request.cookies['_LOCALE_'] process_locale(locale, user, session, LanguagePreferenceOrder.Cookie) elif '_LOCALE_' in request.params: locale = request.params['_LOCALE_'] process_locale(locale, user, session, LanguagePreferenceOrder.Parameter) else: locale = locale_negotiator(request) process_locale(locale, user, session, LanguagePreferenceOrder.OS_Default) else: locale = request.localizer.locale_name target_locale = Locale.get_or_create(strip_country(locale), discussion.db) translation_service_data = {} try: service = discussion.translation_service() if service: translation_service_data = service.serviceData() except: pass context['translation_service_data_json'] = json.dumps( translation_service_data) locale_labels = json.dumps( DummyGoogleTranslationService.target_locale_labels_cls(target_locale)) context['translation_locale_names_json'] = locale_labels context['preferences_json'] = json.dumps(dict(preferences)) response = render_to_response('../../templates/index.jinja2', context, request=request) # Prevent caching the home, especially for proper login/logout response.cache_control.max_age = 0 response.cache_control.prevent_auto = True return response
def home_view(request): user_id = authenticated_userid(request) or Everyone context = get_default_context(request) discussion = context["discussion"] request.session["discussion"] = discussion.slug canRead = user_has_permission(discussion.id, user_id, P_READ) if not canRead and user_id == Everyone: # User isn't logged-in and discussion isn't public: # redirect to login page # need to pass the route to go to *after* login as well # With regards to a next_view, if explicitly stated, then # that is the next view. If not stated, the referer takes # precedence. In case of failure, login redirects to the # discussion which is its context. next_view = request.params.get('next_view', None) if not next_view and discussion: # If referred here from a post url, want to be able to # send the user back. Usually, Assembl will send the user # here to login on private discussions. referrer = request.url next_view = path_qs(referrer) if next_view: login_url = request.route_url("contextual_login", discussion_slug=discussion.slug, _query={"next_view": next_view}) else: login_url = request.route_url( 'contextual_login', discussion_slug=discussion.slug) return HTTPSeeOther(login_url) elif not canRead: # User is logged-in but doesn't have access to the discussion # Would use render_to_response, except for the 401 from pyramid_jinja2 import IJinja2Environment jinja_env = request.registry.queryUtility( IJinja2Environment, name='.jinja2') template = jinja_env.get_template('cannot_read_discussion.jinja2') body = template.render(get_default_context(request)) return Response(body, 401) # if the route asks for a post, get post content (because this is needed for meta tags) route_name = request.matched_route.name if route_name == "purl_posts": post_id = FrontendUrls.getRequestedPostId(request) if not post_id: return HTTPSeeOther(request.route_url( 'home', discussion_slug=discussion.slug)) post = Post.get_instance(post_id) if not post or post.discussion_id != discussion.id: return HTTPSeeOther(request.route_url( 'home', discussion_slug=discussion.slug)) context['post'] = post elif route_name == "purl_idea": idea_id = FrontendUrls.getRequestedIdeaId(request) if not idea_id: return HTTPSeeOther(request.route_url( 'home', discussion_slug=discussion.slug)) idea = Idea.get_instance(idea_id) if not idea or idea.discussion_id != discussion.id: return HTTPSeeOther(request.route_url( 'home', discussion_slug=discussion.slug)) context['idea'] = idea canAddExtract = user_has_permission(discussion.id, user_id, P_ADD_EXTRACT) context['canAddExtract'] = canAddExtract context['canDisplayTabs'] = True preferences = discussion.preferences if user_id != Everyone: from assembl.models import UserPreferenceCollection user = User.get(user_id) preferences = UserPreferenceCollection(user_id, discussion) # TODO: user may not exist. Case of session with BD change. user.is_visiting_discussion(discussion.id) session = Discussion.default_db if '_LOCALE_' in request.cookies: locale = request.cookies['_LOCALE_'] process_locale(locale, user, session, LanguagePreferenceOrder.Cookie) elif '_LOCALE_' in request.params: locale = request.params['_LOCALE_'] process_locale(locale, user, session, LanguagePreferenceOrder.Parameter) else: locale = locale_negotiator(request) process_locale(locale, user, session, LanguagePreferenceOrder.OS_Default) else: locale = request.localizer.locale_name target_locale = Locale.get_or_create( strip_country(locale), discussion.db) translation_service_data = {} try: service = discussion.translation_service() if service: translation_service_data = service.serviceData() except: pass context['translation_service_data_json'] = json.dumps( translation_service_data) locale_labels = json.dumps( DummyGoogleTranslationService.target_locale_labels_cls(target_locale)) context['translation_locale_names_json'] = locale_labels context['preferences_json'] = json.dumps(dict(preferences)) response = render_to_response('../../templates/index.jinja2', context, request=request) # Prevent caching the home, especially for proper login/logout response.cache_control.max_age = 0 response.cache_control.prevent_auto = True return response