awesome-fastapi-projects/fastapi_projects/save.py

34 lines
1.1 KiB
Python
Raw Permalink Normal View History

2021-05-09 01:05:11 +00:00
from typing import List
from github.Repository import Repository
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.sqltypes import Integer
from fastapi_projects.database import SessionManager
from fastapi_projects.models import Dependency, Project
def create_project(repository: Repository, packages: List[str]) -> None:
project_params = {
"name": repository.full_name,
"clone_url": repository.clone_url,
"url": repository.html_url,
"pushed_at": repository.pushed_at,
}
project = Project(**project_params)
with SessionManager() as session:
try:
session.add(project)
session.commit()
except IntegrityError:
session.rollback()
for package in packages:
try:
dependency = Dependency(name=package)
project.dependencies.append(dependency)
session.add(project)
session.commit()
except IntegrityError:
session.rollback()
print([dependency.name for dependency in project.dependencies])