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
|
from django.contrib import admin
|
||||||
|
|
||||||
|
|
|
@ -4,51 +4,55 @@ 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=True)
|
||||||
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=8):
|
def generate_unique_id(self, length=8):
|
||||||
attempts = 0
|
attempts = 0
|
||||||
id = random_id(length)
|
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
|
||||||
id = random_id()
|
id = random_id()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
return id
|
return id
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if not self.pk:
|
if not self.pk:
|
||||||
self.id = self.generate_unique_id()
|
self.id = self.generate_unique_id()
|
||||||
super(Link, self).save(*args, **kwargs)
|
super(Link, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class LinkForm(ModelForm):
|
class LinkForm(ModelForm):
|
||||||
url = CharField(widget=TextInput(attrs={'class':'u-full-width', 'placeholder': 'Paste the link you want to shorten'}), label='')
|
url = CharField(widget=TextInput(attrs={
|
||||||
class Meta:
|
'class': 'u-full-width',
|
||||||
model = Link
|
'placeholder': 'Paste the link you want to shorten'}),
|
||||||
fields = ['url']
|
label='')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Link
|
||||||
|
fields = ['url']
|
||||||
|
|
||||||
|
|
||||||
def random_id(length):
|
def random_id(length):
|
||||||
rand = ''
|
rand = ''
|
||||||
for i in range(0,length):
|
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
|
||||||
|
|
||||||
|
|
||||||
def int_to_char(int):
|
def int_to_char(int):
|
||||||
if(int < 10):
|
if(int < 10):
|
||||||
char = chr(int + 48)
|
char = chr(int + 48)
|
||||||
elif(int < 36):
|
elif(int < 36):
|
||||||
char = chr(int + 55)
|
char = chr(int + 55)
|
||||||
else:
|
else:
|
||||||
char = chr(int + 61)
|
char = chr(int + 61)
|
||||||
return char
|
return char
|
||||||
|
|
60
app/views.py
60
app/views.py
|
@ -6,39 +6,39 @@ from .models import Link, LinkForm
|
||||||
|
|
||||||
|
|
||||||
def get_client_ip(request):
|
def get_client_ip(request):
|
||||||
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
|
||||||
if x_forwarded_for:
|
if x_forwarded_for:
|
||||||
ip = x_forwarded_for.split(',')[0]
|
ip = x_forwarded_for.split(',')[0]
|
||||||
else:
|
else:
|
||||||
ip = request.META.get('REMOTE_ADDR')
|
ip = request.META.get('REMOTE_ADDR')
|
||||||
return ip
|
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)
|
parsed = urlparse(link.url)
|
||||||
if parsed.scheme:
|
if parsed.scheme:
|
||||||
return redirect(link.url)
|
return redirect(link.url)
|
||||||
return redirect("http://" + link.url)
|
return redirect("http://" + link.url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
parsed = urlparse(id)
|
parsed = urlparse(id)
|
||||||
if parsed.netloc:
|
if parsed.netloc:
|
||||||
link = Link(url=id, ip=get_client_ip(request))
|
link = Link(url=id, ip=get_client_ip(request))
|
||||||
link.save();
|
link.save()
|
||||||
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
request.session['short_url'] = "http://" + str(request.get_host()) + "/" + str(link.id)
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
raise Http404("Link does not exist")
|
raise Http404("Link does not exist")
|
||||||
|
|
||||||
|
|
||||||
def home(request):
|
def home(request):
|
||||||
context = {'form': LinkForm}
|
context = {'form': LinkForm}
|
||||||
if 'short_url' in request.session and request.session['short_url']:
|
if 'short_url' in request.session and request.session['short_url']:
|
||||||
context['short_url'] = request.session['short_url']
|
context['short_url'] = request.session['short_url']
|
||||||
request.session['short_url'] = None
|
request.session['short_url'] = None
|
||||||
if 'url' in request.POST:
|
if 'url' in request.POST:
|
||||||
link = Link(url=request.POST['url'], ip=get_client_ip(request))
|
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('/')
|
||||||
return render(request, 'index.html', context)
|
return render(request, 'index.html', context)
|
||||||
|
|
|
@ -24,20 +24,20 @@ ALLOWED_HOSTS = ['*']
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = (
|
INSTALLED_APPS = (
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'app',
|
'app',
|
||||||
)
|
)
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES = (
|
MIDDLEWARE_CLASSES = (
|
||||||
'sslify.middleware.SSLifyMiddleware',
|
'sslify.middleware.SSLifyMiddleware',
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
)
|
)
|
||||||
|
|
||||||
TEMPLATE_DIRS = (
|
TEMPLATE_DIRS = (
|
||||||
os.path.join(BASE_DIR, 'app/templates'),
|
os.path.join(BASE_DIR, 'app/templates'),
|
||||||
)
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = 'project.urls'
|
ROOT_URLCONF = 'project.urls'
|
||||||
|
@ -51,7 +51,7 @@ SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||||
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
|
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': dj_database_url.config()
|
'default': dj_database_url.config()
|
||||||
}
|
}
|
||||||
|
|
||||||
# Internationalization
|
# Internationalization
|
||||||
|
@ -74,5 +74,5 @@ STATIC_ROOT = 'staticfiles'
|
||||||
STATIC_URL = '/static/'
|
STATIC_URL = '/static/'
|
||||||
|
|
||||||
STATICFILES_DIRS = (
|
STATICFILES_DIRS = (
|
||||||
os.path.join(BASE_DIR, 'static'),
|
os.path.join(BASE_DIR, 'static'),
|
||||||
)
|
)
|
||||||
|
|
|
@ -13,4 +13,4 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
|
||||||
from django.core.wsgi import get_wsgi_application
|
from django.core.wsgi import get_wsgi_application
|
||||||
from dj_static import Cling
|
from dj_static import Cling
|
||||||
|
|
||||||
application = Cling(get_wsgi_application())
|
application = Cling(get_wsgi_application())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue