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>
{% else %}
<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>
</button>
{% endif %}</p>

View File

@ -6,7 +6,7 @@
<div class="m-2">
<h4>Link to: <a class="fs-4" href="{{ link.source }}">{{ link.full_source }}</a></h4>
<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>
</button></p>
</div>

View File

@ -5,7 +5,7 @@
{% block content %}
<div class="m-2">
<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>
</button></p>
<p>Viewed: {{ link.viewed }} times</p>

View File

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

View File

@ -1,6 +1,14 @@
from importlib import import_module
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.users.models import User
engine = import_module(settings.SESSION_ENGINE)
sessionstore = engine.SessionStore
@shared_task
@ -8,7 +16,14 @@ def save_view_meta(pk, ip, user_agent, user_id):
link = Link.objects.get(pk=pk)
meta = LinkViewMeta(link=link, ip=ip, user_agent=user_agent)
if user_id:
if type(user_id) is int:
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()
link.viewed += 1
link.save(update_fields=["viewed"])

View File

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

View File

@ -55,5 +55,9 @@
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#task-eager-propagates
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
import django
from django.conf import settings as django_settings
from fastapi import Cookie, Depends, FastAPI, Header
from fastapi.responses import HTMLResponse, RedirectResponse
from sqlalchemy.orm import Session
@ -20,6 +21,8 @@
save_view_meta,
)
settings.relative_base = django_settings.SHORTENER_REDIRECT_TO
@app.exception_handler(LinkNotFoundException)
async def unicorn_exception_handler(request: Request, exc: LinkNotFoundException):