Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def edit_assignment(assignment, new_config):
for k, v in list(new_config.items()):
setattr(assignment, k, v)
db_session.add(assignment)
db_session.commit()
def campaign():
from psiturk.models import Campaign
parameters = {
'codeversion': '1.0',
'mode': 'sandbox',
'goal': 100,
'minutes_between_rounds': 1,
'assignments_per_round': 10,
'hit_reward': 1.00,
'hit_duration_hours': 1,
}
new_campaign = Campaign(**parameters)
from psiturk.db import db_session
db_session.add(new_campaign)
db_session.commit()
return new_campaign
def edit_assignment(assignment, new_config):
for k, v in list(new_config.items()):
setattr(assignment, k, v)
db_session.add(assignment)
db_session.commit()
def test_wrapper_get_all_hits_all_studies(self, amt_services_wrapper, create_dummy_hit, list_hits, helpers):
create_dummy_hit('3XJOUITW8URHJMX7F00H20LGRIAQTX')
create_dummy_hit('3BFNCI9LYKQ2ENUY4MLKKW0NSU437W')
from psiturk.db import db_session
from psiturk.models import Hit
Hit.query.filter_by(hitid='3BFNCI9LYKQ2ENUY4MLKKW0NSU437W').delete()
db_session.commit()
hits = list_hits(all_studies=False)
assert '3BFNCI9LYKQ2ENUY4MLKKW0NSU437W' not in [
hit.options['hitid'] for hit in hits]
hits = list_hits(all_studies=True)
assert '3BFNCI9LYKQ2ENUY4MLKKW0NSU437W' in [
hit.options['hitid'] for hit in hits]
def approve_local_assignment(self, assignment):
try:
assignment_id = assignment.assignmentid
response = self.amt_services.approve_assignment(assignment_id)
if not response.success:
raise response.exception
assignment.status = CREDITED
db_session.add(assignment)
db_session.commit()
return {'assignment_id': assignment_id}
except Exception as e:
return {'exception': e, 'assignment_id': assignment_id}
if local_assignment.status == BONUSED and not override_bonused_status:
message = 'Participant with assignment_id {} already bonused, and override not set. Not bonusing.'.format(
local_assignment.assignmentid)
raise AssignmentAlreadyBonusedError(message=message)
if amount == 'auto':
if not local_assignment.bonus:
return {'exception': NoAutoBonusAmountSetError(), 'assignment_id': assignment_id}
amount = local_assignment.bonus
response = self.bonus_nonlocal_assignment(
assignment_id, amount, reason, worker_id=local_assignment.workerid)
if response.status == 'success':
# result['message'] = "gave bonus of ${} for assignment {}".format(str(amount), local_assignment.assignmentid)
local_assignment.status = BONUSED
db_session.add(local_assignment)
db_session.commit()
return response
message = 'Participant with assignment_id {} already bonused, and override not set. Not bonusing.'.format(
local_assignment.assignmentid)
raise AssignmentAlreadyBonusedError(message=message)
if amount == 'auto':
if not local_assignment.bonus:
return {'exception': NoAutoBonusAmountSetError(), 'assignment_id': assignment_id}
amount = local_assignment.bonus
response = self.bonus_nonlocal_assignment(
assignment_id, amount, reason, worker_id=local_assignment.workerid)
if response.status == 'success':
# result['message'] = "gave bonus of ${} for assignment {}".format(str(amount), local_assignment.assignmentid)
local_assignment.status = BONUSED
db_session.add(local_assignment)
db_session.commit()
return response
try:
# lookup user in database
user = Participant.query.\
filter(Participant.uniqueid == uniqueId).\
one()
user_data = loads(user.datastring) # load datastring from JSON
bonus = 0
for record in user_data['data']: # for line in data file
trial = record['trialdata']
if trial['phase'] == 'TEST':
if trial['hit'] is True:
bonus += 0.02
user.bonus = bonus
db_session.add(user)
db_session.commit()
resp = {"bonusComputed": "success"}
return jsonify(**resp)
except:
abort(404) # again, bad to display HTML, but...
"""
app.logger.info("PUT /sync route with id: %s" % uid)
try:
user = Participant.query.\
filter(Participant.uniqueid == uid).\
one()
except exc.SQLAlchemyError:
app.logger.error("DB error: Unique user not found.")
if hasattr(request, 'json'):
user.datastring = request.data.decode('utf-8').encode(
'ascii', 'xmlcharrefreplace'
)
db_session.add(user)
db_session.commit()
try:
data = json.loads(user.datastring)
except:
data = {}
trial = data.get("currenttrial", None)
app.logger.info("saved data for %s (current trial: %s)", uid, trial)
resp = {"status": "user data saved"}
return jsonify(**resp)
from itertools import groupby
config = PsiturkConfig()
config.load_config()
TABLENAME = config.get('Database Parameters', 'table_name')
CODE_VERSION = config.get('Task Parameters', 'experiment_code_version')
from .tasks import do_campaign_round
from apscheduler.jobstores.base import JobLookupError
# Base class
Base = declarative_base()
Base.query = db_session.query_property()
def object_as_dict(self, filter_these=[]):
return {c.key: getattr(self, c.key)
for c in inspect(self).mapper.column_attrs if c.key not in filter_these}
Base.object_as_dict = object_as_dict
class Participant(Base):
"""
Object representation of a participant in the database.
"""
__tablename__ = TABLENAME
uniqueid = Column(String(128), primary_key=True)
assignmentid = Column(String(128), nullable=False)
workerid = Column(String(128), nullable=False)