fix data base
This commit is contained in:
34
bd/__init__.py
Normal file
34
bd/__init__.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# Database configuration package
|
||||
from pydantic.generics import GenericModel
|
||||
import re
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
class Settings(GenericModel):
|
||||
DB_HOST: str = os.getenv("DB_HOST", "postgres")
|
||||
DB_PORT: int = int(os.getenv("DB_PORT", "5432"))
|
||||
DB_USER: str = os.getenv("DB_USER", "postgres")
|
||||
DB_PASS: str = os.getenv("DB_PASS", "")
|
||||
DB_NAME: str = os.getenv("DB_NAME", "profi")
|
||||
|
||||
@classmethod
|
||||
def validate_db_port(cls, v):
|
||||
# Проверка и преобразование DB_PORT
|
||||
if isinstance(v, str):
|
||||
# Используем регулярное выражение для извлечения целого числа
|
||||
match = re.search(r'\d+', v)
|
||||
if match:
|
||||
return int(match.group())
|
||||
else:
|
||||
raise ValueError(f"Invalid DB_PORT value: {v}")
|
||||
return v
|
||||
|
||||
@property
|
||||
def DATABASE_URL_asyncpg(self) -> str:
|
||||
pwd = urllib.parse.quote_plus(str(self.DB_PASS))
|
||||
return f"postgresql+asyncpg://{self.DB_USER}:{pwd}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
|
||||
|
||||
@property
|
||||
def DATABASE_URL_syncpg(self) -> str:
|
||||
pwd = urllib.parse.quote_plus(str(self.DB_PASS))
|
||||
return f"postgresql+pg8000://{self.DB_USER}:{pwd}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
|
||||
16
bd/tables/choice.py
Normal file
16
bd/tables/choice.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
|
||||
|
||||
class Choice(Base):
|
||||
__tablename__ = "choices"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
question_id = Column(UUID(as_uuid=True), ForeignKey("questions.id", ondelete="CASCADE"), nullable=False)
|
||||
text = Column(String(500), nullable=False)
|
||||
position = Column(Integer, nullable=True)
|
||||
|
||||
|
||||
__all__ = ["Base", "Choice"]
|
||||
17
bd/tables/group.py
Normal file
17
bd/tables/group.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from sqlalchemy import Column, String, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
class Group(Base):
|
||||
__tablename__ = "group"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name_group = Column(String(150), nullable=False)
|
||||
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
|
||||
user = relationship("User", backref="groups", passive_deletes=True)
|
||||
|
||||
__all__ = ["Base", "Group"]
|
||||
|
||||
17
bd/tables/organization.py
Normal file
17
bd/tables/organization.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from sqlalchemy import Column, String, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
class Organization(Base):
|
||||
__tablename__ = "organization"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
name_organization = Column(String(150), nullable=False)
|
||||
group_id = Column(UUID(as_uuid=True), ForeignKey("group.id", ondelete="CASCADE"), nullable=False)
|
||||
|
||||
group = relationship("Group", backref="organizations", passive_deletes=True)
|
||||
|
||||
__all__ = ["Base", "Organization"]
|
||||
|
||||
21
bd/tables/poll.py
Normal file
21
bd/tables/poll.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from sqlalchemy import Column, String, Boolean, DateTime, Text
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
|
||||
|
||||
class Poll(Base):
|
||||
__tablename__ = "polls"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
title = Column(String(255), nullable=False)
|
||||
description = Column(Text, nullable=True)
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
questions = relationship("Question", backref="poll", cascade="all, delete-orphan", passive_deletes=True)
|
||||
|
||||
|
||||
__all__ = ["Base", "Poll"]
|
||||
20
bd/tables/question.py
Normal file
20
bd/tables/question.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from sqlalchemy import Column, Text, Integer, ForeignKey, String
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import relationship
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
|
||||
|
||||
class Question(Base):
|
||||
__tablename__ = "questions"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
poll_id = Column(UUID(as_uuid=True), ForeignKey("polls.id", ondelete="CASCADE"), nullable=False)
|
||||
text = Column(Text, nullable=False)
|
||||
position = Column(Integer, nullable=True)
|
||||
type = Column(String(32), nullable=False, default="single") # single|multi|open
|
||||
|
||||
choices = relationship("Choice", backref="question", cascade="all, delete-orphan", passive_deletes=True)
|
||||
|
||||
|
||||
__all__ = ["Base", "Question"]
|
||||
30
bd/tables/response.py
Normal file
30
bd/tables/response.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from sqlalchemy import Column, DateTime, ForeignKey, Text
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
|
||||
|
||||
class Response(Base):
|
||||
__tablename__ = "responses"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
poll_id = Column(UUID(as_uuid=True), ForeignKey("polls.id"), nullable=False)
|
||||
user_id = Column(UUID(as_uuid=True), nullable=True)
|
||||
submitted_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
answers = relationship("Answer", backref="response", cascade="all, delete-orphan", passive_deletes=True)
|
||||
|
||||
|
||||
class Answer(Base):
|
||||
__tablename__ = "answers"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
response_id = Column(UUID(as_uuid=True), ForeignKey("responses.id", ondelete="CASCADE"), nullable=False)
|
||||
question_id = Column(UUID(as_uuid=True), ForeignKey("questions.id"), nullable=False)
|
||||
choice_id = Column(UUID(as_uuid=True), nullable=True)
|
||||
text = Column(Text, nullable=True)
|
||||
|
||||
|
||||
__all__ = ["Base", "Response", "Answer"]
|
||||
3
bd/tables/simple_base.py
Normal file
3
bd/tables/simple_base.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from sqlalchemy.orm import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
14
bd/tables/users.py
Normal file
14
bd/tables/users.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from sqlalchemy import Column, String
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from .simple_base import Base
|
||||
import uuid
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
first_name = Column(String(150), nullable=False)
|
||||
last_name = Column(String(150), nullable=False)
|
||||
|
||||
__all__ = ["Base", "User"]
|
||||
|
||||
Reference in New Issue
Block a user