Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def do_it():
import psiturk.experiment
reload(psiturk.experiment)
psiturk.experiment.app.wsgi_app = FlaskTestClientProxy(
psiturk.experiment.app.wsgi_app)
return psiturk.experiment.app
yield do_it
def setUp(self):
'''Build up fixtures'''
import psiturk.experiment
reload(psiturk.experiment)
psiturk.experiment.app.wsgi_app = BadFlaskTestClientProxy(
psiturk.experiment.app.wsgi_app)
self.app = psiturk.experiment.app.test_client()
# Fake MTurk data
self.worker_id = fake.md5(raw_output=False)
self.hit_id = fake.md5(raw_output=False)
self.assignment_id = fake.md5(raw_output=False)
def test_campaign_goal_met_cancel(patch_aws_services, campaign, mocker, caplog, stubber):
from psiturk.tasks import do_campaign_round
campaign_args = {
'campaign': campaign,
'job_id': campaign.campaign_job_id
}
from psiturk.experiment import app
mocker.patch.object(app.apscheduler,
'remove_job', lambda *args, **kwargs: True)
import psiturk.tasks
mocker.patch.object(psiturk.models.Participant, 'count_completed', lambda *args, **kwargs: campaign.goal)
import psiturk.experiment
remove_job_mock = mocker.patch.object(psiturk.experiment.app.apscheduler, 'remove_job')
do_campaign_round(**campaign_args)
remove_job_mock.assert_called()
def test_campaign_round_codeversion_change_cancel(patch_aws_services, campaign, mocker, caplog):
from psiturk.tasks import do_campaign_round
campaign_args = {
'campaign': campaign,
'job_id': campaign.campaign_job_id
}
from psiturk.experiment import app
mocker.patch.object(app.apscheduler,
'remove_job', lambda *args, **kwargs: True)
from psiturk.amt_services_wrapper import MTurkServicesWrapper
aws_services_wrapper = MTurkServicesWrapper()
aws_services_wrapper.config['Task Parameters']['experiment_code_version'] = '1.1'
import psiturk.tasks
mocker.patch.object(psiturk.tasks.TaskUtils, 'aws_services_wrapper', aws_services_wrapper)
import psiturk.experiment
remove_job_mock = mocker.patch.object(psiturk.experiment.app.apscheduler, 'remove_job')
do_campaign_round(**campaign_args)
remove_job_mock.assert_called()
def launch_new_campaign(cls, **kwargs):
kwargs['is_active'] = True
new_campaign = cls(**kwargs)
db_session.add(new_campaign)
db_session.commit()
_kwargs = {
'campaign': new_campaign,
'job_id': new_campaign.campaign_job_id
}
from .experiment import app
app.apscheduler.add_job(
id=new_campaign.campaign_job_id,
func=do_campaign_round,
kwargs=_kwargs,
trigger='interval',
minutes=new_campaign.minutes_between_rounds,
next_run_time=datetime.datetime.now()
)
return new_campaign
from json import dumps, loads
# load the configuration options
config = PsiturkConfig()
config.load_config()
# if you want to add a password protect route use this
myauth = PsiTurkAuthorization(config)
# import the Blueprint
dashboard = Blueprint('dashboard', __name__,
template_folder='templates', static_folder='static', url_prefix='/dashboard')
from psiturk.experiment import app
from flask_login import LoginManager, UserMixin
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'dashboard.login'
class DashboardUser(UserMixin):
def __init__(self, username=''):
self.id = username
@login_manager.user_loader
def load_user(username):
return DashboardUser(username=username)
def login_required(view):
@wraps(view)
def wrapped_view(**kwargs):
is_logged_in = current_user.get_id() is not None
is_static_resource_call = str(request.endpoint) == 'dashboard.static'
is_login_route = str(request.url_rule) == '/dashboard/login'
def do_campaign_round(campaign, **kwargs):
from .models import Participant
from .experiment import app
# cancel if codeversion changes
config_code_version = task_utils.aws_services_wrapper.config['Task Parameters']['experiment_code_version']
if config_code_version != campaign.codeversion:
logger.info('Codeversion changed (campaign: {}, config {}), removing job.'.format(campaign.codeversion, config_code_version))
return app.apscheduler.remove_job(kwargs['job_id'])
# cancel if campaign goal met
complete_count = Participant.count_completed(codeversion=campaign.codeversion, mode=campaign.mode)
if complete_count >= campaign.goal:
logger.info('Campaign goal met ({}), removing job.'.format(campaign.goal))
return app.apscheduler.remove_job(kwargs['job_id'])
task_utils.aws_services_wrapper.set_mode(campaign.mode)
# how many for this round?
all_hits = task_utils.aws_services_wrapper.get_all_hits().data
available_count = task_utils.aws_services_wrapper.count_available(hits=all_hits).data
pending_count = task_utils.aws_services_wrapper.count_pending(hits=all_hits).data
maybe_will_complete_count = available_count + pending_count
campaign_remaining = campaign.goal - maybe_will_complete_count - complete_count
round_remaining = campaign.assignments_per_round
remaining = min(campaign_remaining, round_remaining)
logger.info('Posting total of {} assignments this round.'.format(remaining))
while remaining:
this_hit = min(remaining, 9) # max 9 to avoid steep 40% commission
def do_campaign_round(campaign, **kwargs):
from .models import Participant
from .experiment import app
# cancel if codeversion changes
config_code_version = task_utils.aws_services_wrapper.config['Task Parameters']['experiment_code_version']
if config_code_version != campaign.codeversion:
logger.info('Codeversion changed (campaign: {}, config {}), removing job.'.format(campaign.codeversion, config_code_version))
return app.apscheduler.remove_job(kwargs['job_id'])
# cancel if campaign goal met
complete_count = Participant.count_completed(codeversion=campaign.codeversion, mode=campaign.mode)
if complete_count >= campaign.goal:
logger.info('Campaign goal met ({}), removing job.'.format(campaign.goal))
return app.apscheduler.remove_job(kwargs['job_id'])
task_utils.aws_services_wrapper.set_mode(campaign.mode)
# how many for this round?
all_hits = task_utils.aws_services_wrapper.get_all_hits().data
available_count = task_utils.aws_services_wrapper.count_available(hits=all_hits).data
pending_count = task_utils.aws_services_wrapper.count_pending(hits=all_hits).data
maybe_will_complete_count = available_count + pending_count