def test_get_source_submission(session_scope_module): # since we do not record interaction without the front-end, we should get # an empty list submission_id = 1 submissions = get_source_submissions(session_scope_module, submission_id) assert not submissions # we simulate some user interaction # case 1: the interaction come after the file to be submitted so there # is no submission to show submission = get_submission_by_id(session_scope_module, submission_id) event = submission.event_team.event user = submission.event_team.team.admin add_user_interaction(session_scope_module, user=user, interaction='looking at submission', event=event, submission=get_submission_by_id( session_scope_module, 2)) submissions = get_source_submissions(session_scope_module, submission_id) assert not submissions # case 2: we postpone the time of the submission to simulate that we # already check other submission. submission.submission_timestamp += datetime.timedelta(days=1) submissions = get_source_submissions(session_scope_module, submission_id) assert submissions assert all( [sub.event_team.event.name == event.name for sub in submissions])
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)