diff --git a/.gitignore b/.gitignore index 6566983..59ee226 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ venv **/migrations/0* **/pychache__* +**/__pycache__/* # Environments .env diff --git a/config/settings/base.py b/config/settings/base.py index 8133b28..86af77b 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -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) @@ -68,6 +69,7 @@ CUSTOM_APPS = [ "auth_login", 'home', + 'urlshortener' ] INSTALLED_APPS += CUSTOM_APPS + THIRD_PARTY_APPS diff --git a/config/urls.py b/config/urls.py index fe8a38e..dd46f41 100644 --- a/config/urls.py +++ b/config/urls.py @@ -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( @@ -61,6 +62,8 @@ 'swagger', cache_timeout=0), name='schema-swagger-ui'), + path("urlshortener/", include("urlshortener.urls")), + path('', view, name='go'), ] urlpatterns += static(settings.STATIC_URL, diff --git a/static/css/urlshortener.css b/static/css/urlshortener.css new file mode 100644 index 0000000..71b60de --- /dev/null +++ b/static/css/urlshortener.css @@ -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; +} diff --git a/static/js/urlshortener.js b/static/js/urlshortener.js new file mode 100644 index 0000000..9c7ab9b --- /dev/null +++ b/static/js/urlshortener.js @@ -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); + }, + }); + }); +}); diff --git a/templates/urlshortner/index.html b/templates/urlshortner/index.html new file mode 100644 index 0000000..c56b6f7 --- /dev/null +++ b/templates/urlshortner/index.html @@ -0,0 +1,46 @@ +{% load static %} + + + + + + URL Shortener + + + + + +
+

URL Shortener

+
+ {% csrf_token %} + + +
+ + +
+
+ +
+

+
+
+ + diff --git a/urlshortener/__init__.py b/urlshortener/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/urlshortener/admin.py b/urlshortener/admin.py new file mode 100644 index 0000000..c6f10ca --- /dev/null +++ b/urlshortener/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Url +# Register your models here. +admin.site.register(Url) diff --git a/urlshortener/apps.py b/urlshortener/apps.py new file mode 100644 index 0000000..4e5eeb3 --- /dev/null +++ b/urlshortener/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UrlshortenerConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'urlshortener' diff --git a/urlshortener/migrations/__init__.py b/urlshortener/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/urlshortener/models.py b/urlshortener/models.py new file mode 100644 index 0000000..a60bae6 --- /dev/null +++ b/urlshortener/models.py @@ -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}" diff --git a/urlshortener/tests.py b/urlshortener/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/urlshortener/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/urlshortener/urls.py b/urlshortener/urls.py new file mode 100644 index 0000000..1dd191c --- /dev/null +++ b/urlshortener/urls.py @@ -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('', views.go, name='go'), +] diff --git a/urlshortener/views.py b/urlshortener/views.py new file mode 100644 index 0000000..16c5e46 --- /dev/null +++ b/urlshortener/views.py @@ -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)