def updatePublicContext(self, context, entity, params): """Updates the context for the public page with information from the entity. Args: context: the context that should be updated entity: a student proposal_entity used to set context params: dict with params for the view using this context """ from soc.logic.models.review import logic as review_logic from soc.logic.models.review_follower import logic as review_follower_logic student_entity = entity.scope context['student'] = student_entity context['student_name'] = student_entity.name() user_entity = user_logic.logic.getForCurrentAccount() # check if the current user is the student if user_entity.key() == student_entity.user.key(): # show the proposal edit link context['edit_link'] = redirects.getEditRedirect(entity, params) # check if the current user is subscribed to this proposal's public reviews fields = {'user': user_entity, 'scope': entity, 'subscribed_public': True} context['is_subscribed'] = review_follower_logic.getForFields(fields, unique=True) context['public_reviews'] = review_logic.getReviewsForEntity(entity, is_public=True, order=['created'])
def _getDefaultReviewContext(self, entity, org_admin, mentor): """Returns the default context for the review page. Args: entity: Student Proposal entity org_admin: org admin entity for the current user/proposal (iff available) mentor: mentor entity for the current user/proposal (iff available) """ from soc.logic.models.review import logic as review_logic from soc.logic.models.review_follower import logic as review_follower_logic context = {} context['student'] = entity.scope context['student_name'] = entity.scope.name() if entity.mentor: context['mentor_name'] = entity.mentor.name() else: context['mentor_name'] = "No mentor assigned" # set the possible mentors in the context possible_mentors = entity.possible_mentors if not possible_mentors: context['possible_mentors'] = "None" else: mentor_names = [] for mentor_key in possible_mentors: possible_mentor = mentor_logic.logic.getFromKeyName( mentor_key.id_or_name()) mentor_names.append(possible_mentor.name()) context['possible_mentors'] = ', '.join(mentor_names) # order the reviews by ascending creation date order = ['created'] # get the public reviews public_reviews = review_logic.getReviewsForEntity(entity, is_public=True, order=order) # get the private reviews private_reviews = review_logic.getReviewsForEntity(entity, is_public=False, order=order) # store the reviews in the context context['public_reviews'] = public_reviews context['private_reviews'] = private_reviews # create a summary of all the private reviews review_summary = {} for private_review in private_reviews: # make sure there is a reviewer reviewer = private_review.reviewer if not reviewer: continue reviewer_key = reviewer.key() reviewer_summary = review_summary.get(reviewer_key) if reviewer_summary: # we already have something on file for this reviewer old_total_score = reviewer_summary['total_score'] reviewer_summary['total_score'] = old_total_score + private_review.score old_total_comments = reviewer_summary['total_comments'] reviewer_summary['total_comments'] = old_total_comments + 1 else: review_summary[reviewer_key] = { 'name': reviewer.name(), 'total_comments': 1, 'total_score': private_review.score} context['review_summary'] = review_summary # which button should we show to the mentor? if mentor: context['is_mentor'] = True if mentor.key() in possible_mentors: # show "No longer willing to mentor" context['remove_me_as_mentor'] = True else: # show "I am willing to mentor" context['add_me_as_mentor'] = True if org_admin: context['is_org_admin'] = True user_entity = user_logic.logic.getForCurrentAccount() # check if the current user is subscribed to public or private reviews fields = {'scope': entity, 'user': user_entity,} follower_entity = review_follower_logic.getForFields(fields, unique=True) if follower_entity: context['is_subscribed_public'] = follower_entity.subscribed_public context['is_subscribed_private'] = follower_entity.subscribed_private return context