django-link-shortener icon indicating copy to clipboard operation
django-link-shortener copied to clipboard

Patch 1 add get_or_create function to shortener.py

Open millerthegorilla opened this issue 5 months ago • 2 comments

This adds a get_or_create function to shortener.py so that repeated requests to shortener can return the same link if it already exists.

millerthegorilla avatar Jun 27 '25 20:06 millerthegorilla

Hey, I appreciate the contribution however the requirements for a get_or_create function would vary considerably from project to project and so it would be better if this was implemented in the project using the shortener.

Some examples of how requirements can vary:

  • We might want to do case insensitive matching of urls
  • We may want to add additional uses to an existing shortlink if it already exists, alternatively we may want to reset its lifespan
  • We may want to 'reuse' short urls created by other users or we might want to keep shortlinks isolated so a user can only 'get' their own.

ronaldgrn avatar Jul 08 '25 21:07 ronaldgrn

Ah thanks. In my situation its not important who creates the links, as long as they are created so I missed it. I will change it as you suggest, many thanks.

James Stewart Miller Bsc(hons) Psych.

On Tue, 8 Jul 2025, 22:36 Petronald Green, @.***> wrote:

@.**** commented on this pull request.

In shortener/shortener.py https://github.com/ronaldgrn/django-link-shortener/pull/25#discussion_r2193483301 :

@@ -17,7 +16,18 @@ def get_random(tries=0): dictionary = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz234567890" return ''.join(random.choice(dictionary) for _ in range(length))

+def get_or_create(user, link, refresh=False):

  • try:
  •    m = UrlMap.objects.get(full_url=link);
    

Hey, just a fyi in case you're using this in your project - This would also get links created by users other than the one passed to the function. You might want something like:

m = UrlMap.objects.get(user=user, full_url=link)

— Reply to this email directly, view it on GitHub https://github.com/ronaldgrn/django-link-shortener/pull/25#pullrequestreview-2999161450, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA4GSGNDTAZCYPXIRMOP7YL3HQ2XLAVCNFSM6AAAAACAJ4ZY32VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDSOJZGE3DCNBVGA . You are receiving this because you authored the thread.Message ID: @.***>

millerthegorilla avatar Jul 08 '25 22:07 millerthegorilla