From 83677a5d70ea03a06db1c4c3ad781e22327408d4 Mon Sep 17 00:00:00 2001 From: Hussain Nagaria Date: Sat, 21 Dec 2024 12:50:35 +0530 Subject: [PATCH] feat: redirect short links! --- linklite/hooks.py | 3 + linklite/linklite/doctype/__init__.py | 0 .../linklite/doctype/short_link/__init__.py | 0 .../linklite/doctype/short_link/short_link.js | 8 +++ .../doctype/short_link/short_link.json | 66 +++++++++++++++++++ .../linklite/doctype/short_link/short_link.py | 9 +++ .../doctype/short_link/test_short_link.py | 30 +++++++++ linklite/utils.py | 14 ++++ 8 files changed, 130 insertions(+) create mode 100644 linklite/linklite/doctype/__init__.py create mode 100644 linklite/linklite/doctype/short_link/__init__.py create mode 100644 linklite/linklite/doctype/short_link/short_link.js create mode 100644 linklite/linklite/doctype/short_link/short_link.json create mode 100644 linklite/linklite/doctype/short_link/short_link.py create mode 100644 linklite/linklite/doctype/short_link/test_short_link.py create mode 100644 linklite/utils.py diff --git a/linklite/hooks.py b/linklite/hooks.py index 3ae92db..738f255 100644 --- a/linklite/hooks.py +++ b/linklite/hooks.py @@ -5,6 +5,9 @@ app_email = "buildwithhussain@gmail.com" app_license = "agpl-3.0" + +website_path_resolver = "linklite.utils.path_resolver" + # Apps # ------------------ diff --git a/linklite/linklite/doctype/__init__.py b/linklite/linklite/doctype/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/linklite/linklite/doctype/short_link/__init__.py b/linklite/linklite/doctype/short_link/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/linklite/linklite/doctype/short_link/short_link.js b/linklite/linklite/doctype/short_link/short_link.js new file mode 100644 index 0000000..c283c6c --- /dev/null +++ b/linklite/linklite/doctype/short_link/short_link.js @@ -0,0 +1,8 @@ +// Copyright (c) 2024, Build With Hussain and contributors +// For license information, please see license.txt + +// frappe.ui.form.on("Short Link", { +// refresh(frm) { + +// }, +// }); diff --git a/linklite/linklite/doctype/short_link/short_link.json b/linklite/linklite/doctype/short_link/short_link.json new file mode 100644 index 0000000..eff2604 --- /dev/null +++ b/linklite/linklite/doctype/short_link/short_link.json @@ -0,0 +1,66 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "field:short_link", + "creation": "2024-12-21 12:43:49.300176", + "doctype": "DocType", + "engine": "InnoDB", + "field_order": [ + "destination_url", + "column_break_awjo", + "short_link", + "description" + ], + "fields": [ + { + "fieldname": "destination_url", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Destination URL", + "options": "URL", + "reqd": 1 + }, + { + "fieldname": "column_break_awjo", + "fieldtype": "Column Break" + }, + { + "fieldname": "short_link", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Short Link", + "reqd": 1, + "unique": 1 + }, + { + "fieldname": "description", + "fieldtype": "Small Text", + "label": "Description" + } + ], + "index_web_pages_for_search": 1, + "links": [], + "modified": "2024-12-21 12:45:50.795245", + "modified_by": "Administrator", + "module": "LinkLite", + "name": "Short Link", + "naming_rule": "By fieldname", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "sort_field": "creation", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/linklite/linklite/doctype/short_link/short_link.py b/linklite/linklite/doctype/short_link/short_link.py new file mode 100644 index 0000000..e6f2b88 --- /dev/null +++ b/linklite/linklite/doctype/short_link/short_link.py @@ -0,0 +1,9 @@ +# Copyright (c) 2024, Build With Hussain and contributors +# For license information, please see license.txt + +# import frappe +from frappe.model.document import Document + + +class ShortLink(Document): + pass diff --git a/linklite/linklite/doctype/short_link/test_short_link.py b/linklite/linklite/doctype/short_link/test_short_link.py new file mode 100644 index 0000000..28c66db --- /dev/null +++ b/linklite/linklite/doctype/short_link/test_short_link.py @@ -0,0 +1,30 @@ +# Copyright (c) 2024, Build With Hussain and Contributors +# See license.txt + +# import frappe +from frappe.tests import IntegrationTestCase, UnitTestCase + + +# On IntegrationTestCase, the doctype test records and all +# link-field test record depdendencies are recursively loaded +# Use these module variables to add/remove to/from that list +EXTRA_TEST_RECORD_DEPENDENCIES = [] # eg. ["User"] +IGNORE_TEST_RECORD_DEPENDENCIES = [] # eg. ["User"] + + +class UnitTestShortLink(UnitTestCase): + """ + Unit tests for ShortLink. + Use this class for testing individual functions and methods. + """ + + pass + + +class IntegrationTestShortLink(IntegrationTestCase): + """ + Integration tests for ShortLink. + Use this class for testing interactions between multiple components. + """ + + pass diff --git a/linklite/utils.py b/linklite/utils.py new file mode 100644 index 0000000..0605156 --- /dev/null +++ b/linklite/utils.py @@ -0,0 +1,14 @@ +import frappe + +from frappe.website.path_resolver import resolve_path as original_resolve_path + +def path_resolver(path: str): + # if we want to handle the short link + if frappe.db.exists("Short Link", {"short_link": path}): + # we want to redirect + destination = frappe.db.get_value("Short Link", {"short_link": path}, "destination_url") + frappe.redirect(destination) + + + # else pass it on! + return original_resolve_path(path)