Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions shortener/shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from django.conf import settings
from django.db import IntegrityError
from django.db.models import F
from datetime import datetime, timedelta

from django.utils import timezone

from datetime import datetime, timedelta
import random


Expand All @@ -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);
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)

if refresh == True and m.lifespan != -1:
if timezone.now() > m.date_expired:
# m.delete() # should really delete the expired shortlinks at some point
# this seems as good a place as any...
raise PermissionError("shortlink expired")
return m.short_url;
except UrlMap.DoesNotExist:
return create(user, link)

def create(user, link):
# check if user allowed to save link
try:
Expand Down