Skip to content

Conversation

@millerthegorilla
Copy link
Contributor

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.

add get_or_create method
properly type exception
Added url expiry check to get_or_create method in line with closed issue ronaldgrn#2
reviewed and simplified.
lazily editing in github.com's editor.  changing exception handling to prevent null return in the case that a urlmap has been deleted by the admin panel
@ronaldgrn
Copy link
Owner

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.


def get_or_create(user, link, refresh=False):
try:
m = UrlMap.objects.get(full_url=link);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@millerthegorilla
Copy link
Contributor Author

millerthegorilla commented Jul 8, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants