Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/models #8

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions alembic/versions/16786c7048d6_added_default_role.py

This file was deleted.

32 changes: 0 additions & 32 deletions alembic/versions/41b55aea7689_choice_fix.py

This file was deleted.

32 changes: 0 additions & 32 deletions alembic/versions/d532edc24b0c_username.py

This file was deleted.

156 changes: 156 additions & 0 deletions alembic/versions/f880265a392e_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
"""models

Revision ID: f880265a392e
Revises: 4098f76764d4
Create Date: 2023-12-09 05:40:57.506690

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'f880265a392e'
down_revision = '4098f76764d4'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('achievement',
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('course',
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('examination',
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('group',
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('notification',
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tariff',
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('task',
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
op.create_table('course_tariff_association',
sa.Column('course_id', sa.Integer(), nullable=True),
sa.Column('tariff_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['course_id'], ['course.id'], ),
sa.ForeignKeyConstraint(['tariff_id'], ['tariff.id'], )
)
op.create_table('course_task_association',
sa.Column('course_id', sa.Integer(), nullable=True),
sa.Column('task_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['course_id'], ['course.id'], ),
sa.ForeignKeyConstraint(['task_id'], ['task.id'], )
)
op.create_table('group_user_association',
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('group_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['group_id'], ['group.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], )
)
op.create_table('profile',
sa.Column('first_name', sa.String(length=100), nullable=True),
sa.Column('second_name', sa.String(length=100), nullable=True),
sa.Column('age', sa.SmallInteger(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('user_notification_association',
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('notification_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['notification_id'], ['notification.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], )
)
op.create_table('usercourse',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('course_id', sa.Integer(), nullable=False),
sa.Column('progress', sa.SmallInteger(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['course_id'], ['course.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('user_id', 'course_id', 'id')
)
op.create_table('userexamination',
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('examination_id', sa.Integer(), nullable=False),
sa.Column('progress', sa.SmallInteger(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['examination_id'], ['examination.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('user_id', 'examination_id', 'id')
)
op.create_table('achievement_profile_association',
sa.Column('profile_id', sa.Integer(), nullable=True),
sa.Column('achievement_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['achievement_id'], ['achievement.id'], ),
sa.ForeignKeyConstraint(['profile_id'], ['profile.id'], )
)
with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.add_column(sa.Column('role', sa.Enum('user', 'manager', 'admin', name='userroleenum'), nullable=False))
batch_op.add_column(sa.Column('username', sa.String(length=100), nullable=False))
batch_op.add_column(sa.Column('tariff_id', sa.Integer(), nullable=True))
batch_op.create_foreign_key('fk_user_tariff_id_tariff', 'tariff', ['tariff_id'], ['id'])

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.drop_constraint(None, type_='foreignkey')
batch_op.drop_column('tariff_id')
batch_op.drop_column('username')
batch_op.drop_column('role')

op.drop_table('achievement_profile_association')
op.drop_table('userexamination')
op.drop_table('usercourse')
op.drop_table('user_notification_association')
op.drop_table('profile')
op.drop_table('group_user_association')
op.drop_table('course_task_association')
op.drop_table('course_tariff_association')
op.drop_table('task')
op.drop_table('tariff')
op.drop_table('notification')
op.drop_table('group')
op.drop_table('examination')
op.drop_table('course')
op.drop_table('achievement')
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions app/api/endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .user import router as user_router # noqa
from .group import router as group_router # noqa
34 changes: 34 additions & 0 deletions app/api/endpoints/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import List

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.schemas.group import GroupRead, GroupCreate
from app.core.db import get_async_session
from app.crud import group_crud

router = APIRouter()


@router.get('/', response_model=List[GroupRead])
async def get_all_groups(
session: AsyncSession = Depends(get_async_session)
) -> List[GroupRead]:
"""Возвращает все группы."""
return await group_crud.get_multi(session)


@router.post('/', response_model=GroupRead)
async def create_group(
group: GroupCreate,
session: AsyncSession = Depends(get_async_session)
):
"""Создать группу""" # For admin?
# TODO Check name duplicate
group = await group_crud.create(
obj_in=group, session=session
)
session.add(group)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

внутри метода базвого круда уже есть add, commit и refresh
crud/base: 35-37

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

группы проверял. создавал. но связь с юзером не проверял. тут понять какая ручка будет. К юзеру добавлем группу или к группе юзера? И кто это делает: сам юзер или менеджер или оба могут сделать?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

я в новом пр поправил это дело,
У нас же многие ко многим через связующую модельку )

await session.commit()
await session.refresh(group)
return group
5 changes: 4 additions & 1 deletion app/api/routers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from fastapi import APIRouter

from app.api.endpoints import (
user_router,
user_router, group_router
)

main_router = APIRouter()

main_router.include_router(user_router)
main_router.include_router(
group_router, prefix='/group', tags=['Group']
)
4 changes: 3 additions & 1 deletion app/core/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ async def create_user(
async with get_user_manager_context(user_db) as user_manager:
await user_manager.create(
UserCreate(
username='admin',
email=email, password=password,
is_superuser=is_superuser
is_superuser=is_superuser,
role='admin',
)
)

Expand Down
1 change: 1 addition & 0 deletions app/crud/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .group import group_crud # noqa
9 changes: 9 additions & 0 deletions app/crud/group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from app.crud.base import CRUDBase
from app.models.group import Group


class CRUDGroup(CRUDBase):
pass


group_crud = CRUDGroup(Group)
8 changes: 8 additions & 0 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
from .user import User # noqa
from .group import Group # noqa
from .achievements import Achievement # noqa
from .courses import Course # noqa
from .examinations import Examination # noqa
from .notifications import Notification # noqa
from .profiles import Profile # noqa
from .tariffs import Tariff # noqa
from .tasks import Task # noqa
13 changes: 13 additions & 0 deletions app/models/achievements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from sqlalchemy.orm import relationship

from app.core.db import Base
from app.models.base import AbstractBaseMixin
from app.models.associations import achievement_profile_association


class Achievement(AbstractBaseMixin, Base):
profiles = relationship(
'Profile',
secondary=achievement_profile_association,
back_populates='profiles'
)
Loading