random pic api

This commit is contained in:
ClF3 2024-04-27 15:50:22 +08:00
parent aeb4b2e9ad
commit 2718113e2f
12 changed files with 41 additions and 1 deletions

3
.gitignore vendored
View File

@ -0,0 +1,3 @@
backend/randpic/__pycache__
backend/tianyi/__pycache__
backend/db.sqlite3

1
backend/jsons/pics.json Normal file

File diff suppressed because one or more lines are too long

View File

3
backend/randpic/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
backend/randpic/apps.py Normal file
View File

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

View File

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@ -0,0 +1,7 @@
import json
import random
def get_random_picture():
with open("./jsons/pics.json") as f:
pictures = json.load(f)
return random.choice(pictures)

3
backend/randpic/tests.py Normal file
View File

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

7
backend/randpic/urls.py Normal file
View File

@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]

6
backend/randpic/views.py Normal file
View File

@ -0,0 +1,6 @@
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .random_picture import get_random_picture
# Create your views here.
def index(request):
return redirect("https://cdn.clf3.org/tianyi-random/{}".format(get_random_picture()))

View File

@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import include,path
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('randpic/', include('randpic.urls')),
] ]