def put(self, performance_meta_category): data = MyForm( Field('name', is_mandatory=True, validators=[ performance_meta_category.check_other_name_unique, ]), Field('extractor', is_mandatory=True, normalizer=PerformanceMetaCategory.normalize_extractor, validators=[ performance_meta_category.recording_platform.check_extractor ] ), Field('validatorSpec', is_mandatory=True, validators=[ PerformanceMetaCategory.check_validator ] ), ).get_data() performance_meta_category.name = data["name"] performance_meta_category.extractor = data.get("extractor") performance_meta_category.validator_spec = data["validatorSpec"] db.session.flush() return {"metaCategory": PerformanceMetaCategory.dump(performance_meta_category)}
def update_audio_checking_group(audio_checking_group): data = MyForm( Field('name', is_mandatory=True, validators=[audio_checking_group.check_other_name_unique]), Field('selectionSize', is_mandatory=True, validators=[ (validators.is_number, (), dict(min_value=1)), ]), Field('corpusCodes', is_mandatory=True, validators=[ validators.is_list, CorpusCode.check_all_exists, ]), ).get_data() # update group audio_checking_group.name = data["name"] audio_checking_group.selection_size = data["selectionSize"] audio_checking_group.assign_corpus_codes(data["corpusCodes"]) db.session.flush() db.session.commit() return jsonify( {"audioCheckingGroup": AudioCheckingGroup.dump(audio_checking_group)})
def create_recording_feedback_entry(recording): data = MyForm( Field('comment', validators=[ validators.is_string, ]), Field('flags', validators=[ validators.is_list, RecordingFlag.check_all_exists, ]), Field('changeMethod', is_mandatory=True, validators=[ AudioCheckingChangeMethod.is_valid, ]), ).get_data() change_method = AudioCheckingChangeMethod.from_name(data["changeMethod"]) flags = RecordingFlag.get_list(*data.get('flags', [])) entry = RecordingFeedbackEntry( recording=recording, user=session["current_user"], change_method=change_method, comment=data.get("comment"), ) db.session.add(entry) db.session.flush() entry.add_flags(flags) db.session.commit() return jsonify(entry=RecordingFeedbackEntry.dump(entry))
def update_track(track): data = MyForm( Field("trackIndex", validators=[ simple_validators.is_number(min_value=0), track.check_updated_index_unique, ]), Field("name", validators=[ validators.non_blank, track.check_updated_name_unique, ]), Field("mode", validators=[ validators.is_string, Track.is_valid_mode(validator=True) ]), ).get_data() if "trackIndex" in data: track.track_index = data["trackIndex"] if "name" in data: track.name = data["name"] if "mode" in data: track.update_mode(data["mode"]) db.session.commit() return jsonify(track=Track.dump(track))
def update_performance_flag(performance_flag): data = MyForm( Field('name', validators=[ performance_flag.check_updated_name_unique, ]), Field('severity', validators=[ PerformanceFlag.check_valid_severity ]), Field('enabled', validators=[ validators.is_bool, ]), ).get_data() if "name" in data: performance_flag.name = data["name"] if "severity" in data: performance_flag.severity = data["severity"] if "enabled" in data: performance_flag.enabled = data["enabled"] db.session.flush() return jsonify({"performanceFlag": PerformanceFlag.dump(performance_flag)})
def post(self, recording_platform): data = MyForm( Field('name', is_mandatory=True, validators=[ (PerformanceMetaCategory.check_name_unique, (recording_platform,)), ]), Field('extractor', is_mandatory=True, normalizer=PerformanceMetaCategory.normalize_extractor, validators=[ recording_platform.check_extractor, ] ), Field('validatorSpec', is_mandatory=True, validators=[ PerformanceMetaCategory.check_validator, ] ), ).get_data() performance_meta_category = PerformanceMetaCategory( recording_platform=recording_platform, name=data["name"], extractor=data.get("extractor"), validator_spec=data["validatorSpec"], ) db.session.add(performance_meta_category) db.session.commit() return {"metaCategory": performance_meta_category.serialize()}
def post(self, recording_platform): data = MyForm( Field("trackIndex", is_mandatory=True, validators=[ simple_validators.is_number(min_value=0), Track.check_new_index_unique(recording_platform), ]), Field("name", is_mandatory=True, validators=[ validators.non_blank, Track.check_new_name_unique(recording_platform), ]), ).get_data() track = Track( recording_platform=recording_platform, track_index=data["trackIndex"], name=data["name"], ) db.session.add(track) db.session.flush() return {"track": Track.dump(track)}
def get_loaders(): data = MyForm( Field("taskTypeId", validators=[validators.is_number, TaskType.check_exists]), Field("recordingPlatformTypeId", validators=[ validators.is_number, RecordingPlatformType.check_exists ]), ).get_data(is_json=False, use_args=True) if "taskTypeId" in data: task_type = TaskType.query.get(data["taskTypeId"]) loaders = get_available_loaders(task_type=task_type.name) elif "recordingPlatformTypeId" in data: recording_platform_type = RecordingPlatformType.query.get( data["recordingPlatformTypeId"]) loaders = get_available_loaders( recording_platform_type=recording_platform_type.name) else: loaders = [] return jsonify(loaders=loaders)
def create_alphabet(): data = MyForm( Field('name', is_mandatory=True, validators=[ validators.non_blank, check_name_uniqueness, ]), Field('description'), Field('dialectId', is_mandatory=True, validators=[ validators.is_number, check_dialect_existence, ]), Field( 'url', default=lambda: None, ), ).get_data() alphabet = m.Alphabet(**data) SS.add(alphabet) SS.flush() return jsonify({ 'message': _('created alphabet {0} successfully').format(alphabet.name), 'alphabet': m.Alphabet.dump(alphabet), })
def update_batch_router(batch_router): data = MyForm( Field("enabled", validators=[ validators.is_bool, ]), Field( "name", validators=[ validators.is_string, # TODO check unique ]), Field( "subTasks", validators=[ simple_validators.is_dict, # TODO more complex? ])).get_data() if "enabled" in data: batch_router.enabled = data["enabled"] if "name" in data: batch_router.name = data["name"] if "subTasks" in data: for sub_task in batch_router.sub_tasks: db.session.delete(sub_task) db.session.flush() batch_router.update_sub_task_criteria(data["subTasks"]) db.session.commit() return jsonify({"batchRouter": batch_router.serialize()})
def create_label_group(labelSetId): ''' creates a new label group ''' labelSet = m.LabelSet.query.get(labelSetId) if not labelSet: raise InvalidUsage( _('label set {0} not found').format(labelSetId), 404) data = MyForm( Field('name', is_mandatory=True, validators=[ validators.is_string, (check_label_group_name_uniqueness, (labelSetId, None)), ]), Field('dropDownDisplay', default=False, validators=[ validators.is_bool, ]), Field('isMandatory', default=False, validators=[ validators.is_bool, ]), ).get_data() labelGroup = m.LabelGroup(**data) SS.add(labelGroup) SS.flush() return jsonify({ 'message': _('created label group {0} successfully').format(labelGroup.name), 'labelGroup': m.LabelGroup.dump(labelGroup), })
def webservices_available_qualifications(): data = MyForm( Field('userId', is_mandatory=True, default=lambda: None, normalizer=normalize_u), Field('languageIds', is_mandatory=True, default=[], normalizer=normalize_l), ).get_data(is_json=False, copy=True) userId = data['userId'] languageIds = data['languageIds'] user = _get_user(userId) if not user or not user.isActive: raise InvalidUsage('user {} not found or inactive'.format(userId)) candidates = m.Test.query.filter_by(isEnabled=True).order_by( m.Test.testId).all() result = Filterable() for test in candidates: record = TestManager.report_eligibility(test, user, languageIds) if record is None: continue result.append(record) return dict(entries=result)
def create_sub_task_rate_record(subTaskId): subTask = m.SubTask.query.get(subTaskId) if not subTask: raise InvalidUsage(_('sub task {0} not found').format(subTaskId)) data = MyForm( Field('rateId', is_mandatory=True, validators=[]), Field('multiplier', is_mandatory=True, normalizer=lambda data, key, value: float(value), validators=[ (validators.is_number, (), dict(min_value=0)), ]), Field('bonus', default=None, validators=[ (validators.is_number, (), dict(ge=0)), ]), ).get_data() me = session['current_user'] subTaskRate = m.SubTaskRate(taskId=subTask.taskId, updatedBy=me.userId, **data) SS.add(subTaskRate) SS.flush() return jsonify({ 'message': _('created sub task rate {0} successfully').format( subTaskRate.subTaskRateId), 'subTaskRate': m.SubTaskRate.dump(subTaskRate), })
def create_error_type(): ''' creates a new error type ''' data = MyForm( Field('name', is_mandatory=True, validators=[ validators.non_blank, check_name_uniqueness, ]), Field('errorClassId', is_mandatory=True, validators=[ check_error_class_existence, ]), Field('defaultSeverity', is_mandatory=True, normalizer=lambda data, key, value: float(value), validators=[ (validators.is_number, (), dict(max_value=1, min_value=0)), ]), ).get_data() errorType = m.ErrorType(**data) SS.add(errorType) SS.flush() return jsonify({ 'message': _('created error type {0} successfully').format(errorType.name), 'errorType': m.ErrorType.dump(errorType), })
def create_audio_checking_section(recording_platform): data = MyForm( Field("startPosition", is_mandatory=True, validators=[(validators.is_number, (), dict(ge=0, lt=1))]), Field("endPosition", is_mandatory=True, validators=[(validators.is_number, (), dict(gt=0, le=1))]), Field("checkPercentage", is_mandatory=True, validators=[(validators.is_number, (), dict(gt=0, le=1))]), ).get_data() audio_checking_section = AudioCheckingSection( recording_platform=recording_platform, start_position=data["startPosition"], end_position=data["endPosition"], check_percentage=data["checkPercentage"], ) db.session.add(audio_checking_section) db.session.flush() return jsonify({ "audioCheckingSection": AudioCheckingSection.dump(audio_checking_section) })
def post(self, task): if not task.is_type(TaskType.TRANSCRIPTION, TaskType.AUDIO_CHECKING): raise InvalidUsage( "audio uploads are only available for audio tasks", 400) data = MyForm( Field("loadManager", is_mandatory=True, validators=[ simple_validators.is_dict(), ]), Field("isEmpty", is_mandatory=True, validators=[ validators.is_bool, ]), ).get_data() audio_upload = AudioUpload( task=task, data=data["loadManager"], hidden=AudioUpload.get_hidden_flag(data["loadManager"], data["isEmpty"]), ) db.session.add(audio_upload) db.session.commit() return {"audioUpload": audio_upload.serialize()}
def create_new_alphabet_rule(alphabetId): alphabet = m.Alphabet.query.get(alphabetId) if not alphabet: raise InvalidUsage(_('alphabet {0} not found').format(alphabetId), 404) data = MyForm( Field('name'), Field('type'), Field('description'), ).get_data() rule = m.Rule(**data) rule.alphabetId = alphabetId SS.add(rule) SS.flush() return jsonify(rule=m.Rule.dump(rule))
def add_wav_audio_sandbox_file(audio_sandbox): data = MyForm( Field("filePath", is_mandatory=True, validators=[ validators.non_blank, ]), ).get_data() spec, data_pointer = audio_server.api.parse_wav(data["filePath"]) waveform_data = audio_server.api.get_waveform(spec, data_pointer, data["filePath"]) audio_sandbox_file = AudioSandboxFile( audio_sandbox=audio_sandbox, file_path=data["filePath"], audio_spec=spec.to_dict(), audio_data_pointer=data_pointer.to_dict(), is_wav=True, data={"stats": { "waveform": waveform_data, }}) db.session.add(audio_sandbox_file) db.session.flush() return jsonify( {"audioSandboxFile": AudioSandboxFile.dump(audio_sandbox_file)})
def save_headwords(taskId): task = m.Task.query.get(taskId) if not task: raise InvalidUsage(_('task {0} not found').format(taskId), 404) if task.taskType != 'Spelling': raise InvalidUsage( _('task {0} has unexpected task type').format(taskId)) data = MyForm(Field( 'headwords', is_mandatory=True, )).get_data() load = m.Load(taskId=taskId, createdBy=699) SS.add(load) SS.flush() rawPieces = [] for i, r in enumerate(data['headwords']): assemblyContext = 'L_%05d_%05d' % (load.loadId, i) allocationContext = 'L_%05d' % load.loadId try: del r['meta'] except KeyError: pass print r rawPiece = m.RawPiece(taskId=taskId, loadId=load.loadId, assemblyContext=assemblyContext, allocationContext=allocationContext, words=1, **r) rawPieces.append(rawPiece) SS.add(rawPiece) SS.flush() return jsonify(rawPieces=m.RawPiece.dump(rawPieces))
def populate_rework_sub_task_from_extract(subTaskId): subTask = m.SubTask.query.get(subTaskId) if not subTask: raise InvalidUsage(_('sub task {0} not found').format(subTaskId), 404) if not subTask.workType == m.WorkType.REWORK: raise InvalidUsage( _('work type {0} not supported').format(m.WorkType.REWORK)) data = MyForm( Field('srcSubTaskId', ), Field('dataFile', is_mandatory=True, validators=[ validators.is_file, ]), Field('validation', default='false', normalizer=normalize_bool_literal, validators=[ validators.is_bool, ]), ).get_data(is_json=False) if data['validation']: return jsonify(message=_('data file validated')) srcSubTask = m.SubTask.query.get(data['srcSubTaskId']) dstSubTask = subTask fakeUser = session['current_user'] tx_loader = TxLoader(subTask.taskId) result = tx_loader.load_tx_file(data['dataFile'], srcSubTask, fakeUser, dstSubTask) itemCount = result['itemCount'] me = session['current_user'] now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) # add rework content event content_event = m.SubTaskContentEvent(subTaskId=subTaskId, isAdding=True, tProcessedAt=now, itemCount=itemCount, operator=me.userId) SS.add(content_event) SS.flush() return jsonify( message=_('okay'), event=m.SubTaskContentEvent.dump(content_event), )
def submit_answer(sheetId): sheet = m.Sheet.query.get(sheetId) if not sheet: raise InvalidUsage(_('sheet {0} not found').format(sheetId), 404) now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) if sheet.tExpiresBy < now: sheet.tExpiredAt = now SS.flush() SS.commit() raise InvalidUsage(_('sheet {0} has expired already').format(sheetId)) me = session['current_user'] if sheet.userId != me.userId: raise InvalidUsage( _('you are not the owner of sheet {0}').format(sheetId)) data = MyForm( Field('sheetEntryId', is_mandatory=True, validators=[ (check_sheet_entry_existence, (sheetId, )), ]), Field('answer', is_mandatory=True, validators=[ validators.is_string, check_answer, ]), ).get_data(with_view_args=False) answer = m.Answer(**data) SS.add(answer) SS.flush() assert answer.answerId # TODO: define relationship on SheetEntry entry = m.SheetEntry.query.get(data['sheetEntryId']) entry.answerId = answer.answerId return jsonify({ 'message': _('created answer {0} successfully').format(answer.answerId), 'answer': m.Answer.dump(answer), })
def submit_marking(sheetId): sheet = m.Sheet.query.get(sheetId) if not sheet: raise InvalidUsage(_('sheet {0} not found').format(sheetId), 404) # TODO: add policy check to enable/disable re-marking and/or marking of expired sheets data = MyForm( Field('moreAttempts', is_mandatory=True, validators=[ validators.is_bool, ]), Field('comment', validators=[ validators.is_string, ]), Field('markings', is_mandatory=True, normalizer=normalize_marking_data), Field('score', is_mandatory=True, default=0, normalizer=calculate_sheet_score), ).get_data() # TODO: define relationship marking on SheetEntry me = session['current_user'] markings = data.pop('markings') for entry, md in zip(sheet.entries, markings): marking = m.Marking(**md) marking.sheetEntryId = entry.sheetEntryId marking.scorerId = me.userId SS.add(marking) SS.flush() entry.markingId = marking.markingId for key in ['moreAttempts', 'comment', 'score']: setattr(sheet, key, data[key]) return jsonify({ 'message': _('marked sheet {0} successfully').format(sheetId), 'sheet': m.Sheet.dump(sheet), })
def update_label_group(labelSetId, labelGroupId): ''' updates label group settings ''' labelSet = m.LabelSet.query.get(labelSetId) if not labelSet: raise InvalidUsage( _('label set {0} not found').format(labelSetId), 404) labelGroup = m.LabelGroup.query.get(labelGroupId) if not labelGroup or labelGroup.labelSetId != labelSetId: raise InvalidUsage( _('label group {0} not found').format(labelGroupId), 404) data = MyForm( Field('name', validators=[ validators.is_string, (check_label_group_name_uniqueness, (labelSetId, labelGroupId)), ]), Field('dropDownDisplay', validators=[ validators.is_bool, ]), Field('isMandatory', validators=[ validators.is_bool, ]), ).get_data() for key in data.keys(): value = data[key] if getattr(labelGroup, key) != value: setattr(labelGroup, key, value) else: del data[key] SS.flush() return jsonify({ 'message': _('updated label group {0} successfully').format(labelGroup.name), 'labelGroup': m.LabelGroup.dump(labelGroup), 'updatedFields': data.keys(), })
def create_label(labelSetId): ''' creates a new label ''' labelSet = m.LabelSet.query.get(labelSetId) if not labelSet: raise InvalidUsage( _('label set {0} not found').format(labelSetId), 404) data = MyForm( Field('name', is_mandatory=True, validators=[ (check_label_name_uniqueness, (labelSetId, None)), ]), Field('description'), Field('shortcutKey', validators=[ (validators.is_string, (), dict(length=1)), check_label_shortcut_key_non_space, (check_label_shortcut_key_uniqueness, (labelSetId, None)), ]), Field('extract', is_mandatory=True, validators=[ validators.non_blank, (check_label_extract_uniqueness, (labelSetId, None)), ]), Field('labelGroupId', validators=[ (check_label_group_existence, (labelSetId, )), ]), ).get_data() label = m.Label(**data) SS.add(label) SS.flush() return jsonify({ 'message': _('created label {0} successfully').format(label.name), 'label': m.Label.dump(label), })
def update_corpus_code(corpus_code): data = MyForm( Field("code", is_mandatory=True, validators=[ corpus_code.check_updated_code_unique, ]), Field("regex"), Field("isScripted", is_mandatory=True, validators=[validators.is_bool]), ).get_data() corpus_code.code = data.get("code") corpus_code.regex = data["regex"] corpus_code.is_scripted = data["isScripted"] db.session.commit() return jsonify({"corpusCode": CorpusCode.dump(corpus_code)})
def update_api_access_pair(api_access_pair): data = MyForm( Field("enabled", is_mandatory=True, validators=[validators.is_bool]), ).get_data() api_access_pair.enabled = data["enabled"] db.session.commit() return jsonify({"apiAccessPair": ApiAccessPair.dump(api_access_pair)})
def put(self, audio_upload): data = MyForm(Field("hidden", validators=[ validators.is_bool, ])).get_data() if "hidden" in data: audio_upload.hidden = data["hidden"] db.session.commit() return {"audioUpload": audio_upload.serialize()}
def update_sub_task_worker_settings(subTaskId, userId): subTask = m.SubTask.query.get(subTaskId) if not subTask: raise InvalidUsage(_('sub task {0} not found').format(subTaskId), 404) user = m.User.query.get(userId) if not user: raise InvalidUsage(_('user {0} not found').format(userId), 404) if not subTask._workType.workable: raise InvalidUsage("no workers allowed for this sub task", 400) data = MyForm( Field('hasReadInstructions', validators=[ validators.is_bool, ]), Field('isNew', validators=[ validators.is_bool, ]), Field('paymentFactor', normalizer=lambda data, key, value: float(value), validators=[ (validators.is_number, (), dict(min_value=0)), ]), Field('removed', validators=[ validators.is_bool, ]), ).get_data() worker = m.TaskWorker.query.get((userId, subTask.taskId, subTaskId)) if not worker: worker = m.TaskWorker(taskId=subTask.taskId, **data) SS.add(worker) else: for key in data: setattr(worker, key, data[key]) SS.flush() return jsonify({ 'worker': m.TaskWorker.dump(worker), })
def upload_performance_list(recording_platform): data = MyForm( Field("changeMethod", is_mandatory=True, validators=[AudioCheckingChangeMethod.is_valid]), Field("subTaskId", is_mandatory=True, validators=[SubTask.check_exists]), Field("performanceNames", is_mandatory=True, validators=[ validators.is_list, ]), ).get_data() destination = SubTask.query.get(data["subTaskId"]) performances = [] for performance_name in data["performanceNames"]: # TODO handle exception performance = Performance.query.filter_by( recording_platform=recording_platform, name=performance_name).one() performances.append(performance) moved, at_destination, no_transition, locked = recording_platform.move_performances( performances, destination, session["current_user"].user_id, data["changeMethod"]) db.session.flush() return jsonify({ "atDestination": len(at_destination), "noTransition": len(no_transition), "moved": len(moved), "locked": len(locked), "performances": Performance.dump(performances, use="full"), })
def webservices_apply_user_search_action(): data = MyForm( Field('identifier', is_mandatory=True, validators=[ check_action_identifier, ]), Field('option', is_mandatory=True), Field('users', is_mandatory=True, validators=[ validators.non_blank, ]), Field('userIds', is_mandatory=True, default=[], normalizer=normalize_user_ids), ).get_data(is_json=False, copy=True) action = UserSearchAction.get_at_action(data['identifier']) userIds = data['userIds'] option = data['option'] reply = action.func(userIds, option) return dict(reply=reply)