-
Notifications
You must be signed in to change notification settings - Fork 17
Patch 1 add get_or_create function to shortener.py #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
add get_or_create method
properly type exception
debug
Added url expiry check to get_or_create method in line with closed issue ronaldgrn#2
debug
debug
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
|
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:
|
|
|
||
| def get_or_create(user, link, refresh=False): | ||
| try: | ||
| m = UrlMap.objects.get(full_url=link); |
There was a problem hiding this comment.
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)
|
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
<#25 (comment)>
:
> @@ -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
<#25 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA4GSGNDTAZCYPXIRMOP7YL3HQ2XLAVCNFSM6AAAAACAJ4ZY32VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDSOJZGE3DCNBVGA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
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.