Exemple #1
0
def index(request):
    if request.method == 'POST':
        form = NewShortURL(request.POST)
        if form.is_valid():
            url = form.cleaned_data["url"]

            #Regex URL validation from John Gruber https://gist.github.com/uogbuji/705383
            p = re.compile(
                r'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
            m = p.match(url)
            if m:
                url_to_insert = Url()
                url_to_insert.url = url
                url_to_insert.save()

                shorted_url = settings.PROTOCOL+'//'+settings.HOSTNAME+settings.PORT+'/'+str(url_to_insert.id_url)

                data = {
                    'shorted_url': shorted_url,
                    'original_url' : url
                }

                return render(request, 'web/result.html', data)
            else:
                data = {
                    'form': form,
                    'error': 'Field URL is not valid URL'
                }
                return render(request, 'web/form.html', data)
        else:
            data = {
                'form': form,
                'error': 'Please introduce url to short'
            }
            return render(request, 'web/form.html', data)

    else:
        form = NewShortURL()
        data = {
            'form': form,
        }
        return render(request, 'web/form.html', data)
Exemple #2
0
def add_url(url="", thumbs=None, is_check=True):
    from web.models import Url
    if not url:
        return False

    # /で終わっていたら最後の文字を削除
    if url[len(url) - 1:len(url)] == '/':
        url = url[:-1]

    is_exists = Url.objects.filter(url=url).exists()
    if is_exists:
        # print("(重複): %s" % url)
        return False

    # URLチェック
    if is_check:
        # 無視URLが含まれていたら、登録しない
        for ig in ignore_urls:
            if ig in url:
                return False
        # 許可URL以外は登録しない
        for eu in enabled_urls:
            if eu not in url:
                return False

    u = Url()
    u.url = url
    if thumbs:
        if isinstance(thumbs, list):
            u.thumbnails = json.dumps(thumbs)
            u.thumbnail = thumbs[9]
        else:
            u.thumbnails = None
            u.thumbnail = thumbs
    u.save()
    print("add : %s (%s)" % (u.url, u.thumbnail))

    return True