dev #1

Merged
calum merged 5 commits from dev into master 2025-11-06 12:34:26 +00:00
14 changed files with 113 additions and 2 deletions

View File

@ -45,6 +45,7 @@ INSTALLED_APPS = [
"django.contrib.staticfiles",
"core.apps.CoreConfig",
"dashboard.apps.DashboardConfig",
"shorturls.apps.ShorturlsConfig",
]
MIDDLEWARE = [

View File

@ -23,4 +23,5 @@ urlpatterns = [
path("", include("core.urls", namespace="core")),
path("accounts/", include("accounts.urls")),
path("dashboard/", include("dashboard.urls", namespace="dashboard")),
path("", include("shorturls.urls", namespace="shorturls")),
]

View File

@ -1,6 +1,6 @@
[project]
name = "tdg-rocks"
version = "0.1.0"
version = "0.2.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.13"

0
shorturls/__init__.py Normal file
View File

8
shorturls/admin.py Normal file
View File

@ -0,0 +1,8 @@
from django.contrib import admin
from .models import ShortURL
@admin.register(ShortURL)
class ShortURLAdmin(admin.ModelAdmin):
list_display = ["long_url", "short_url", "followed"]

6
shorturls/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class ShorturlsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "shorturls"

View File

@ -0,0 +1,34 @@
# 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"],
},
),
]

View File

18
shorturls/models.py Normal file
View File

@ -0,0 +1,18 @@
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"]

3
shorturls/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

9
shorturls/urls.py Normal file
View File

@ -0,0 +1,9 @@
from django.urls import path
from . import views
app_name = "shorturls"
urlpatterns = [
path("<str:shorturl>/", views.ShorturlsRedirectView.as_view(), name="redirect"),
]

19
shorturls/utils.py Normal file
View File

@ -0,0 +1,19 @@
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

12
shorturls/views.py Normal file
View File

@ -0,0 +1,12 @@
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

View File

@ -57,7 +57,7 @@ wheels = [
[[package]]
name = "tdg-rocks"
version = "0.1.0"
version = "0.2.0"
source = { virtual = "." }
dependencies = [
{ name = "django" },