def test_list_fields_with_nested_inherited(self): api = Api(self.app) person = api.model('Person', { 'name': fields.String, 'age': fields.Integer }) child = api.inherit('Child', person, { 'attr': fields.String }) family = api.model('Family', { 'children': fields.List(fields.Nested(child)) }) result = mask.apply(family.resolved, 'children{name,attr}') data = {'children': [ {'name': 'John', 'age': 5, 'attr': 'value-john'}, {'name': 'Jane', 'age': 42, 'attr': 'value-jane'}, ]} expected = {'children': [ {'name': 'John', 'attr': 'value-john'}, {'name': 'Jane', 'attr': 'value-jane'}, ]} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family), data)
def test_multiple_nested_api_fields(self): level_2 = {'nested_2': fields.Nested(person_fields)} level_1 = {'nested_1': fields.Nested(level_2)} root = {'nested': fields.Nested(level_1)} result = mask.apply(root, 'nested{nested_1{nested_2{name}}}') self.assertEqual(set(result.keys()), set(['nested'])) self.assertIsInstance(result['nested'], fields.Nested) self.assertEqual(set(result['nested'].nested.keys()), set(['nested_1'])) data = { 'nested': { 'nested_1': { 'nested_2': {'name': 'John', 'age': 42} } } } expected = { 'nested': { 'nested_1': { 'nested_2': {'name': 'John'} } } } self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, root), data)
def export_event_json(event_id, settings): """ Exports the event as a zip on the server and return its path """ # make directory dir_path = 'static/exports/event%d' % event_id if os.path.isdir(dir_path): shutil.rmtree(dir_path, ignore_errors=True) os.mkdir(dir_path) # save to directory for e in EXPORTS: if e[0] == 'event': data = _order_json(marshal(e[1].get(event_id), e[2]), e) _download_media(data, 'event', dir_path, settings) else: data = marshal(e[1].list(event_id), e[2]) for count in range(len(data)): data[count] = _order_json(data[count], e) _download_media(data[count], e[0], dir_path, settings) data_str = json.dumps(data, indent=4, ensure_ascii=False).encode('utf-8') fp = open(dir_path + '/' + e[0], 'w') fp.write(data_str) fp.close() # add meta data_str = json.dumps( _generate_meta(), sort_keys=True, indent=4, ensure_ascii=False ).encode('utf-8') fp = open(dir_path + '/meta', 'w') fp.write(data_str) fp.close() # make zip shutil.make_archive(dir_path, 'zip', dir_path) return os.path.realpath('.') + '/' + dir_path + '.zip'
def get(self, aid, sid, cid, length, algorithm): '''Summarize reviews given assignment id, reviewee id, criterion id, and length of the summary''' alg = str(algorithm).lower() if not alg in SUPPORTED_ALGORITHMS: return marshal({'message':MESSAGE_ALGORITHM_NOT_SUPPORTED}, message_marshaller), 404 results = self.sum.get_summary_by_assignment_criterion_reviewee(aid,cid,sid, length=length, algorithm=alg) try: return marshal({'summary':results}, summary_marshaller), 200 except Exception as e: abort(500, message=str(e))
def test_marshal_nested(self): model = OrderedDict([("foo", fields.Raw), ("fee", fields.Nested({"fye": fields.String}))]) marshal_fields = OrderedDict([("foo", "bar"), ("bat", "baz"), ("fee", {"fye": "fum"})]) output = marshal(marshal_fields, model) expected = OrderedDict([("foo", "bar"), ("fee", OrderedDict([("fye", "fum")]))]) self.assertEquals(output, expected)
def test_list_fields_with_raw(self): family_fields = { 'members': fields.List(fields.Raw) } result = mask.apply(family_fields, 'members{name}') data = {'members': [ {'name': 'John', 'age': 42}, {'name': 'Jane', 'age': 42}, ]} expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_marshal_handle_inheritance(self): api = Api(self.app) person = api.model('Person', { 'name': fields.String, 'age': fields.Integer, }) child = api.inherit('Child', person, { 'extra': fields.String, }) data = { 'name': 'John Doe', 'age': 42, 'extra': 'extra' } values = ( ('name', {'name': 'John Doe'}), ('name,extra', {'name': 'John Doe', 'extra': 'extra'}), ('extra', {'extra': 'extra'}), ) for mask, expected in values: result = marshal(data, child, mask=mask) self.assertEqual(result, expected)
def test_marshal(self): model = OrderedDict([('foo', fields.Raw)]) marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')]) output = marshal(marshal_dict, model) assert isinstance(output, dict) assert not isinstance(output, OrderedDict) assert output == {'foo': 'bar'}
def post(self, subsystem, volume_name): if DISALLOWED_VOLUME_NAME_RE.match(volume_name): api.abort(400, "Invalid volume name") data = marshal(storage_api.apis.api.payload, volume_create_w_snapshot_model) if data['from_volume'] and data['from_snapshot']: with keyerror_is_404(), valueerror_is_400(): new_vol = backend(subsystem).clone_volume( volume_name, data['from_volume'], data['from_snapshot']) elif data['from_snapshot']: with keyerror_is_404(): new_vol = backend(subsystem).rollback_volume( volume_name, restore_snapshot_name=data['from_snapshot']) else: with valueerror_is_400(), keyerror_is_400(): new_vol = backend(subsystem).create_volume( volume_name, **dict_without(dict(data), 'from_snapshot', 'from_volume')) return new_vol, 201
def test_marshal_nested_property_with_skip_none(self): class TestObject(object): @property def fee(self): return {'blah': 'cool', 'foe': None} model = OrderedDict([ ('foo', fields.Raw), ('fee', fields.Nested( OrderedDict([ ('fye', fields.String), ('blah', fields.String), ('foe', fields.String) ]), skip_none=True)) ]) obj = TestObject() obj.foo = 'bar' obj.bat = 'baz' output = marshal([obj], model) expected = [OrderedDict([ ('foo', 'bar'), ('fee', OrderedDict([ ('blah', 'cool') ])) ])] assert output == expected
def get(self, aid, cid): '''Fetch list of reviews on an assignment based on a criteria''' results = self.dal.get_reviews_by_assignment_criterion(aid,cid) try: return marshal({'reviews':results}, review_list_marshaller), 200 except Exception as e: abort(500, message=str(e))
def delete(self, aid, sid, cid): '''Delete reviews given assignment id, reviewee id, and criterion id''' if not self.dal.is_record_exist(aid, reviewee_id=sid, criterion_id=cid): abort(404, message=MESSAGE_DOESNT_EXIST) else: self.dal.del_reviews_by_assignment_criterion_reviewee(aid, sid, cid) return marshal({'message':MESSAGE_DELETED}, message_marshaller), 200
def get(self, aid, sid, cid): '''Fetch reviews given assignment id, reviewee id, and criterion id''' results = self.dal.get_reviews_by_assignment_criterion_reviewee(aid, sid, cid) try: return marshal({'reviews':results}, review_list_marshaller), 200 except Exception as e: abort(500, message=str(e))
def put(self, taxi_id): hj = request.json t, last_update_at = self.get_descriptions(taxi_id) new_status = hj['data'][0]['status'] if new_status != t[0]['vehicle_description_status'] or\ t[0]['taxi_last_update_at'] is None or\ t[0]['taxi_last_update_at'] <= (datetime.now() - timedelta(hours=4)): cur = current_app.extensions['sqlalchemy'].db.session.\ connection().connection.cursor() cur.execute("UPDATE vehicle_description SET status=%s WHERE id=%s", (new_status, t[0]['vehicle_description_id']) ) cur.execute("UPDATE taxi set last_update_at = %s WHERE id = %s", (datetime.now(), t[0]['taxi_id']) ) current_app.extensions['sqlalchemy'].db.session.commit() taxis_models.RawTaxi.flush(taxi_id) t[0]['vehicle_description_status'] = new_status taxi_id_operator = "{}:{}".format(taxi_id, current_user.email) if t[0]['vehicle_description_status'] == 'free': redis_store.zrem(current_app.config['REDIS_NOT_AVAILABLE'], taxi_id_operator) else: redis_store.zadd(current_app.config['REDIS_NOT_AVAILABLE'], 0., taxi_id_operator) taxi_m = marshal({'data':[ taxis_models.RawTaxi.generate_dict(t, operator=current_user.email)] }, taxi_model) taxi_m['data'][0]['operator'] = current_user.email taxi_m['data'][0]['last_update'] = last_update_at return taxi_m
def get(self, aid, sid, cid, length): '''Summarize reviews given assignment id, reviewee id, criterion id, and length of the summary''' results = self.sum.get_summary_by_assignment_criterion_reviewee(aid,cid,sid, length=length) try: return marshal({'summary':results}, summary_marshaller), 200 except Exception as e: abort(500, message=str(e))
def get(self, aid, sid): '''Summarize reviews given assignment id and reviewee id''' results = self.sum.get_summary_by_assignment_reviewee(aid, sid) try: return marshal({'summary':results}, summary_marshaller), 200 except Exception as e: abort(500, message=str(e))
def export_event_json(event_id, settings): """ Exports the event as a zip on the server and return its path """ # make directory exports_dir = app.config['BASE_DIR'] + '/static/uploads/exports/' if not os.path.isdir(exports_dir): os.mkdir(exports_dir) dir_path = exports_dir + 'event%d' % event_id if os.path.isdir(dir_path): shutil.rmtree(dir_path, ignore_errors=True) os.mkdir(dir_path) # save to directory for e in EXPORTS: if e[0] == 'event': data = _order_json(marshal(e[1].get(event_id), e[2]), e) _download_media(data, 'event', dir_path, settings) else: data = marshal(e[1].list(event_id), e[2]) for count in range(len(data)): data[count] = _order_json(data[count], e) _download_media(data[count], e[0], dir_path, settings) data_str = json.dumps(data, indent=4, ensure_ascii=False).encode('utf-8') fp = open(dir_path + '/' + e[0], 'w') fp.write(data_str) fp.close() # add meta data_str = json.dumps( _generate_meta(), sort_keys=True, indent=4, ensure_ascii=False ).encode('utf-8') fp = open(dir_path + '/meta', 'w') fp.write(data_str) fp.close() # make zip shutil.make_archive(dir_path, 'zip', dir_path) dir_path = dir_path + ".zip" storage_path = UPLOAD_PATHS['exports']['zip'].format( event_id=event_id ) uploaded_file = UploadedFile(dir_path, dir_path.rsplit('/', 1)[1]) storage_url = upload(uploaded_file, storage_path) if get_settings()['storage_place'] != "s3" and get_settings()['storage_place'] != 'gs': storage_url = app.config['BASE_DIR'] + storage_url.replace("/serve_", "/") return storage_url
def test_list_fields_with_simple_field(self): family_fields = { 'name': fields.String, 'members': fields.List(fields.String) } result = mask.apply(family_fields, 'members') self.assertEqual(set(result.keys()), set(['members'])) self.assertIsInstance(result['members'], fields.List) self.assertIsInstance(result['members'].container, fields.String) data = {'name': 'Doe', 'members': ['John', 'Jane']} expected = {'members': ['John', 'Jane']} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def post(self): '''Summarize a given set of sentences''' try: js = request.get_json() results = self.sum.get_summary_generic(js) return marshal({'summary':results}, summary_marshaller), 200 except Exception as e: abort(500, message=str(e))
def get(self, taxi_id): t, last_update_at = self.get_descriptions(taxi_id) taxi_m = marshal({'data':[ taxis_models.RawTaxi.generate_dict(t, operator=current_user.email)]}, taxi_model) taxi_m['data'][0]['operator'] = current_user.email taxi_m['data'][0]['last_update'] = last_update_at return taxi_m
def post(self, length, algorithm): '''Summarize a given set of sentences, length of the summar, and type of algorithm''' try: js = request.get_json() results=self.sum.get_summary_generic(js, length, algorithm) return marshal({'summary':results}, summary_marshaller), 200 except Exception as e: abort(500, message=str(e))
def test_raw_api_fields(self): family_fields = { 'father': fields.Raw, 'mother': fields.Raw, } result = mask.apply(family_fields, 'father{name},mother{age}') data = { 'father': {'name': 'John', 'age': 42}, 'mother': {'name': 'Jane', 'age': 42}, } expected = {'father': {'name': 'John'}, 'mother': {'age': 42}} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_marshal_nested_dict(self): model = OrderedDict([("foo", fields.Raw), ("bar", OrderedDict([("a", fields.Raw), ("b", fields.Raw)]))]) marshal_fields = OrderedDict( [("foo", "foo-val"), ("bar", "bar-val"), ("bat", "bat-val"), ("a", 1), ("b", 2), ("c", 3)] ) output = marshal(marshal_fields, model) expected = OrderedDict([("foo", "foo-val"), ("bar", OrderedDict([("a", 1), ("b", 2)]))]) self.assertEquals(output, expected)
def post(self, aid, sid, cid): '''Add all reviews given assignment id, reviewee id, and criterion id''' try: js = request.get_json() tuples = [(aid, sid, cid, row["reviewer_id"], row["score"], row["feedback"]) for row in js["reviews"]] self.dal.insert_tuples(tuples) except Exception as e: return abort(500, message=str(e)) return marshal({'message':MESSAGE_ADDED}, message_marshaller), 200
def test_marshal_list(self): fields = OrderedDict([ ('foo', restplus.fields.Raw), ('fee', restplus.fields.List(restplus.fields.String)) ]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', ['fye', 'fum'])]) output = restplus.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', (['fye', 'fum']))]) self.assertEqual(output, expected)
def send_request_operator(hail_id, endpoint, operator_header_name, operator_api_key, operator_email): operator_api_key = operator_api_key.encode('utf-8') operator_header_name = operator_header_name.encode('utf-8') hail = Hail.cache.get(hail_id) if not hail: current_app.logger.error('Unable to find hail: {}'.format(hail_id)) return False headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} if operator_header_name is not None and operator_header_name != '': headers[operator_header_name] = operator_api_key data = None try: data = json.dumps(marshal({"data": [hail]}, hail_model)) except ValueError: current_app.logger.error('Unable to dump JSON ({})'.format(hail)) if data: r = None hail_log = HailLog('POST to operator', hail, data) try: r = requests.post(endpoint, data=data, headers=headers ) except requests.exceptions.RequestException as e: current_app.logger.error('Error calling: {}, endpoint: {}, headers: {}'.format( operator_email, endpoint, headers)) current_app.logger.error(e) hail_log.store(None, redis_store_haillog, str(e)) if r: hail_log.store(r, redis_store_haillog) if not r or r.status_code < 200 or r.status_code >= 300: hail.status = 'failure' current_app.extensions['sqlalchemy'].db.session.commit() current_app.logger.error("Unable to reach hail's endpoint {} of operator {}"\ .format(endpoint, operator_email)) return False r_json = None try: r_json = r.json() except ValueError: pass if r_json and 'data' in r_json and len(r_json['data']) == 1\ and 'taxi_phone_number' in r_json['data'][0]: hail.taxi_phone_number = r_json['data'][0]['taxi_phone_number'] else: current_app.logger.error('No JSON in operator answer of {} : {}'.format( operator_email, r.text)) hail.status = 'received_by_operator' current_app.extensions['sqlalchemy'].db.session.commit() return True
def get(self, aid, cid, len, alg): """Summarize reviews given assignment id, reviewee id, and length of the summary""" alg = str(alg).lower() if not alg in SUPPORTED_ALGORITHMS: return marshal({"message": MESSAGE_ALGORITHM_NOT_SUPPORTED}, message_marshaller), 404 results = self.sum.get_summary_by_assignment_criterion(aid, cid, length=len, algorithm=alg) try: return {"summary": results}, 200 except Exception as e: abort(500, message=str(e))
def test_marshal_list(self): model = OrderedDict([ ('foo', fields.Raw), ('fee', fields.List(fields.String)) ]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', ['fye', 'fum'])]) output = marshal(marshal_fields, model) expected = OrderedDict([('foo', 'bar'), ('fee', (['fye', 'fum']))]) assert output == expected
def test_list_fields_with_nested(self): family_fields = { 'members': fields.List(fields.Nested(person_fields)) } result = mask.apply(family_fields, 'members{name}') self.assertEqual(set(result.keys()), set(['members'])) self.assertIsInstance(result['members'], fields.List) self.assertIsInstance(result['members'].container, fields.Nested) self.assertEqual(set(result['members'].container.nested.keys()), set(['name'])) data = {'members': [ {'name': 'John', 'age': 42}, {'name': 'Jane', 'age': 42}, ]} expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]} self.assertDataEqual(marshal(data, result), expected) # Should leave th original mask untouched self.assertDataEqual(marshal(data, family_fields), data)
def test_marshal_list_of_nesteds(self): fields = OrderedDict([ ('foo', restplus.fields.Raw), ('fee', restplus.fields.List(restplus.fields.Nested({ 'fye': restplus.fields.String }))) ]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'fye': 'fum'})]) output = restplus.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', [OrderedDict([('fye', 'fum')])])]) self.assertEqual(output, expected)
def test_get_loci_when_valid_species_id(self): """ Positive case: test getting back all QTL records for a specific valid species. """ species_id = QTLS_DATA[0][0] expected_loci_ids = [ locus[11] for locus in QTLS_DATA if locus[0] == species_id ] loci = SESSION.query(Feature)\ .filter(and_(Feature.taxon_id == species_id, Feature.type == 'QTL')).all() self.assertIsNotNone(loci) for locus in loci: serialized = marshal(locus, QTLS_SCHEMA) self.assertTrue(serialized["id"] in expected_loci_ids)
def testdata_delete_comment(app, test_user, testdata_posts): posts, courses, categories, semesters = testdata_posts with app.app_context(): post = Post.query.get(posts[0]['id']) # add a bunch of comments to a post for i in range(10): comment = Comment(content='%d' % i, post=post, author_id=test_user.id) db.session.add(comment) db.session.commit() return marshal(post, post_marshal_model)
def test_marshal_nested(self): fields = OrderedDict([('foo', restplus.fields.Raw), ('fee', restplus.fields.Nested({ 'fye': restplus.fields.String, }))]) marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', { 'fye': 'fum' })]) output = restplus.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', 'fum')]))]) self.assertEqual(output, expected)
def test_marshal_nested_dict(self): fields = OrderedDict([ ('foo', restplus.fields.Raw), ('bar', OrderedDict([ ('a', restplus.fields.Raw), ('b', restplus.fields.Raw), ])), ]) marshal_fields = OrderedDict([('foo', 'foo-val'), ('bar', 'bar-val'), ('bat', 'bat-val'), ('a', 1), ('b', 2), ('c', 3)]) output = restplus.marshal(marshal_fields, fields) expected = OrderedDict([('foo', 'foo-val'), ('bar', OrderedDict([('a', 1), ('b', 2)]))]) self.assertEqual(output, expected)
def create(self, args): # set up the args args['id'] = self.n + 1 # marshal/format the args newReport = marshal(args, models.nested_article_model) newReport = format_raw_article(newReport) # update reportDAO self.reports.append(newReport) self.n = self.n + 1 # update clean.json dumpData(self.reports) return newReport
def test_marshal_wildcard_with_envelope(self): wild = fields.Wildcard(fields.String) model = OrderedDict([('foo', fields.Raw), ('*', wild)]) marshal_dict = OrderedDict([('foo', { 'bat': 'baz' }), ('a', 'toto'), ('b', 'tata')]) output = marshal(marshal_dict, model, envelope='hey') assert output == { 'hey': { 'a': 'toto', 'b': 'tata', 'foo': { 'bat': 'baz' } } }
def get_booking_by_id(self, booking_id, raise_error=True): """ get entities by _id :param booking_type :param booking_id: :return: """ _count, _booking = self.get(BOOKING_COLLECTION, {f"{META}.{IS_DELETED}": False, ID: ObjectId(booking_id)}) if _count == 1 and _booking: return marshal(_booking[0], booking_response) elif raise_error: abort(ENTITY_NOT_FOUND, message="Entity with ID : {} does not exists.".format(booking_id), status=ENTITY_NOT_FOUND) else: return None
def post(self, host_identifier): args = self.parser.parse_args() # need to exists for input payload validation tags = args['tags'].split(',') node = dao.get_node_by_host_identifier(host_identifier) if not node: message = 'Invalid host identifier. This node does not exist' data = None status = "failure" else: obj_list = get_tags_list_to_add(tags) node.tags.extend(obj_list) node.save() message = 'Successfully created the tag(s) to node' status = 'success' return marshal(respcls(message,status), parentwrapper.common_response_wrapper, skip_none=True)
def test_missing_nested_item_not_deleted(db_session): init_length = 5 mine = MineFactory(mine_permit=init_length) partial_mine_permit_dict = marshal( {'mine_permit': mine.mine_permit}, api.model('test_list', {'mine_permit': fields.List(fields.Nested(PERMIT_MODEL))})) partial_mine_permit_dict['mine_permit'] = partial_mine_permit_dict[ 'mine_permit'][:2] assert len(partial_mine_permit_dict['mine_permit']) < init_length mine.deep_update_from_dict(partial_mine_permit_dict, _edit_key=PERMIT_EDIT_GROUP) mine = Mine.query.filter_by(mine_guid=mine.mine_guid).first() assert len(mine.mine_permit) == init_length
def get(self, id): """ Get the tasks in the task room. :param id: :return: """ args = state_parser.parse_args() if self.validate_state(args['State']): response = tasks_service.get_tasks(id, state=args['State']) logger.debug(response) return { 'Message': "Rooms rendered successfully", 'records': marshal(response, task_response) } else: return {"Message": "State is not in (active|archived|deleted)"}
def put(self, booking_type, booking_id): """ update booking :param: booking_type :return: returns updated entities """ request_data = marshal(request.get_json(), booking_update_request) return { "status": SUCCESS, "message": "{} updated successfully".format(booking_type), "data": BOOKING_SERVICE.update_booking(request_data, booking_type, booking_id) }
def test_marshal_nested_dict(self): model = OrderedDict([ ('foo', fields.Raw), ('bar', OrderedDict([ ('a', fields.Raw), ('b', fields.Raw), ])), ]) marshal_fields = OrderedDict([('foo', 'foo-val'), ('bar', 'bar-val'), ('bat', 'bat-val'), ('a', 1), ('b', 2), ('c', 3)]) output = marshal(marshal_fields, model) expected = OrderedDict([('foo', 'foo-val'), ('bar', OrderedDict([('a', 1), ('b', 2)]))]) assert output == expected
def get(self, rid): dataset = Dataset.objects(resources__id=rid).first() if dataset: resource = get_by(dataset.resources, 'id', rid) else: resource = CommunityResource.objects(id=rid).first() if not resource: apiv2.abort(404, 'Resource does not exist') # Manually marshalling to make sure resource.dataset is in the scope. # See discussions in https://github.com/opendatateam/udata/pull/2732/files return marshal( { 'resource': resource, 'dataset_id': dataset.id if dataset else None }, specific_resource_fields)
def post(self): """ Add a new attribute definition to the system """ new = AttributeDefinition(**request.get_json()) try: DB.session.add(new) DB.session.commit() return marshal(new, ATTRIBUTE_DEFINITION_GET), 201 except IntegrityError as err: message = str(err) if APP.config['DB_UNIQUE_CONSTRAIN_FAIL'] in message: APP.logger.info('Name is not unique. %s', err) abort(409, 'Name is not unique!') APP.logger.error('SQL Error: %s', err) abort(500)
def get(self): """ Get Active, Archived and Deleted Locations :return: """ email = get_jwt_identity() args = state_parser.parse_args() try: response = location_service.get_locations_user(args["user_id"]) except Exception as ex: return {"Exception": str(ex)} return { 'Message': "Locations rendered successfully", 'records': marshal(response, location_response) }
def post(self): data = marshal(request.get_json(), basic_user_marshal_model) email = data['email'] # check for existing user user = User.query.filter_by(email=email).one_or_none() if user is None: return abort(400, 'No account for email {}'.format(email)) if user.is_verified: return abort(400, 'Account with email {} already verified'.format(email)) verification_email.queue(email) return 'OK', 200
def post(self): args = self.parser.parse_args() status = "failure" payload = jwt.decode(request.headers.environ.get('HTTP_X_ACCESS_TOKEN'), current_app.config['SECRET_KEY']) user = User.query.filter_by(id=payload['id']).first() if args['new_password']==args['confirm_new_password']: if bcrypt.check_password_hash(user.password, args['old_password']): current_app.logger.info("%s has changed password and the old password and new passwords are %s and %s", user.username, args['old_password'], args['new_password']) user.update(password=bcrypt.generate_password_hash(args['new_password'].encode("utf-8")).decode("utf-8")) message = "password is updated successfully" status = "success" else: message = "old password is not matching" else: message = "new password and confirm new password are not matching for the user" return marshal(respcls(message,status),parentwrapper.common_response_wrapper,skip_none=True)
def post(self): """ Add a new item tag to the system """ new = Tag(**request.get_json()) try: DB.session.add(new) DB.session.commit() return marshal(new, ITEM_TAG_GET), 201 except IntegrityError as err: message = str(err) if APP.config['DB_UNIQUE_CONSTRAIN_FAIL'] in message: APP.logger.info('Name is not unique. %s', err) abort(409, 'Name is not unique!') APP.logger.error('SQL Error: %s', err) abort(500)
def test_delete_flag_in_nested_item_success_nested(db_session): now_app = NOWApplicationIdentityFactory() assert len(now_app.now_application.camps.details) == 0 new_camp_detail = CampDetail(length=10) now_app.now_application.camps.details.append(new_camp_detail) assert len(now_app.now_application.camps.details) == 1 now_app_dict = marshal(now_app.now_application, NOW_APPLICATION_MODEL) now_app_dict['camps']['details'][0][ 'state_modified'] = STATE_MODIFIED_DELETE_ON_PUT now_app.now_application.deep_update_from_dict(now_app_dict) na = NOWApplication.query.filter_by( now_application_id=now_app.now_application_id).first() assert len(na.camps.details) == 0
def test_rule_list_without_payloads(self, client, url_prefix, token, rule): """ Test-case without payloads and with existing rule data, expected output:- status is success, and count of rule i.e., 1 in this case and resultant data """ resp = client.post(url_prefix + '/rules', headers={'x-access-token': token}) assert resp.status_code == 200 response_dict = json.loads(resp.data) assert response_dict['status'] == 'success' assert response_dict['data']['count'] == 1 data = marshal( rules_dao.get_all_rules().offset(None).limit(None).all(), rule_wrappers.rule_wrapper) assert response_dict['data']['results'] == data
def get(self, id): """ Gets results """ cleanid = id.strip() taskpath = get_task(cleanid, basedir=get_submit_dir()) if taskpath is not None: resp = jsonify({STATUS_RESULT_KEY: SUBMITTED_STATUS, PARAMETERS_KEY: self._get_task_parameters(taskpath)}) resp.status_code = 200 return resp taskpath = get_task(cleanid, basedir=get_processing_dir()) if taskpath is not None: resp = jsonify({STATUS_RESULT_KEY: PROCESSING_STATUS, PARAMETERS_KEY: self._get_task_parameters(taskpath)}) resp.status_code = 200 return resp taskpath = get_task(cleanid, basedir=get_done_dir()) if taskpath is None: resp = jsonify({STATUS_RESULT_KEY: NOTFOUND_STATUS, PARAMETERS_KEY: None}) resp.status_code = 410 return resp result = os.path.join(taskpath, RESULT) if not os.path.isfile(result): er = ErrorResponse() er.message = 'No result found' er.description = self._get_task_parameters(taskpath) return marshal(er, ERROR_RESP), 500 log_task_json_file(taskpath) app.logger.info('Result file is ' + str(os.path.getsize(result)) + ' bytes') with open(result, 'r') as f: data = json.load(f) return jsonify({STATUS_RESULT_KEY: DONE_STATUS, RESULT_KEY: data, PARAMETERS_KEY: self._get_task_parameters(taskpath)})
def patch_invitation(id_invitation, data, user_input): """ Update information of a invitation on database. In this case only the status of invitation can be change. Updating an email may cause conflicts in the server :param id_invitation: invitation id to update :type: int :param data: new status of invitation :type: json :param user_input: User who made the request :type: User Model :return: Invitation updated if success, error message otherwise. HTTP status code :type: tuple(json, int) """ roles = get_roles_user(user_input) if roles.intersection({ADMIN_ROLE}): invitation = Invitation.query.filter( Invitation.id == id_invitation).first() email = data.get('email') status = data.get('status') if not invitation: response = dict(RESPONSES_CATALOG['no_invitation_server']) response['error'] = \ response['error'].format(id_invitation=id_invitation) code = CODE_NOT_FOUND elif email is not None and email != invitation.email: response = RESPONSES_CATALOG['no_change_email_invitation'] code = CODE_FORBIDDEN elif status in STATUS_INVITATION: invitation.status = data.get('status') DB.session.add(invitation) DB.session.commit() response = marshal(invitation, INVITATION_MODEL) code = CODE_OK else: response = { 'error': 'Possible values for status: pending, ' 'accepted. No available: ' + status } code = CODE_NOT_ACCEPTABLE else: response = dict(RESPONSES_CATALOG['no_permission_update_resource']) response['error'] = response['error'].format(id_user=user_input.id) code = CODE_FORBIDDEN return response, code
def post(self): data = request.get_json() data = marshal(data, SearchRecipeModel, skip_none=True) db = DB() id_list = [] res = {} # get all the recipes recipe_list = db.select('recipe_tag_ingredient') \ .execute() # if ingredient list is not null # we first select all recipes from the recipe_list where recipe.ingredients >= searched_ingredients if 'ingredients' in data.keys(): recipe_list = list( filter( lambda x: set(x['ingredients'].split(',')) >= set(data[ 'ingredients']), recipe_list)) # if tags list is not null # we then select all recipes from recipe_list where recipe.tags >= searched_tags if 'tags' in data.keys(): recipe_list = list( filter( lambda x: set(x['tags'].split(',')) >= set(data['tags']), recipe_list)) # if blacklist list is not null # we then select all recipes from recipes where recipe.tags join blacklist is empty if 'tags' in data.keys(): recipe_list = list( filter( lambda x: set(x['tags'].split(',')).isdisjoint( set(data['blackList'])), recipe_list)) if not recipe_list: return abort(400, message="recipe doesn't exist") # add ids of all remaining recipes to res for row in recipe_list: id_list.append(row['recipe_id']) searched = db.select_one('Recipe').where( id=row['recipe_id']).execute()['searched'] + 1 db.update("Recipe").set(searched=searched).where( id=row['recipe_id']).execute() db.commit() return dict(ids=id_list)
def get(self): ''' Retrieve people''' search_term = request.args.get('q') or None limit = request.args.get('limit') or Config.MAX_PAGE_SIZE page_limit = 100 if int(limit) > 100 else int(limit) page = request.args.get('page') or 1 if page_limit < 1 or page < 1: return abort(400, 'Page or Limit cannot be negative values') person_data = Person.query.filter_by(active=True).\ order_by(desc(Person.date_created)) if person_data.all(): people = person_data if search_term: people = person_data.filter( Person.last_name.ilike('%' + search_term + '%'), Person.first_name.ilike('%' + search_term + '%')) person_paged = people.paginate(page=page, per_page=page_limit, error_out=True) results = dict(data=marshal(person_paged.items, person_fields)) pages = { 'page': page, 'per_page': page_limit, 'total_data': person_paged.total, 'pages': person_paged.pages } if page == 1: pages['prev_page'] = url_for('api.person') + \ '?limit={}'.format(page_limit) if page > 1: pages['prev_page'] = url_for('api.person') + \ '?limit={}&page={}'.format(page_limit, page-1) if page < person_paged.pages: pages['next_page'] = url_for('api.person') + \ '?limit={}&page={}'.format(page_limit, page+1) results.update(pages) return results, 200 return abort(404, message='No people found for specified user')
def get(self): ''' Retrieve numbering records''' search_term = request.args.get('q') or None limit = request.args.get('limit') or Config.MAX_PAGE_SIZE page_limit = 100 if int(limit) > 100 else int(limit) page = request.args.get('page') or 1 if page_limit < 1 or page < 1: return abort(400, 'Page or Limit cannot be negative values') numbering_data = Numbering.query.filter_by(active=True).\ order_by(desc(Numbering.date_created)) if numbering_data.all(): numbering_records = numbering_data if search_term: numbering_records = numbering_data.filter( Numbering.name.ilike('%' + search_term + '%')) numbering_paged = numbering_records.paginate(page=page, per_page=page_limit, error_out=True) results = dict( data=marshal(numbering_paged.items, numbering_fields)) pages = { 'page': page, 'per_page': page_limit, 'total_data': numbering_paged.total, 'pages': numbering_paged.pages } if page == 1: pages['prev_page'] = url_for('api.numbering') + \ '?limit={}'.format(page_limit) if page > 1: pages['prev_page'] = url_for('api.numbering') + \ '?limit={}&page={}'.format(page_limit, page-1) if page < numbering_paged.pages: pages['next_page'] = url_for('api.numbering') + \ '?limit={}&page={}'.format(page_limit, page+1) results.update(pages) return results, 200 return abort(404, message='No Numbering found for specified user')
def get(self): """ Get the user information from the identity of the given access token. """ current_user = get_jwt_identity() if current_user.get('type') != "user": return {'message': 'Only user access tokens are allowed.'}, 422 user = app_db_mgr.get_users(id=current_user.get('id')) if user is None: return {'message': 'There isn\'t any registered user with this identity.'}, 404 authorization_data = auth_db_mgr.get_users(id=user.id) return marshal({"user_data": user, "authorization_data": authorization_data}, user_model), 200
def get(self): ''' Retrieve typeapproval records''' search_term = request.args.get('q') or None limit = request.args.get('limit') or Config.MAX_PAGE_SIZE page_limit = 100 if int(limit) > 100 else int(limit) page = request.args.get('page') or 1 if page_limit < 1 or page < 1: return abort(400, 'Page or Limit cannot be negative values') typeapproval = Typeapproval.query.filter_by(active=True).\ order_by(desc(Typeapproval.date_created)) if typeapproval.all(): typeapproval_records = typeapproval if search_term: typeapproval_records = typeapproval_data.filter( Typeapproval.ta_unique_id.ilike('%' + search_term + '%')) typeapproval_paged = typeapproval_records.paginate( page=page, per_page=page_limit, error_out=True) results = dict( data=marshal(typeapproval_paged.items, typeapproval_fields)) pages = { 'page': page, 'per_page': page_limit, 'total_data': typeapproval_paged.total, 'pages': typeapproval_paged.pages } if page == 1: pages['prev_page'] = url_for('api.typeapproval') + \ '?limit={}'.format(page_limit) if page > 1: pages['prev_page'] = url_for('api.typeapproval') + \ '?limit={}&page={}'.format(page_limit, page-1) if page < typeapproval_paged.pages: pages['next_page'] = url_for('api.typeapproval') + \ '?limit={}&page={}'.format(page_limit, page+1) results.update(pages) return results, 200 return abort(404, message='No Typeapproval found for specified user')
def inner(*args, **kwargs): rv = func(*args, **kwargs) # If a Flask response has been made already, it is passed through unchanged if isinstance(rv, Response): return rv if schema: serialized = schema.dump(rv) else: from flask_restplus import marshal serialized = marshal(rv, model_from_parser) if not _is_method(func): # Regular route, need to manually create Response return jsonify(serialized), status_code return serialized, status_code
def get(self, play_id): """ Gets play by id Use Case: This endpoint can be used by a client to view the details of a play by providing a play id. A message is returned to the client if no play exists for the provided play id. """ with db.engine.raw_connection().cursor( MySQLdb.cursors.DictCursor) as cursor: cursor.callproc("getPlayById", [play_id]) results = cursor.fetchall() if not results: return { 'message': 'No play exists with the id: {}'.format(play_id) }, 404 results = format_results(results) return marshal(results, play), 200
def get_table(table_name=None): """Returns table with given table name. """ project_id = Project.query.filter_by( name=settings.BIGQUERY_PROJECT).first().id dataset_id = Dataset.query.filter_by( name=settings.BIGQUERY_DATASET).first().id if table_name is not None: table = Table.query.filter_by(name=table_name, dataset_id=dataset_id).first() columns = get_columns(table) table_dict = marshal(table, table_response) table_dict['columns'] = columns return table_dict else: # all tables return Table.query.filter_by(dataset_id=dataset_id).all()