Skip to content

Commit

Permalink
Refactor (#6)
Browse files Browse the repository at this point in the history
* type hints

* more type hints etc
  • Loading branch information
angusgoody authored Nov 22, 2024
1 parent c089f33 commit 0d49c8c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 25 deletions.
5 changes: 1 addition & 4 deletions shortener/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

from shortener.models import URL


# Register your models here.
# Register the model with the admin site
@admin.register(URL)
class ShortenedURLAdmin(admin.ModelAdmin):

# Display the new fields in the admin list view
list_display = ('original_url', 'short_code', 'created_at', 'clicks', 'last_accessed')
search_fields = ('original_url', 'short_code')
list_filter = ('created_at', 'last_accessed')

readonly_fields = ('clicks', 'last_accessed')
10 changes: 0 additions & 10 deletions shortener/forms.py

This file was deleted.

21 changes: 14 additions & 7 deletions shortener/models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from django.db import models
from django.utils import timezone

# Create your models here.

class URL(models.Model):
original_url = models.URLField()
short_code = models.CharField(max_length=10, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
"""
The main model for storing URL shortening data.
"""

last_accessed = models.DateTimeField(null=True, blank=True)
clicks = models.PositiveIntegerField(default=0)
original_url = models.URLField() # Original URL
short_code = models.CharField(max_length=10, unique=True) # Short code for the URL
created_at = models.DateTimeField(auto_now_add=True) # Creation timestamp

last_accessed = models.DateTimeField(null=True, blank=True) # Last accessed timestamp
clicks = models.PositiveIntegerField(default=0) # Number of times the URL was accessed

def __str__(self):
return f'{self.short_code} -> {self.original_url}'

# Method to update access info
def record_access(self):
"""
Every time the URL is accessed
this method should be called to update
the last accessed timestamp and the number of clicks.
"""
self.last_accessed = timezone.now()
self.clicks += 1
self.save(update_fields=['last_accessed', 'clicks'])
Expand Down
1 change: 0 additions & 1 deletion shortener/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
def generate_short_code(initial_length: int = 6, max_length: int = 10) -> str:
"""
Generate a random short code for the URL. Automatically increases length if no unique code is found.
:param initial_length: Starting length for the short code.
:param max_length: Maximum length the short code can grow to.
:return: A unique short code as a string.
Expand Down
7 changes: 4 additions & 3 deletions shortener/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.shortcuts import render, redirect, get_object_or_404
from .models import URL
from .utils import generate_short_code
from django.http import HttpRequest, HttpResponse

def shorten_url(request):
def shorten_url(request: HttpRequest) -> HttpResponse:
if request.method == 'POST':
# Retrieve the original URL from POST data
original_url = request.POST.get('original_url')
Expand All @@ -29,15 +30,15 @@ def shorten_url(request):
# Render the form if the request method is not POST
return render(request, 'shortener/index.html')

def shortened_url(request, short_code):
def shortened_url(request: HttpRequest, short_code: str) -> HttpResponse:
"""View to display the shortened URL."""

# Validate the short code exists in the database
get_object_or_404(URL, short_code=short_code)

return render(request, 'shortener/shortened.html', {'short_url': request.build_absolute_uri(f"/{short_code}")})

def redirect_url(request, short_code):
def redirect_url(request: HttpRequest, short_code: str) -> HttpResponse:
url = get_object_or_404(URL, short_code=short_code)

# Update the access information
Expand Down

0 comments on commit 0d49c8c

Please sign in to comment.