Skip to content

Commit

Permalink
[v1.0.5] Merge pull request #21 from KageRyo/develop
Browse files Browse the repository at this point in the history
Update to v1.0.5
  • Loading branch information
KageRyo authored Jul 30, 2024
2 parents b8e28ee + 17ac816 commit a9d85ea
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ create_date : datetime.datetime # 創建日期
- /api/short-url
- 提供使用者創建新的短網址
- 創建邏輯為隨機生成 6 位數的英數亂碼,並檢查是否已經存在於資料庫,若無則建立其與原網址的關聯
- /api/custom-url
- /api/custom-url/
- 提供使用者自訂新的短網址
### GET
- /api/ (root)
- 可提供用於測試與 API 的連線狀態使用
- /api/orign-url
- /api/orign-url/{short_string}
- 提供使用者以短網址查詢原網址
- /api/all-url
- 提供查詢目前所有已被建立的短網址
### DELETE
- /api/short-url
- /api/short-url/{short_string}
- 提供使用者刪除指定的短網址
- /api/expire-url
- 刪除過期的短網址

## 如何在本地架設 RyoURL 環境
1. 您必須先將此專案 Clone 到您的環境
Expand Down
2 changes: 1 addition & 1 deletion RyoURL/shortURL/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from .models import Url

class UrlAdmin(admin.ModelAdmin):
list_display = ('orign_url', 'short_string', 'short_url', 'create_date', 'visit_count')
list_display = ('orign_url', 'short_string', 'short_url', 'create_date', 'expire_date', 'visit_count')

admin.site.register(Url, UrlAdmin)
25 changes: 17 additions & 8 deletions RyoURL/shortURL/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import datetime
import requests

from typing import List
from typing import List, Optional
from pydantic import HttpUrl, AnyUrl

from ninja import NinjaAPI, Schema
Expand Down Expand Up @@ -35,6 +35,7 @@ class UrlSchema(Schema):
short_string: str
short_url: HttpUrl
create_date: datetime.datetime
expire_date: Optional[datetime.datetime]
visit_count: int

# 定義錯誤回應的 Schema 類別
Expand All @@ -55,12 +56,13 @@ def handle_domain(request, short_string):
return f'{domain}/{short_string}'

# 建立短網址物件的函式
def create_url_entry(orign_url: HttpUrl, short_string: str, short_url: HttpUrl) -> Url:
def create_url_entry(orign_url: HttpUrl, short_string: str, short_url: HttpUrl, expire_date: Optional[datetime.datetime] = None) -> Url:
return Url.objects.create(
orign_url = str(orign_url),
short_string = short_string,
short_url = str(short_url),
create_date = datetime.datetime.now()
create_date = datetime.datetime.now(),
expire_date = expire_date
)

# GET : 首頁 API /
Expand All @@ -70,20 +72,20 @@ def index(request):

# POST : 新增短網址 API /short_url
@api.post("short-url", response={200: UrlSchema, 404: ErrorSchema})
def create_short_url(request, orign_url: HttpUrl):
def create_short_url(request, orign_url: HttpUrl, expire_date: Optional[datetime.datetime] = None):
short_string = generator_short_url()
short_url = HttpUrl(handle_domain(request, short_string))
url = create_url_entry(orign_url, short_string, short_url)
url = create_url_entry(orign_url, short_string, short_url, expire_date)
return 200, url

# POST : 新增自訂短網址 API /custom_url
@api.post("custom-url", response={200: UrlSchema, 403: ErrorSchema})
def create_custom_url(request, orign_url: HttpUrl, short_string: str):
def create_custom_url(request, orign_url: HttpUrl, short_string: str, expire_date: Optional[datetime.datetime] = None):
short_url = HttpUrl(handle_domain(request, short_string))
if Url.objects.filter(short_url=str(short_url)).exists():
return 403, {"message": "自訂短網址已存在,請更換其他短網址。"}
else:
url = create_url_entry(orign_url, short_string, short_url)
url = create_url_entry(orign_url, short_string, short_url, expire_date)
return 200, url

# GET : 以縮短網址字符查詢原網址 API /orign_url/{short_string}
Expand All @@ -103,4 +105,11 @@ def get_all_url(request):
def delete_short_url(request, short_string: str):
url = get_object_or_404(Url, short_string=short_string)
url.delete()
return 200, {"message": "成功刪除!"}
return 200, {"message": "成功刪除!"}

# DELETE : 刪除過期短網址 API /expire_url
@api.delete('expire-url', response={200: ErrorSchema})
def delete_expire_url(request):
url = Url.objects.filter(expire_date__lt=datetime.datetime.now())
url.delete()
return 200, {"message": "成功刪除過期的短網址!"}
18 changes: 18 additions & 0 deletions RyoURL/shortURL/migrations/0006_url_expire_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2 on 2024-07-30 08:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('shortURL', '0005_url_visit_count'),
]

operations = [
migrations.AddField(
model_name='url',
name='expire_date',
field=models.DateTimeField(blank=True, null=True),
),
]
1 change: 1 addition & 0 deletions RyoURL/shortURL/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class Url(models.Model):
short_string = models.CharField(max_length=10, unique=True, default='NULL') # 短網址的字符串
short_url = models.URLField() # 完整的短網址
create_date = models.DateTimeField(default=datetime.datetime.now) # 創建日期
expire_date = models.DateTimeField(null=True, blank=True) # 過期日期
visit_count = models.IntegerField(default=0) # 訪問次數
11 changes: 11 additions & 0 deletions RyoURL/shortURL/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
from django.http import HttpResponse
from .models import Url

# 將短網址導向原網址的函式
def redirectShortUrl(request, short_string):
url = get_object_or_404(Url, short_string=short_string)

# 檢查短網址是否已過期
if url.expire_date and url.expire_date < timezone.now():
url.delete()
return HttpResponse("此短網址已過期並已被刪除。", status=404)

# 更新訪問次數
url.visit_count += 1
url.save()

# 進行重定向
return redirect(url.orign_url)

0 comments on commit a9d85ea

Please sign in to comment.