diff --git a/app/admin.py b/app/admin.py index c6fe108..694323f 100644 --- a/app/admin.py +++ b/app/admin.py @@ -1,2 +1 @@ from django.contrib import admin - diff --git a/app/models.py b/app/models.py index d157d81..4506734 100644 --- a/app/models.py +++ b/app/models.py @@ -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 \ No newline at end of file + if(int < 10): + char = chr(int + 48) + elif(int < 36): + char = chr(int + 55) + else: + char = chr(int + 61) + return char diff --git a/app/views.py b/app/views.py index 38eafc0..c35266d 100644 --- a/app/views.py +++ b/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) \ No newline at end of file + 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) diff --git a/project/settings.py b/project/settings.py index b06aec5..8837be9 100644 --- a/project/settings.py +++ b/project/settings.py @@ -24,20 +24,20 @@ ALLOWED_HOSTS = ['*'] # Application definition INSTALLED_APPS = ( - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.staticfiles', - 'app', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.staticfiles', + 'app', ) MIDDLEWARE_CLASSES = ( - 'sslify.middleware.SSLifyMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', + 'sslify.middleware.SSLifyMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', ) TEMPLATE_DIRS = ( - os.path.join(BASE_DIR, 'app/templates'), + os.path.join(BASE_DIR, 'app/templates'), ) 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 DATABASES = { - 'default': dj_database_url.config() + 'default': dj_database_url.config() } # Internationalization @@ -74,5 +74,5 @@ STATIC_ROOT = 'staticfiles' STATIC_URL = '/static/' STATICFILES_DIRS = ( - os.path.join(BASE_DIR, 'static'), + os.path.join(BASE_DIR, 'static'), ) diff --git a/project/wsgi.py b/project/wsgi.py index 2751791..23828be 100644 --- a/project/wsgi.py +++ b/project/wsgi.py @@ -13,4 +13,4 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") from django.core.wsgi import get_wsgi_application from dj_static import Cling -application = Cling(get_wsgi_application()) \ No newline at end of file +application = Cling(get_wsgi_application())