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):
|
||||
id = models.CharField(primary_key=True, max_length=12)
|
||||
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)
|
||||
ip = models.GenericIPAddressField(null=True)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.url
|
||||
|
||||
def generate_unique_id(self, length=12):
|
||||
def generate_unique_id(self, length=8):
|
||||
attempts = 0
|
||||
id = random_id()
|
||||
id = random_id(length)
|
||||
try:
|
||||
while Link.objects.get(id=id) and attempts < 10:
|
||||
attempts = attempts + 1
|
||||
|
@ -37,9 +37,9 @@ class LinkForm(ModelForm):
|
|||
fields = ['url']
|
||||
|
||||
|
||||
def random_id():
|
||||
def random_id(length):
|
||||
rand = ''
|
||||
for i in range(0,12):
|
||||
for i in range(0,length):
|
||||
rand += int_to_char(random.randint(0,61));
|
||||
return rand;
|
||||
|
||||
|
|
|
@ -1,29 +1,44 @@
|
|||
from urlparse import urlparse
|
||||
from django.shortcuts import render, redirect
|
||||
from django.http import Http404
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.contrib import messages
|
||||
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):
|
||||
try:
|
||||
link = Link.objects.get(id=id)
|
||||
try:
|
||||
link = Link.objects.get(id=id)
|
||||
parsed = urlparse(link.url)
|
||||
if parsed.scheme:
|
||||
return redirect(link.url)
|
||||
except:
|
||||
parsed = urlparse(id)
|
||||
if parsed.netloc:
|
||||
link = Link(url=id)
|
||||
link.save();
|
||||
context = {'form': LinkForm}
|
||||
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||
return redirect('/')
|
||||
raise Http404("Link does not exist")
|
||||
return redirect("http://" + link.url)
|
||||
except Exception as e:
|
||||
return HttpResponse(e)
|
||||
parsed = urlparse(id)
|
||||
if parsed.netloc:
|
||||
link = Link(url=id, ip=get_client_ip(request))
|
||||
link.save();
|
||||
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||
return redirect('/')
|
||||
raise Http404("Link does not exist")
|
||||
|
||||
|
||||
def home(request):
|
||||
context = {'form': LinkForm}
|
||||
if 'short_url' in request.session:
|
||||
context['short_url'] = request.session['short_url']
|
||||
request.session['short_url'] = None
|
||||
if 'url' in request.POST:
|
||||
link = Link(url=request.POST['url'])
|
||||
link = Link(url=request.POST['url'], ip=get_client_ip(request))
|
||||
link.save();
|
||||
request.session['short_url'] = "http://" + request.get_host() + "/" + link.id
|
||||
return redirect('/')
|
||||
|
|
|
@ -24,11 +24,8 @@ ALLOWED_HOSTS = ['*']
|
|||
# Application definition
|
||||
|
||||
INSTALLED_APPS = (
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'links',
|
||||
)
|
||||
|
@ -67,7 +64,7 @@ USE_I18N = True
|
|||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
USE_TZ = False
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/1.7/howto/static-files/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue