Skip to content

Commit

Permalink
Сделал ручки post, get всех локалей и get по id
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Abramov committed Jun 21, 2024
1 parent 368d5e0 commit 340b295
Show file tree
Hide file tree
Showing 11 changed files with 956 additions and 1 deletion.
32 changes: 32 additions & 0 deletions alembic/versions/b508b9e595d0_language_unique_true.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""language unique true
Revision ID: b508b9e595d0
Revises: c291b9a2fbe5
Create Date: 2024-06-22 01:32:34.346560
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'b508b9e595d0'
down_revision = 'c291b9a2fbe5'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('locale', schema=None) as batch_op:
batch_op.create_unique_constraint('language_unique_constraint', ['language'])

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('locale', schema=None) as batch_op:
batch_op.drop_constraint('language_unique_constraint', type_='unique')

# ### end Alembic commands ###
182 changes: 182 additions & 0 deletions alembic/versions/c291b9a2fbe5_locale_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
"""locale models
Revision ID: c291b9a2fbe5
Revises: 132f5d88e682
Create Date: 2024-06-19 22:04:55.856481
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c291b9a2fbe5'
down_revision = '132f5d88e682'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('locale',
sa.Column('language', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('auth',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('welcome', sa.String(), nullable=False),
sa.Column('auth_title1', sa.String(), nullable=False),
sa.Column('auth_title2', sa.String(), nullable=False),
sa.Column('forgot_password', sa.String(), nullable=False),
sa.Column('enter', sa.String(), nullable=False),
sa.Column('register_', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('common',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('all_notification', sa.String(), nullable=False),
sa.Column('profile', sa.String(), nullable=False),
sa.Column('ok', sa.String(), nullable=False),
sa.Column('exit', sa.String(), nullable=False),
sa.Column('cancel', sa.String(), nullable=False),
sa.Column('main', sa.String(), nullable=False),
sa.Column('tasks', sa.String(), nullable=False),
sa.Column('subscription', sa.String(), nullable=False),
sa.Column('account', sa.String(), nullable=False),
sa.Column('notifications', sa.String(), nullable=False),
sa.Column('help', sa.String(), nullable=False),
sa.Column('contacts', sa.String(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('email_placeholder', sa.String(), nullable=False),
sa.Column('login', sa.String(), nullable=False),
sa.Column('login_placeholder', sa.String(), nullable=False),
sa.Column('password', sa.String(), nullable=False),
sa.Column('password_placeholder', sa.String(), nullable=False),
sa.Column('message', sa.String(), nullable=False),
sa.Column('message_placeholder', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('contacts',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('support', sa.String(), nullable=False),
sa.Column('text', sa.String(), nullable=False),
sa.Column('our_contacts', sa.String(), nullable=False),
sa.Column('phones', sa.String(), nullable=False),
sa.Column('phone1', sa.String(), nullable=False),
sa.Column('phone2', sa.String(), nullable=False),
sa.Column('select_label', sa.String(), nullable=False),
sa.Column('select_placeholder', sa.String(), nullable=False),
sa.Column('select_option1', sa.String(), nullable=False),
sa.Column('select_option2', sa.String(), nullable=False),
sa.Column('select_option3', sa.String(), nullable=False),
sa.Column('email_placeholder', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('errors',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('incorect_email', sa.String(), nullable=False),
sa.Column('required_email', sa.String(), nullable=False),
sa.Column('required_login', sa.String(), nullable=False),
sa.Column('required_password', sa.String(), nullable=False),
sa.Column('requirement_password', sa.String(), nullable=False),
sa.Column('error400', sa.String(), nullable=False),
sa.Column('error409', sa.String(), nullable=False),
sa.Column('error500', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('header',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('modal_title', sa.String(), nullable=False),
sa.Column('modal_question', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('help',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('title', sa.String(), nullable=False),
sa.Column('questions', sa.String(), nullable=False),
sa.Column('write_us', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('main',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('title1', sa.String(), nullable=False),
sa.Column('text1', sa.String(), nullable=False),
sa.Column('text2', sa.String(), nullable=False),
sa.Column('text3', sa.String(), nullable=False),
sa.Column('title2', sa.String(), nullable=False),
sa.Column('text4', sa.String(), nullable=False),
sa.Column('button_text', sa.String(), nullable=False),
sa.Column('title3', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('restore',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('restore_password', sa.String(), nullable=False),
sa.Column('text', sa.String(), nullable=False),
sa.Column('button_text', sa.String(), nullable=False),
sa.Column('modal_title', sa.String(), nullable=False),
sa.Column('modal_agreements', sa.String(), nullable=False),
sa.Column('modal_option1', sa.String(), nullable=False),
sa.Column('modal_option2', sa.String(), nullable=False),
sa.Column('modal_option3', sa.String(), nullable=False),
sa.Column('modal_option4', sa.String(), nullable=False),
sa.Column('modal_check1', sa.String(), nullable=False),
sa.Column('modal_check2', sa.String(), nullable=False),
sa.Column('modal_check3', sa.String(), nullable=False),
sa.Column('modal_check4', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('subscription',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('button_text1', sa.String(), nullable=False),
sa.Column('button_text2', sa.String(), nullable=False),
sa.Column('button_text3', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tasks',
sa.Column('locale_id', sa.Integer(), nullable=True),
sa.Column('breadcrumb', sa.String(), nullable=False),
sa.Column('loading', sa.String(), nullable=False),
sa.Column('button_text1', sa.String(), nullable=False),
sa.Column('button_text2', sa.String(), nullable=False),
sa.Column('button_text3', sa.String(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['locale_id'], ['locale.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('tasks')
op.drop_table('subscription')
op.drop_table('restore')
op.drop_table('main')
op.drop_table('help')
op.drop_table('header')
op.drop_table('errors')
op.drop_table('contacts')
op.drop_table('common')
op.drop_table('auth')
op.drop_table('locale')
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions app/api/endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .tariff import router as tariff_router # noqa
from .task import router as task_router # noqa
from .user import router as user_router # noqa
from .locale import router as locale_router # noqa
50 changes: 50 additions & 0 deletions app/api/endpoints/locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.db import get_async_session
from app.core.user import current_superuser
from app.schemas.locale import (
LocaleCreate, LocaleReadByID,
LocaleRead, LocaleCreated
)
from app.crud import locale_crud


router = APIRouter()


@router.post(
'/',
response_model=LocaleCreated,
dependencies=[Depends(current_superuser)],
status_code=status.HTTP_201_CREATED
)
async def create_locale(
locale: LocaleCreate,
session: AsyncSession = Depends(get_async_session)
):
"""Создать локаль."""
return await locale_crud.create(locale=locale, session=session)


@router.get(
'/',
response_model=list[LocaleRead]
)
async def get_locales(
session: AsyncSession = Depends(get_async_session)
):
"""Получить список доступных языков."""
return await locale_crud.get_multi(session)


@router.get(
'/{locale_id}',
response_model=LocaleReadByID
)
async def get_locale_by_id(
locale_id: int,
session: AsyncSession = Depends(get_async_session)
):
"""Получение локали по id."""
return await locale_crud.get_by_id(locale_id, session)
5 changes: 4 additions & 1 deletion app/api/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from app.api.endpoints import (
achievement_router, course_router, examination_router, group_router,
profile_router, tariff_router, task_router, user_router,
profile_router, tariff_router, task_router, user_router, locale_router
)

main_router = APIRouter()
Expand All @@ -21,3 +21,6 @@
main_router.include_router(
profile_router, prefix='/profiles', tags=['Profiles']
)
main_router.include_router(
locale_router, prefix='/locales', tags=['Locales']
)
1 change: 1 addition & 0 deletions app/crud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .tariff import tariff_crud # noqa
from .task import task_crud # noqa
from .user import user_crud # noqa
from .locale import locale_crud # noqa
69 changes: 69 additions & 0 deletions app/crud/locale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from sqlalchemy.ext.asyncio import AsyncSession
from app.crud.base import CRUDBase
from app.models import (
Locale, Common, Header, Auth, Contacts, Help, Main, Restore, Subscription,
Tasks, Errors
)
from app.schemas.locale import LocaleCreate


class LocaleCRUD(CRUDBase):
async def create(self, locale: LocaleCreate, session: AsyncSession):
language = Locale(language=locale.language)
session.add(language)
await session.commit()
await session.refresh(language)
common_data = locale.common.model_dump()
common = Common(**common_data, locale_id=language.id)
header_data = locale.header.model_dump()
header = Header(**header_data, locale_id=language.id)
auth_data = locale.auth.model_dump()
auth = Auth(**auth_data, locale_id=language.id)
contacts_data = locale.contacts.model_dump()
contacts = Contacts(**contacts_data, locale_id=language.id)
help_data = locale.help.model_dump()
help_ = Help(**help_data, locale_id=language.id)
main_data = locale.main.model_dump()
main = Main(**main_data, locale_id=language.id)
restore_data = locale.restore.model_dump()
restore = Restore(**restore_data, locale_id=language.id)
subscription_data = locale.subscription.model_dump()
subscription = Subscription(**subscription_data, locale_id=language.id)
tasks_data = locale.tasks.model_dump()
tasks = Tasks(**tasks_data, locale_id=language.id)
errors_data = locale.errors.model_dump()
errors = Errors(**errors_data, locale_id=language.id)
session.add_all(
(
common, header, auth, contacts, help_, main,
restore, subscription, tasks, errors
)
)
await session.commit()
await session.refresh(language)
return await self.get_by_id(language.id, session)

async def get_by_id(self, id, session: AsyncSession):
stmt = (
select(Locale)
.where(Locale.id == id)
.options(
selectinload(Locale.common),
selectinload(Locale.header),
selectinload(Locale.auth),
selectinload(Locale.contacts),
selectinload(Locale.help),
selectinload(Locale.main),
selectinload(Locale.restore),
selectinload(Locale.subscription),
selectinload(Locale.tasks),
selectinload(Locale.errors)
)
)
locale = await session.execute(stmt)
return locale.scalars().first()


locale_crud = LocaleCRUD(Locale)
4 changes: 4 additions & 0 deletions app/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
from .tariff import Tariff # noqa
from .task import Task # noqa
from .user import User # noqa
from .locale import (
Locale, Common, Header, Help, Auth, Subscription, Contacts, Main,
Restore, Tasks, Errors
) # noqa
Loading

0 comments on commit 340b295

Please sign in to comment.