Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
106 changes: 28 additions & 78 deletions articles/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ class SubmissionType(str, Enum):


class ArticleCommunityDetails(ModelSchema):
class Config:
class Meta:
model = Community
model_fields = ["id", "name", "description", "profile_pic_url"]
# RENAMED 'model_fields' to 'fields' (Fixes ConfigError)
fields = ["id", "name", "description", "profile_pic_url"]


class CommunityArticleForList(Schema):
Expand Down Expand Up @@ -89,11 +90,10 @@ class CommunityArticleOut(ModelSchema):
is_pseudonymous: bool
is_admin: bool

class Config:
class Meta:
model = CommunityArticle
model_fields = [
fields = [
"id",
"community",
"status",
"submitted_at",
"published_at",
Expand All @@ -108,8 +108,6 @@ def from_orm(
not hasattr(community_article, "_prefetched_objects_cache")
or "assigned_reviewers" not in community_article._prefetched_objects_cache
):
# Optional: Log warning in development
# logger.warning("assigned_reviewers not prefetched for CommunityArticle id=%s", community_article.id)
pass

reviewer_ids = (
Expand Down Expand Up @@ -160,9 +158,9 @@ class ArticlesListOut(ModelSchema):
abstract: str
article_image_url: Optional[str] = None

class Config:
class Meta:
model = Article
model_fields = ["id", "slug", "title", "abstract", "article_image_url"]
fields = ["id", "slug", "title", "abstract", "article_image_url"]

@classmethod
def from_orm_with_fields(
Expand Down Expand Up @@ -202,9 +200,10 @@ class ArticleOut(ModelSchema):
submission_type: SubmissionType
is_pseudonymous: bool = Field(False)

class Config:
class Meta:
model = Article
model_fields = [
# FIX: Only include safe fields. This prevents 'submitter_id' lookup errors.
fields = [
"id",
"slug",
"title",
Expand Down Expand Up @@ -267,9 +266,9 @@ class ArticleBasicOut(ModelSchema):
user: UserStats
is_submitter: bool

class Config:
class Meta:
model = Article
model_fields = [
fields = [
"id",
"slug",
"title",
Expand Down Expand Up @@ -299,9 +298,9 @@ def from_orm_with_custom_fields(


class ArticleMetaOut(ModelSchema):
class Config:
class Meta:
model = Article
model_fields = [
fields = [
"title",
"abstract",
"article_image_url",
Expand Down Expand Up @@ -330,7 +329,6 @@ class PaginatedArticlesListResponse(Schema):
class ArticleCreateDetails(Schema):
title: str
abstract: str
# keywords: List[str]
authors: List[Tag]
article_link: Optional[str] = Field(default=None)
submission_type: Literal["Public", "Private"]
Expand All @@ -345,7 +343,6 @@ class ArticleCreateSchema(Schema):
class UpdateArticleDetails(Schema):
title: str | None
abstract: str | None
# keywords: List[str] | None
authors: List[Tag] | None
submission_type: Literal["Public", "Private"] | None
faqs: List[FAQSchema] = []
Expand Down Expand Up @@ -374,9 +371,9 @@ class CreateReviewSchema(Schema):


class ReviewVersionSchema(ModelSchema):
class Config:
class Meta:
model = ReviewVersion
model_fields = [
fields = [
"id",
"rating",
"subject",
Expand All @@ -394,14 +391,12 @@ class ReviewOut(ModelSchema):
article_id: int
comments_count: int = Field(0)
comments_ratings: float = Field(0)
# anonymous_name: str = Field(None)
# avatar: str = Field(None)
is_pseudonymous: bool = Field(False)
is_approved: bool = Field(False)

class Config:
class Meta:
model = Review
model_fields = [
fields = [
"id",
"rating",
"review_type",
Expand All @@ -424,15 +419,6 @@ def from_orm(cls, review: Review, current_user: Optional[User]):
for version in review.versions.all().order_by("-version")[:3]
]
is_pseudonymous = review.is_pseudonymous
# if is_pseudonymous:
# pseudonym = AnonymousIdentity.objects.get(
# article=review.article, user=review.user, community=review.community
# )
# anonymous_name = pseudonym.fake_name
# avatar = pseudonym.identicon
# else:
# anonymous_name = None
# avatar = None
user = UserStats.from_model(review.user, basic_details_with_reputation=True)
if is_pseudonymous:
pseudonym = AnonymousIdentity.objects.get(
Expand Down Expand Up @@ -479,8 +465,6 @@ def from_orm(cls, review: Review, current_user: Optional[User]):
is_author=review.user == current_user,
is_approved=review.is_approved,
versions=versions,
# anonymous_name=anonymous_name,
# avatar=avatar if avatar else None,
is_pseudonymous=is_pseudonymous,
community_article=community_article,
comments_ratings=comments_ratings if comments_ratings else 0,
Expand Down Expand Up @@ -511,13 +495,11 @@ class ReviewCommentOut(ModelSchema):
upvotes: int
is_author: bool = Field(False)
is_deleted: bool = Field(False)
# anonymous_name: str = Field(None)
# avatar: str = Field(None)
is_pseudonymous: bool = Field(False)

class Config:
class Meta:
model = ReviewComment
model_fields = ["id", "content", "rating", "created_at"]
fields = ["id", "content", "rating", "created_at"]

@staticmethod
def from_orm_with_replies(comment: ReviewComment, current_user: Optional[User]):
Expand All @@ -529,15 +511,6 @@ def from_orm_with_replies(comment: ReviewComment, current_user: Optional[User]):
for reply in comment.review_replies.all()
]
is_pseudonymous = comment.is_pseudonymous
# if is_pseudonymous:
# pseudonym = AnonymousIdentity.objects.get(
# article=comment.review.article, user=comment.author, community=comment.review.community
# )
# anonymous_name = pseudonym.fake_name
# avatar = pseudonym.identicon
# else:
# anonymous_name = None
# avatar = None
if is_pseudonymous:
pseudonym = AnonymousIdentity.objects.get(
article=comment.review.article,
Expand All @@ -555,8 +528,6 @@ def from_orm_with_replies(comment: ReviewComment, current_user: Optional[User]):
created_at=comment.created_at,
upvotes=comment.reactions.filter(vote=1).count(),
replies=replies,
# anonymous_name=anonymous_name,
# avatar=avatar if avatar else None,
is_author=(comment.author == current_user) if current_user else False,
is_deleted=comment.is_deleted,
is_pseudonymous=is_pseudonymous,
Expand Down Expand Up @@ -590,13 +561,11 @@ class DiscussionOut(ModelSchema):
user: UserStats = Field(...)
article_id: int
comments_count: int = Field(0)
# anonymous_name: str = Field(None)
# avatar: str = Field(None)
is_pseudonymous: bool = Field(False)

class Config:
class Meta:
model = Discussion
model_fields = [
fields = [
"id",
"topic",
"content",
Expand All @@ -609,15 +578,6 @@ class Config:
def from_orm(cls, discussion: Discussion, current_user: Optional[User]):
comments_count = DiscussionComment.objects.filter(discussion=discussion).count()
is_pseudonymous = discussion.is_pseudonymous
# if is_pseudonymous:
# pseudonym = AnonymousIdentity.objects.get(
# article=discussion.article, user=discussion.author, community=discussion.community
# )
# anonymous_name = pseudonym.fake_name
# avatar = pseudonym.identicon
# else:
# anonymous_name = None
# avatar = None
user = UserStats.from_model(
discussion.author, basic_details_with_reputation=True
)
Expand All @@ -641,8 +601,6 @@ def from_orm(cls, discussion: Discussion, current_user: Optional[User]):
deleted_at=discussion.deleted_at,
comments_count=comments_count,
is_author=discussion.author == current_user,
# anonymous_name=anonymous_name,
# avatar=avatar if avatar else None,
is_pseudonymous=is_pseudonymous,
)

Expand All @@ -669,13 +627,11 @@ class DiscussionCommentOut(ModelSchema):
replies: list["DiscussionCommentOut"] = Field(...)
upvotes: int
is_author: bool = Field(False)
# anonymous_name: str = Field(None)
# avatar: str = Field(None)
is_pseudonymous: bool = Field(False)

class Config:
class Meta:
model = DiscussionComment
model_fields = ["id", "content", "created_at"]
fields = ["id", "content", "created_at"]

@staticmethod
def from_orm_with_replies(comment: DiscussionComment, current_user: Optional[User]):
Expand All @@ -686,11 +642,6 @@ def from_orm_with_replies(comment: DiscussionComment, current_user: Optional[Use
DiscussionCommentOut.from_orm_with_replies(reply, current_user)
for reply in DiscussionComment.objects.filter(parent=comment)
]
# pseudonym = AnonymousIdentity.objects.get(
# article=comment.discussion.article, user=comment.author, community=comment.discussion.community
# )
# anonymous_name = pseudonym.fake_name
# avatar = pseudonym.identicon
is_pseudonymous = comment.is_pseudonymous
if is_pseudonymous:
pseudonym = AnonymousIdentity.objects.get(
Expand All @@ -708,9 +659,7 @@ def from_orm_with_replies(comment: DiscussionComment, current_user: Optional[Use
created_at=comment.created_at,
upvotes=comment.reactions.filter(vote=1).count(),
replies=replies,
# anonymous_name=anonymous_name,
is_author=(comment.author == current_user) if current_user else False,
# avatar=avatar if avatar else None,
is_pseudonymous=is_pseudonymous,
)

Expand Down Expand Up @@ -738,9 +687,9 @@ class DiscussionSubscriptionOut(ModelSchema):
subscribed_at: datetime
is_active: bool

class Config:
class Meta:
model = DiscussionSubscription
model_fields = [
fields = [
"id",
"subscribed_at",
"is_active",
Expand Down Expand Up @@ -770,7 +719,7 @@ class SubscriptionStatusSchema(Schema):
class CommunitySubscriptionOut(Schema):
community_id: int
community_name: str
articles: List[dict] # List of article info user is subscribed to
articles: List[dict]


class UserSubscriptionsOut(Schema):
Expand Down Expand Up @@ -812,3 +761,4 @@ class CommunityArticleStatsResponse(Schema):
reviews_over_time: List[DateCount]
likes_over_time: List[DateCount]
average_rating: float

9 changes: 5 additions & 4 deletions communities/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class CommunityListOut(ModelSchema):
# is_request_sent: bool = False
# requested_at: Optional[datetime] = None

class Config:
class Meta:
model = Community
model_fields = [
fields = [
"id",
"name",
"description",
Expand Down Expand Up @@ -122,9 +122,9 @@ class CommunityOut(ModelSchema):
join_request_status: Optional[str] = None
community_settings: Optional[str] = None

class Config:
class Meta:
model = Community
model_fields = [
fields = [
"id",
"name",
"description",
Expand All @@ -136,6 +136,7 @@ class Config:
"rules",
"about",
]


@staticmethod
def from_orm_with_custom_fields(community: Community, user: Optional[User] = None):
Expand Down
2 changes: 1 addition & 1 deletion myapp/asgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
ASGI config for myapp project.
ASGI Meta for myapp project.

It exposes the ASGI callable as a module-level variable named ``application``.

Expand Down
2 changes: 1 addition & 1 deletion myapp/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

app = Celery("myapp")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.config_from_object("django.conf:settings", namespace="CELERY") # <--- Fix this line
app.autodiscover_tasks()


Expand Down
15 changes: 8 additions & 7 deletions myapp/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ class Tag(Schema):


# Generic Pagination schema
# The disadvantage of this approach is that proper response schema is not
# generated for the paginated response.


class Message(Schema):
message: str

Expand All @@ -38,9 +34,11 @@ class UserStats(ModelSchema):
# posts created or commented
contributed_posts: Optional[int] = None

class Config:
class Meta:
model = User
model_fields = ["id", "username", "bio", "profile_pic_url", "home_page_url"]
# FIX: Explicitly list ONLY safe fields.
# Removed 'fields = "__all__"' to prevent password/permission errors.
fields = ["id", "username", "bio", "profile_pic_url", "home_page_url"]

@staticmethod
def from_model(
Expand All @@ -57,12 +55,15 @@ def from_model(
if basic_details:
return UserStats(**basic_data)

# Handle Reputation safely (create if missing)
reputation, created = Reputation.objects.get_or_create(user=user)

if basic_details_with_reputation:
basic_data["reputation_score"] = reputation.score
basic_data["reputation_level"] = reputation.level
return UserStats(**basic_data)

# Calculate stats
contributed_articles = (
Article.objects.filter(submitter=user).count()
+ Review.objects.filter(user=user).count()
Expand Down Expand Up @@ -121,4 +122,4 @@ class RealtimeStatusOut(Schema):


class RealtimeHeartbeatOut(Schema):
message: str
message: str
Loading