From 06f7698e8f441d5a84efe599a8b3436f7e5e523b Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Tue, 6 Apr 2021 17:52:23 +0530 Subject: [PATCH] refactor: added custom routing for profiles - installed a regex conveter to werkzeug to support regular expresions in routes - added a website route rule to match all profiles --- community/hooks.py | 25 +++++++++++++++++++++++-- community/routing.py | 25 +++++++++++++++++++++++++ community/www/profiles/profile.html | 25 +++++++++++++++++++++++++ community/www/profiles/profile.py | 18 ++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 community/routing.py create mode 100644 community/www/profiles/profile.html create mode 100644 community/www/profiles/profile.py diff --git a/community/hooks.py b/community/hooks.py index cb60397d..dff32fe2 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -91,7 +91,7 @@ app_include_js = "/assets/community/js/community.js" # Hook on document methods and events doc_events = { - "User": { + "User": { "after_insert": "community.community.doctype.community_member.community_member.create_member_from_user" } } @@ -127,8 +127,29 @@ scheduler_events = { # # auto_cancel_exempted_doctypes = ["Auto Repeat"] -website_route_rules = [ +from .routing import install_regex_converter +install_regex_converter() + +# Add all simple route rules here +primary_rules = [ {"from_route": "/sketches/", "to_route": "sketches/sketch"}, {"from_route": "/courses/", "to_route": "courses/course"}, {"from_route": "/courses//", "to_route": "courses/topic"}, + {"from_route": "/courses//", "to_route": "courses/topic"} ] + +# Any frappe default URL is blocked by profile-rules, add it here to unblock it +whitelist = [ + "/login", + "/update-password", + "/update-profile", + "/third-party-apps" +] +whitelist_rules = [{"from_route": p, "to_route": p[1:]} for p in whitelist] + +# regex rule to match all profiles +profile_rules = [ + {"from_route": "/", "to_route": "profiles/profile"}, +] + +website_route_rules = primary_rules + whitelist_rules + profile_rules diff --git a/community/routing.py b/community/routing.py new file mode 100644 index 00000000..26ed052b --- /dev/null +++ b/community/routing.py @@ -0,0 +1,25 @@ +"""Utilities for making custom routing. +""" + +from werkzeug.routing import BaseConverter, Map +from werkzeug.datastructures import ImmutableDict + +class RegexConverter(BaseConverter): + """werkzeug converter that supports custom regular expression. + + The `install_regex_converter` function must be called before using + regex converter in rules. + """ + def __init__(self, map, regex): + super().__init__(map) + self.regex = regex + +def install_regex_converter(): + """Installs the RegexConvetor to the default converters supported by werkzeug. + + This allows specifing rules using regex. For example: + + /profiles/ + """ + default_converters = dict(Map.default_converters, regex=RegexConverter) + Map.default_converters = ImmutableDict(default_converters) diff --git a/community/www/profiles/profile.html b/community/www/profiles/profile.html new file mode 100644 index 00000000..5a1887b0 --- /dev/null +++ b/community/www/profiles/profile.html @@ -0,0 +1,25 @@ +{% extends "templates/web.html" %} +{% block page_content %} +
+ {% if user.photo %} +
+ {{ user.full_name }} +
+ {% else %} +
+
{{ user.abbr }}
+
+ {% endif %} +
+

{{ user.full_name }}

+ {% if user.short_intro %} +

{{ user.short_intro }}

+ {% endif %} + {% if user.bio %} +

{{ frappe.utils.md_to_html(user.bio) }}

+ {% endif %} +
+
+{% endblock %} + + diff --git a/community/www/profiles/profile.py b/community/www/profiles/profile.py new file mode 100644 index 00000000..0f9f76e0 --- /dev/null +++ b/community/www/profiles/profile.py @@ -0,0 +1,18 @@ +import frappe + +def get_context(context): + context.no_cache = 1 + + username = frappe.form_dict.get('username') + user = username and get_user(username) + if not user: + context.template = "www/404.html" + + user.abbr = "".join([s[0] for s in user.full_name.split()]) + context.user = user + +def get_user(username): + try: + return frappe.get_doc("Community Member", username) + except frappe.DoesNotExistError: + return