pep8
This commit is contained in:
parent
df10c67999
commit
b397561cce
5 changed files with 83 additions and 80 deletions
|
@ -1,2 +1 @@
|
|||
from django.contrib import admin
|
||||
|
||||
|
|
|
@ -4,51 +4,55 @@ 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)
|
||||
clicks = models.IntegerField(default=0)
|
||||
ip = models.GenericIPAddressField(null=True)
|
||||
id = models.CharField(primary_key=True, max_length=12)
|
||||
url = models.URLField(max_length=2048)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
clicks = models.IntegerField(default=0)
|
||||
ip = models.GenericIPAddressField(null=True)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.url
|
||||
def __unicode__(self):
|
||||
return self.url
|
||||
|
||||
def generate_unique_id(self, length=8):
|
||||
attempts = 0
|
||||
id = random_id(length)
|
||||
try:
|
||||
while Link.objects.get(id=id) and attempts < 10:
|
||||
attempts = attempts + 1
|
||||
id = random_id()
|
||||
except:
|
||||
pass
|
||||
return id
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.pk:
|
||||
self.id = self.generate_unique_id()
|
||||
super(Link, self).save(*args, **kwargs)
|
||||
def generate_unique_id(self, length=8):
|
||||
attempts = 0
|
||||
id = random_id(length)
|
||||
try:
|
||||
while Link.objects.get(id=id) and attempts < 10:
|
||||
attempts = attempts + 1
|
||||
id = random_id()
|
||||
except:
|
||||
pass
|
||||
return id
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.pk:
|
||||
self.id = self.generate_unique_id()
|
||||
super(Link, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class LinkForm(ModelForm):
|
||||
url = CharField(widget=TextInput(attrs={'class':'u-full-width', 'placeholder': 'Paste the link you want to shorten'}), label='')
|
||||
class Meta:
|
||||
model = Link
|
||||
fields = ['url']
|
||||
|
||||
url = CharField(widget=TextInput(attrs={
|
||||
'class': 'u-full-width',
|
||||
'placeholder': 'Paste the link you want to shorten'}),
|
||||
label='')
|
||||
|
||||
class Meta:
|
||||
model = Link
|
||||
fields = ['url']
|
||||
|
||||
|
||||
def random_id(length):
|
||||
rand = ''
|
||||
for i in range(0,length):
|
||||
rand += int_to_char(random.randint(0,61));
|
||||
return rand;
|
||||
rand = ''
|
||||
for i in range(0, length):
|
||||
rand += int_to_char(random.randint(0, 61))
|
||||
return rand
|
||||
|
||||
|
||||
def int_to_char(int):
|
||||
if(int < 10):
|
||||
char = chr(int + 48)
|
||||
elif(int < 36):
|
||||
char = chr(int + 55)
|
||||
else:
|
||||
char = chr(int + 61)
|
||||
return char
|
||||
if(int < 10):
|
||||
char = chr(int + 48)
|
||||
elif(int < 36):
|
||||
char = chr(int + 55)
|
||||
else:
|
||||
char = chr(int + 61)
|
||||
return char
|
||||
|
|
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