def add_group_currency(): group_currency_schema = GroupCurrency.from_json(json.loads(request.data.decode('utf-8'))) if group_currency_schema.errors: return {'errors': group_currency_schema.errors}, 400 group_currency = GroupCurrency(**group_currency_schema.data) group_currency.group_id = current_user.group_id db.session.add(group_currency) db.session.commit() return group_currency, 201
def edit_group_currency(group_currency_id): group_currency = GroupCurrency.query.filter( GroupCurrency.id == group_currency_id, GroupCurrency.group_id == current_user.group_id ).first_or_404() group_currency_schema = GroupCurrency.from_json(json.loads(request.data.decode('utf-8')), partial=True) if group_currency_schema.errors: return {'errors': group_currency_schema.errors}, 400 if 'group_id' in group_currency_schema.data: group = Group.query.filter( Group.id == group_currency_schema.data['group_id'] ).first() if not group: return {'errors': {'group': 'Group with this id does not exist'}}, 400 for field, value in group_currency_schema.data.items(): if hasattr(group_currency, field): setattr(group_currency, field, value) db.session.commit() return group_currency, 200