def init_search(endpoint, values): q = request.args.get("q") try: page = max(int(request.args.get('page', 1), 1)) except: page = 1 g.breadcrumb.append( BreadcrumbItem(label='"{}"'.format(q), icon="search", url=Endpoint('search.search_main', q=q))) page_kw = OrderedDict(q=q) object_types = request.args.getlist('object_type') if object_types: page_kw['object_type'] = object_types g.breadcrumb.append( BreadcrumbItem(label=' | '.join( friendly_fqcn(name) for name in object_types), url=Endpoint('search.search_main', **page_kw))) if page > 1: g.breadcrumb.append( BreadcrumbItem(label=text_type(page), url=Endpoint('search.search_main', page=page, **page_kw))) values['q'] = q values['page'] = page
def init_search(endpoint, values): q = request.args.get("q", "") try: page = max(int(request.args.get("page", 1)), 1) except Exception: page = 1 g.breadcrumb.append( BreadcrumbItem(label=f'"{q}"', icon="search", url=Endpoint("search.search_main", q=q))) page_kw = OrderedDict(q=q) object_types = request.args.getlist("object_type") if object_types: page_kw["object_type"] = object_types g.breadcrumb.append( BreadcrumbItem( label=" | ".join(friendly_fqcn(name) for name in object_types), url=Endpoint("search.search_main", **page_kw), )) if page > 1: g.breadcrumb.append( BreadcrumbItem( label=str(page), url=Endpoint("search.search_main", page=page, **page_kw), )) values["q"] = q values["page"] = page
def url_value_preprocess(self, endpoint, view_args): model_name = view_args.pop("Model", None) group = view_args.pop("group", _MARKER) if group == "_": # "General" group group = "" if group is not _MARKER: view_args["group"] = group if model_name is not None: svc = get_service("vocabularies") Model = svc.get_vocabulary(name=model_name, group=group) g.breadcrumb.append( BreadcrumbItem( label=Model.Meta.group if group else _("Global"), url=url_for(".vocabularies_group", group=group or "_"), )) g.breadcrumb.append( BreadcrumbItem( label=Model.Meta.label, url=url_for(".vocabularies_model", group=group or "_", Model=Model.Meta.name), )) view_args["Model"] = Model
def url_value_preprocess(self, endpoint, view_args): Model = view_args.pop('Model', None) group = view_args.pop('group', _MARKER) if group == u'_': # "General" group group = None if group is not _MARKER: view_args['group'] = group if Model is not None: svc = self.svc Model = svc.get_vocabulary(name=Model, group=group) g.breadcrumb.append( BreadcrumbItem( label=Model.Meta.group if group else _('Global'), url=url_for('.vocabularies_group', group=group or u'_'), )) g.breadcrumb.append( BreadcrumbItem( label=Model.Meta.label, url=url_for('.vocabularies_model', group=group or u'_', Model=Model.Meta.name), )) view_args['Model'] = Model
def init_wiki_values(endpoint, values): g.current_tab = "wiki" endpoint = Endpoint("wiki.index", community_id=g.community.slug) g.breadcrumb.append(BreadcrumbItem(label=_l("Wiki"), url=endpoint)) title = request.args.get("title", "").strip() if title and title != "Home": url = Endpoint("wiki.page", community_id=g.community.slug, title=title) g.breadcrumb.append(BreadcrumbItem(label=title, url=url))
def init_wiki_values(endpoint, values): g.current_tab = 'wiki' endpoint = Endpoint('wiki.index', community_id=g.community.slug) g.breadcrumb.append(BreadcrumbItem(label=_l(u'Wiki'), url=endpoint)) title = request.args.get('title', u'').strip() if title and title != 'Home': g.breadcrumb.append( BreadcrumbItem(label=title, url=Endpoint('wiki.page', community_id=g.community.slug, title=title)))
def register_panel(self, panel, app=None): state = self.app_state if app is None else app.extensions[self.name] if state.blueprint_registered: raise ValueError( "Extension already initialized for app, " "cannot add more panel", ) state.panels.append(panel) panel.preferences = self rule = "/" + getattr(panel, 'path', panel.id) endpoint = panel.id abs_endpoint = 'preferences.{}'.format(endpoint) if hasattr(panel, 'get'): state.blueprint.add_url_rule(rule, endpoint, panel.get) if hasattr(panel, 'post'): endpoint += "_post" state.blueprint.add_url_rule( rule, endpoint, panel.post, methods=['POST'], ) state.breadcrumb_items[abs_endpoint] = BreadcrumbItem( label=panel.label, icon=None, url=Endpoint(abs_endpoint), )
def init_forum_values(endpoint, values): g.current_tab = 'forum' g.breadcrumb.append( BreadcrumbItem(label=_l(u'Conversations'), url=Endpoint('forum.index', community_id=g.community.slug)))
def wizard_new_accounts(): """Complete new emails information.""" if request.method == "GET": return redirect(url_for(".members", community_id=g.community.slug)) g.breadcrumb.append( BreadcrumbItem( label=_("Members"), url=Endpoint("communities.members", community_id=g.community.slug), ) ) wizard_emails = request.form.get("wizard-emails") wizard_accounts = json.loads(wizard_emails) wizard_existing_account = {} new_accounts = [] for user in wizard_accounts: if user["status"] == "existing": wizard_existing_account[user["email"]] = user["role"] elif user["status"] == "new": new_accounts.append(user) existing_account = json.dumps(wizard_existing_account) return render_template( "community/wizard_new_accounts.html", existing_account=existing_account, new_accounts=new_accounts, )
def init_document_values(endpoint, values): g.current_tab = 'documents' g.breadcrumb.append( BreadcrumbItem(label=_l(u'Documents'), url=Endpoint('documents.index', community_id=g.community.slug)))
def init_breadcrumbs(self): """Insert the first element in breadcrumbs. This happens during `request_started` event, which is triggered before any url_value_preprocessor and `before_request` handlers. """ g.breadcrumb.append( BreadcrumbItem(icon='home', url='/' + request.script_root))
def init_forum_values(endpoint, values): g.current_tab = "forum" g.breadcrumb.append( BreadcrumbItem( label=_l("Conversations"), url=Endpoint("forum.index", community_id=g.community.slug), ))
def init_document_values(endpoint, values): g.current_tab = "documents" g.is_manager = is_manager() g.breadcrumb.append( BreadcrumbItem( label=_l("Documents"), url=Endpoint("documents.index", community_id=g.community.slug), ))
def pull_community(endpoint, values): """url_value_preprocessor function.""" g.nav["active"] = "section:communities" g.breadcrumb.append( BreadcrumbItem(label=_l("Communities"), url=Endpoint("communities.index")) ) try: slug = values.pop("community_id") community = Community.query.filter(Community.slug == slug).first() if community: g.community = CommunityPresenter(community) wall_url = Endpoint("wall.index", community_id=community.slug) breadcrumb_item = BreadcrumbItem(label=community.name, url=wall_url) g.breadcrumb.append(breadcrumb_item) else: raise NotFound() except KeyError: pass
def wizard_data_insertion(): """Insert new members data into the community via emails or CSV file.""" g.breadcrumb.append( BreadcrumbItem( label=_("Members"), url=Endpoint("communities.members", community_id=g.community.slug), ) ) return render_template("community/wizard_add_emails.html")
def members(): g.breadcrumb.append( BreadcrumbItem(label=_(u'Members'), url=Endpoint('communities.members', community_id=g.community.slug))) memberships = _members_query().all() return render_template("community/members.html", seconds_since_epoch=seconds_since_epoch, memberships=memberships, csrf_token=csrf.field())
def setup_blueprint(self, app): bp = self.app_state.blueprint = Blueprint( "preferences", __name__, template_folder='templates', url_prefix="/preferences", ) # we need to delay blueprint registration to allow adding more panels during # initialization @signals.components_registered.connect_via(app) def register_bp(app): app.register_blueprint(bp) app.extensions[self.name].blueprint_registered = True self.app_state.root_breadcrumb_item = BreadcrumbItem( label=_('Preferences'), url=Endpoint('preferences.index'), ) bp.url_value_preprocessor(self.build_breadcrumbs) @bp.context_processor def inject_menu(): menu = [] for panel in self.app_state.panels: if not panel.is_accessible(): continue endpoint = 'preferences.' + panel.id active = endpoint == request.endpoint entry = { 'endpoint': endpoint, 'label': panel.label, 'url': url_for(endpoint), 'active': active, } menu.append(entry) return dict(menu=menu) @bp.route("/") def index(): """Index redirects to the first accessible panel.""" # Work around unit test failure. FIXME. if current_user.is_anonymous: return "OK" for panel in self.app_state.panels: if panel.is_accessible(): return redirect(url_for("preferences." + panel.id)) else: # Should not happen. raise InternalServerError()
def setup_blueprint(self, app: Flask) -> None: bp = self.app_state.blueprint = Blueprint( "preferences", __name__, template_folder="templates", url_prefix="/preferences", ) # we need to delay blueprint registration to allow adding more panels during # initialization @signals.components_registered.connect_via(app) def register_bp(app: Flask) -> None: app.register_blueprint(bp) app.extensions[self.name].blueprint_registered = True self.app_state.root_breadcrumb_item = BreadcrumbItem( label=_("Preferences"), url=Endpoint("preferences.index") ) bp.url_value_preprocessor(self.build_breadcrumbs) @bp.context_processor def inject_menu() -> Dict[str, List[Dict[str, Any]]]: menu = [] for panel in self.app_state.panels: if not panel.is_accessible(): continue endpoint = "preferences." + panel.id active = endpoint == request.endpoint entry = { "endpoint": endpoint, "label": panel.label, "url": url_for(endpoint), "active": active, } menu.append(entry) return {"menu": menu} @bp.route("/") def index(): """Index redirects to the first accessible panel.""" # Work around unit test failure. FIXME. if current_user.is_anonymous: return "OK" for panel in self.app_state.panels: if panel.is_accessible(): return redirect(url_for("preferences." + panel.id)) # Should not happen. raise InternalServerError()
def wizard_check_data(): """Filter and detect existing members, existing accounts and new emails.""" if request.method == "GET": return redirect(url_for(".members", community_id=g.community.slug)) g.breadcrumb.append( BreadcrumbItem( label=_("Members"), url=Endpoint("communities.members", community_id=g.community.slug), ) ) is_csv = False if request.form.get("wizard-emails"): wizard_emails = request.form.get("wizard-emails").split(",") existing_accounts_object, existing_members_objects, final_email_list = wizard_extract_data( wizard_emails ) final_email_list_json = json.dumps(final_email_list) else: is_csv = True accounts_data = wizard_read_csv(request.files["csv_file"]) if not accounts_data: flash(_("To add new members, please follow the CSV file model."), "warning") return redirect( url_for(".wizard_data_insertion", community_id=g.community.slug) ) existing_accounts, existing_members_objects, final_email_list = wizard_extract_data( accounts_data, is_csv=True ) existing_accounts_object = existing_accounts["account_objects"] existing_accounts_csv_roles = existing_accounts["csv_roles"] final_email_list_json = json.dumps(final_email_list) if not final_email_list: flash(_("No new members were found"), "warning") return redirect( url_for(".wizard_data_insertion", community_id=g.community.slug) ) ctx = { "existing_accounts_object": existing_accounts_object, "csv_roles": existing_accounts_csv_roles if is_csv else False, "wizard_emails": final_email_list_json, "existing_members_objects": existing_members_objects, } return render_template("community/wizard_check_members.html", **ctx)
def members(): g.breadcrumb.append( BreadcrumbItem( label=_("Members"), url=Endpoint("communities.members", community_id=g.community.slug), ) ) memberships = _members_query().all() community_threads_users = [thread.creator for thread in g.community.threads] threads_count = Counter(community_threads_users) ctx = { "seconds_since_epoch": seconds_since_epoch, "is_manager": is_manager(user=current_user), "memberships": memberships, "threads_count": threads_count, } return render_template("community/members.html", **ctx)
def init_app(self, app: Flask) -> None: panels = app.config.get("ADMIN_PANELS", ()) # resolve fully qualified name into an AdminPanel object for fqn in panels: panel_class = import_string(fqn, silent=True) if panel_class is None: logger.warning('Could not import panel: "%s"', fqn) continue if not issubclass(panel_class, AdminPanel): logger.error( '"%s" is not a %s.AdminPanel, skipping', fqn, AdminPanel.__module__ ) continue self.register_panel(panel_class()) logger.debug('Registered panel "%s"', fqn) if not self.panels: @self.blueprint.route("", endpoint="no_panel") def no_panels_view(): return "No panels registered" self.nav_root.endpoint = "admin.no_panel" else: self.nav_root.endpoint = self.nav_root.items[0].endpoint self.root_breadcrumb_item = BreadcrumbItem( label=self.nav_root.title, url=self.nav_root.endpoint ) app.register_blueprint(self.blueprint) with app.app_context(): actions.register(self.nav_root, *self.nav_root.items) self.app = app app.extensions["admin"] = self
def register_panel(self, panel: Any) -> None: if self.app: raise ValueError( "Extension already initialized for app, cannot add more" " panel" ) self.panels.append(panel) panel.admin = self rule = "/" + panel.id endpoint = nav_id = panel.id abs_endpoint = f"admin.{endpoint}" if hasattr(panel, "get"): self.blueprint.add_url_rule(rule, endpoint, panel.get) self._panels_endpoints[abs_endpoint] = panel if hasattr(panel, "post"): post_endpoint = endpoint + "_post" self.blueprint.add_url_rule( rule, post_endpoint, panel.post, methods=["POST"] ) self._panels_endpoints["admin." + post_endpoint] = panel panel.install_additional_rules( self.get_panel_url_rule_adder(panel, rule, endpoint) ) nav = NavItem( "admin:panel", nav_id, title=panel.label, icon=panel.icon, endpoint=abs_endpoint, ) self.nav_root.append(nav) self.nav_paths[abs_endpoint] = nav.path self.breadcrumb_items[panel] = BreadcrumbItem( label=panel.label, icon=panel.icon, url=Endpoint(abs_endpoint) )
def register_panel( self, panel: PreferencePanel, app: Optional[Flask] = None ) -> None: state = self.app_state if app is None else app.extensions[self.name] if state.blueprint_registered: raise ValueError( "Extension already initialized for app, " "cannot add more panel" ) state.panels.append(panel) panel.preferences = self rule = "/" + getattr(panel, "path", panel.id) endpoint = panel.id abs_endpoint = f"preferences.{endpoint}" if hasattr(panel, "get"): state.blueprint.add_url_rule(rule, endpoint, panel.get) if hasattr(panel, "post"): endpoint += "_post" state.blueprint.add_url_rule(rule, endpoint, panel.post, methods=["POST"]) state.breadcrumb_items[abs_endpoint] = BreadcrumbItem( label=panel.label, icon=None, url=Endpoint(abs_endpoint) )
def register_panel(self, panel): if self.app: raise ValueError( 'Extension already initialized for app, cannot add more' ' panel') self.panels.append(panel) panel.admin = self rule = "/" + panel.id endpoint = nav_id = panel.id abs_endpoint = 'admin.{}'.format(endpoint) if hasattr(panel, 'get'): self.blueprint.add_url_rule(rule, endpoint, panel.get) self._panels_endpoints[abs_endpoint] = panel if hasattr(panel, 'post'): post_endpoint = endpoint + "_post" self.blueprint.add_url_rule(rule, post_endpoint, panel.post, methods=['POST']) self._panels_endpoints['admin.' + post_endpoint] = panel panel.install_additional_rules( self.get_panel_url_rule_adder(panel, rule, endpoint)) nav = NavItem('admin:panel', nav_id, title=panel.label, icon=panel.icon, divider=False, endpoint=abs_endpoint) self.nav_root.append(nav) self.nav_paths[abs_endpoint] = nav.path self.breadcrumb_items[panel] = BreadcrumbItem( label=panel.label, icon=panel.icon, url=Endpoint(abs_endpoint))
def breadcrumb(self): label = render_template_string('<em>{{ g }}</em>', g=self.obj.name) return BreadcrumbItem(label=label, url='', description=self.obj.name)
def breadcrumb(self): label = render_template_string("<em>{{ u.email }}</em>", u=self.obj) return BreadcrumbItem(label=label, url="", description=self.obj.name)
def breadcrumb(self): return BreadcrumbItem(label=_(u'Settings'), icon='cog', url=Endpoint('communities.settings', community_id=g.community.slug))
def breadcrumb(self): return BreadcrumbItem( label=_("Settings"), icon="cog", url=Endpoint("communities.settings", community_id=g.community.slug), )
def breadcrumb(self): return BreadcrumbItem(label=_("Create new community"))