Initial commit

This commit is contained in:
2023-11-22 16:01:05 +03:00
commit b661bb83f9
25 changed files with 571 additions and 0 deletions

0
links/__init__.py Normal file
View File

14
links/admin.py Normal file
View File

@@ -0,0 +1,14 @@
from django.contrib import admin
from links.models import Link
# Register your models here.
class LinkAdmin(admin.ModelAdmin):
list_display = ("name", "url")
search_fields = ['name', 'url']
admin.site.register(Link, LinkAdmin)

6
links/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class LinksConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'links'

View File

@@ -0,0 +1,22 @@
# Generated by Django 4.2.7 on 2023-11-22 05:30
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Link',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
('url', models.CharField(max_length=1024)),
],
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-22 09:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('links', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='link',
name='name',
field=models.CharField(max_length=10, unique=True),
),
]

View File

6
links/models.py Normal file
View File

@@ -0,0 +1,6 @@
from django.db import models
class Link(models.Model):
name = models.CharField(max_length=10, unique=True)
url = models.CharField(max_length=1024)

9
links/serializers.py Normal file
View File

@@ -0,0 +1,9 @@
from rest_framework import serializers
from links.models import Link
class LinkSerializer(serializers.ModelSerializer):
class Meta:
model = Link
fields = '__all__'

3
links/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
links/urls.py Normal file
View File

@@ -0,0 +1,11 @@
from django.urls import path, include
from rest_framework import routers
from links.views import LinkViewSet
router = routers.DefaultRouter()
router.register(r"", LinkViewSet, basename="links")
urlpatterns = [
path("", include(router.urls))
]

38
links/views.py Normal file
View File

@@ -0,0 +1,38 @@
from requests import post
from rest_framework import viewsets
from link_backend.settings import ENABLE_TELEGRAM_BOT, TELEGRAM_CHAT_ID, TELEGRAM_BOT_TOKEN
from links.serializers import LinkSerializer
from links.models import Link
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=Link)
def new_link_notification(sender, instance, created, **kwargs):
if created:
if ENABLE_TELEGRAM_BOT:
print("test")
response = post(
url=f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
json={
"chat_id": TELEGRAM_CHAT_ID,
"text": f"""<b>🔗 Link created!</b>
📛 Link shorted name: <b>{instance.name}</b>
🖼️ Original URL: <b>{instance.url}</b>""",
"parse_mode": "HTML"
}
)
print(response.text)
class LinkViewSet(viewsets.ModelViewSet):
serializer_class = LinkSerializer
def get_queryset(self):
link_name = self.request.query_params.get("link")
if link_name:
return Link.objects.filter(name=link_name)
return []