Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
venv
**/migrations/0*
**/pychache__*
**/__pycache__/*

# Environments
.env
Expand Down
4 changes: 3 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str("SECRET_KEY", default="django-insecure-cag@!muz(kv)t31hxk6w3b)^vzt62_n1wo8&@89)ueefs6p4-7")
SECRET_KEY = env.str(
"SECRET_KEY", default="django-insecure-cag@!muz(kv)t31hxk6w3b)^vzt62_n1wo8&@89)ueefs6p4-7")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool('DEBUG', default=False)
Expand Down Expand Up @@ -68,6 +69,7 @@
CUSTOM_APPS = [
"auth_login",
'home',
'urlshortener'
]

INSTALLED_APPS += CUSTOM_APPS + THIRD_PARTY_APPS
Expand Down
3 changes: 3 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions, authentication
from urlshortener.views import go as view

schema_view = get_schema_view(
openapi.Info(
Expand Down Expand Up @@ -61,6 +62,8 @@
'swagger',
cache_timeout=0),
name='schema-swagger-ui'),
path("urlshortener/", include("urlshortener.urls")),
path('<str:pk>', view, name='go'),
]

urlpatterns += static(settings.STATIC_URL,
Expand Down
63 changes: 63 additions & 0 deletions static/css/urlshortener.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container {
max-width: 600px;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h1 {
font-size: 24px;
margin-bottom: 20px;
}

form {
display: flex;
flex-direction: column;
}

input[type="text"] {
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}

.buttons {
display: flex;
justify-content: space-between;
margin-top: 10px;
}

button {
padding: 10px 20px;
border: none;
border-radius: 5px;
color: #fff;
cursor: pointer;
margin: 0 5px;
}

#random {
background-color: #3498db;
}

#shorten {
background-color: #27ae60;
}

section {
text-align: center;
margin-top: 20px;
}
28 changes: 28 additions & 0 deletions static/js/urlshortener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
$(document).ready(function () {
$("#random").click(function () {
const characters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let randomId = "";

for (let i = 0; i < 10; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
randomId += characters.charAt(randomIndex);
}
$("#short_link").val(randomId);
});
$(document).on("submit", "#post-form", function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/urlshortener/create",
data: {
link: $("#link").val(),
short_link: $("#short_link").val(),
csrfmiddlewaretoken: $("input[name=csrfmiddlewaretoken]").val(),
},
success: function (data) {
$("h2").html(data);
},
});
});
});
46 changes: 46 additions & 0 deletions templates/urlshortner/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>URL Shortener</title>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"
></script>
<script
type="text/javascript"
src="{% static 'js/urlshortener.js' %}"
></script>
<link
rel="stylesheet"
type="text/css"
href="{% static 'css/urlshortener.css' %}"
/>
</head>
<body>
<div class="container">
<h1>URL Shortener</h1>
<form id="post-form">
{% csrf_token %}
<input type="text" name="link" id="link" placeholder="Enter URL here" />
<input
type="text"
name="short_link"
id="short_link"
placeholder="Enter shortened URL here"
/>
<div class="buttons">
<button type="button" id="random">Generate random short URL</button>
<button type="submit" id="shorten">Create a URL</button>
</div>
</form>

<section id="section">
<h2></h2>
</section>
</div>
</body>
</html>
Empty file added urlshortener/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions urlshortener/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Url
# Register your models here.
admin.site.register(Url)
6 changes: 6 additions & 0 deletions urlshortener/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UrlshortenerConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'urlshortener'
Empty file.
14 changes: 14 additions & 0 deletions urlshortener/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import models

# Create your models here.


class Url(models.Model):
orginal_url = models.CharField(max_length=1000)
shortned_url = models.CharField(max_length=10)
no_of_clicks = models.IntegerField(default=0)
location = models.CharField(max_length=100, default="")
referrals = models.CharField(max_length=100, default="")

def __str__(self):
return f"{self.orginal_url} --> {self.shortned_url}"
3 changes: 3 additions & 0 deletions urlshortener/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
8 changes: 8 additions & 0 deletions urlshortener/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name='index'),
path('create', views.create, name='create'),
path('<str:pk>', views.go, name='go'),
]
32 changes: 32 additions & 0 deletions urlshortener/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.shortcuts import render, redirect
import uuid
from .models import Url
from django.http import HttpResponse
# Create your views here.


def index(req):
return render(req, 'urlshortner/index.html')


def create(req):
if req.method == 'POST':
orginal_url = req.POST['link']
short_url = req.POST['short_link']
if Url.objects.filter(orginal_url=orginal_url).exists():
str = "Shortend url already exists: localhost:8000/" + \
Url.objects.get(orginal_url=orginal_url).shortned_url
elif Url.objects.filter(shortned_url=short_url).exists():
str = "Short Url already taken"
else:
new_url = Url(orginal_url=orginal_url, shortned_url=short_url)
new_url.save()
str = "localhost:8000/" + short_url
return HttpResponse(str)


def go(req, pk):
url_details = Url.objects.get(shortned_url=pk)
url_details.no_of_clicks += 1
url_details.save()
return redirect(url_details.orginal_url)