updated shortner to show and process new host

This commit is contained in:
Alexander Karpov 2023-08-10 17:25:10 +03:00
parent 21c02df7f0
commit ec120e5883
8 changed files with 37 additions and 7 deletions

View File

@ -51,7 +51,7 @@
<p>File is private</p> <p>File is private</p>
{% else %} {% else %}
<p>File is public{% if file.short_link %}, <p>File is public{% if file.short_link %},
<a href="{{ file.get_short_link }}">short link</a><button class="btn" data-clipboard-text="{{ request.get_host }}{{ file.get_short_link }}"> <a href="{{ file.get_short_link }}">short link</a><button class="btn" data-clipboard-text="{{ file.get_short_link }}">
<i style="font-size: 0.8em" class="bi bi-clipboard ml-2"></i> <i style="font-size: 0.8em" class="bi bi-clipboard ml-2"></i>
</button> </button>
{% endif %}</p> {% endif %}</p>

View File

@ -6,7 +6,7 @@
<div class="m-2"> <div class="m-2">
<h4>Link to: <a class="fs-4" href="{{ link.source }}">{{ link.full_source }}</a></h4> <h4>Link to: <a class="fs-4" href="{{ link.source }}">{{ link.full_source }}</a></h4>
<p>Issued by: {% user_badge link.creator %}</p> <p>Issued by: {% user_badge link.creator %}</p>
<p>{{ request.get_host }}{% url 'short_url' slug=link.slug %} <button class="btn" data-clipboard-text="{{ request.get_host }}{% url 'short_url' slug=link.slug %}"> <p>{{ link.outer_host_link }} <button class="btn" data-clipboard-text="{{ link.outer_host_link }}">
<i style="font-size: 0.8em" class="bi bi-clipboard ml-2"></i> <i style="font-size: 0.8em" class="bi bi-clipboard ml-2"></i>
</button></p> </button></p>
</div> </div>

View File

@ -5,7 +5,7 @@
{% block content %} {% block content %}
<div class="m-2"> <div class="m-2">
<h4>Link to: <a class="fs-4" href="{{ link.source }}">{{ link.full_source }}</a></h4> <h4>Link to: <a class="fs-4" href="{{ link.source }}">{{ link.full_source }}</a></h4>
<p>{{ request.get_host }}{% url 'short_url' slug=link.slug %} <button class="btn" data-clipboard-text="{{ request.get_host }}{% url 'short_url' slug=link.slug %}"> <p>{{ link.outer_host_link }} <button class="btn" data-clipboard-text="{{ link.outer_host_link }}">
<i style="font-size: 0.8em" class="bi bi-clipboard ml-2"></i> <i style="font-size: 0.8em" class="bi bi-clipboard ml-2"></i>
</button></p> </button></p>
<p>Viewed: {{ link.viewed }} times</p> <p>Viewed: {{ link.viewed }} times</p>

View File

@ -1,5 +1,6 @@
from abc import abstractmethod from abc import abstractmethod
from django.conf import settings
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
@ -17,10 +18,14 @@ class Link(TimeStampedModel):
viewed = models.IntegerField(default=0) viewed = models.IntegerField(default=0)
@property
def outer_host_link(self):
return settings.SHORTENER_HOST + "/" + self.slug
@property @property
def full_source(self): def full_source(self):
return ( return (
"https://akarpov.ru" + self.source settings.SHORTENER_REDIRECT_TO + self.source
if self.source.startswith("/") if self.source.startswith("/")
else self.source else self.source
) )
@ -136,7 +141,7 @@ def get_absolute_url(self):
@property @property
def get_short_link(self) -> str: def get_short_link(self) -> str:
if self.short_link: if self.short_link:
return reverse("short_url", kwargs={"slug": self.short_link.slug}) return self.short_link.outer_host_link
return reverse("tools:shortener:revoked") return reverse("tools:shortener:revoked")
class Meta: class Meta:

View File

@ -1,6 +1,14 @@
from importlib import import_module
from celery import shared_task from celery import shared_task
from django.conf import settings
from django.contrib.sessions.models import Session
from akarpov.tools.shortener.models import Link, LinkViewMeta from akarpov.tools.shortener.models import Link, LinkViewMeta
from akarpov.users.models import User
engine = import_module(settings.SESSION_ENGINE)
sessionstore = engine.SessionStore
@shared_task @shared_task
@ -8,7 +16,14 @@ def save_view_meta(pk, ip, user_agent, user_id):
link = Link.objects.get(pk=pk) link = Link.objects.get(pk=pk)
meta = LinkViewMeta(link=link, ip=ip, user_agent=user_agent) meta = LinkViewMeta(link=link, ip=ip, user_agent=user_agent)
if user_id: if user_id:
if type(user_id) is int:
meta.user_id = user_id meta.user_id = user_id
elif type(user_id) is str:
try:
session = sessionstore(user_id)
meta.user_id = session["_auth_user_id"]
except (Session.DoesNotExist, User.DoesNotExist, KeyError):
pass
meta.save() meta.save()
link.viewed += 1 link.viewed += 1
link.save(update_fields=["viewed"]) link.save(update_fields=["viewed"])

View File

@ -524,6 +524,9 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
SHORTENER_ADD_SLUG = True SHORTENER_ADD_SLUG = True
SHORTENER_SLUG_LENGTH = 3 SHORTENER_SLUG_LENGTH = 3
# let nginx do some magic here
SHORTENER_REDIRECT_TO = "https://akarpov.ru"
SHORTENER_HOST = "https://akarpov.ru"
# ACTIVE_LINK # ACTIVE_LINK

View File

@ -55,5 +55,9 @@
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#task-eager-propagates # https://docs.celeryq.dev/en/stable/userguide/configuration.html#task-eager-propagates
CELERY_TASK_EAGER_PROPAGATES = True CELERY_TASK_EAGER_PROPAGATES = True
# Your stuff...
# SHORTENER
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
SHORTENER_REDIRECT_TO = "http://127.0.0.1:8000"
SHORTENER_HOST = "http://127.0.0.1:3000"

View File

@ -2,6 +2,7 @@
from typing import Annotated from typing import Annotated
import django import django
from django.conf import settings as django_settings
from fastapi import Cookie, Depends, FastAPI, Header from fastapi import Cookie, Depends, FastAPI, Header
from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.responses import HTMLResponse, RedirectResponse
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
@ -20,6 +21,8 @@
save_view_meta, save_view_meta,
) )
settings.relative_base = django_settings.SHORTENER_REDIRECT_TO
@app.exception_handler(LinkNotFoundException) @app.exception_handler(LinkNotFoundException)
async def unicorn_exception_handler(request: Request, exc: LinkNotFoundException): async def unicorn_exception_handler(request: Request, exc: LinkNotFoundException):