Example #1
0
def form():
    with models.db.atomic():
        # Check if a submission from this user has already been received.
        # This and inserting new submissions should be done atomically to avoid
        # race conditions with multiple submissions from the same user at the
        # same time.
        if len((models.UserHash.select().where(
                models.UserHash.hash == session['hash']))) != 0:
            return render_template(
                'message.html',
                message="You've already responded to this survey.",
                title='Already responded')

        # Insert into database if UserHash is new
        if request.method == 'POST':

            # No previous submission; record this one.
            models.UserHash().create(hash=session['hash'])

            # Dump form to JSON
            form_json = json.dumps(request.form)
            models.Submission().create(form=form_json, version=SURVEY_VERSION)

            return render_template('message.html',
                                   message="""Your submission has
                been recorded anonymously. Thank you for your participation in the survey
                and your contribution to improving the student experience at
                Rensselaer.""",
                                   title='Submission recorded')

        else:
            return render_template('form.html', title='Take survey')
Example #2
0
def manage(request):
    """ Handles uploading of bots """

    if request.POST:
        data = request.POST
        file_data = request.FILES
        sub = models.Submission(user = request.user)
        form = forms.SubmissionForm(data=data, files=file_data, instance=sub)
        if form.is_valid():
            form.save()
            # Set the first MAX_ACTIVE_BOTS bots 'active', and make all the rest inactive
            bots = models.Submission.objects.filter(user = request.user, game=form.cleaned_data['game'], active=True, user__is_active = True).order_by('-timestamp')
            if len(bots) > settings.MAX_ACTIVE_BOTS:
                for bot in bots[settings.MAX_ACTIVE_BOTS:]: 
                    bot.active = False
                    bot.save()
    else:
        form = forms.SubmissionForm()

    submissions = models.Submission.objects.filter(user = request.user, user__is_active = True).order_by('-timestamp')

    return render_to_response("manage.html", 
            {'form':form,
            'submissions':submissions},
            context_instance = RequestContext(request))
Example #3
0
def form_auth_key(auth_key):
    with models.db.atomic():
        # Check if this is a valid survey authorization key.
        # This and inserting new submissions should be done atomically to avoid
        # race conditions with multiple submissions with the same key at the
        # same time.
        key_models = (models.AuthorizationKey.select().where(
            models.AuthorizationKey.key == auth_key))
        if len(key_models) != 1:
            return render_template('message.html',
                                   message="Invalid authorization key.",
                                   title='Not authorized')

        # Insert into database if UserHash is new
        if request.method == 'POST':

            # Delete this authorization key. We can assume len == 0 because of
            # the check a few lines above.
            key_models[0].delete_instance()

            # Dump form to JSON
            form_json = json.dumps(request.form)
            models.Submission().create(form=form_json, version=SURVEY_VERSION)

            return render_template('message.html',
                                   message="""Your submission has
                been recorded anonymously. Thank you for your participation in the survey
                and your contribution to improving the student experience at
                Rensselaer.""",
                                   title='Submission recorded')

        else:
            return render_template('form.html', title='Take survey')
Example #4
0
def submit(source, num_submissions):
    """Generates and submits *num_submissions*
    :class:`models.Submission`s on behalf of a :class:`models.Source`
    *source*.

    :param models.Source source: The source on who's behalf to make
                             submissions.

    :param int num_submissions: Number of random-data submissions
                                to make.

    :returns: A list of the :class:`models.Submission`s submitted.
    """
    assert num_submissions >= 1
    source.last_updated = datetime.datetime.utcnow()
    db.session.add(source)
    submissions = []
    for _ in range(num_submissions):
        source.interaction_count += 1
        source.pending = False
        fpath = current_app.storage.save_message_submission(
            source.filesystem_id, source.interaction_count,
            source.journalist_filename, str(os.urandom(1)))
        submission = models.Submission(source, fpath)
        submissions.append(submission)
        db.session.add(source)
        db.session.add(submission)

    db.session.commit()
    return submissions
Example #5
0
def submit_story():
    if not session['is_logged_in']:
        return redirect('/login')
    user = methods.User.objects.get(username=session['username'])
    story_id = request.forms.get('story_id', None)
    market_id = request.forms.get('market_id', None)
    if story_id is None:
        flash.message = "Something went wrong and we didn't receive the data we were expecting!"
        return redirect('/dashboard')
    if market_id is None:
        flash.message = "Market id is required"
        # TODO corgiw use the proper story ID
        return redirect('/story/fakestoryid')
    story = None
    for s in user.stories:
        if s.id == story_id:
            story = s
    if story is None:
        flash.message = "That story wasn't found"
        return redirect('/dashboard')
    market = models.Market.objects.get(id=market_id)
    if market is not None:
        # add the story to the market by creating a submission in the database, with the status of 'waiting' and the current date
        submission = models.Submission()
        submission.market = market
        submission.status = 'waiting'
        story.submissions.append(submission)
        user.save()
        # redirect to the story page
        return redirect('/story/fakestoryid')
    else
        flash.message = "We couldn't find that market!"
    # TODO corgiw use the proper story ID here
        return redirect('/story/<story_id')
Example #6
0
def submit_submission(r, solver):
    challenge = models.Challenge.query.filter_by(id=r['id'])
    if challenge is None:
        return False
    submission = models.Submission()
    submission.challenge_id = r['id']
    submission.solver_id = solver
    models.db.session.add(submission)
    models.db.session.commit()
    return submission.id
Example #7
0
    async def submit(self, info, contest=None):
        if len(info['message'].attachments) < 1:
            await info['bot'].send_message(
                info['channel'],
                "Please upload one file for judging in the message.")
            return
        url = info['message'].attachments[0]["url"]
        submission_time = datetime.datetime.now()

        exceptions = {
            IndexError: "Please select a problem to submit the code to.",
            KeyError: "Invalid problem code.",
            UnicodeDecodeError: "Please upload a valid code file.",
            ValueError: "Please upload a file less than 65536 characters.",
        }
        try:
            problem_code = info['content'][0]
            problem = database.problem_list[problem_code]
            if contest is not None and get_element(contest.contest.problems,
                                                   problem) is None:
                raise KeyError
            code = requests.get(url).content.decode("utf-8")
            if len(code) > 65536:
                raise ValueError
        except (*exceptions, ) as e:
            await info['bot'].send_message(info['channel'],
                                           exceptions[type(e)])
            return
        finally:
            try:
                await info['bot'].delete_message(info['message'])
            except errors.Forbidden:
                pass
        if not await has_perm(info['bot'], info['channel'], info['user'],
                              "submit to private problems",
                              not problem.is_public and contest is None):
            return
        id = database.create_submission(
            models.Submission(None,
                              result="QU",
                              user=info['user'].id,
                              problem=problem_code,
                              submission_time=submission_time,
                              source=code,
                              contest=contest))
        database.judgeserver.judges.judge(id, problem,
                                          judge_lang[info['user'].language],
                                          code)
        await info['bot'].send_message(
            info['channel'],
            "{0}, Submitting code to `{1}`. Please wait.".format(
                info['user'].discord_user.mention, problem_code))
        await info['bot'].loop.create_task(
            wait_submission_finish(info['bot'], info['channel'], id,
                                   info['user'], contest))
Example #8
0
def test_populate_submission():
    filepath = \
        "data/cohort1/14/pre2-programming/20180724_152918"
    with open("tests/test_data/result_1.json", "r") as fp:
        result = json.load(fp)

    submisison = models.Submission().load(filepath, result)
    assert submisison.group_name == "cohort1"
    assert submisison.student_id == 14
    assert submisison.module_name == "pre2-programming"
    assert submisison.submission_date == \
        datetime.datetime.strptime(
            "20180724_152918",
            config.INPUT_DATE_FORMAT
        )
    for sf in submisison.submission_files:
        assert sf.filename.endswith(".py")
Example #9
0
def submit_article(userobj, articleobj):
    try:
        subm = articleobj.submission
        # submission exists
        subm.submit()
    except models.Submission.DoesNotExist:
        discussionobj = models.Discussion()
        discussionobj.save()
        kwargs = {
            'submitter': userobj,
            'article': articleobj,
            'discussion': discussionobj,
        }
        subm = models.Submission(**kwargs)
        subm.save()  # default state of a Submission object is 'submitted' ;)
        subm.submit()
    subm.save()
    return subm
Example #10
0
def recv_solution(uid):
    prob_id = int(request.forms.get('prob_id'))
    link = request.forms.get('link')
    user_id = int(request.forms.get('user_id'))
    upload = request.files.get('upload', None)
    language = request.forms.get('language')
    timestamp = datetime.datetime.utcnow()
    level = 'jiwls' if prob_id <= 104 else 'oweiur'
    with session_scope() as session:
        prev_submission = session.query(models.Submission).filter_by(
            user_id=user_id, prob_id=prob_id).first()

        redirect_url = '/user/{}/prob/{}?msg=success'.format(uid, level)
        if prev_submission:
            filename = os.path.basename(prev_submission.link)
            upload.save(os.path.join(config.FILE_SAVE_DIR, filename),
                        overwrite=True)
            session.query(models.Submission).filter_by(
                uid=prev_submission.uid).update({'timestamp': timestamp})
            session.commit()
        else:
            if upload is not None:
                if link:
                    bottle.redirect(
                        ('/user/{}/prob?msg=cannot+'
                         'upload+file+and+link+at+the+same+time').format(uid))
                orig_name, ext = os.path.splitext(upload.filename)
                new_filename = uuid.uuid4().hex + ext
                upload.save(os.path.join(config.FILE_SAVE_DIR, new_filename))
                link = os.path.join(config.STATIC_FILE_URL, new_filename)
            sub = models.Submission()
            sub.link = link
            sub.user_id = user_id
            sub.prob_id = prob_id
            sub.language = language
            sub.timestamp = timestamp
            session.add(sub)
            session.commit()
        return bottle.redirect(redirect_url)
def get_top_submissions(
    subreddit,
    reddit,
    time_filter='all',
):
    """Get top submissions
    """
    sr = reddit.subreddit(subreddit)

    day_submissions = sr.top(time_filter=time_filter)

    submissions = [
        models.Submission(id=sub.id,
                          created_date=datetime.fromtimestamp(sub.created_utc),
                          title=sub.title,
                          body=sub.selftext,
                          author=sub.author.name if sub.author else None,
                          score=sub.score,
                          stickied=sub.stickied) for sub in day_submissions
    ]

    return submissions
Example #12
0
    def insert_data(self,
                    class_c,
                    submit_id,
                    client_id,
                    data,
                    ns_map,
                    cached_index_hosts,
                    excess_listings=None):
        """Process data uploaded by namebench."""

        notes = []
        listed = True

        if data['config']['query_count'] < MIN_QUERY_COUNT:
            notes.append("Not enough queries to list (need %s)." %
                         MIN_QUERY_COUNT)
            listed = False

        if len(data['nameservers']) < MIN_SERVER_COUNT:
            notes.append("Not enough servers to list (need %s)." %
                         MIN_SERVER_COUNT)
            listed = False

        if excess_listings:
            notes.append(
                "You have already submitted a listed entry within %s" %
                MIN_LISTING_DELTA)
            listed = False

        submission = models.Submission()
        submission.client_id = client_id
        submission.submit_id = submit_id
        submission.class_c = class_c
        # Hide from the main index.
        hide_me = self.request.get('hidden', False)
        # json does not seem to convert booleans
        if hide_me and hide_me != 'False':
            notes.append("Hidden on request: %s [%s]" %
                         (hide_me, type(hide_me)))
            submission.hidden = True
            listed = False
        elif is_private_ip(class_c):
            notes.append("Hidden due to internal IP.")
            submission.hidden = True
            listed = False
        else:
            submission.hidden = False
        submission.listed = listed

        if 'geodata' in data and data['geodata']:
            if 'latitude' in data['geodata']:
                submission.coordinates = ','.join(
                    (str(data['geodata']['latitude']),
                     str(data['geodata']['longitude'])))
                submission.city = data['geodata'].get('city', None)
                submission.region = data['geodata'].get('region_name', None)
                submission.country = data['geodata'].get('country_name', None)
                submission.country_code = data['geodata'].get(
                    'country_code', None)
        submission.put()

        # Dump configuration for later reference.
        config = models.SubmissionConfig(parent=submission)
        config.submission = submission
        save_variables = [
            'query_count', 'platform', 'run_count', 'benchmark_thread_count',
            'health_thread_count', 'health_timeout', 'timeout', 'version',
            'input_source'
        ]
        for var in save_variables:
            if data['config'].get(var) != None:
                setattr(config, var, data['config'][var])

        config.put()

        for nsdata in data['nameservers']:
            # TODO(tstromberg): Look into caching this from our previous results.
            ns_record = ns_map[nsdata['ip']]
            ns_sub = models.SubmissionNameServer(parent=submission)
            ns_sub.submission = submission
            ns_sub.nameserver = ns_record

            save_variables = [
                'averages', 'check_average', 'node_ids', 'version',
                'error_count', 'is_disabled', 'is_error_prone', 'is_reference',
                'duration_max', 'duration_min', 'nx_count', 'overall_average',
                'position', 'sys_position', 'diff', 'port_behavior',
                'timeout_count'
            ]
            for var in save_variables:
                if nsdata.get(var) != None:
                    if '_average' in var:
                        setattr(ns_sub, var, float(nsdata[var]))
                    else:
                        setattr(ns_sub, var, nsdata[var])

            if nsdata['sys_position'] == 0:
                submission.primary_nameserver = ns_record
            if nsdata.get('notes'):
                # Only include the text information, not the URL.
                ns_sub.notes = [x['text'] for x in nsdata['notes']]
            ns_sub_instance = ns_sub.put()

            # The fastest ns wins a special award.
            if ns_sub.position == 0:
                submission.best_nameserver = ns_record
                if not ns_sub.sys_position == 0 and ns_sub.diff:
                    submission.best_improvement = ns_sub.diff

            if nsdata.get('durations'):
                result_list = []
                for idx, run in enumerate(nsdata['durations']):
                    run_results = models.RunResult(parent=submission)
                    run_results.submission_nameserver = ns_sub
                    run_results.run_number = idx
                    run_results.durations = list(run)
                    result_list.append(run_results)
                db.put(result_list)

            if nsdata.get('index'):
                self._process_index_submission(nsdata['index'], submission,
                                               ns_sub_instance,
                                               cached_index_hosts)

        # Final update with the primary_nameserver / best_nameserver data.
        submission.put()
        if listed:
            state = 'public'
            # invalidate the data for the frontpage
            memcache.delete('submissions')
        elif submission.hidden:
            state = 'hidden'
        else:
            state = 'unlisted'
        response = {
            'state': state,
            'url': '/id/%s' % submission.key().id(),
            'notes': notes
        }
        self.response.out.write(json.dumps(response))
Example #13
0
def main():
    conn = pymysql.connect(host=migrate_config.OLD_HOST,
                           port=migrate_config.OLD_PORT,
                           user=migrate_config.OLD_USER,
                           password=migrate_config.OLD_PASSWORD,
                           db=migrate_config.OLD_DATABASE)
    joined_teams: Dict[int, List[int]] = {}
    user_admin_teams: Dict[int, List[int]] = {}
    cursor = conn.cursor(pymysql.cursors.DictCursor)
    cursor.execute("SELECT * FROM users")
    print("loading user...")
    for item in cursor.fetchall():
        joined_teams[item["id"]] = json.loads(item["joined_teams"])
        print(item["username"])
        user = models.User(id=item["id"],
                           banned=item["banned"],
                           username=item["username"],
                           password=item["password"],
                           description=item["description"],
                           email=item["email"],
                           register_time=item["register_time"],
                           rating_history=json.loads(item["rating_history"]),
                           rating=item["rating"],
                           permission_group=item["permission_group"],
                           permissions=json.loads(item["permissions"]),
                           force_logout_before=item["force_logout_before"],
                           phone_number=None,
                           phone_verified=False)
        db.session.add(user)
    db.session.commit()
    print("loading problems...")
    cursor.execute("SELECT * FROM problems")
    for item in cursor.fetchall():
        print(item["id"], item["title"])
        prob = models.Problem(id=item["id"],
                              uploader_id=item["uploader_id"],
                              title=item["title"],
                              background=item["background"],
                              content=item["content"],
                              input_format=item["input_format"],
                              output_format=item["output_format"],
                              hint=item["hint"],
                              example=json.loads(item["example"]),
                              files=json.loads(item["files"]),
                              downloads=json.loads(item["downloads"]),
                              provides=json.loads(item["provides"]),
                              subtasks=json.loads(item["subtasks"]),
                              public=item["public"],
                              spj_filename=item["spj_filename"],
                              using_file_io=item["using_file_io"],
                              input_file_name=item["input_file_name"],
                              output_file_name=item["output_file_name"],
                              problem_type=item["problem_type"],
                              can_see_results=item["can_see_results"],
                              create_time=item["create_time"],
                              extra_parameter=json.loads(
                                  item["extra_parameter"]),
                              remote_judge_oj=item["remote_judge_oj"],
                              remote_problem_id=item["remote_problem_id"],
                              cached_submit_count=0,
                              cached_accepted_count=0,
                              team_id=None,
                              invite_code=str(uuid.uuid1()),
                              submission_visible=True)
        db.session.add(prob)
    db.session.commit()
    print("loading submissions...")
    cursor.execute("SELECT * FROM submissions")
    for item in cursor.fetchall():
        print(item["id"])
        item.update(
            dict(judge_result=json.loads(item["judge_result"]),
                 selected_compile_parameters=json.loads(
                     item["selected_compile_parameters"])))
        submission = models.Submission(**item)
        db.session.add(submission)
    db.session.commit()
    print("loading contests..")
    cursor.execute("SELECT * FROM contest")
    for item in cursor.fetchall():
        print(item["id"])
        item.update(dict(closed=False, problems=json.loads(item["problems"])))
        contest = models.Contest(**item)
        db.session.add(contest)
    db.session.commit()
    print("loading teams..")
    cursor.execute("SELECT * FROM team")
    for item in cursor.fetchall():
        team = models.Team(id=item["id"],
                           name=item["name"],
                           description=item["description"],
                           owner_id=item["owner_id"],
                           tasks=json.loads(item["tasks"]),
                           create_time=item["create_time"],
                           invite_code=str(uuid.uuid1()),
                           private=False,
                           team_contests=[],
                           team_problems=[],
                           team_problemsets=[])
        for uid in item["admins"]:
            if uid not in user_admin_teams:
                user_admin_teams[uid] = []
            user_admin_teams[uid].append(item["id"])
        db.session.add(team)
    db.session.commit()
    for user, teams in joined_teams.items():
        admin_teams = user_admin_teams.get(user, set())
        for x in teams:
            db.session.add(
                models.TeamMember(uid=user,
                                  team_id=x,
                                  is_admin=x in admin_teams))
    db.session.commit()
    print("loading permission groups..")
    db.session.query(models.PermissionGroup).delete()
    db.session.commit()
    cursor.execute("SELECT * FROM permission_groups")
    for item in cursor.fetchall():
        item.update(dict(permissions=json.loads(item["permissions"])))
        db.session.add(models.PermissionGroup(**item))
    db.session.commit()
    print("loading problemsets...")
    cursor.execute("SELECT * FROM problem_set")
    for item in cursor.fetchall():
        item.update(dict(problems=json.loads(item["problems"])))
        db.session.add(models.ProblemSet(**item))
    db.session.commit()
    print("loading remote accounts...")
    cursor.execute("SELECT * FROM remote_accounts")
    for item in cursor.fetchall():
        item.update(dict(session=item["session"]))
        db.session.add(models.RemoteAccount(**item))
    db.session.commit()
    print("loading discussions..")
    cursor.execute("SELECT * FROM discussions")
    for item in cursor.fetchall():
        db.session.add(models.Discussion(**item, private=False))
    db.session.commit()
    print("loading comments..")
    cursor.execute("SELECT * FROM comments")
    for item in cursor.fetchall():
        if not db.session.query(
                models.Discussion).filter_by(id=item["discussion_id"]).count():
            continue
        db.session.add(models.Comment(**item))
    db.session.commit()
    print("Done!")
Example #14
0
s = Session()
#%%
u = models.User(lms_user='******', is_prof=True, token='alks')
s.add(u)
s.commit()
# %%
print(sess.query(models.User).all())

# %%

u = s.query(models.User).first()

c = models.Course(name='test', responsible=u)
s.add(c)
s.commit()

# %%

a = models.Activity(course_specific_id='at1', course=c)
s.add(a)
s.commit()

#%%
u2 = models.User(lms_user='******', is_prof=False, token='alks2')
s.add(u2)
sub = models.Submission(sender_id=u2.id, activity_id=a.id, content='sdlkfjsdlfsdlfjsdkl')
s.add(sub)
s.commit()
# %%