pep8
This commit is contained in:
parent
df10c67999
commit
b397561cce
5 changed files with 83 additions and 80 deletions
60
app/views.py
60
app/views.py
|
@ -6,39 +6,39 @@ 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
|
||||
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)
|
||||
parsed = urlparse(link.url)
|
||||
if parsed.scheme:
|
||||
return redirect(link.url)
|
||||
return redirect("http://" + link.url)
|
||||
except Exception as 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")
|
||||
try:
|
||||
link = Link.objects.get(id=id)
|
||||
parsed = urlparse(link.url)
|
||||
if parsed.scheme:
|
||||
return redirect(link.url)
|
||||
return redirect("http://" + link.url)
|
||||
except Exception as 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 and request.session['short_url']:
|
||||
context['short_url'] = request.session['short_url']
|
||||
request.session['short_url'] = None
|
||||
if 'url' in request.POST:
|
||||
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('/')
|
||||
return render(request, 'index.html', context)
|
||||
context = {'form': LinkForm}
|
||||
if 'short_url' in request.session and request.session['short_url']:
|
||||
context['short_url'] = request.session['short_url']
|
||||
request.session['short_url'] = None
|
||||
if 'url' in request.POST:
|
||||
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('/')
|
||||
return render(request, 'index.html', context)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue