Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…thon_fastapi_web into fix/cors-issue
  • Loading branch information
joboy-dev committed Aug 8, 2024
2 parents 4624f8d + 48edbbe commit 6fe6707
Show file tree
Hide file tree
Showing 67 changed files with 2,623 additions and 1,144 deletions.
26 changes: 26 additions & 0 deletions alembic/versions/299ab5d402fc_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""empty message
Revision ID: 299ab5d402fc
Revises: 224b03e9169c, 44f7da26ee88
Create Date: 2024-08-08 18:59:46.314362
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '299ab5d402fc'
down_revision: Union[str, None] = ('224b03e9169c', '44f7da26ee88')
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
pass


def downgrade() -> None:
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Ensure id and foreign keys are of type String
Revision ID: 44f7da26ee88
Revises: b27028b0327e
Create Date: 2024-08-08 18:21:40.759920
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '44f7da26ee88'
down_revision: Union[str, None] = 'b27028b0327e'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Ensure id and foreign keys are of type String
Revision ID: b27028b0327e
Revises: 8caa1a06240c
Create Date: 2024-08-08 17:49:49.925575
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'b27028b0327e'
down_revision: Union[str, None] = '8caa1a06240c'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('permissions', sa.Column('title', sa.String(), nullable=False))
op.drop_constraint('permissions_name_key', 'permissions', type_='unique')
op.create_unique_constraint(None, 'permissions', ['title'])
op.drop_column('permissions', 'name')
op.add_column('roles', sa.Column('description', sa.Text(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('roles', 'description')
op.add_column('permissions', sa.Column('name', sa.VARCHAR(), autoincrement=False, nullable=False))
op.drop_constraint(None, 'permissions', type_='unique')
op.create_unique_constraint('permissions_name_key', 'permissions', ['name'])
op.drop_column('permissions', 'title')
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Ensure id and foreign keys are of type String
Revision ID: ff92a0037698
Revises: 299ab5d402fc
Create Date: 2024-08-08 19:03:17.115224
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'ff92a0037698'
down_revision: Union[str, None] = '299ab5d402fc'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('product_comments', sa.Column('user_id', sa.String(), nullable=True))
op.create_foreign_key(None, 'product_comments', 'users', ['user_id'], ['id'], ondelete='SET NULL')
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'product_comments', type_='foreignkey')
op.drop_column('product_comments', 'user_id')
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion api/v1/models/permissions/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class Permission(BaseTableModel):
__tablename__ = 'permissions'

#id = Column(String, primary_key=True, index=True, default=lambda: str(uuid7()))
name = Column(String, unique=True, nullable=False)
title = Column(String, unique=True, nullable=False)

3 changes: 2 additions & 1 deletion api/v1/models/permissions/role.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, String, Boolean
from sqlalchemy import Column, String, Boolean, Text
from api.v1.models.base_model import BaseTableModel
from uuid_extensions import uuid7

Expand All @@ -7,4 +7,5 @@ class Role(BaseTableModel):

id = Column(String, primary_key=True, index=True, default=lambda: str(uuid7()))
name = Column(String, unique=True, nullable=False)
description = Column(Text, nullable=True)
is_builtin = Column(Boolean, default=False) # True for built-in roles, False for custom roles
4 changes: 2 additions & 2 deletions api/v1/routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from api.v1.routes.auth import auth
from api.v1.routes.newsletter import newsletter
from api.v1.routes.user import user_router
from api.v1.routes.product import product
from api.v1.routes.product import product, non_organization_product
from api.v1.routes.product_comment import product_comment
from api.v1.routes.notification import notification
from api.v1.routes.testimonial import testimonial
Expand Down Expand Up @@ -53,6 +53,7 @@
api_version_one.include_router(user_router)
api_version_one.include_router(profile)
api_version_one.include_router(organization)
api_version_one.include_router(non_organization_product)
api_version_one.include_router(product)
api_version_one.include_router(payment)
api_version_one.include_router(bill_plan)
Expand Down Expand Up @@ -88,4 +89,3 @@
api_version_one.include_router(team)
api_version_one.include_router(terms_and_conditions)
api_version_one.include_router(product_comment)

32 changes: 0 additions & 32 deletions api/v1/routes/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
analytics = APIRouter(prefix='/analytics')


def get_current_month_date_range():
now = datetime.utcnow()
start_date = datetime(now.year, now.month, 1)
end_date = (start_date + timedelta(days=32)
).replace(day=1) - timedelta(seconds=1)
return start_date, end_date


@analytics.get('/line-chart-data', status_code=status.HTTP_200_OK)
async def get_analytics_line_chart_data(token: Annotated[OAuth2, Depends(oauth2_scheme)],
db: Annotated[Session, Depends(get_db)]):
Expand All @@ -30,27 +22,3 @@ async def get_analytics_line_chart_data(token: Annotated[OAuth2, Depends(oauth2_
analytics response: contains the analytics data
"""
return analytics_service.get_analytics_line_chart(token, db)


@analytics.get('/summary', status_code=status.HTTP_200_OK)
async def get_analytics_summary(
token: Annotated[str, Depends(oauth2_scheme)],
db: Annotated[Session, Depends(get_db)],
analytics_service: Annotated[AnalyticsServices, Depends()],
start_date: datetime = None,
end_date: datetime = None
):

"""
Retrieves analytics summary data for an organization or super admin.
Args:
token: access_token
db: database Session object
start_date: start date for filtering
end_date: end date for filtering
Returns:
analytics response: contains the analytics summary data
"""
if not start_date or not end_date:
start_date, end_date = get_current_month_date_range()
return analytics_service.get_analytics_summary(token=token, db=db, start_date=start_date, end_date=end_date)
38 changes: 30 additions & 8 deletions api/v1/routes/billing_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from api.v1.services.user import user_service
from api.v1.schemas.plans import CreateSubscriptionPlan

bill_plan = APIRouter(prefix='/organizations', tags=['Billing-Plan'])
bill_plan = APIRouter(prefix="/organisations", tags=["Billing-Plan"])

@bill_plan.get('/{organization_id}/billing-plans', response_model=success_response)

@bill_plan.get("/{organization_id}/billing-plans", response_model=success_response)
async def retrieve_all_billing_plans(
organization_id: str,
db: Session = Depends(get_db)
organization_id: str, db: Session = Depends(get_db)
):
"""
Endpoint to get all billing plans
Expand All @@ -33,6 +33,7 @@ async def retrieve_all_billing_plans(
},
)


@bill_plan.post("/billing-plans", response_model=success_response)
async def create_new_billing_plan(
request: CreateSubscriptionPlan,
Expand All @@ -51,12 +52,13 @@ async def create_new_billing_plan(
data=jsonable_encoder(plan),
)

@bill_plan.patch('/billing-plans/{billing_plan_id}', response_model=success_response)

@bill_plan.patch("/billing-plans/{billing_plan_id}", response_model=success_response)
async def update_a_billing_plan(
billing_plan_id: str,
request: CreateSubscriptionPlan,
current_user: User = Depends(user_service.get_current_super_admin),
db: Session = Depends(get_db)
db: Session = Depends(get_db),
):
"""
Endpoint to update a billing plan by ID
Expand All @@ -70,11 +72,12 @@ async def update_a_billing_plan(
data=jsonable_encoder(plan),
)

@bill_plan.delete('/billing-plans/{billing_plan_id}', response_model=success_response)

@bill_plan.delete("/billing-plans/{billing_plan_id}", response_model=success_response)
async def delete_a_billing_plan(
billing_plan_id: str,
current_user: User = Depends(user_service.get_current_super_admin),
db: Session = Depends(get_db)
db: Session = Depends(get_db),
):
"""
Endpoint to delete a billing plan by ID
Expand All @@ -86,3 +89,22 @@ async def delete_a_billing_plan(
status_code=status.HTTP_200_OK,
message="Plan deleted successfully",
)


@bill_plan.get('/billing-plans/{billing_plan_id}', response_model=success_response)
async def retrieve_single_billing_plans(
billing_plan_id: str,
db: Session = Depends(get_db),
current_user: User = Depends(user_service.get_current_user)
):
"""
Endpoint to get single billing plan by id
"""

billing_plan = billing_plan_service.fetch(db, billing_plan_id)

return success_response(
status_code=status.HTTP_200_OK,
message="Plan fetched successfully",
data=jsonable_encoder(billing_plan)
)
42 changes: 40 additions & 2 deletions api/v1/routes/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
BlogPostResponse,
BlogRequest,
BlogUpdateResponseModel,
BlogLikeDislikeResponse
BlogLikeDislikeResponse,
CommentRequest,
CommentUpdateResponseModel
)
from api.v1.services.blog import BlogService
from api.v1.services.user import user_service
Expand Down Expand Up @@ -260,4 +262,40 @@ async def comments(
if comments_response == 'Blog not found':
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="Blog not found")
return comments_response
return comments_response

# Update a blog comment
@blog.put("/{blog_id}/comments/{comment_id}", response_model=CommentUpdateResponseModel)
async def update_blog_comment(
blog_id: str,
comment_id: str,
blogComment: CommentRequest,
current_user: Annotated[User, Depends(user_service.get_current_user)],
db: Annotated[Session, Depends(get_db)],
):
"""Updates a blog comment
Args:
- blog_id (str): the ID of the blog
- comment_id (str): the ID of the comment
- blogComment: the new comment to update to
- current_user: the current authenticated user
- db: the database session
Returns:
dict: updated comment body
"""

blog_service = BlogService(db)
updated_blog_comment = blog_service.update_blog_comment(
blog_id=blog_id,
comment_id=comment_id,
content=blogComment.content,
current_user=current_user,
)

return success_response(
message="Blog comment updated successfully",
status_code=200,
data=jsonable_encoder(updated_blog_comment)
)
Loading

0 comments on commit 6fe6707

Please sign in to comment.