def test_add_submission_similarity(session_scope_module): user = get_user_by_name(session_scope_module, 'test_user') source_submission = get_submission_by_id(session_scope_module, 1) target_submission = get_submission_by_id(session_scope_module, 2) add_submission_similarity(session_scope_module, 'target_credit', user, source_submission, target_submission, 0.5, datetime.datetime.utcnow()) similarity = session_scope_module.query(SubmissionSimilarity).all() assert len(similarity) == 1 similarity = similarity[0] assert similarity.type == 'target_credit' assert similarity.user == user assert similarity.source_submission == source_submission assert similarity.target_submission == target_submission assert similarity.similarity == pytest.approx(0.5) assert isinstance(similarity.timestamp, datetime.datetime)
def credit(submission_hash): """The landing page to credit other submission when a user submit is own. Parameters ---------- submission_hash : str The submission hash of the current submission. """ submission = (Submission.query.filter_by( hash_=submission_hash).one_or_none()) access_code = is_accessible_code(db.session, submission.event_team.event.name, flask_login.current_user.name, submission.name) if submission is None or not access_code: error_str = u'Missing submission: {}'.format(submission_hash) return redirect_to_user(error_str) event_team = submission.event_team event = event_team.event source_submissions = get_source_submissions(db.session, submission.id) def get_s_field(source_submission): return u'{}/{}/{}'.format(source_submission.event_team.event.name, source_submission.event_team.team.name, source_submission.name) # Make sure that CreditForm is empty CreditForm.name_credits = [] credit_form_kwargs = {} for source_submission in source_submissions: s_field = get_s_field(source_submission) setattr(CreditForm, s_field, StringField(u'Text')) credit_form = CreditForm(**credit_form_kwargs) sum_credit = 0 # new = True for source_submission in source_submissions: s_field = get_s_field(source_submission) submission_similaritys = \ (SubmissionSimilarity.query .filter_by( type='target_credit', user=flask_login.current_user, source_submission=source_submission, target_submission=submission) .all()) if not submission_similaritys: submission_credit = 0 else: # new = False # find the last credit (in case crediter changes her mind) submission_similaritys.sort(key=lambda x: x.timestamp, reverse=True) submission_credit = int( round(100 * submission_similaritys[0].similarity)) sum_credit += submission_credit credit_form.name_credits.append( (s_field, str(submission_credit), source_submission.link)) # This doesnt work, not sure why # if not new: # credit_form.self_credit.data = str(100 - sum_credit) if credit_form.validate_on_submit(): try: sum_credit = int(credit_form.self_credit.data) logger.info(sum_credit) for source_submission in source_submissions: s_field = get_s_field(source_submission) sum_credit += int(getattr(credit_form, s_field).data) if sum_credit != 100: return redirect_to_credit( submission_hash, 'Error: The total credit should add up to 100') except Exception as e: return redirect_to_credit(submission_hash, u'Error: {}'.format(e)) for source_submission in source_submissions: s_field = get_s_field(source_submission) similarity = int(getattr(credit_form, s_field).data) / 100. submission_similarity = \ (SubmissionSimilarity.query .filter_by( type='target_credit', user=flask_login.current_user, source_submission=source_submission, target_submission=submission) .all()) # if submission_similarity is not empty, we need to # add zero to cancel previous credits explicitly if similarity > 0 or submission_similarity: add_submission_similarity(db.session, credit_type='target_credit', user=flask_login.current_user, source_submission=source_submission, target_submission=submission, similarity=similarity, timestamp=datetime.datetime.utcnow()) if app.config['TRACK_USER_INTERACTION']: add_user_interaction(db.session, interaction='giving credit', user=flask_login.current_user, event=event, submission=submission) return redirect(u'/events/{}/sandbox'.format(event.name)) admin = is_admin(db.session, event.name, flask_login.current_user.name) return render_template('credit.html', submission=submission, source_submissions=source_submissions, credit_form=credit_form, event=event, admin=admin)