Skip to content

Commit

Permalink
Upgrades to Issue Tracking Table (#2192)
Browse files Browse the repository at this point in the history
* Changes to show all survey results to superusers

* removing hard coded values

* fixing linting

* splitting to seperate end points

* fixing auth check

* fixing linting

* merging method in service

* Handle no data error for graphs

* adding new nodata component

* adding new email for submission response

* fixing linting and testing

* Upgrades to Issue Tracking Table

* removing try catch
  • Loading branch information
VineetBala-AOT authored Sep 14, 2023
1 parent 069983b commit e035e3c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 29 deletions.
23 changes: 23 additions & 0 deletions met-api/src/met_api/constants/export_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright © 2021 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Constants for export comments to sheet."""
from enum import Enum


class RejectionReason(Enum):
"""Rejection Reason."""

has_personal_info = 'Contains personal information'
has_profanity = 'Contains profanity or inappropriate language'
has_threat = 'Contains threat/menace'
Binary file not shown.
23 changes: 10 additions & 13 deletions met-api/src/met_api/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from met_api.models.submission import Submission
from met_api.models.survey import Survey
from met_api.schemas.comment import CommentSchema
from met_api.schemas.submission import SubmissionSchema

from .base_model import BaseModel
from .comment_status import CommentStatus as CommentStatusModel
Expand Down Expand Up @@ -219,16 +220,12 @@ def update(cls, submission_id, comment: CommentSchema, session=None) -> Comment:
@classmethod
def get_comments_by_survey_id(cls, survey_id):
"""Get comments paginated."""
query = db.session.query(
Comment.id,
Comment.submission_date,
Comment.text,
Submission.reviewed_by
)\
.join(Submission, Submission.id == Comment.submission_id) \
.add_entity(Submission)\
.filter(Comment.survey_id == survey_id)

query = query.order_by(Comment.id.asc())

return query.all()
null_value = None
query = db.session.query(Submission)\
.join(Comment, Submission.id == Comment.submission_id)\
.filter(and_(Submission.survey_id == survey_id,
or_(Submission.reviewed_by != 'System', Submission.reviewed_by == null_value)))

query = query.order_by(Submission.id.asc())
items = query.all()
return SubmissionSchema(many=True, exclude=['submission_json']).dump(items)
82 changes: 67 additions & 15 deletions met-api/src/met_api/services/comment_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Service for comment management."""
import itertools
from datetime import datetime

from met_api.constants.comment_status import Status
from met_api.constants.membership_type import MembershipType
from met_api.constants.export_comments import RejectionReason
from met_api.models import Survey as SurveyModel
from met_api.models.comment import Comment
from met_api.models.engagement_metadata import EngagementMetadataModel
from met_api.models.membership import Membership as MembershipModel
from met_api.models.pagination_options import PaginationOptions
from met_api.models.submission import Submission as SubmissionModel
Expand Down Expand Up @@ -165,25 +166,76 @@ def export_comments_to_spread_sheet(cls, survey_id):
)
authorization.check_auth(one_of_roles=one_of_roles, engagement_id=engagement.engagement_id)
comments = Comment.get_comments_by_survey_id(survey_id)
formatted_comments = [
{
'commentNumber': comment.id,
'dateSubmitted': str(comment.submission_date),
'author': '',
'commentText': comment.text,
'reviewer': comment.reviewed_by,
'exportDate': str(datetime.utcnow())
}
for comment in comments]

data = {
'comments': formatted_comments
metadata_model = EngagementMetadataModel.find_by_id(engagement.engagement_id)
project_name = metadata_model.project_metadata.get('project_name') if metadata_model else None

titles = cls.get_titles(comments)
data_rows = cls.get_data_rows(titles, comments, project_name)

formatted_comments = {
'titles': titles,
'comments': data_rows
}
document_options = {
'document_type': GeneratedDocumentTypes.COMMENT_SHEET.value,
'template_name': 'staff_comments_sheet.xlsx',
'convert_to': 'csv',
'report_name': 'comments_sheet'
}
return DocumentGenerationService().generate_document(data=formatted_comments, options=document_options)

@classmethod
def get_titles(cls, comments):
"""Get the titles to be displayed on the sheet."""
# Title could be dynamic based on the number of comment type questions on the survey
return [{'label': label.get('label', None)} for label in comments[0].get('comments')]

@classmethod
def get_data_rows(cls, titles, comments, project_name):
"""Get the content to be exported on to the sheet."""
data_rows = []

for comment in comments:
comment_text = cls.get_comment_text(titles, comment)

return DocumentGenerationService().generate_document(data=data, options=document_options)
rejection_note = cls.get_rejection_note(comment)

data_rows.append({
'commentNumber': comment.get('id'),
'dateSubmitted': str(comment.get('created_date')),
'commentText': comment_text,
'status': Status(comment.get('comment_status_id')).name,
'datePublished': str(comment.get('review_date')),
'rejectionNote': rejection_note,
'reviewer': comment.get('reviewed_by'),
'projectName': project_name
})

return data_rows

@classmethod
def get_comment_text(cls, titles, comment):
"""Get the comments to be exported to the sheet."""
comments = [{'text': text.get('text', None)} for text in comment.get('comments')]
# Making sure that the number of comments matches the number of questions on the comment to keep
# the layout intact. In case user has not responded to a question the column value should be
# blank.
comments.extend([{'text': ''}] * (len(titles) - len(comments)))

return comments

@classmethod
def get_rejection_note(cls, comment):
"""Get the rejection note."""
rejection_note = []

if comment.get('has_personal_info'):
rejection_note.append(RejectionReason.has_personal_info.value)
if comment.get('has_profanity'):
rejection_note.append(RejectionReason.has_profanity.value)
if comment.get('has_threat'):
rejection_note.append(RejectionReason.has_threat.value)
if comment.get('rejected_reason_other'):
rejection_note.append(comment.get('rejected_reason_other'))

return ', '.join(rejection_note)
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const Submissions = () => {
const handleExportComments = async () => {
setIsExporting(true);
const response = await getCommentsSheet({ survey_id: survey.id });
downloadFile(response, `${survey.engagement?.name || ''} - ${formatToUTC(Date())}.csv`);
downloadFile(response, `INTERNAL ONLY - ${survey.engagement?.name || ''} - ${formatToUTC(Date())}.csv`);
setIsExporting(false);
};

Expand Down

0 comments on commit e035e3c

Please sign in to comment.