Exemplo n.º 1
0
def edit_user_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 = AddUserContractForm()
    form.room.choices = [(r.id, r.name) for r in contract.room.property.rooms if r.id != contract.room.id]
    form.room.choices.insert(0,(contract.room.id,contract.room.name))
    if form.validate_on_submit():
        contract.room_id = form.room.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('user', nickname=contract.user.nickname))
    elif request.method != 'POST':
        form.start_date.data = contract.start_date
        form.end_date.data = contract.end_date
    return render_template('edit_user_contract.html',
        title = 'User Contract',
        contract = contract,
        user = contract.user,
        form = form)
Exemplo n.º 2
0
def add_user_contract(id):
    user = User.query.get(id)
    if user == None:
        flash('User not found.')
        abort(404)

    if user.property_id < 0:
        flash('User has to have a property first.')
        abort(404)

    if g.user.role != ROLE_ADMIN:
        if g.user.property_id != user.property_id:
            abort(401)

    form = AddUserContractForm()
    form.room.choices = [(r.id, r.name) for r in user.property.rooms]
    form.room.choices.insert(0,(-1,'Select...'))
    if form.validate_on_submit():
        newContract = Contract(user=user,room_id=form.room.data,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('user', nickname=user.nickname))
    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_user_contract.html',
        title = 'User Contract',
        user = user,
        form = form)