Add link to absolute url in list view

This commit is contained in:
Gabriel Augendre 2022-03-25 19:57:42 +01:00
parent e3b08694a3
commit d0dcaa28b6
5 changed files with 25 additions and 11 deletions

View file

@ -24,11 +24,11 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 21.12b0
rev: 22.1.0
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
rev: v2.31.1
hooks:
- id: pyupgrade
args:
@ -37,9 +37,9 @@ repos:
rev: 1.4.0
hooks:
- id: django-upgrade
args: [--target-version, "3.2"]
args: [--target-version, "4.0"]
- repo: https://github.com/rtts/djhtml
rev: v1.4.11
rev: v1.5.0
hooks:
- id: djhtml
- repo: https://github.com/flakeheaven/flakeheaven
@ -57,12 +57,12 @@ repos:
- flake8-noqa
- pep8-naming
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.5.1
rev: v2.6.1
hooks:
- id: prettier
types_or: [javascript, css]
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.7.0
rev: v8.11.0
hooks:
- id: eslint
types_or: [javascript, css]

4
poetry.lock generated
View file

@ -440,8 +440,8 @@ brotli = ["brotli"]
[metadata]
lock-version = "1.1"
python-versions = "^3.9"
content-hash = "66becd63613c6a294bb592257135a7f67fb03e4b9e0b1917ba9d6862b8316dd2"
python-versions = "^3.10"
content-hash = "b4862f3fb2a75ea6e86535e516d5d62d64b6eae9ca59b9d319d3bd89e9c2030e"
[metadata.files]
asgiref = [

View file

@ -5,7 +5,7 @@ description = ""
authors = ["Gabriel Augendre <gabriel@augendre.info>"]
[tool.poetry.dependencies]
python = "^3.9"
python = "^3.10"
requests = "^2.25.1"
gunicorn = "^20.1.0"
Django = "^4.0"
@ -25,7 +25,7 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.black]
target-version = ['py38']
target-version = ['py310']
[tool.isort]
profile = "black"

View file

@ -2,6 +2,7 @@ from django.contrib import admin
from django.contrib.auth import get_permission_codename
from django.contrib.auth.admin import UserAdmin
from django.http import HttpRequest
from django.utils.safestring import mark_safe
from redirect.models import Redirect
@ -10,7 +11,11 @@ from .models import RedirectUser
@admin.register(Redirect)
class RedirectAdmin(admin.ModelAdmin):
list_display = ["short_code", "owner", "group_owner", "target_url"]
list_display = ["short_code", "link", "owner", "group_owner", "target_url"]
def changelist_view(self, request, extra_context=None):
self.request = request
return super().changelist_view(request, extra_context)
def has_change_permission(self, request: HttpRequest, obj: Redirect = None):
opts = self.opts
@ -29,5 +34,10 @@ class RedirectAdmin(admin.ModelAdmin):
get_data["owner"] = request.user.pk
return get_data
def link(self, instance: Redirect) -> str:
url = instance.get_absolute_url()
url = self.request.build_absolute_uri(url)
return mark_safe(f'<a href="{url}">link</a>')
admin.site.register(RedirectUser, UserAdmin)

View file

@ -1,6 +1,7 @@
from django.conf import settings
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
from django.urls import reverse
class Redirect(models.Model):
@ -30,6 +31,9 @@ class Redirect(models.Model):
def __str__(self):
return f"{self.short_code} => {self.target_url}"
def get_absolute_url(self):
return reverse("redirect", args=(self.short_code,))
class RedirectUser(AbstractUser):
pass