def handle_submission():
    req = request.form
    s = Student.query.filter_by(email=req["email"]).one_or_none()
    c = Contest.query.filter_by(id=contest_id).one_or_none()
    if (not c.active):
        return redirect(url_for(".contest", status="expired"))
    if (s == None):
        return redirect(url_for(".contest", status="dne"))
    salted_password = hashlib.md5((req["password"] + SALT).encode())
    if (s.password != salted_password.hexdigest()):
        return redirect(url_for(".contest", status="incorrect"))
    if (s.email == req["email"] and s.password == salted_password.hexdigest()):
        answers = []
        for i in range(1, 16):
            if (len(req[f"q{i}"]) == 0):
                answers.append(-1)
            else:
                answers.append(int(req[f"q{i}"]))
        submission = Submission.query.filter_by(student_id=s.id).filter_by(
            contest_id=contest_id).one_or_none()
        if (submission == None):
            submission = Submission()
        submission.populate(s.id, contest_id, answers, -1, -1)
        db.session.add(submission)
        db.session.commit()
        return redirect(url_for(".submitted"))
    else:
        return "Error, please contact us at [email protected]"
Exemple #2
0
def update_submissions(subreddit):
    """
    Pulls new submissions from Reddit API and stores them in Postgres
    :return:
    """
    submissions = list(get_new_submissions(subreddit))

    check_ids = ', '.join(f"('{sub.id}')" for sub in submissions)
    stmt = text("""
        select t.id 
        from (
          values """ + check_ids + """
        ) as t(id)
        left join submission s on s.reddit_id = t.id
        where s.id is null;
    """)

    with engine.connect() as conn:
        result = conn.execute(stmt).fetchall()

        if not result:  # No new submissions from subreddit
            return False

        safe_ids = [r[0] for r in result]

    with get_session() as sess:
        for submission in submissions:
            if submission.id in safe_ids:
                sub = Submission()
                sub.from_praw_object(submission)
                sess.add(sub)
    def process_zipfile(self, zfile):
        """
        extracts the uploaded zipfile and saves the files to the database for one user_id.
        files are concatenated into one file.
        filters out binary files.
        :param zfile:
        :return: none
        """
        with ZipFile(zfile) as zip:

            file_paths = [file for file in zip.namelist() if not file.endswith("/")]
            submission = Submission()
            submission.file_contents = ""
            submission.filename = os.path.basename(file_paths[0])
            submission.user_id = self.user_id
            print(submission.user_id)
            submission.description = self.description

            for file_path in file_paths:
                    # check if file is text and not binary
                    filetype = mimetypes.guess_type(file_path)[0]
                    # filters out mac generated files which aren't detected by mimetypes
                    if not file_path.__contains__("__MACOSX") and "text/" in filetype:
                        # concatenate files
                        file = zip.open(file_path)
                        file_content = file.read().decode('utf-8', "ignore")
                        # append file to other files already in the submission
                        submission.file_contents += file_content

            # discard the submission if the file contents is zero lenth
            if len(submission.file_contents) != 0:
                    self.submissions.append(submission)
Exemple #4
0
def submission_creator(self, serializer):
    """Method for creating new submissions

    Arguments:
        serializer - passed from the views class, contains POST information

    Returns:
        HttpResponse - whether creation was successful or not
    """

    try:
        assessment_id = self.request.POST['assessment_id']
        language = self.request.POST['language']
        submission_file = self.request.FILES['submission']
        late = self.request.POST['late']
    except MultiValueDictKeyError:
        return HttpResponseBadRequest(
            "Looks like you have an empty upload field.")
    except DataError:
        return HttpResponseBadRequest(
            "Looks like you have an empty dropdown field.")
    except Exception as e:
        print(e)
        logger.error(e)
        return HttpResponseBadRequest("Unexpected error.")

    if language == "undefined":
        return HttpResponseBadRequest(
            "Looks like you have an empty language field.")

    assessment = Assessment.objects.get(id=assessment_id)
    course = Course.objects.get(id=assessment.course_id)

    if self.request.user not in course.students.all():
        return HttpResponseForbidden(
            "You cannot submit to a course you are not enrolled in.")

    submission = Submission(filename=submission_file.name,
                            status="start",
                            result="fail",
                            info="Awaiting result...",
                            marks=0,
                            user_id=self.request.user.id,
                            assessment_id=assessment_id,
                            timeTaken=0,
                            language=language,
                            late=late)
    submission.save()

    os.makedirs(os.path.join(settings.MEDIA_ROOT, assessment_id, 'submissions',
                             str(submission.id)),
                exist_ok=True)
    fs = FileSystemStorage(location=os.path.join(
        settings.MEDIA_ROOT, assessment_id, 'submissions', str(submission.id)))

    filename = fs.save(submission_file.name, submission_file)
    uploaded_file_url = fs.url(filename)

    return Response(submission.id, 201)
Exemple #5
0
 def test_submission_model_can_make_winner(self, mock_increase_score):
     submission1 = Submission('a/b/c', 'abcdef', user_id=self.user2.id, post_id=self.post.id)
     db.session.add(submission1)
     submission1.make_winner()
     db.session.commit()
     self.assertTrue(submission1.won)
     self.assertRaises(Exception, self.submission.make_winner)
     mock_increase_score.assert_has_calls([call(50), call(50), call(100), call(104)])
Exemple #6
0
 def test_post_model_get_archived(self):
     user = User('a', '*****@*****.**', '12312')
     db.session.add(user)
     db.session.commit()
     date = datetime.today() - timedelta(10)
     post = Post('Title', 'Body', user_id=user.id, publish_time=date, difficulty=3)
     db.session.add(post)
     db.session.commit()
     self.assertEqual(Post.get_archived_posts(), [])
     submission = Submission('a/b/c', 'asdf', post_id=post.id, user_id=user.id)
     submission.won = True
     db.session.add(submission)
     db.session.commit()
     self.assertEqual(Post.get_archived_posts(), [post])
Exemple #7
0
def submit():
    data = request.json
    answers = [(k, v) for k, v in data["answers"].items()]
    incorrect = []
    correctAnswers = []
    correctChoices = []
    for answer in answers:
        correctAnswer = AnswerJoin.query.filter(AnswerJoin.questionId == int(answer[0])).first().answerChoiceId
        correctAnswers.append(correctAnswer)
        if int(answer[1]) != correctAnswer:
            incorrect.append(answer[1])
        else:
            correctChoices.append(answer[1])
    newSubmission = Submission(userId=data["userId"],
                                quizId=data["quizId"],
                                score=int(((len(answers) - len(incorrect))/len(answers)) * 100))
    db.session.add(newSubmission)
    db.session.commit()
    submission = Submission.query.filter(Submission.userId == data["userId"]).order_by(Submission.id.desc()).first()
    for answerChoice in incorrect:
        wrongAnswer = IncorrectAnswers(submissionId=submission.id, answerChoiceId=answerChoice)
        db.session.add(wrongAnswer)
    db.session.commit()
    return  {"quizId": int(data["quizId"]), "current":
            {"score": submission.score, "incorrectChoices": incorrect, "correctChoices": correctChoices, "correctAnswers": correctAnswers}}
def test_comment_sentiment_avg(session):
    s = Submission.create(session, **const.MOCK_SUBMISSION)

    c1 = Comment.create(session, **const.MOCK_COMMENT1)
    c1_sentiment = sentiment.comment_sentiment(c1)
    c1s = CommentSentiment.create(session, **c1_sentiment)

    c2 = Comment.create(session, **const.MOCK_COMMENT2)
    c2_sentiment = sentiment.comment_sentiment(c2)
    c2s = CommentSentiment.create(session, **c2_sentiment)

    comments = session.query(Comment).\
        options(joinedload('sentiment')).\
        all()

    comment_sentiments = []
    for c in comments:
        comment_sentiments.append({
            "polarity": c.sentiment.polarity,
            "subjectivity": c.sentiment.subjectivity
        })

    csa = sentiment.comment_sentiment_avg(comment_sentiments)
    csa.update({'submission_id': s.id})

    expected_keys = ['submission_id', 'polarity', 'subjectivity']
    assert sorted(csa.keys()) == sorted(expected_keys)

    assert isinstance(csa['polarity'], float)
    assert isinstance(csa['subjectivity'], float)
def test_submissionsentiment_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)

    c1 = Comment.create(session, **MOCK_COMMENT1)
    c1_sentiment = comment_sentiment(c1)
    c1s = CommentSentiment.create(session, **c1_sentiment)

    c2 = Comment.create(session, **MOCK_COMMENT2)
    c2_sentiment = comment_sentiment(c2)
    c2s = CommentSentiment.create(session, **c2_sentiment)

    comments = session.query(Comment).options(joinedload("sentiment")).all()

    comment_sentiments = []
    for c in comments:
        comment_sentiments.append({"polarity": c.sentiment.polarity, "subjectivity": c.sentiment.subjectivity})

    submission_sentiment = comment_sentiment_avg(comment_sentiments)
    submission_sentiment.update({"submission_id": s.id})

    ss = SubmissionSentiment.create(session, **submission_sentiment)

    submission_sentiments = session.query(SubmissionSentiment).all()
    ss1 = submission_sentiments[0]

    # test object form
    assert ss1.id == 1
    assert ss1.submission_id == s.id
    for k in submission_sentiment.keys():
        assert getattr(ss1, k) == submission_sentiment[k]

    # test values
    assert ss1.polarity < 0.5 and ss1.polarity > -0.5
    assert ss1.subjectivity > 0.8
def test_commentsentiment_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)

    c1 = Comment.create(session, **MOCK_COMMENT1)
    c1_sentiment = comment_sentiment(c1)
    c1s = CommentSentiment.create(session, **c1_sentiment)

    c2 = Comment.create(session, **MOCK_COMMENT2)
    c2_sentiment = comment_sentiment(c2)
    c2s = CommentSentiment.create(session, **c2_sentiment)

    # test object form
    for k in c1_sentiment.keys():
        assert getattr(c1s, k) == c1_sentiment[k]

    # test relationship
    assert c1.sentiment == c1s

    # test values
    assert c1s.id == 1
    assert c1s.polarity > 0.5
    assert c1s.subjectivity > 0.8
    assert c2s.id == 2
    assert c2s.polarity < -0.5
    assert c2s.subjectivity > 0.8
Exemple #11
0
def db_seed():
    """"Converts sentiment data from text files (original form) to app database.

        Usage: To be run when connecting to a empty database for the first time.

        Returns: 3000 instances of 'seed' sentiment data for ML model"""
    # Aggregate .txt files
    data = []
    for file in seed_datafiles:
        with open(file, "r") as fin:
            data += fin.read().split('\n')
    # Data cleanup
    data = [
        line.split('\t') for line in data
        if len(line.split('\t')) == 2 and line.split("\t")[1] != ''
    ]
    # Format data into list of tuples ('sentence', 'is_good')
    sentence = [item[0] for item in data]
    is_good = [int(item[1]) for item in data]
    submissions = zip(sentence, is_good)
    # Add dummy user
    u = User(username='******', email='*****@*****.**')
    db.session.add(u)
    # Push to database
    for submission in submissions:
        s = Submission(sentence=submission[0], is_good=submission[1], author=u)
        db.session.add(s)
    db.session.commit()
Exemple #12
0
    def test_post_model_is_archived(self):
        user = User('dan', '*****@*****.**', '12345')
        db.session.add(user)
        db.session.commit()

        post = Post('Title', 'Body', user.id, difficulty=5)
        db.session.add(post)
        db.session.commit()
        self.assertFalse(post.is_archived())

        submission = Submission('a/b/c', 'abcdef', user_id=user.id, post_id=post.id)
        submission.won = True
        db.session.add(submission)
        db.session.commit()

        self.assertTrue(post.is_archived())
Exemple #13
0
def submit_standup_handler(**kwargs):
    payload = kwargs.get("data")
    standup_submission = json.dumps(payload.get("view"))

    if payload and utils.is_submission_eligible(payload):
        user_payload = payload.get("user", {})
        _, team_name = payload.get("view", {}).get("callback_id",
                                                   "").split("%")

        user = User.query.filter_by(user_id=user_payload.get("id")).first()
        standup = Standup.query.filter(Standup.trigger == team_name).first()

        todays_datetime = datetime(datetime.today().year,
                                   datetime.today().month,
                                   datetime.today().day)

        is_edit = False
        if submission := utils.submission_exists(user, standup):
            client.chat_postMessage(channel=user.user_id,
                                    text=constants.SUBMISSION_UPDATED_MESSAGE)
            submission.standup_submission = standup_submission
            is_edit = True
        else:
            submission = Submission(user_id=user.id,
                                    standup_submission=standup_submission,
                                    standup_id=standup.id,
                                    standup=standup)

        db.session.add(submission)
        db.session.commit()
Exemple #14
0
def submission(request, id):
    competition = get_object_or_404(Competition, pk=id)

    if request.method == "POST":
        # 今日のクエリ数と終了時刻から外れているかの確認
        today = datetime.datetime.now(pytz.timezone('Asia/Tokyo'))
        today_query = competition.submission.filter(
            user=request.user,
            submission_date__year=today.year,
            submission_date__month=today.month,
            submission_date__day=today.day)
        isOk = True
        is_expired = 1
        if competition.end_date > datetime.datetime.now(
                pytz.timezone('Asia/Tokyo')):
            is_expired = 0
            if len(today_query) >= competition.max_submission:
                isOk = False

        if isOk:
            result = request.POST["result"].rstrip("\n").split("\n")
            if len(result) == competition.data_number:
                # submissionされたデータがすべて数字であるかの確認
                try:
                    map(float, result)
                except:
                    competition.errors = True
                    return render(request, 'submission.html',
                                  {"competition": competition})

                # スコアの導出
                mid_score, f_score = scoring(result, competition)
                temp = Submission(user=request.user,
                                  competition_name=competition,
                                  mid_result=mid_score,
                                  f_result=f_score,
                                  submission_date=datetime.datetime.now(),
                                  short_comment=request.POST["short_comment"],
                                  is_expired=is_expired)
                temp.save()

                return redirect("app:competition_detail", id=id)

        competition.errors = True

    return render(request, 'submission.html', {"competition": competition})
Exemple #15
0
    def get(self, sha1):
        """Finds reports by the SHA-1."""

        submission = Submission().query.filter_by(sha1=sha1).first()
        if submission:
            return json.loads(submission.data), 200

        return {"message" : "Sorry, that SHA-1 does not exist in our database."}, 404
Exemple #16
0
def submission(request,id):
    competition = get_object_or_404(Competition, pk=id)


    if request.method =="POST":
        # 今日のクエリ数と終了時刻から外れているかの確認
        today = datetime.datetime.now(pytz.timezone('Asia/Tokyo'))
        today_query = competition.submission.filter(user=request.user,
                                                    submission_date__year=today.year,
                                                    submission_date__month=today.month,
                                                    submission_date__day=today.day)
        isOk = True
        is_expired = 1
        if competition.end_date > datetime.datetime.now(pytz.timezone('Asia/Tokyo')):
            is_expired = 0
            if len(today_query) >= competition.max_submission:
                isOk = False

        if isOk:
            result = request.POST["result"].rstrip("\n").split("\n")
            if len(result) == competition.data_number:
                # submissionされたデータがすべて数字であるかの確認
                try:
                    map(float,result)
                except:
                    competition.errors = True
                    return render(request, 'submission.html', {"competition": competition})

                # スコアの導出
                mid_score, f_score = scoring(result,competition)
                temp = Submission(user=request.user,
                                  competition_name=competition,
                                  mid_result=mid_score,
                                  f_result=f_score,
                                  submission_date=datetime.datetime.now(),
                                  short_comment=request.POST["short_comment"],
                                  is_expired=is_expired)
                temp.save()


                return redirect("app:competition_detail",id=id)

        competition.errors = True

    return render(request, 'submission.html', {"competition": competition})
Exemple #17
0
def uploads_signatures(filename):
    query = Submission.get_submission_by_signature(signature_filename=filename)
    # if elevated user or submission owner or major professor
    if current_user.has_roles(['admin', 'viewer', 'reviewer', 'helper']) or \
       current_user.id == query.user_id or \
       current_user.net_id == query.professor:
        return send_from_directory(current_app.config['SIGNATURE_FOLDER'], query.signature_file)
    else:
        return redirect(url_for('main.index'))
Exemple #18
0
async def check():
    files = await request.files
    if 'file' not in files:
        return jsonify({'status': 400, 'err': "Please upload a file."})

    file = files['file']
    if file.filename == '':
        return jsonify({'status': 400, 'err': "Please upload a file."})

    test_name, extension = os.path.splitext(file.filename)
    if extension != '.py':
        return jsonify({'status': 400, 'err': "Only python."})

    problem_db = Problem.query.filter_by(test_folder=test_name).first()
    if not problem_db:
        return jsonify({'status': 400, 'err': "No test with that name."})

    dirname = f'script/{current_user.username}'
    filename = test_name + ' - ' + ''.join(
        random.choices(string.ascii_uppercase + string.digits, k=6)) + '.py'
    save_path = f'{dirname}/{filename}'

    if not os.path.exists(dirname):
        os.mkdir(dirname)
    file.save(save_path)

    result = await asyncio.get_running_loop().run_in_executor(
        None,
        functools.partial(check_script, save_path, test_name,
                          problem_db.max_time))

    if result['status'] == 406:
        os.unlink(save_path)
        return jsonify(result)

    best = Submission.query.filter_by(problem=problem_db,
                                      user=current_user).order_by(
                                          desc(Submission.score)).first()
    submit_db = True
    if best and best.score > result['score']:
        os.unlink(save_path)
        submit_db = False

    if submit_db:
        user_submission = Submission(score=result['score'],
                                     file=save_path,
                                     problem=problem_db,
                                     user=current_user)
        if best:
            current_user.score -= best.score
        current_user.score += result['score']

        db.session.add(user_submission)
        db.session.commit()

    return jsonify(result)
def test_submission_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)

    db_submissions = session.query(Submission).all()

    assert len(db_submissions) == 1

    db_s = db_submissions[0]
    for k in MOCK_SUBMISSION.keys():
        assert getattr(db_s, k) == MOCK_SUBMISSION[k]
Exemple #20
0
def uploads_submissions(filename):
    query = Revision.get_revision_by_filename(filename=filename)
    submission = Submission.get_submission_by_id(submission_id=query.submission_id)
    # if elevated user or submission owner or major professor
    if current_user.has_roles(['admin', 'viewer', 'reviewer', 'helper']) or \
       current_user.id == submission.user_id or \
       current_user.net_id == submission.professor:
        return send_from_directory(current_app.config['SUBMISSION_FOLDER'], query.file)
    else:
        return redirect(url_for('main.index'))
Exemple #21
0
def scan_file():
    """Analyzes a file if it is new or returns an existing analysis."""

    # Get the filename, contents and SHA-1.
    uploaded_file = request.files["file"]
    filename = secure_filename(uploaded_file.filename)
    contents = uploaded_file.read()
    sha1 = Hashes().get_sha1(contents)

    # Redirect the user to the analysis page
    # if the file was already analyzed.
    submission = Submission().query.filter_by(sha1=sha1).first()
    if submission:
        save_log(filename, submission.id, current_user.id)
        return redirect("/analysis?sha1={}&name={}".format(sha1, filename))

    # Save the file at the default samples folder.
    file_path = save_file(sha1, contents)

    # Get the VirusTotal report if it exists, else
    # send the file to analysis.
    virustotal = VirusTotal(current_user.vt_key)
    virustotal_detection = virustotal.report(sha1)
    if virustotal_detection["response_code"] == 0:
        virustotal_detection = virustotal.detection(contents)

    # Get hashes and basic information.
    hashes = Hashes(contents).get_all()
    basic_information = get_basic_information(file_path)
    pe_info = None
    capa_data = None
    data = {"file_name" : filename,
            "hashes" : hashes,
            "basic_information" : basic_information,
            "virustotal_detection" : virustotal_detection,
            "yara" : YaraAnalysis().get_matches(contents),
           }

    # If the file is a PE, analyze it.
    if basic_information["mime_type"] == "application/x-dosexec":
        pe_file = PE(contents)
        pe_info = pe_file.get_all()
        capa_data = Capa().analyze(file_path)
        foremost_data = Foremost().analyze(file_path)
        pe_info["strings"] = Strings("iso-8859-1", file_path).get()

        data["pe_info"] = pe_info
        data["capa"] = capa_data
        data["foremost"] = foremost_data

    # Log the submission and zip the sample.
    save_submission(data, current_user.id)
    zip_file(file_path)

    return redirect("/analysis?sha1={}&name={}".format(sha1, filename))
Exemple #22
0
    def get(self):
        """Lists all unique submissions."""

        submissions = Submission().query.all()
        sha1_list = []

        if submissions:
            for sub in submissions:
                sha1_list.append(sub.sha1)

        return sha1_list, 200
def test_comment_sentiment(session):
    s = Submission.create(session, **const.MOCK_SUBMISSION)

    c = Comment.create(session, **const.MOCK_COMMENT1)
    cs = sentiment.comment_sentiment(c)

    expected_keys = ['comment_id', 'polarity', 'subjectivity']
    assert sorted(cs.keys()) == sorted(expected_keys)

    assert isinstance(cs['polarity'], float)
    assert isinstance(cs['subjectivity'], float)
Exemple #24
0
def apply():
    form = ApplyForm(obj=current_user, meta={'csrf': False})
    if form.validate_on_submit():
        current_user.name = form.name.data
        current_user.location = form.location.data
        current_user.linkedin_url = form.linkedin_url.data

        s = Submission(applicant=current_user._get_current_object())

        db.session.add(s)
        db.session.commit()

        session['submission'] = s.to_dict()

        msg = 'Thanks, submission requested for user {}, mail={}'.format(
            form.name.data, current_user.email)
        flash(msg)
        return redirect(url_for('main.find_question'))

    questions = Question.query.order_by("order_pos asc").all()
    return render_template('apply.html', questions=questions, form=form)
Exemple #25
0
def get_personality():
    if request.method == 'POST':
        answers_json = request.get_json()['personality']
        big_five = get_big_five(answers_json)
        current_app.logger.info(big_five)

        sub = Submission.from_dict(session['submission'])
        sub.personality = PersonalitySchema().dump(big_five).data
        db.session.commit()
        return jsonify(big_five)
        
    return render_template('survey.html')
Exemple #26
0
 def setUp(self):
     super(TestSubmissionModel, self).setUp()
     self.user1 = User('Dan', '*****@*****.**', '12345')
     self.user2 = User('Dan1', '*****@*****.**', '12345')
     db.session.add(self.user1)
     db.session.add(self.user2)
     db.session.commit()
     self.post = Post('Title', 'Body', self.user1.id, difficulty=1)
     db.session.add(self.post)
     db.session.commit()
     self.submission = Submission('a/b/c', 'abcdef', user_id=self.user1.id, post_id=self.post.id)
     db.session.add(self.submission)
     db.session.commit()
Exemple #27
0
def add_submission():
    form = SubmissionForm()
    if form.validate_on_submit():
        submission = Submission(sentence=form.sentence.data,
                                is_good=form.is_good.data,
                                author=current_user)
        db.session.add(submission)
        db.session.commit()
        flash('Thank you, add another or train our model!!')
        return redirect(url_for('add_submission'))
    return render_template('add_submission.html',
                           title='Add Submission',
                           form=form)
def submitassignment(code, id):
    user = current_user
    # form = SubmitAnswer()
    assignment = Assignment.query.filter_by(id=id).first_or_404()
    if request.method == "POST":
        submission = Submission(body=request.form.get("body"))
        assignment.submissions.append(submission)
        user.submissions.append(submission)
        db.session.add(submission)
        db.session.commit()

        flash('Congratulations, you have submitted the solution!')
        return redirect(url_for('opencourse', code=code))
def submit():
    form = SubmitForm()
    if form.validate_on_submit():
        submission = Submission(title=form.title.data,
                                submitter=form.submitter.data,
                                submission=form.submission.data,
                                publishing_status="new")
        db.session.add(submission)
        db.session.commit()
        flash(
            'Your leak has been saved and is going to be reviewed. Thank you for leaking with <em>Leakyleaks</em>'
        )
        return redirect(url_for('process'))
    return render_template('submit.html', form=form)
Exemple #30
0
def save_submission(data, user_id):
    """Inserts a new submission in the database."""

    vt_analyzed = False
    if data["virustotal_detection"]:
        vt_analyzed = True

    new_submission = Submission(sha1=data["hashes"]["SHA-1"],
                                sha256=data["hashes"]["SHA-256"],
                                data=json.dumps(data),
                                vt_analyzed=vt_analyzed)
    db.session.add(new_submission)
    db.session.commit()

    save_log(data["file_name"], new_submission.id, user_id)
Exemple #31
0
def add_submissions(saturdays, users, teams, positions, open=0):
    # "open" is the number of weeks to leave available for testing (no rankings submitted for those weeks)
    submissions = []
    for num, saturday in enumerate(saturdays):
        if num >= len(saturdays) - open:
            break

        for user in users:
            submissions.append(Submission(week=saturday, user=user))

    session.add_all(submissions)
    session.commit()

    for submission in submissions:
        gen_rankings(teams, submission, positions)
Exemple #32
0
def review(revision_id):
    """Revision review page"""
    # only allow if reviewer
    form = CreateReviewForm()
    revision = Revision.get_revision_by_id(revision_id=revision_id)
    submission = Submission.get_submission_by_id(
        submission_id=revision.submission_id)

    # if has been approved
    if submission.state:
        return redirect(url_for('revisions.view', revision_id=revision_id))

    # if post
    if form.validate_on_submit():
        # if approved
        if form.approve.data:
            Submission.complete_submission_by_id(
                submission_id=revision.submission_id)
            return redirect(
                url_for('submissions.view',
                        submission_id=revision.submission_id))
        else:
            params = {
                'form_data': form.data,
                'revision_id': revision_id,
                'reviewer_id': current_user.id
            }
            Review.create_review(params=params)
            return redirect(url_for('revisions.view', revision_id=revision_id))
    else:
        user = User.get_user_by_id(user_id=submission.user_id)
        return render_template('revisions/review.jinja2',
                               revision=revision,
                               submission=submission,
                               user=user,
                               form=form)
Exemple #33
0
def standup_modal():
    payload = json.loads(request.form.get("payload"))

    if payload:
        user_payload = payload.get("user", {})
        data = dict(
            user_id=user_payload.get("id"),
            username=user_payload.get("username"),
            standup_submission=json.dumps(payload.get("view")),
        )

        submission = Submission(**data)
        db.session.add(submission)
        db.session.commit()

    return make_response("", 200)
Exemple #34
0
def new_comment(sha1):
    """Inserts a new comment."""

    text = request.form.get("comment_text")
    submission = Submission().query.filter_by(sha1=sha1).first()

    comment = Comment(text=text,
                      submission_id=submission.id,
                      user_id=current_user.id,
                      date=datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
    db.session.add(comment)
    try:
        db.session.commit()
    except DataError:
        flash("Your comment is too long!")

    return redirect(request.referrer)
def test_comment_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)
    c1 = Comment.create(session, **MOCK_COMMENT1)
    c2 = Comment.create(session, **MOCK_COMMENT2)

    db_submissions = session.query(Submission).all()
    db_comments = session.query(Comment).all()

    assert db_submissions[0].comments[0] == c1
    assert db_submissions[0].comments[1] == c2
    assert len(db_comments) == 2

    db_c1 = db_comments[0]
    for k in MOCK_COMMENT1.keys():
        assert getattr(db_c1, k) == MOCK_COMMENT1[k]

    # test relationship
    assert s.comments == db_comments
Exemple #36
0
def transfer_data_to_prod():
    with get_session() as dev_s:
        data = dev_s.query(Submission).all()
        data = [from_sql(row) for row in data]

    prod_engine = create_engine(os.environ.get('PROD_PROXY_URI'))
    Base.metadata.drop_all(prod_engine)
    Base.metadata.create_all(prod_engine)

    Session = sessionmaker(prod_engine)
    prod_s = Session()

    for d in data:
        sub = Submission(**d)
        prod_s.add(sub)

    prod_s.commit()
    prod_s.close()
Exemple #37
0
def create(submission_id):
    """Create revision page"""
    # TODO make this nicer....
    form = CreateRevisionForm()
    submission = Submission.get_submission_by_id(submission_id=submission_id)
    # if current user does not own submission
    # or if submission has been approved, they can't create revision
    if submission.user_id != current_user.id or submission.state:
        return redirect(
            url_for('submissions.view', submission_id=submission_id))
    else:
        revisions = Revision.get_all_revisions_by_submission_id(
            submission_id=submission_id)
        # if a revision exist
        if revisions:
            # if latest revision has not been reviewed
            if Review.get_review_by_revision_id(
                    revision_id=revisions[-1].id) is None:
                return redirect(
                    url_for('revisions.view', revision_id=revisions[-1].id))
            # else latest revision has been reviewed
            else:
                # if a post process it
                if form.validate_on_submit():
                    revision_id = create_post(form, submission_id)
                    return redirect(
                        url_for('revisions.view', revision_id=revision_id))
                # else present the create page
                else:
                    return render_template('revisions/create.jinja2',
                                           form=form,
                                           submission_id=submission_id)
        # if no revision exists
        else:
            # if a post process it
            if form.validate_on_submit():
                revision_id = create_post(form, submission_id)
                return redirect(
                    url_for('revisions.view', revision_id=revision_id))
            # else present the create page
            else:
                return render_template('revisions/create.jinja2',
                                       form=form,
                                       submission_id=submission_id)
Exemple #38
0
def view(revision_id):
    """Revision view page"""
    revision = Revision.get_revision_by_id(revision_id=revision_id)
    submission = Submission.get_submission_by_id(
        submission_id=revision.submission_id)
    # if elevated user or submission owner or major professor
    if current_user.has_roles(['admin', 'viewer', 'reviewer', 'helper']) or \
       current_user.id == submission.user_id or \
       current_user.net_id == submission.professor:
        review_data = Review.get_review_by_revision_id(revision_id=revision_id)
        user = User.get_user_by_id(user_id=submission.user_id)
        return render_template('revisions/view.jinja2',
                               revision=revision,
                               submission=submission,
                               user=user,
                               review=review_data)
    # else not supposed to view it
    else:
        return redirect(url_for('submissions.index'))
Exemple #39
0
def submit():

	form = SubmitForm()
	SubmitTime = datetime.now()

	if not current_user.adm and not ok_addr(request.remote_addr):
		flash("Invalid submission")
		with open('log/badIP', 'a') as file:
			file.write("{}	{}	{}\n".format(current_user.uid, SubmitTime, request.remote_addr))
	elif SubmitTime <= Config.data["Time"]["end"] and SubmitTime >= Config.data["Time"]["start"]:
		f = form.code.data
		SubmitTime = SubmitTime.strftime("%Y-%m-%d %H:%M:%S")
		sid = None
		if ok_file(f):

                        prefix = "VJudge Submission, {{User:'******', Time:'{}', Language:'{}'}}\n".format(current_user.uid, SubmitTime, form.language.data)
			if file_ext(f.filename) == 'py':
				prefix = "#" + prefix
			else:
				prefix = "//" + prefix

			if Config.data["User"]["maxSubmission"] != -1 and Config.data["User"]["maxSubmission"] <= len(Submission.query.filter_by(uid=current_user.uid).all()):
				flash("Too many submissions")
				return redirect(url_for('index'))
			else:
				sid = OJ.submit(form.problem.data, form.language.data, "Main.{}".format(file_ext(f.filename)), f, prefix=prefix)
		else:
			flash("Invalid file extension")
			return redirect(url_for('index'))

		if sid == None:
			flash("Submitted failed, please try again later")
		else:
			with open("log/submissions", "a") as log:
				log.write("sid: {}, user:{}, pid:{}, lang:{}, time:{}\n".format(sid, current_user.uid, form.problem.data, form.language.data, SubmitTime))
			
			db.session.add(Submission(sid=sid, uid=current_user.uid, pid=form.problem.data))
			db.session.commit()
		return redirect(url_for('index'))
	else:
		flash("Invalid submission")
	return redirect(url_for('index'))
Exemple #40
0
def standup_modal():
    payload = json.loads(request.form.get("payload"))

    # Triggered by action button click
    if payload.get("type") == "block_actions":
        return redirect(
            url_for("standup_trigger", messages=request.form.get("payload")))

    if payload and utils.is_submission_eligible(payload):
        user_payload = payload.get("user", {})
        data = dict(standup_submission=json.dumps(payload.get("view")), )

        user = User.query.filter_by(user_id=user_payload.get("id")).first()
        submission = Submission(user_id=user.id, **data)
        db.session.add(submission)
        db.session.commit()

    utils.after_submission(submission, payload)

    return make_response("", 200)
Exemple #41
0
    def new_submission(course_id, test_id, question_id):
        q = Question.query.filter_by(id=question_id).first()
        sub = q.get_user_submission(current_user.id)
        if not sub:  # if no existing submission exists
            sub = Submission()
            sub.user_id = current_user.id
            sub.test_id = test_id
            sub.question_id = question_id

        form = QuestionSubmissionForm()
        if form.validate_on_submit():
            if q.question_type == 1:
                sub.output_sub = form.output_answer.data
            elif q.question_type == 2:
                sub.mcq_sub = form.mcq_answer.data
            elif q.question_type == 3:
                sub.code_sub = repr(form.code_answer.data)[1:-1]

            db.session.add(sub)
            db.session.commit()

        return redirect(
            url_for('take_test', course_id=course_id, test_id=test_id))
Exemple #42
0
def index():
    form = SubmissionForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            fn = form.submission_file.data.filename
            cat = fn.split('.', 1)[0]
            if cat in categories:
                filename = pycode.save(form.submission_file.data,
                                       folder=secure_filename(
                                           current_user.username))
                new_submission = Submission(filename=filename,
                                            comment=str(form.comment.data),
                                            user_id=current_user.id,
                                            category=cat)
                db.session.add(new_submission)
                db.session.commit()
                flash('Bestand ingeleverd en aan queue toegevoegd.')
            else:
                flash(
                    'Filename {fn} impliceert opdracht {cat}. Deze categorie bestaat niet!'
                    .format(fn=fn, cat=cat))

            return redirect(url_for('index'))
        else:
            flash('ERROR: Er ging iets mis. Het bestand is NIET ingeleverd.',
                  'error')
    queued_submissions = current_user.ungraded_submissions().all()

    results = best_submissions(current_user)
    score = total_score(current_user)

    return render_template("index.html",
                           title='Home Page',
                           form=form,
                           queued_submissions=queued_submissions,
                           results=results,
                           score=score)
Exemple #43
0
    def process_batch_zipfile(self, zfile):
        """
        extracts the uploaded zipfile and saves the files to the database.
        files for each user_id are concatenated into one file.
        filters out binary files.
        :param zfile:
        :return: none
        """
        with ZipFile(zfile) as zip:
            submission = Submission()
            file_paths = [file for file in zip.namelist() if not file.endswith("/")]
            for file_path in file_paths:
                    # check if file is text and not binary
                    filetype = mimetypes.guess_type(file_path)[0]
                    # filters out mac generated files which aren't detected by mimetypes

                    if not file_path.__contains__("__MACOSX") and (filetype is not None) and "text/" in filetype:

                        dirname = os.path.dirname(file_path)
                        user_id = dirname.split("/")[0]
                        if user_id == "":
                            user_id = "None"

                        # concatenate files if it is the same user as previous iteration
                        # the file listing is in order of folders so this should always work to find all files for one user_id
                        if submission.user_id == user_id:
                            file = zip.open(file_path)
                            file_content = file.read().decode('utf-8', "ignore")
                            # if previous file was empty it wouldn't have been added to submission list so add it now
                            if len(submission.file_contents) == 0 and len(file_content) != 0:
                                self.submissions.append(submission)
                            # append file to other files already in the submission
                            submission.file_contents += file_content
                        else:
                            # if a different user_id make a new submission
                            submission = Submission()
                            submission.description = self.description
                            submission.user_id = user_id
                            submission.filename = os.path.basename(file_path)
                            file = zip.open(file_path)
                            submission.file_contents = file.read().decode('utf-8', "ignore")
                            # discard the submission if the file contents is zero lenth
                            if len(submission.file_contents) != 0:
                                self.submissions.append(submission)
Exemple #44
0
def main():
    db.drop_all()
    db.create_all()

    today = datetime.today()

    user1 = User('Erik', '*****@*****.**', '12345')
    user2 = User('Jane', '*****@*****.**', 'qwert')
    user3 = User('Mike', '*****@*****.**', 'asdfg')
    user4 = User('John', '*****@*****.**', 'zxcvb')
    db.session.add(user1)
    db.session.add(user2)
    db.session.add(user3)
    db.session.add(user4)
    db.session.commit()

    post1 = Post('Chickpea curry',
                 "Heat a deep saucepan or a medium sized wok and add the oil or butter followed by the onions and"
                 "garlic. Fry the mixture till the onions are caramelised. Then add the salt, cumin, coriander, turmeric"
                 "and red chilli powders. Mix for a minute and tip in the tomato. Cook the sauce until it begins to"
                 "thicken. Add 1/4 cup of water and stir. Then tip in the chickpeas and mix. Mash a few of the chickpeas"
                 "while cooking. Cover and simmer for five minutes. Then add the ginger and the garam masala. Cook for"
                 "another minute. Serve with pitta breads or plain basmati rice.",
                 user3.id, publish_time=today - timedelta(11), difficulty=3)
    post2 = Post('Black pudding',
                 "Wash the pudding skins and soak in salty water overnight. Put the blood into a bowl and add 1"
                 "teaspoon of salt, strain through a fine sieve to a larger bowl. Stir in the milk, suet, onions and"
                 "oatmeal and season with salt and pepper. Now to fill the pudding skins...tie at one end (with string)"
                 "and then turn inside out. Fill it with mixture tie off in equal lengths...like sausages. Place in water"
                 "that is off the boil, after about 5 minutes prick all over with a large needle. Boil for 30 minutes,"
                 "then take out of pan and hang in a cool place to dry.",
                 user3.id, publish_time=today - timedelta(8), difficulty=5)
    post3 = Post('Carrot cake',
                 "Preheat the oven to 180C/Gas 4/fan 160C. Oil and line the base and sides of an 18cm square cake tin"
                 "with baking parchment. The easiest way to do this is to cut two long strips the width of the tin and"
                 "put each strip crossways, covering the base and sides of the tin, with a double layer in the base."
                 "Tip the sugar into a large mixing bowl, pour in the oil and add the eggs. Lightly mix with a wooden"
                 "spoon. Stir in the grated carrots, raisins and orange rind. Mix the flour, bicarbonate of soda and"
                 "spices, then sift into the bowl. Lightly mix all the ingredients - when everything is evenly"
                 "amalgamated stop mixing. The mixture will be fairly soft and almost runny. Pour the mixture into the"
                 "prepared tin and bake for 40-45 minutes, until it feels firm and springy when you press it in the"
                 "centre. Cool in the tin for 5 minutes, then turn it out, peel off the paper and cool on a wire rack."
                 "(You can freeze the cake at this point.) Beat together the frosting ingredients in a small bowl until"
                 "smooth - you want the icing about as runny as single cream. Set the cake on a serving plate and boldly"
                 "drizzle the icing back and forth in diagonal lines over the top, letting it drip down the sides.",
                 user2.id, publish_time=today - timedelta(3), difficulty=2)
    post4 = Post('Soup',
                 "In a large soup pot, heat oil over medium heat. Add onions, carrots and celery; cook and stir until"
                 "onion is tender. Stir in garlic, bay leaf, oregano and basil; cook for 2 minutes. Stir in lentils and"
                 "add water and tomatoes. Bring to the boil. Reduce heat and simmer for at least 1 hour. When ready to"
                 "serve, stir in spinach and cook until it wilts. Stir in vinegar and season to taste with salt and"
                 "pepper and more vinegar if desired.",
                 user1.id, publish_time=today, difficulty=1)
    post5 = Post('Stew',
                 "Preheat the oven to 220 degrees C/425 degrees F/gas 7. Place a large roasting tray in the oven to"
                 "preheat. Carefully remove the hot tray from the oven, then add the oxtail. Season and drizzle over a"
                 "lug of olive oil, then toss to coat and place in the hot oven for around 20 minutes, or until golden"
                 "and caramelized. Meanwhile, trim and halve the leeks and celery lengthways, then chop into rough 2cm"
                 "chunks. Peel and chop the carrots into 2cm pieces, then place into a large ovenproof casserole pan"
                 "over a medium-low heat with 1 tablespoon of olive oil. Pick, roughly chop and add the thyme and"
                 "rosemary leaves, then add the bay and cook for around 20 minutes, or until soft and sweet, stirring"
                 "frequently. Meanwhile, remove the oxtail from the oven and set aside. Reduce the oven temperature to"
                 "170 degrees C/325 degrees F/gas 3. Add the cloves and flour to the veg, stirring well to combine,"
                 "then pour in the tomatoes and porter (or wine, if using). Add the oxtail and any roasting juices,"
                 "cover with the beef stock or 1 litre of cold water and stir well. Turn the heat up to high and bring"
                 "to the boil, then pop the lid on and place in the hot oven for around 5 hours, or until the meat falls"
                 "away from the bone, stirring every hour or so and adding a splash of water to loosen, if needed."
                 "Remove the pan from the oven and leave to cool for about 10 minutes. Using rubber gloves, strip the"
                 "meat from the bones and return to the pan, discarding the bones. Add a good splash of Worcestershire"
                 "sauce, season to taste and enjoy with creamy mash and seasonal steamed greens.",
                 user1.id, publish_time=today + timedelta(3), difficulty=4)
    db.session.add(post1)
    db.session.add(post2)
    db.session.add(post3)
    db.session.add(post4)
    db.session.add(post5)
    db.session.commit()
    post1.add_ingredients(["1 tbsp oil", "1 onion, chopped", "1 garlic clove, crushed", "1/4 tsp salt",
                           "1/2 tsp cumin powder", "1/4 tsp coriander powder", "1/4 tsp turmeric powder",
                           "1/4 tsp red chilli powder", "1 fresh tomato, chopped",
                           "1 x 400g/14oz can chickpeas, drained and rinsed"])
    post2.add_ingredients(["2 Pints Pig or Ox Blood", "1 lb. Chopped Suet", "8 oz. Diced Onions",
                           "1/2 pint Milk", "3 oz. Oatmeal", "Salt and Pepper"])
    post3.add_ingredients(["175g light muscovado sugar", "175ml sunflower oil", "3 large eggs, lightly beaten",
                           "140g grated carrots (about 3 medium)", "100g raisins", "grated zest of 1 large orange",
                           "175g self-raising flour", "1 tsp bicarbonate of soda", "1 tsp ground cinnamon",
                           "1/2 tsp grated nutmeg (freshly grated will give you the best flavour)"])
    post4.add_ingredients(["1 onion, chopped", "4 tablespoons olive oil", "2 carrots, diced",
                           "2 sticks celery, chopped", "2 cloves garlic, finely chopped", "1 teaspoon dried oregano",
                           "1 bay leaf", "1 teaspoon dried basil", "400g passata", "385g dry lentils", "2 litres water",
                           "15g spinach, rinsed and thinly sliced", "2 tablespoons vinegar", "salt to taste",
                           "ground black pepper to taste"])
    post5.add_ingredients(["2.5 kg oxtail, chopped into 4cm chunks (ask your butcher to do this)", "sea salt",
                           "freshly ground black pepper", "olive oil", "2 medium leeks", "2 stalks of celery",
                           "4 medium carrots", "a few sprigs of fresh thyme", "a few sprigs of fresh rosemary",
                           "4 fresh bay leaves", "4 cloves", "2 heaped tablespoons plain flour",
                           "2 x 400 g tins of plum tomatoes", "275 ml porter or red wine"])
    db.session.commit()
    post1.add_tag("curry")
    post1.add_tag("savoury")
    post2.add_tag("blood")
    post3.add_tag("cake")
    post4.add_tag("vegan")
    db.session.commit()

    submission1 = Submission('a/b/c', 'This is my chickpea curry', post_id=post1.id, user_id=user2.id)
    submission2 = Submission('a/b/c', 'This is my chickpea curry', post_id=post1.id, user_id=user1.id)
    submission3 = Submission('a/b/c', 'This is my black pudding', post_id=post2.id, user_id=user1.id)
    submission4 = Submission('a/b/c', 'This is my black pudding', post_id=post2.id, user_id=user2.id)
    submission5 = Submission('a/b/c', 'This is my carrot cake', post_id=post3.id, user_id=user3.id)
    submission6 = Submission('a/b/c', 'This is my carrot cake', post_id=post3.id, user_id=user4.id)
    submission7 = Submission('a/b/c', 'This is my carrot cake', post_id=post3.id, user_id=user1.id)
    db.session.add(submission1)
    db.session.add(submission2)
    db.session.add(submission3)
    db.session.add(submission4)
    db.session.add(submission5)
    db.session.add(submission6)
    db.session.add(submission7)
    db.session.commit()

    submission1.toggle_upvote(user1.id)
    submission1.toggle_upvote(user3.id)
    submission1.toggle_upvote(user4.id)
    submission2.toggle_upvote(user4.id)
    submission2.toggle_upvote(user3.id)
    submission3.toggle_upvote(user3.id)
    submission4.toggle_upvote(user4.id)
    submission5.toggle_upvote(user1.id)
    submission1.make_winner()
    db.session.commit()
Exemple #45
0
class TestSubmissionModel(BaseTest):
    @patch('app.models.ImgurClient.upload_from_path', Mock(return_value={'link': ''}))
    def setUp(self):
        super(TestSubmissionModel, self).setUp()
        self.user1 = User('Dan', '*****@*****.**', '12345')
        self.user2 = User('Dan1', '*****@*****.**', '12345')
        db.session.add(self.user1)
        db.session.add(self.user2)
        db.session.commit()
        self.post = Post('Title', 'Body', self.user1.id, difficulty=1)
        db.session.add(self.post)
        db.session.commit()
        self.submission = Submission('a/b/c', 'abcdef', user_id=self.user1.id, post_id=self.post.id)
        db.session.add(self.submission)
        db.session.commit()

    @patch('app.models.ImgurClient.upload_from_path')
    def test_submission_model_can_init(self, patch_imgur):
        patch_imgur.return_value={'link': ''}
        submission = Submission('a/b/c', 'abcdef', 1, 1)
        self.assertEqual(submission.text, 'abcdef')
        patch_imgur.assert_called_with('a/b/c', config=None, anon=True)

    def test_submission_model_knows_user_upvoted(self):
        self.assertTrue(self.submission.has_user_upvoted(self.user1.id))
        self.assertFalse(self.submission.has_user_upvoted(self.user2.id))

    @patch('app.models.Submission.has_user_upvoted', Mock(return_value=False))
    def test_submission_model_can_toggle_upvotes_to_true(self):
        self.assertEqual(self.submission.votes.all(), [self.user1])
        self.submission.toggle_upvote(self.user2.id)
        db.session.commit()
        self.assertEqual(self.submission.votes.all(), [self.user1, self.user2])

    @patch('app.models.Submission.has_user_upvoted', Mock(return_value=True))
    def test_submission_model_can_toggle_upvotes_to_false(self):
        self.assertEqual(self.submission.votes.all(), [self.user1])
        self.submission.toggle_upvote(self.user1.id)
        db.session.commit()
        self.assertEqual(self.submission.votes.all(), [])

    def test_submission_model_can_count_upvotes(self):
        self.assertEqual(self.submission.count_upvotes(), 1)
        self.submission.votes.append(self.user2)
        self.assertEqual(self.submission.count_upvotes(), 2)

    @patch('app.models.ImgurClient.upload_from_path', Mock(return_value={'link': ''}))
    @patch('app.models.User.increase_score')
    def test_submission_model_can_make_winner(self, mock_increase_score):
        submission1 = Submission('a/b/c', 'abcdef', user_id=self.user2.id, post_id=self.post.id)
        db.session.add(submission1)
        submission1.make_winner()
        db.session.commit()
        self.assertTrue(submission1.won)
        self.assertRaises(Exception, self.submission.make_winner)
        mock_increase_score.assert_has_calls([call(50), call(50), call(100), call(104)])

    @patch('app.models.ImgurClient.upload_from_path', Mock(return_value={'link': ''}))
    def test_submission_model_does_not_allow_multiple_submissions(self):
        submission1 = Submission('a/b/c', 'abcdef', user_id=self.user1.id, post_id=self.post.id)
        db.session.add(submission1)
        self.assertRaises(Exception, db.session.commit)