From c089f330508b40914bc468e548ae7e80f13de908 Mon Sep 17 00:00:00 2001 From: Angus Goody Date: Fri, 22 Nov 2024 15:39:57 +0000 Subject: [PATCH] Admin (#5) * added url admin page * added new fields * updated admin ui --- shortener/admin.py | 12 ++++++++++ .../0002_url_clicks_url_last_accessed.py | 23 +++++++++++++++++++ shortener/models.py | 11 +++++++++ shortener/views.py | 5 ++++ 4 files changed, 51 insertions(+) create mode 100644 shortener/migrations/0002_url_clicks_url_last_accessed.py diff --git a/shortener/admin.py b/shortener/admin.py index 8c38f3f..22bff5b 100644 --- a/shortener/admin.py +++ b/shortener/admin.py @@ -1,3 +1,15 @@ from django.contrib import admin +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') diff --git a/shortener/migrations/0002_url_clicks_url_last_accessed.py b/shortener/migrations/0002_url_clicks_url_last_accessed.py new file mode 100644 index 0000000..7dcf83a --- /dev/null +++ b/shortener/migrations/0002_url_clicks_url_last_accessed.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.3 on 2024-11-22 15:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('shortener', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='url', + name='clicks', + field=models.PositiveIntegerField(default=0), + ), + migrations.AddField( + model_name='url', + name='last_accessed', + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/shortener/models.py b/shortener/models.py index df3cb72..6ebcb5b 100644 --- a/shortener/models.py +++ b/shortener/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.utils import timezone # Create your models here. @@ -7,5 +8,15 @@ class URL(models.Model): short_code = models.CharField(max_length=10, unique=True) created_at = models.DateTimeField(auto_now_add=True) + last_accessed = models.DateTimeField(null=True, blank=True) + clicks = models.PositiveIntegerField(default=0) + def __str__(self): return f'{self.short_code} -> {self.original_url}' + + # Method to update access info + def record_access(self): + self.last_accessed = timezone.now() + self.clicks += 1 + self.save(update_fields=['last_accessed', 'clicks']) + diff --git a/shortener/views.py b/shortener/views.py index 2bc27ba..4234cbf 100644 --- a/shortener/views.py +++ b/shortener/views.py @@ -39,4 +39,9 @@ def shortened_url(request, short_code): def redirect_url(request, short_code): url = get_object_or_404(URL, short_code=short_code) + + # Update the access information + url.record_access() + + # Redirect to the original URL return redirect(url.original_url)