Compare commits
No commits in common. "988bdd3808c41b7c6b5eb1583fba4b2f6be15b56" and "bbf90e7c3b758ecd3f4a40d4b046df42b135c9a7" have entirely different histories.
988bdd3808
...
bbf90e7c3b
|
|
@ -45,7 +45,6 @@ INSTALLED_APPS = [
|
||||||
"django.contrib.staticfiles",
|
"django.contrib.staticfiles",
|
||||||
"core.apps.CoreConfig",
|
"core.apps.CoreConfig",
|
||||||
"dashboard.apps.DashboardConfig",
|
"dashboard.apps.DashboardConfig",
|
||||||
"shorturls.apps.ShorturlsConfig",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
|
||||||
|
|
@ -23,5 +23,4 @@ urlpatterns = [
|
||||||
path("", include("core.urls", namespace="core")),
|
path("", include("core.urls", namespace="core")),
|
||||||
path("accounts/", include("accounts.urls")),
|
path("accounts/", include("accounts.urls")),
|
||||||
path("dashboard/", include("dashboard.urls", namespace="dashboard")),
|
path("dashboard/", include("dashboard.urls", namespace="dashboard")),
|
||||||
path("", include("shorturls.urls", namespace="shorturls")),
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[project]
|
[project]
|
||||||
name = "tdg-rocks"
|
name = "tdg-rocks"
|
||||||
version = "0.2.0"
|
version = "0.1.0"
|
||||||
description = "Add your description here"
|
description = "Add your description here"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
|
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
from django.contrib import admin
|
|
||||||
|
|
||||||
from .models import ShortURL
|
|
||||||
|
|
||||||
|
|
||||||
@admin.register(ShortURL)
|
|
||||||
class ShortURLAdmin(admin.ModelAdmin):
|
|
||||||
list_display = ["long_url", "short_url", "followed"]
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
from django.apps import AppConfig
|
|
||||||
|
|
||||||
|
|
||||||
class ShorturlsConfig(AppConfig):
|
|
||||||
default_auto_field = "django.db.models.BigAutoField"
|
|
||||||
name = "shorturls"
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
# Generated by Django 5.2.7 on 2025-11-06 12:17
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
initial = True
|
|
||||||
|
|
||||||
dependencies = []
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.CreateModel(
|
|
||||||
name="ShortURL",
|
|
||||||
fields=[
|
|
||||||
(
|
|
||||||
"id",
|
|
||||||
models.BigAutoField(
|
|
||||||
auto_created=True,
|
|
||||||
primary_key=True,
|
|
||||||
serialize=False,
|
|
||||||
verbose_name="ID",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
("created", models.DateTimeField(auto_now_add=True)),
|
|
||||||
("followed", models.PositiveIntegerField(default=0)),
|
|
||||||
("long_url", models.TextField()),
|
|
||||||
("short_url", models.CharField(blank=True, max_length=20, unique=True)),
|
|
||||||
],
|
|
||||||
options={
|
|
||||||
"ordering": ["created"],
|
|
||||||
},
|
|
||||||
),
|
|
||||||
]
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
from django.db import models
|
|
||||||
|
|
||||||
from .utils import create_short_url
|
|
||||||
|
|
||||||
|
|
||||||
class ShortURL(models.Model):
|
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
|
||||||
followed = models.PositiveIntegerField(default=0)
|
|
||||||
long_url = models.TextField()
|
|
||||||
short_url = models.CharField(max_length=20, unique=True, blank=True)
|
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
|
||||||
if not self.short_url:
|
|
||||||
self.short_url = create_short_url(self)
|
|
||||||
super().save()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
ordering = ["created"]
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
# Create your tests here.
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
from django.urls import path
|
|
||||||
|
|
||||||
from . import views
|
|
||||||
|
|
||||||
app_name = "shorturls"
|
|
||||||
|
|
||||||
urlpatterns = [
|
|
||||||
path("<str:shorturl>/", views.ShorturlsRedirectView.as_view(), name="redirect"),
|
|
||||||
]
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
from random import choice
|
|
||||||
from string import ascii_letters, digits
|
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
SIZE = getattr(settings, "MAXIMUM_URL_CHARS", 10)
|
|
||||||
AVAILABLE_CHARS = ascii_letters + digits
|
|
||||||
|
|
||||||
|
|
||||||
def create_random_code(chars=AVAILABLE_CHARS):
|
|
||||||
return "".join([choice(chars) for _ in range(SIZE)])
|
|
||||||
|
|
||||||
|
|
||||||
def create_short_url(model_instance):
|
|
||||||
random_code = create_random_code()
|
|
||||||
model_class = model_instance.__class__
|
|
||||||
if model_class.objects.filter(short_url=random_code).exists():
|
|
||||||
return create_short_url(model_instance)
|
|
||||||
return random_code
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
from django.shortcuts import get_object_or_404
|
|
||||||
from django.views.generic.base import RedirectView
|
|
||||||
|
|
||||||
from .models import ShortURL
|
|
||||||
|
|
||||||
|
|
||||||
class ShorturlsRedirectView(RedirectView):
|
|
||||||
def get_redirect_url(self, *args, **kwargs):
|
|
||||||
url = get_object_or_404(ShortURL, short_url=kwargs["shorturl"])
|
|
||||||
url.followed += 1
|
|
||||||
url.save()
|
|
||||||
return url.long_url
|
|
||||||
Loading…
Reference in New Issue