mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2025-08-16 03:40:26 +08:00
add Visitor in example
This commit is contained in:
12
examples/common.py
Normal file
12
examples/common.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
|
|
||||||
|
def get_client_ip(request: Request):
|
||||||
|
"""
|
||||||
|
:param request:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
forwarded = request.headers.get("X-Forwarded-For")
|
||||||
|
if forwarded:
|
||||||
|
return forwarded.split(",")[0]
|
||||||
|
return request.client.host
|
@ -17,6 +17,21 @@
|
|||||||
SET NAMES utf8mb4;
|
SET NAMES utf8mb4;
|
||||||
SET FOREIGN_KEY_CHECKS = 0;
|
SET FOREIGN_KEY_CHECKS = 0;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Table structure for category
|
||||||
|
-- ----------------------------
|
||||||
|
DROP TABLE IF EXISTS `visitor`;
|
||||||
|
CREATE TABLE `visitor`
|
||||||
|
(
|
||||||
|
`id` int NOT NULL AUTO_INCREMENT,
|
||||||
|
`ip` varchar(200) NOT NULL UNIQUE,
|
||||||
|
`updated_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
|
||||||
|
`created_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE = InnoDB
|
||||||
|
DEFAULT CHARSET = utf8mb4
|
||||||
|
COLLATE = utf8mb4_general_ci;
|
||||||
|
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Table structure for category
|
-- Table structure for category
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
|
@ -3,10 +3,14 @@ import os
|
|||||||
import uvicorn
|
import uvicorn
|
||||||
from fastapi import Depends, FastAPI
|
from fastapi import Depends, FastAPI
|
||||||
from starlette.middleware.cors import CORSMiddleware
|
from starlette.middleware.cors import CORSMiddleware
|
||||||
|
from starlette.requests import Request
|
||||||
from starlette.templating import Jinja2Templates
|
from starlette.templating import Jinja2Templates
|
||||||
from tortoise.contrib.fastapi import register_tortoise
|
from tortoise.contrib.fastapi import register_tortoise
|
||||||
from tortoise.contrib.pydantic import pydantic_queryset_creator
|
from tortoise.contrib.pydantic import pydantic_queryset_creator
|
||||||
|
from tortoise.exceptions import IntegrityError
|
||||||
|
|
||||||
|
from examples.common import get_client_ip
|
||||||
|
from examples.models import Visitor
|
||||||
from fastapi_admin.depends import get_model
|
from fastapi_admin.depends import get_model
|
||||||
from fastapi_admin.factory import app as admin_app
|
from fastapi_admin.factory import app as admin_app
|
||||||
from fastapi_admin.schemas import BulkIn
|
from fastapi_admin.schemas import BulkIn
|
||||||
@ -52,6 +56,18 @@ def create_app():
|
|||||||
app = create_app()
|
app = create_app()
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def add_process_time_header(request: Request, call_next):
|
||||||
|
ip = get_client_ip(request)
|
||||||
|
v = await Visitor.get_or_none(ip=ip)
|
||||||
|
try:
|
||||||
|
await v.save() if v else await Visitor.create(ip=ip)
|
||||||
|
except IntegrityError:
|
||||||
|
pass
|
||||||
|
response = await call_next(request)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def start_up():
|
async def start_up():
|
||||||
await admin_app.init( # nosec
|
await admin_app.init( # nosec
|
||||||
@ -89,6 +105,9 @@ async def start_up():
|
|||||||
icon="fa fa-table",
|
icon="fa fa-table",
|
||||||
search_fields=("name",),
|
search_fields=("name",),
|
||||||
),
|
),
|
||||||
|
Menu(
|
||||||
|
name="Visitor", url="/rest/Visitor", icon="fa fa-child", exclude=("ip",)
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Menu(
|
Menu(
|
||||||
|
@ -82,3 +82,9 @@ class Config(Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.pk}#{self.label}"
|
return f"{self.pk}#{self.label}"
|
||||||
|
|
||||||
|
|
||||||
|
class Visitor(Model):
|
||||||
|
ip = fields.CharField(max_length=200, unique=True)
|
||||||
|
updated_at = fields.DatetimeField(auto_now=True)
|
||||||
|
created_at = fields.DatetimeField(auto_now_add=True)
|
||||||
|
Reference in New Issue
Block a user