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

4
poetry.lock generated
View file

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

View file

@ -5,7 +5,7 @@ description = ""
authors = ["Gabriel Augendre <gabriel@augendre.info>"] authors = ["Gabriel Augendre <gabriel@augendre.info>"]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.9" python = "^3.10"
requests = "^2.25.1" requests = "^2.25.1"
gunicorn = "^20.1.0" gunicorn = "^20.1.0"
Django = "^4.0" Django = "^4.0"
@ -25,7 +25,7 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api" build-backend = "poetry.core.masonry.api"
[tool.black] [tool.black]
target-version = ['py38'] target-version = ['py310']
[tool.isort] [tool.isort]
profile = "black" 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 import get_permission_codename
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from django.http import HttpRequest from django.http import HttpRequest
from django.utils.safestring import mark_safe
from redirect.models import Redirect from redirect.models import Redirect
@ -10,7 +11,11 @@ from .models import RedirectUser
@admin.register(Redirect) @admin.register(Redirect)
class RedirectAdmin(admin.ModelAdmin): 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): def has_change_permission(self, request: HttpRequest, obj: Redirect = None):
opts = self.opts opts = self.opts
@ -29,5 +34,10 @@ class RedirectAdmin(admin.ModelAdmin):
get_data["owner"] = request.user.pk get_data["owner"] = request.user.pk
return get_data 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) admin.site.register(RedirectUser, UserAdmin)

View file

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