Redirect on urls with no scheme, reset flash session
This commit is contained in:
parent
d52fea9afa
commit
bc1c5ff08a
3 changed files with 34 additions and 22 deletions
|
@ -6,16 +6,16 @@ import random
|
||||||
class Link(models.Model):
|
class Link(models.Model):
|
||||||
id = models.CharField(primary_key=True, max_length=12)
|
id = models.CharField(primary_key=True, max_length=12)
|
||||||
url = models.URLField(max_length=2048)
|
url = models.URLField(max_length=2048)
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=False)
|
||||||
clicks = models.IntegerField(default=0)
|
clicks = models.IntegerField(default=0)
|
||||||
ip = models.GenericIPAddressField(null=True)
|
ip = models.GenericIPAddressField(null=True)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.url
|
return self.url
|
||||||
|
|
||||||
def generate_unique_id(self, length=12):
|
def generate_unique_id(self, length=8):
|
||||||
attempts = 0
|
attempts = 0
|
||||||
id = random_id()
|
id = random_id(length)
|
||||||
try:
|
try:
|
||||||
while Link.objects.get(id=id) and attempts < 10:
|
while Link.objects.get(id=id) and attempts < 10:
|
||||||
attempts = attempts + 1
|
attempts = attempts + 1
|
||||||
|
@ -37,9 +37,9 @@ class LinkForm(ModelForm):
|
||||||
fields = ['url']
|
fields = ['url']
|
||||||
|
|
||||||
|
|
||||||
def random_id():
|
def random_id(length):
|
||||||
rand = ''
|
rand = ''
|
||||||
for i in range(0,12):
|
for i in range(0,length):
|
||||||
rand += int_to_char(random.randint(0,61));
|
rand += int_to_char(random.randint(0,61));
|
||||||
return rand;
|
return rand;
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,44 @@
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from django.http import Http404
|
from django.http import Http404, HttpResponse
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from .models import Link, LinkForm
|
from .models import Link, LinkForm
|
||||||
|
|
||||||
|
|
||||||
|
def get_client_ip(request):
|
||||||
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||||
|
if x_forwarded_for:
|
||||||
|
ip = x_forwarded_for.split(',')[0]
|
||||||
|
else:
|
||||||
|
ip = request.META.get('REMOTE_ADDR')
|
||||||
|
return ip
|
||||||
|
|
||||||
|
|
||||||
def catchall(request, id):
|
def catchall(request, id):
|
||||||
try:
|
try:
|
||||||
link = Link.objects.get(id=id)
|
link = Link.objects.get(id=id)
|
||||||
|
parsed = urlparse(link.url)
|
||||||
|
if parsed.scheme:
|
||||||
return redirect(link.url)
|
return redirect(link.url)
|
||||||
except:
|
return redirect("http://" + link.url)
|
||||||
parsed = urlparse(id)
|
except Exception as e:
|
||||||
if parsed.netloc:
|
return HttpResponse(e)
|
||||||
link = Link(url=id)
|
parsed = urlparse(id)
|
||||||
link.save();
|
if parsed.netloc:
|
||||||
context = {'form': LinkForm}
|
link = Link(url=id, ip=get_client_ip(request))
|
||||||
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
link.save();
|
||||||
return redirect('/')
|
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||||
raise Http404("Link does not exist")
|
return redirect('/')
|
||||||
|
raise Http404("Link does not exist")
|
||||||
|
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
context = {'form': LinkForm}
|
context = {'form': LinkForm}
|
||||||
if 'short_url' in request.session:
|
if 'short_url' in request.session:
|
||||||
context['short_url'] = request.session['short_url']
|
context['short_url'] = request.session['short_url']
|
||||||
|
request.session['short_url'] = None
|
||||||
if 'url' in request.POST:
|
if 'url' in request.POST:
|
||||||
link = Link(url=request.POST['url'])
|
link = Link(url=request.POST['url'], ip=get_client_ip(request))
|
||||||
link.save();
|
link.save();
|
||||||
request.session['short_url'] = "http://" + request.get_host() + "/" + link.id
|
request.session['short_url'] = "http://" + request.get_host() + "/" + link.id
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
|
@ -24,11 +24,8 @@ ALLOWED_HOSTS = ['*']
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = (
|
INSTALLED_APPS = (
|
||||||
'django.contrib.admin',
|
|
||||||
'django.contrib.auth',
|
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'links',
|
'links',
|
||||||
)
|
)
|
||||||
|
@ -67,7 +64,7 @@ USE_I18N = True
|
||||||
|
|
||||||
USE_L10N = True
|
USE_L10N = True
|
||||||
|
|
||||||
USE_TZ = True
|
USE_TZ = False
|
||||||
|
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue