From ebe352fd27c0a4adc924264706af073c9bee24bd Mon Sep 17 00:00:00 2001 From: john-George510 Date: Sun, 27 Aug 2023 16:35:08 +0530 Subject: [PATCH 1/2] first commit --- .gitignore | 1 + config/settings/base.py | 4 +- config/urls.py | 3 ++ static/css/urlshortener.css | 71 +++++++++++++++++++++++++++++ static/js/urlshortener.js | 28 ++++++++++++ templates/urlshortner/index.html | 44 ++++++++++++++++++ urlshortener/__init__.py | 0 urlshortener/admin.py | 4 ++ urlshortener/apps.py | 6 +++ urlshortener/migrations/__init__.py | 0 urlshortener/models.py | 14 ++++++ urlshortener/tests.py | 3 ++ urlshortener/urls.py | 8 ++++ urlshortener/views.py | 32 +++++++++++++ 14 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 static/css/urlshortener.css create mode 100644 static/js/urlshortener.js create mode 100644 templates/urlshortner/index.html create mode 100644 urlshortener/__init__.py create mode 100644 urlshortener/admin.py create mode 100644 urlshortener/apps.py create mode 100644 urlshortener/migrations/__init__.py create mode 100644 urlshortener/models.py create mode 100644 urlshortener/tests.py create mode 100644 urlshortener/urls.py create mode 100644 urlshortener/views.py 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..3c7fe21 --- /dev/null +++ b/static/css/urlshortener.css @@ -0,0 +1,71 @@ +*, +*::before, +*::after { + box-sizing: border-box; +} + +body, +section { + display: flex; + align-items: center; + justify-content: center; + padding: 1rem; +} + +body { + min-height: 100vh; + font-family: "Lato", sans-serif; + line-height: 1.5; + color: #111; +} + +main { + max-width: 720px; + width: 100%; + border: 2.5px solid #330867; + border-radius: 2.5px; + box-shadow: 0 10px 20px rgba(17, 17, 17, 0.125), + 0 5px 5px rgba(17, 17, 17, 0.25); + background-image: linear-gradient(to top, #30cfd0 0%, #330867 100%); +} + +h1 { + padding: 1rem; + color: white; +} + +input, +button { + font-family: inherit; + line-height: inherit; + color: inherit; + border: 0; + padding: 1rem; +} + +input { + box-sizing: border-box; + width: 100%; + border: 1px solid; +} + +.buttons { + display: flex; +} + +button { + cursor: pointer; + flex: 1; + background-color: transparent; + color: white; + border: 1px solid; + &:hover { + background-color: rgba(255, 255, 255, 0.5); + } +} + +section { + min-height: 120px; + text-align: center; + background-color: white; +} 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..e1f3a17 --- /dev/null +++ b/templates/urlshortner/index.html @@ -0,0 +1,44 @@ +{% 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) From ff2c55bf3c541168781c39f8a6b6dd4b876015cd Mon Sep 17 00:00:00 2001 From: john-George510 Date: Sun, 27 Aug 2023 16:49:54 +0530 Subject: [PATCH 2/2] styling changes --- static/css/urlshortener.css | 86 +++++++++++++++----------------- templates/urlshortner/index.html | 12 +++-- 2 files changed, 46 insertions(+), 52 deletions(-) diff --git a/static/css/urlshortener.css b/static/css/urlshortener.css index 3c7fe21..71b60de 100644 --- a/static/css/urlshortener.css +++ b/static/css/urlshortener.css @@ -1,71 +1,63 @@ -*, -*::before, -*::after { - box-sizing: border-box; -} - -body, -section { +body { + font-family: Arial, sans-serif; + background-color: #f0f0f0; + margin: 0; + padding: 0; display: flex; - align-items: center; justify-content: center; - padding: 1rem; -} - -body { - min-height: 100vh; - font-family: "Lato", sans-serif; - line-height: 1.5; - color: #111; + align-items: center; + height: 100vh; } -main { - max-width: 720px; - width: 100%; - border: 2.5px solid #330867; - border-radius: 2.5px; - box-shadow: 0 10px 20px rgba(17, 17, 17, 0.125), - 0 5px 5px rgba(17, 17, 17, 0.25); - background-image: linear-gradient(to top, #30cfd0 0%, #330867 100%); +.container { + max-width: 600px; + background-color: #fff; + padding: 20px; + border-radius: 10px; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); } h1 { - padding: 1rem; - color: white; + font-size: 24px; + margin-bottom: 20px; } -input, -button { - font-family: inherit; - line-height: inherit; - color: inherit; - border: 0; - padding: 1rem; +form { + display: flex; + flex-direction: column; } -input { - box-sizing: border-box; - width: 100%; - border: 1px solid; +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; - flex: 1; - background-color: transparent; - color: white; - border: 1px solid; - &:hover { - background-color: rgba(255, 255, 255, 0.5); - } + margin: 0 5px; +} + +#random { + background-color: #3498db; +} + +#shorten { + background-color: #27ae60; } section { - min-height: 120px; text-align: center; - background-color: white; + margin-top: 20px; } diff --git a/templates/urlshortner/index.html b/templates/urlshortner/index.html index e1f3a17..c56b6f7 100644 --- a/templates/urlshortner/index.html +++ b/templates/urlshortner/index.html @@ -21,7 +21,7 @@ /> -
+

URL Shortener

{% csrf_token %} @@ -33,12 +33,14 @@

URL Shortener

placeholder="Enter shortened URL here" />
- - + +
-

-
+
+

+
+