示例#1
0
def edit_property():
    from database import Property, Room
    informations = request.get_json()
    if 'id' in informations:
        property = Property.query.filter_by(id=informations['id'],
                                            owner_id=g.user.id).first()
        if not property:
            return jsonify({'error': 'Property not existing or not yours'})
        else:
            if 'name' in informations:
                if informations[
                        'name'] != property.name and Property.query.filter_by(
                            name=informations['name']).count() == 0:
                    property.name = informations['name']
            if 'city' in informations:
                property.city = informations['city']
            if 'type' in informations:
                property.type = informations['type']
            if 'description' in informations:
                property.description = informations['description']
            if 'address' in informations:
                property.address = informations['address']
            if 'rooms' in informations:
                for room in informations['rooms']:
                    if 'action' in room:
                        if room['action'] == 'add':
                            var = Room()
                            var.property_id = property.id
                            if 'area' in room:
                                var.area = room['area']
                            else:
                                var.area = None
                            if 'description' in room:
                                var.description = room['description']
                            else:
                                var.description = ""
                            db.session.add(var)
                        elif room['action'] == 'delete' and 'id' in room:
                            var = Room.query.filter_by(id=room['id']).first()
                            db.session.delete(var)
                        elif room['action'] == 'edit' and 'id' in room:
                            var = Room.query.filter_by(id=room['id']).first()
                            if 'area' in room:
                                var.area = room['area']
                            if 'description' in room:
                                var.description = room['description']
            db.session.commit()
            final_property = Property.query.filter_by(
                id=informations['id']).first()
            return jsonify(final_property.serialize)
    else:
        return jsonify({'error': 'ID of property missing'})
示例#2
0
def add_property():
    from database import Property, Room
    informations = request.get_json()
    if 'name' in informations and 'city' in informations:
        if Property.query.filter_by(name=informations['name'],
                                    owner_id=g.user.id).first() is None:
            property = Property()
            property.name = informations['name']
            property.city = informations['city']
            property.owner_id = g.user.id
            if 'type' in informations:
                property.type = informations['type']
            else:
                property.type = ""
            if 'description' in informations:
                property.description = informations['description']
            else:
                property.description = ""
            if 'address' in informations:
                property.address = informations['address']
            else:
                property.address = ""
            db.session.add(property)
            db.session.commit()
            if 'rooms' in informations:
                property_id = Property.query.filter_by(
                    name=informations['name'], owner_id=g.user.id).first().id
                for room in informations['rooms']:
                    var = Room()
                    var.property_id = property_id
                    if 'area' in room:
                        var.area = room['area']
                    else:
                        var.area = None
                    if 'description' in room:
                        var.description = room['description']
                    else:
                        var.description = ""
                    db.session.add(var)
                db.session.commit()
            final_property = Property.query.filter_by(
                name=informations['name'], owner_id=g.user.id).first()
            return jsonify(final_property.serialize)
        else:
            return jsonify({'error': 'Name already existing'})
    else:
        return jsonify({'error': 'City or name is missing'})