def edit_room_contract(id): contract = Contract.query.get(id) if contract == None: flash('contract not found.') abort(404) if g.user.role != ROLE_ADMIN: if g.user.property_id != contract.room.property_id: abort(401) form = AddRoomContractForm() form.user.choices = [(u.id, u.nickname) for u in contract.room.property.users if u.id != contract.user.id] form.user.choices.insert(0,(contract.user.id,contract.user.nickname)) if form.validate_on_submit(): contract.user_id = form.user.data contract.start_date = form.start_date.data contract.end_date = form.end_date.data db.session.add(contract) db.session.commit() flash('Changes saved!') return redirect(url_for('room', id=contract.room.id)) elif request.method != 'POST': form.start_date.data = contract.start_date form.end_date.data = contract.end_date return render_template('edit_room_contract.html', title = 'Room Contract', contract = contract, room = contract.room, form = form)
def add_room_contract(id): room = Room.query.get(id) if room == None: flash('Room not found.') abort(404) if g.user.role != ROLE_ADMIN: if g.user.property_id != room.property_id: abort(401) form = AddRoomContractForm() form.user.choices = [(u.id, u.nickname) for u in room.property.users] form.user.choices.insert(0,(-1,'Select...')) if form.validate_on_submit(): newContract = Contract(user_id=form.user.data,room=room,start_date=form.start_date.data,end_date=form.end_date.data) db.session.add(newContract) db.session.commit() flash('New contract between user '+newContract.user.nickname+' and room '+newContract.room.name+' was added.') return redirect(url_for('room', id=room.id)) elif request.method != 'POST': form.start_date.data = datetime.utcnow() form.end_date.data = datetime.utcnow().replace(year=date.today().year+1) return render_template('add_room_contract.html', title = 'Room Contract', room = room, form = form)