def get_data(): """ search by alias :return: """ log['GET savings'] = request.url type = request.args.get('type') value = request.args.get('value') filter = request.args.get('filter') if not type: result = collection('savings').find_one() return jsonify({'keys': result.keys()}) elif not value and not filter: result = collection('savings').find() response = {type: []} for line in result: value = [v for k, v in line.items() if k in (type,)] if value and value[0] not in response[type]: response[type].append(value[0]) return jsonify(response) elif not value and filter: filter = url_query(filter) result = collection('savings').find({type:filter}) response = {type: filter, 'details': {}} else: result = collection('savings').find({type: value}) response = {type: value, 'details': {}} for line in result: line = dict([(k, unicode(v)) for k, v in line.items() if (not value or k not in (type,))]) response['details'].update({str(line.pop('_id')): line}) return jsonify(response)
def modify_record(): "must be {<id>: {amount: <>, currency <>, alias: <>}}" data = request.get_json() id, values = data.items()[0] current_data = collection('savings').find({'_id': ObjectId(id)}).next() values.update({"modified": str(datetime.now())}) current_data.update(values) current_data.update({'_id': id}) collection('savings').update({'_id': ObjectId(id)}, consistent_saving(current_data)) return jsonify(current_data), 201
def get_total_by_alias(alias): result = collection('savings').find({'alias': alias}, {'amount': 1, 'currency': 1, '_id': 0}) all_amount = [] for i in result: all_amount.append((i.get('amount'), i.get('currency') or 'RUB')) all_amount = sum_by_currency(*all_amount) to_currency = request.args.get('currency') or None if to_currency: all_amount = convert_currency(to=to_currency.upper(), **all_amount) return jsonify(all_amount)
def add_record(): data = request.get_json() data.update({'user': request.authorization.username, 'date': str(datetime.now())}) # clean data before saving log['POST savings'] = dumps(data) try: data = consistent_saving(data) except MissingDataException : log['POST savings Error'] = "request does not have enough data to complete the POST operation into '\savings'" return jsonify(data), 500 rec_id = collection('savings').insert(data) log['POST savings success'] = str(rec_id) data.update({'_id': str(data['_id'])}) return jsonify(data), 201