app/soc/views/models/student_proposal.py
changeset 1715 3ec1a9518452
parent 1666 cdb7e5581694
child 1741 0da1285f5bc0
equal deleted inserted replaced
1714:bfdef9380954 1715:3ec1a9518452
   181                      (3,'3: Almost there'),
   181                      (3,'3: Almost there'),
   182                      (4,'4: Made. Of. Awesome.')]
   182                      (4,'4: Made. Of. Awesome.')]
   183         },
   183         },
   184         {'name': 'comment',
   184         {'name': 'comment',
   185          'base': forms.CharField,
   185          'base': forms.CharField,
   186          'widget': forms.Textarea,
   186          'widget': widgets.FullTinyMCE(attrs={'rows': 10, 'cols': 40}),
   187          'label': 'Comment',
   187          'label': 'Comment',
   188          'required': False,
   188          'required': False,
   189          },
   189          },
   190         {'name': 'public',
   190         {'name': 'public',
   191          'base': forms.BooleanField,
   191          'base': forms.BooleanField,
   396 
   396 
   397     # get the context for this webpage
   397     # get the context for this webpage
   398     context = responses.getUniversalContext(request)
   398     context = responses.getUniversalContext(request)
   399     responses.useJavaScript(context, params['js_uses_all'])
   399     responses.useJavaScript(context, params['js_uses_all'])
   400     context['page_name'] = page_name
   400     context['page_name'] = page_name
   401 
       
   402     context['student_name'] = entity.scope.name()
       
   403 
       
   404     if entity.mentor:
       
   405       context['mentor_name'] = entity.mentor.name()
       
   406     else:
       
   407       context['mentor_name'] = "No mentor assigned"
       
   408 
       
   409     context['entity'] = entity
   401     context['entity'] = entity
   410     context['entity_type'] = params['name']
   402     context['entity_type'] = params['name']
   411     context['entity_type_url'] = params['url_name']
   403     context['entity_type_url'] = params['url_name']
   412 
   404 
   413     # get the roles important for reviewing an application
   405     # get the roles important for reviewing an application
   452     # populate the form using the POST data
   444     # populate the form using the POST data
   453     form = form(request.POST)
   445     form = form(request.POST)
   454 
   446 
   455     if not form.is_valid():
   447     if not form.is_valid():
   456       # return the invalid form response
   448       # return the invalid form response
       
   449       # get all the extra information that should be in the context
       
   450       review_context = self._getDefaultReviewContext(entity, org_admin, mentor)
       
   451       context = dicts.merge(context, review_context)
       
   452 
   457       return self._constructResponse(request, entity=entity, context=context,
   453       return self._constructResponse(request, entity=entity, context=context,
   458           form=form, params=params, template=params['review_template'])
   454           form=form, params=params, template=params['review_template'])
   459 
   455 
   460     fields = form.cleaned_data
   456     fields = form.cleaned_data
   461 
   457 
   462     if org_admin:
   458     if org_admin:
   463       # org admin found, try to adjust the assigned mentor
   459       # org admin found, try to adjust the assigned mentor
   464       self._adjustMentor(entity, fields['mentor'])
   460       self._adjustMentor(entity, fields['mentor'])
       
   461       reviewer = org_admin
       
   462     else:
       
   463       # might be None (if Host or Developer is commenting)
       
   464       reviewer = mentor
   465 
   465 
   466     is_public = fields['public']
   466     is_public = fields['public']
   467     comment = fields['comment']
   467     comment = fields['comment']
   468     given_score = int(fields['score'])
   468     given_score = int(fields['score'])
   469 
   469 
   470     if (org_admin or mentor) and (not is_public) and (given_score is not 0):
   470     if reviewer and (not is_public) and (given_score is not 0):
   471       # if it is not a public comment and it's made by a member of the
   471       # if it is not a public comment and it's made by a member of the
   472       # organization we score and display an additional message in the comment
   472       # organization we update the score of the proposal
   473       new_score = given_score + entity.score
   473       new_score = given_score + entity.score
   474 
       
   475       if org_admin:
       
   476         name = org_admin.name()
       
   477       elif mentor:
       
   478         name = mentor.name()
       
   479 
       
   480       # TODO(ljvderijk) hook up comments
       
   481       comment = '%s has given %i points \n %s' % (name, given_score, comment)
       
   482 
   474 
   483       properties = {'score': new_score}
   475       properties = {'score': new_score}
   484 
   476 
   485       # if the proposal is new we change it status to pending
   477       # if the proposal is new we change it status to pending
   486       if entity.status == 'new':
   478       if entity.status == 'new':
   487         properties['status'] = 'pending'
   479         properties['status'] = 'pending'
   488 
   480 
   489       # update the proposal with the new score
   481       # update the proposal with the new score
   490       self._logic.updateEntityProperties(entity, properties)
   482       self._logic.updateEntityProperties(entity, properties)
       
   483 
       
   484     # create the review entity
       
   485     if comment or (given_score is not 0):
       
   486       self._createReviewFor(entity, reviewer, comment, given_score, is_public)
   491 
   487 
   492     # redirect to the same page
   488     # redirect to the same page
   493     return http.HttpResponseRedirect('')
   489     return http.HttpResponseRedirect('')
   494 
   490 
   495   def reviewGet(self, request, context, params, entity, form,
   491   def reviewGet(self, request, context, params, entity, form,
   510     if org_admin and entity.mentor:
   506     if org_admin and entity.mentor:
   511       # set the mentor field to the current mentor
   507       # set the mentor field to the current mentor
   512       initial['mentor'] = entity.mentor.link_id
   508       initial['mentor'] = entity.mentor.link_id
   513 
   509 
   514     context['form'] = form(initial)
   510     context['form'] = form(initial)
       
   511 
       
   512     # get all the extra information that should be in the context
       
   513     review_context = self._getDefaultReviewContext(entity, org_admin, mentor)
       
   514     context = dicts.merge(context, review_context)
       
   515 
   515     template = params['review_template']
   516     template = params['review_template']
       
   517 
       
   518     return responses.respond(request, template, context=context)
       
   519 
       
   520   def _getDefaultReviewContext(self, entity, org_admin,
       
   521                                mentor):
       
   522     """Returns the default context for the review page
       
   523 
       
   524     Args:
       
   525       entity: Student Proposal entity
       
   526       org_admin: org admin entity for the current user/proposal (iff available)
       
   527       mentor: mentor entity for the current user/proposal (iff available)
       
   528     """
       
   529 
       
   530     from soc.logic.models.review import logic as review_logic
       
   531 
       
   532     context = {}
       
   533 
       
   534     context['student_name'] = entity.scope.name()
       
   535 
       
   536     if entity.mentor:
       
   537       context['mentor_name'] = entity.mentor.name()
       
   538     else:
       
   539       context['mentor_name'] = "No mentor assigned"
   516 
   540 
   517     # set the possible mentors in the context
   541     # set the possible mentors in the context
   518     possible_mentors = entity.possible_mentors
   542     possible_mentors = entity.possible_mentors
   519 
   543 
   520     if not possible_mentors:
   544     if not possible_mentors:
   521       context['possible_mentors'] = "None"
   545       context['possible_mentors'] = "None"
   522     else:
   546     else:
   523       mentor_names = []
   547       mentor_names = []
   524 
   548 
   525       for mentor_key in possible_mentors:
   549       for mentor_key in possible_mentors:
   526         mentor = mentor_logic.logic.getFromKeyName(mentor_key.name())
   550         possible_mentor = mentor_logic.logic.getFromKeyName(mentor_key.name())
   527         mentor_names.append(mentor.name())
   551         mentor_names.append(possible_mentor.name())
   528 
   552 
   529       context['possible_mentors'] = ', '.join(mentor_names)
   553       context['possible_mentors'] = ', '.join(mentor_names)
   530 
   554 
       
   555     # TODO(ljvderijk) listing of total given scores per mentor
       
   556     # a dict with key as role.user ?
       
   557 
       
   558     # get the public reviews
       
   559     fields = {'scope': entity,
       
   560               'is_public': True}
       
   561 
       
   562     order = ['modified']
       
   563 
       
   564     query = review_logic.getQueryForFields(filter=fields, order=order)
       
   565     context['public_reviews'] = review_logic.getAll(query)
       
   566 
       
   567     # get the private reviews
       
   568     fields['is_public'] = False
       
   569 
       
   570     query = review_logic.getQueryForFields(filter=fields, order=order)
       
   571     context['private_reviews'] = review_logic.getAll(query)
       
   572 
       
   573     # which button should we show to the mentor?
   531     if mentor:
   574     if mentor:
   532       if mentor.key() in possible_mentors:
   575       if mentor.key() in possible_mentors:
   533         # show "No longer willing to mentor"
   576         # show "No longer willing to mentor"
   534         context['remove_me_as_mentor'] = True
   577         context['remove_me_as_mentor'] = True
   535       else:
   578       else:
   536         # show "I am willing to mentor"
   579         # show "I am willing to mentor"
   537         context['add_me_as_mentor'] = True
   580         context['add_me_as_mentor'] = True
   538 
   581 
   539     return responses.respond(request, template, context=context)
   582     return context
   540 
   583 
   541   def _adjustPossibleMentors(self, entity, mentor, choice):
   584   def _adjustPossibleMentors(self, entity, mentor, choice):
   542     """Adjusts the possible mentors list for a proposal.
   585     """Adjusts the possible mentors list for a proposal.
   543 
   586 
   544     Args:
   587     Args:
   591 
   634 
   592     # update the proposal
   635     # update the proposal
   593     properties = {'mentor': mentor_entity}
   636     properties = {'mentor': mentor_entity}
   594     self._logic.updateEntityProperties(entity, properties)
   637     self._logic.updateEntityProperties(entity, properties)
   595 
   638 
       
   639   def _createReviewFor(self, entity, reviewer, comment, score, is_public):
       
   640     """Creates a review for the given proposal.
       
   641 
       
   642     Args:
       
   643       entity: Student Proposal entity for which the review should be created
       
   644       reviewer: A role entity of the reviewer (if possible, else None)
       
   645       comment: The textual contents of the review
       
   646       score: The score of the review (only used if the review is not public)
       
   647       is_public: Determines if the review is a public review
       
   648 
       
   649     Returns:
       
   650       - The newly created review
       
   651     """
       
   652 
       
   653     import time
       
   654 
       
   655     from soc.logic.models.review import logic as review_logic
       
   656 
       
   657     # create the fields for the review entity
       
   658     fields = {'link_id': 't%i' %(time.time()),
       
   659         'scope': entity,
       
   660         'scope_path': entity.key().name(),
       
   661         'author': user_logic.logic.getForCurrentAccount(),
       
   662         'content': comment,
       
   663         'is_public': is_public,
       
   664         'reviewer': reviewer
       
   665         }
       
   666 
       
   667     # add the given score if the review is not public
       
   668     if not is_public:
       
   669       fields['score'] = score
       
   670 
       
   671     key_name = review_logic.getKeyNameFromFields(fields)
       
   672 
       
   673     return review_logic.updateOrCreateFromKeyName(fields, key_name)
       
   674 
       
   675 
   596 view = View()
   676 view = View()
   597 
   677 
   598 admin = decorators.view(view.admin)
   678 admin = decorators.view(view.admin)
   599 apply = decorators.view(view.apply)
   679 apply = decorators.view(view.apply)
   600 create = decorators.view(view.create)
   680 create = decorators.view(view.create)