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: add community question and answer models, schemas, and routes #1183

Open
wants to merge 20 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions api/v1/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from api.v1.models.terms import TermsAndConditions
from api.v1.models.reset_password_token import ResetPasswordToken
from api.v1.models.faq_inquiries import FAQInquiries
from api.v1.models.community import CommunityQuestion, CommunityAnswer
from api.v1.models.wishlist import Wishlist
from api.v1.models.totp_device import TOTPDevice
from api.v1.models.bookmark import Bookmark

from api.v1.models.bookmark import Bookmark
38 changes: 38 additions & 0 deletions api/v1/models/community.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from sqlalchemy import Column, String, DateTime, Text, ForeignKey, Boolean
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from api.v1.models.base_model import BaseTableModel

class CommunityQuestion(BaseTableModel):
__tablename__ = "community_questions"

title = Column(String, nullable=False)
message = Column(Text, nullable=False)
user_id = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
timestamp = Column(DateTime(timezone=True), server_default=func.now())
is_resolved = Column(Boolean, default=False)

# Relationships
user = relationship("User", back_populates="questions")
answers = relationship("CommunityAnswer", back_populates="question", cascade="all, delete-orphan")

def __repr__(self):
return f"<CommunityQuestion(id='{self.id}', title='{self.title}', user_id='{self.user_id}')>"


class CommunityAnswer(BaseTableModel):
__tablename__ = "community_answers"

message = Column(Text, nullable=False)
user_id = Column(String, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
question_id = Column(String, ForeignKey("community_questions.id", ondelete="CASCADE"), nullable=False)
timestamp = Column(DateTime(timezone=True), server_default=func.now())
is_accepted = Column(Boolean, default=False)

# Relationships
user = relationship("User", back_populates="answers")
question = relationship("CommunityQuestion", back_populates="answers")

def __repr__(self):
return f"<CommunityAnswer(id='{self.id}', question_id='{self.question_id}', user_id='{self.user_id}')>"

15 changes: 9 additions & 6 deletions api/v1/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,23 @@ class User(BaseTableModel):
"Reply", back_populates="user", cascade="all, delete-orphan"
)


reset_password_token = relationship("ResetPasswordToken",
back_populates="user",
cascade="all, delete-orphan")

wishlist = relationship("Wishlist",
back_populates="user",
cascade="all, delete-orphan")

questions = relationship("CommunityQuestion", back_populates="user", cascade="all, delete-orphan")
answers = relationship("CommunityAnswer", back_populates="user", cascade="all, delete-orphan")
wishlist = relationship("Wishlist",
back_populates="user",
cascade="all, delete-orphan")

totp_device = relationship("TOTPDevice", back_populates="user", cascade="all, delete-orphan")

bookmarks = relationship(
"Bookmark", back_populates="user", cascade="delete"
"Bookmark", back_populates="user", cascade="delete"
)

def to_dict(self):
obj_dict = super().to_dict()
obj_dict.pop("password")
Expand Down
5 changes: 4 additions & 1 deletion api/v1/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
from api.v1.routes.settings import settings
from api.v1.routes.terms_and_conditions import terms_and_conditions
from api.v1.routes.stripe import subscription_
from api.v1.routes.community import community_questions
from api.v1.routes.community import community_answers
from api.v1.routes.wishlist import wishlist

api_version_one = APIRouter(prefix="/api/v1")

api_version_one.include_router(api_status)
Expand Down Expand Up @@ -97,4 +98,6 @@
api_version_one.include_router(terms_and_conditions)
api_version_one.include_router(product_comment)
api_version_one.include_router(subscription_)
api_version_one.include_router(community_questions)
api_version_one.include_router(community_answers)
api_version_one.include_router(wishlist)
Loading