-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kougen/feature/add-dynamic-models
Feature/add dynamic models
- Loading branch information
Showing
9 changed files
with
184 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Upload Python Package | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v3 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install build | ||
- name: Build package | ||
run: python -m build | ||
- name: Publish package | ||
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 | ||
with: | ||
user: __token__ | ||
password: ${{ secrets.PYPI_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
venv/ | ||
__pycache__/ | ||
.idea/ | ||
|
||
data/ | ||
|
||
dist/ | ||
build/ | ||
*.egg-info/ | ||
*.egg/ | ||
*.log | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[project] | ||
name = "fastapi-crud" | ||
version = "3.1.0" | ||
version = "1.0.0" | ||
authors = [ | ||
{ name="kougen", email="[email protected]" }, | ||
] | ||
|
@@ -21,6 +21,7 @@ classifiers = [ | |
dependencies = [ | ||
'fastapi', | ||
'uvicorn', | ||
'pyrepositories', | ||
] | ||
|
||
[project.urls] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from fastapi import FastAPI, Query | ||
from pydantic import BaseModel | ||
import uvicorn | ||
from typing import List, Optional | ||
from pyrepositories import JsonTable, DataSource, Entity, IdTypes | ||
|
||
|
||
path_root = Path(__file__).parents[1] | ||
sys.path.append(os.path.join(path_root, 'src')) | ||
|
||
from fastapi_crud import CRUDApi, Model, EntityFactory | ||
|
||
|
||
class Organizer(Model): | ||
email: str | ||
|
||
|
||
class Joiner(Model): | ||
name: str | ||
company: str | ||
|
||
|
||
class Event(Model): | ||
date: str | ||
organizer: Organizer | ||
status: str | ||
max_attendees: int | ||
joiners: Optional[List[Joiner]] = None | ||
|
||
|
||
class EventEntity(Entity): | ||
@property | ||
def date(self): | ||
return self.fields.get("date") | ||
@date.setter | ||
def date(self, value): | ||
self.fields["date"] = value | ||
@property | ||
def organizer(self): | ||
return self.fields.get("organizer") | ||
@organizer.setter | ||
def organizer(self, value): | ||
self.fields["organizer"] = value | ||
@property | ||
def status(self): | ||
return self.fields.get("status") | ||
@status.setter | ||
def status(self, value): | ||
self.fields["status"] = value | ||
@property | ||
def max_attendees(self): | ||
return self.fields.get("max_attendees") | ||
@max_attendees.setter | ||
def max_attendees(self, value): | ||
self.fields["max_attendees"] = value | ||
@property | ||
def joiners(self): | ||
return self.fields.get("joiners") or [] | ||
@joiners.setter | ||
def joiners(self, value): | ||
self.fields["joiners"] = value | ||
|
||
|
||
|
||
app = FastAPI() | ||
|
||
ds = DataSource(auto_increment=True, id_type=IdTypes.UUID) | ||
t = JsonTable("event", os.path.join(path_root, "data")) | ||
filters = { "date": (str, ""), "organizer": (str, ""), "status": (str, ""), "event_type": (str, ""), } | ||
t.set_filter_fields(filters) | ||
|
||
ds.add_table(t) | ||
api = CRUDApi(ds, app) | ||
|
||
api.add_router("event" , Event) | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="0.0.0.0", port=1112) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .app import CRUDApi | ||
from .datasource import DataSource, DataTable, FilterField | ||
from .app import CRUDApi, CRUDApiRouter | ||
from .lib import Model | ||
from .entities import EntityFactory | ||
|
||
__all__ = ['CRUDApi', 'DataSource', 'DataTable'] | ||
__all__ = ['CRUDApi', 'CRUDApiRouter', 'Model', 'EntityFactory'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pydantic import BaseModel | ||
from pyrepositories import Entity | ||
|
||
|
||
class EntityFactory: | ||
def convert_model(self, model: BaseModel) -> Entity: | ||
fields = model.model_dump() | ||
if 'id' in fields: | ||
entity = Entity(id=fields['id']) | ||
entity.fields = fields | ||
print("Override this method to create an entity") | ||
return Entity() | ||
|
||
def create_entity(self, fields: dict) -> Entity: | ||
entity = Entity(fields.get('id')) | ||
entity.fields = fields | ||
return entity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
from pyrepositories import IdTypes | ||
|
||
class Model(BaseModel): | ||
pass |
This file was deleted.
Oops, something went wrong.