Skip to content

Commit

Permalink
Fix comments count (#1969)
Browse files Browse the repository at this point in the history
* Fix comments count

* fix isort issue

* fix lint issues

* fix lint issues

* fix lint issue
  • Loading branch information
jadmsaadaot authored Aug 4, 2023
1 parent eb0c4da commit c4b004f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions met-api/src/met_api/constants/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@


SYSTEM_USER = 1
SYSTEM_REVIEWER = 'System'
7 changes: 5 additions & 2 deletions met-api/src/met_api/models/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
Manages the Submission
"""
from __future__ import annotations

from datetime import datetime
from typing import List

from sqlalchemy import ForeignKey
from sqlalchemy.dialects import postgresql

from met_api.constants.comment_status import Status
from met_api.models.survey import Survey
from met_api.constants.user import SYSTEM_REVIEWER
from met_api.models.participant import Participant
from met_api.models.survey import Survey
from met_api.schemas.submission import SubmissionSchema

from .base_model import BaseModel
Expand Down Expand Up @@ -63,7 +66,7 @@ def create(cls, submission: SubmissionSchema, session=None) -> Submission:
const_review_date = None
else:
const_comment_status = Status.Approved.value
const_reviewed_by = 'System'
const_reviewed_by = SYSTEM_REVIEWER
const_review_date = datetime.utcnow()

new_submission = Submission(
Expand Down
17 changes: 7 additions & 10 deletions met-api/src/met_api/schemas/engagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

from marshmallow import EXCLUDE, Schema, ValidationError, fields, validate, validates_schema

from met_api.constants.engagement_status import Status, SubmissionStatus
from met_api.constants.comment_status import Status as CommentStatus
from met_api.constants.engagement_status import Status, SubmissionStatus
from met_api.schemas.engagement_status_block import EngagementStatusBlockSchema
from met_api.schemas.engagement_survey import EngagementSurveySchema
from met_api.schemas.utils import count_comments_by_status
from met_api.utils.datetime import local_datetime
from met_api.schemas.engagement_status_block import EngagementStatusBlockSchema

from .engagement_status import EngagementStatusSchema

Expand Down Expand Up @@ -61,18 +62,14 @@ def get_submissions_meta_data(self, obj):
submissions = obj.surveys[0].submissions
return {
'total': len(submissions),
'pending': self._count_comments_by_status(submissions, CommentStatus.Pending.value),
'approved': self._count_comments_by_status(submissions, CommentStatus.Approved.value),
'rejected': self._count_comments_by_status(submissions, CommentStatus.Rejected.value),
'needs_further_review': self._count_comments_by_status(
'pending': count_comments_by_status(submissions, CommentStatus.Pending.value),
'approved': count_comments_by_status(submissions, CommentStatus.Approved.value),
'rejected': count_comments_by_status(submissions, CommentStatus.Rejected.value),
'needs_further_review': count_comments_by_status(
submissions,
CommentStatus.Needs_further_review.value)
}

def _count_comments_by_status(self, submissios, status):
return len([submission for submission in submissios
if submission.comment_status_id == status])

def get_submission_status(self, obj):
"""Get the submission status of the engagement."""
if obj.status_id == Status.Draft.value or obj.status_id == Status.Scheduled.value:
Expand Down
17 changes: 8 additions & 9 deletions met-api/src/met_api/schemas/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
"""

from marshmallow import EXCLUDE, Schema, fields
from .engagement import EngagementSchema

from met_api.constants.comment_status import Status
from met_api.schemas.utils import count_comments_by_status

from .engagement import EngagementSchema


class SurveySchema(Schema):
Expand Down Expand Up @@ -34,14 +37,10 @@ def get_comments_meta_data(self, obj):
"""Get the meta data of the comments made in the survey."""
return {
'total': len(obj.submissions),
'pending': self._count_comments_by_status(obj.submissions, Status.Pending.value),
'approved': self._count_comments_by_status(obj.submissions, Status.Approved.value),
'rejected': self._count_comments_by_status(obj.submissions, Status.Rejected.value),
'needs_further_review': self._count_comments_by_status(
'pending': count_comments_by_status(obj.submissions, Status.Pending.value),
'approved': count_comments_by_status(obj.submissions, Status.Approved.value),
'rejected': count_comments_by_status(obj.submissions, Status.Rejected.value),
'needs_further_review': count_comments_by_status(
obj.submissions,
Status.Needs_further_review.value)
}

def _count_comments_by_status(self, submissios, status):
return len([submission for submission in submissios
if submission.comment_status_id == status])
16 changes: 16 additions & 0 deletions met-api/src/met_api/schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from typing import Tuple

from jsonschema import Draft7Validator, RefResolver, SchemaError, draft7_format_checker
from met_api.constants.user import SYSTEM_REVIEWER


BASE_URI = 'https://met.gov.bc.ca/.well_known/schemas'
Expand Down Expand Up @@ -115,3 +116,18 @@ def serialize(errors):
for error in errors:
error_message.append(error.message)
return error_message


def count_comments_by_status(submissions, status):
"""Count the comments by their status.
:param submissions: List of submissions
:param status: Status of the comments
:return: Number of comments with the provided status
"""
return len([
submission
for submission in submissions
if (submission.comment_status_id == status and
submission.reviewed_by != SYSTEM_REVIEWER)
])

0 comments on commit c4b004f

Please sign in to comment.