You've already forked Promocode-API
mirror of
https://github.com/devitq/Promocode-API.git
synced 2026-05-22 22:07:12 +00:00
feat: added activations history
This commit is contained in:
@@ -136,3 +136,12 @@ class CommentDeletedOut(Schema):
|
|||||||
|
|
||||||
class PromocodeActivateOut(Schema):
|
class PromocodeActivateOut(Schema):
|
||||||
promo: str
|
promo: str
|
||||||
|
|
||||||
|
|
||||||
|
class ActivationsHistoryFilters(Schema):
|
||||||
|
limit: int = Field(
|
||||||
|
10, ge=0, description="Limit must be greater than or equal 0"
|
||||||
|
)
|
||||||
|
offset: int = Field(
|
||||||
|
0, ge=0, description="Offset must be greater than or equal to 0"
|
||||||
|
)
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ def signin(
|
|||||||
auth=UserAuth(),
|
auth=UserAuth(),
|
||||||
response={
|
response={
|
||||||
status.OK: schemas.ViewUserOut,
|
status.OK: schemas.ViewUserOut,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -101,6 +102,7 @@ def get_profile(request: HttpRequest) -> tuple[int, schemas.ViewUserOut]:
|
|||||||
response={
|
response={
|
||||||
status.OK: schemas.ViewUserOut,
|
status.OK: schemas.ViewUserOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -124,6 +126,7 @@ def patch_profile(
|
|||||||
response={
|
response={
|
||||||
status.OK: list[schemas.PromocodeViewOut],
|
status.OK: list[schemas.PromocodeViewOut],
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -181,12 +184,57 @@ def feed(
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/promo/history",
|
||||||
|
auth=UserAuth(),
|
||||||
|
response={
|
||||||
|
status.OK: list[schemas.PromocodeViewOut],
|
||||||
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
def get_activations_history(
|
||||||
|
request: HttpRequest,
|
||||||
|
filters: Query[schemas.ActivationsHistoryFilters],
|
||||||
|
response: HttpResponse,
|
||||||
|
) -> tuple[int, list[schemas.PromocodeViewOut]]:
|
||||||
|
user: User = request.auth
|
||||||
|
|
||||||
|
activations = (
|
||||||
|
PromocodeActivation.objects.filter(user=user)
|
||||||
|
.select_related("promocode", "promocode__business")
|
||||||
|
.prefetch_related("promocode__likes", "promocode__comments")
|
||||||
|
.annotate(
|
||||||
|
like_count=Count("promocode__likes", distinct=True),
|
||||||
|
comment_count=Count("promocode__comments", distinct=True),
|
||||||
|
is_liked_by_user=Exists(
|
||||||
|
PromocodeLike.objects.filter(
|
||||||
|
promocode=OuterRef("promocode"), user=user
|
||||||
|
)
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.order_by("-timestamp")
|
||||||
|
)
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for activation in activations:
|
||||||
|
promocode = activation.promocode
|
||||||
|
promocode.like_count = activation.like_count
|
||||||
|
promocode.comment_count = activation.comment_count
|
||||||
|
promocode.is_liked_by_user = activation.is_liked_by_user
|
||||||
|
promocode.is_activated_by_user = True
|
||||||
|
result.append(utils.map_promocode_to_schema(promocode))
|
||||||
|
|
||||||
|
return status.OK, result
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/promo/{promocode_id}",
|
"/promo/{promocode_id}",
|
||||||
auth=UserAuth(),
|
auth=UserAuth(),
|
||||||
response={
|
response={
|
||||||
status.OK: schemas.PromocodeViewOut,
|
status.OK: schemas.PromocodeViewOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -230,6 +278,7 @@ def get_promocode(
|
|||||||
response={
|
response={
|
||||||
status.OK: schemas.PromocodeLikeOut,
|
status.OK: schemas.PromocodeLikeOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -255,6 +304,7 @@ def add_like(
|
|||||||
response={
|
response={
|
||||||
status.OK: schemas.PromocodeRemoveLikeOut,
|
status.OK: schemas.PromocodeRemoveLikeOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -282,6 +332,7 @@ def delete_like(
|
|||||||
response={
|
response={
|
||||||
status.CREATED: schemas.CommentOut,
|
status.CREATED: schemas.CommentOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -309,6 +360,7 @@ def add_comment(
|
|||||||
response={
|
response={
|
||||||
status.OK: list[schemas.CommentOut],
|
status.OK: list[schemas.CommentOut],
|
||||||
status.NOT_FOUND: global_schemas.NotFoundError,
|
status.NOT_FOUND: global_schemas.NotFoundError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -341,7 +393,8 @@ def list_comments(
|
|||||||
auth=UserAuth(),
|
auth=UserAuth(),
|
||||||
response={
|
response={
|
||||||
status.OK: schemas.CommentOut,
|
status.OK: schemas.CommentOut,
|
||||||
status.NOT_FOUND: global_schemas.NotFoundError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -366,6 +419,7 @@ def get_comment(
|
|||||||
response={
|
response={
|
||||||
status.OK: schemas.CommentOut,
|
status.OK: schemas.CommentOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
exclude_none=True,
|
exclude_none=True,
|
||||||
)
|
)
|
||||||
@@ -406,7 +460,7 @@ def update_comment(
|
|||||||
response={
|
response={
|
||||||
status.OK: schemas.CommentDeletedOut,
|
status.OK: schemas.CommentDeletedOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
status.NOT_FOUND: global_schemas.NotFoundError,
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
def delete_comment(
|
def delete_comment(
|
||||||
@@ -441,7 +495,7 @@ def delete_comment(
|
|||||||
response={
|
response={
|
||||||
status.OK: schemas.PromocodeActivateOut,
|
status.OK: schemas.PromocodeActivateOut,
|
||||||
status.BAD_REQUEST: global_schemas.BadRequestError,
|
status.BAD_REQUEST: global_schemas.BadRequestError,
|
||||||
status.NOT_FOUND: global_schemas.NotFoundError,
|
status.UNAUTHORIZED: global_schemas.UnauthorizedError,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
def activate_promocode(
|
def activate_promocode(
|
||||||
|
|||||||
Reference in New Issue
Block a user