Esempio n. 1
0
def post(request):
	request = request['service']

	ns_instance_id = request['ns_instance_id']
	client_mkt_id = request['client_mkt_id']

	nap_id = NAP.query.filter_by(mkt_id=request['nap_mkt_id']).first().id
	nfvi_id = NFVI.query.filter_by(mkt_id=request['nfvi_mkt_id']).first().id
	switch = NFVI.query.filter_by(mkt_id=request['nfvi_mkt_id']).first().switch

	used_vlans = Vlan.query.join(Service, db.or_(
		Service.ce_transport_id == Vlan.id, 
		Service.pe_transport_id == Vlan.id)
		).join(NFVI, Service.NFVI_id == NFVI.id).filter(
			db.and_(
				db.or_(Service.status == 'ALLOCATED', Service.status == 'ACTIVE'), 
				NFVI.switch==switch)).all()

	available_vlans = set(xrange(1, 4096))
	vlans = available_vlans - set([vlan.vlan_id for vlan in used_vlans])
	vlans = list(vlans)

	if len(vlans) > 1:
		vlans = vlans[0:2]
		status = 'ALLOCATED'
		ce_transport = Transport()
		pe_transport = Transport()
		db.session.add(ce_transport)
		db.session.add(pe_transport)
		db.session.flush()

		ce_vlan = Vlan(ce_transport.id, vlans[0])
		pe_vlan = Vlan(pe_transport.id, vlans[1])
		db.session.add(ce_vlan)
		db.session.add(pe_vlan)
		db.session.flush()
		service = Service(ns_instance_id, client_mkt_id, status, 
			nap_id, nfvi_id, ce_transport.id, pe_transport.id)
	else:
		vlans = []
		status = 'NOT AVAILABLE'
		service = Service(ns_instance_id, client_mkt_id, status, 
			nap_id, nfvi_id, None, None)
	
	db.session.add(service)
	db.session.commit()

	return [int(x) for x in vlans[0:2]]
Esempio n. 2
0
def get_unread_messages():
    messages = (
        Message.query.filter(Message.to_user==g.user)
                     .filter(db.or_(
                         Message.expire == None,
                         Message.expire > datetime.datetime.now()))
                     .order_by('created'))
    return api_response(
        [dict(id=m.id, email=m.from_user.email, avatar=m.from_user.avatar,
              has_media=bool(m.photo), created=m.created_stamp)
         for m in messages], 200, 'messages retrieved successfully')
Esempio n. 3
0
def get_message(message_id):
    try:
        message = (
            Message.query.filter(Message.id == message_id, Message.to_user == g.user).filter(
                db.or_(Message.expire == None, Message.expire > datetime.datetime.now())
            )
        ).one()
    except NoResultFound:
        return api_response(None, 404, "message not found")

    # Update message with expired to schedule it for removal.
    if message.ttl > 10:
        message.expire = datetime.datetime.now() + datetime.timedelta(seconds=message.ttl)
    else:
        message.expire = datetime.datetime.now()
    db.session.commit()

    return api_response(message.to_json(), 200, "message retrieved successfully")
Esempio n. 4
0
def viewReservation(admin):
    if request.method == 'POST':
        fromDate = request.form['fromDate']
        toDate = request.form['toDate']
        if (ifNone(toDate)):
            toDate = 100000000000
        else:
            toDate = int(toDate)
        if (ifNone(fromDate)):
            fromDate = 0
        else:
            fromDate = int(fromDate)

        query_c = db.session.query(Reservation, Camping).filter(
            Reservation.id == Camping.res_id, Reservation.deleted == False,
            Reservation.isCamping == 1,
            db.or_(
                db.and_(Camping.fromDate >= fromDate,
                        Camping.fromDate <= toDate),
                db.and_(Camping.toDate >= fromDate,
                        Camping.toDate <= toDate))).all()
        query_p = db.session.query(Reservation, Picnic).filter(
            Reservation.id == Picnic.res_id, Reservation.deleted == False,
            Reservation.isPicnic == 1,
            db.and_(Picnic.date >= fromDate, Picnic.date <= toDate)).all()
        query_r = db.session.query(Reservation, Resto).filter(
            Reservation.id == Resto.res_id, Reservation.deleted == False,
            Reservation.isResto == 1,
            db.and_(Resto.date >= fromDate, Resto.date <= toDate)).all()

        resp = []
        for c in query_c:
            resp.append({
                "id": c.Reservation.id,
                "name": c.Reservation.name,
                "phone": c.Reservation.phone,
                "numPeople": c.Reservation.numPeople,
                "moreInfo": c.Reservation.moreInfo,
                "type": "Camping",
                "added_date": c.Reservation.added_date,
                "added_by": c.Reservation.added_by,
                "fromDate": c.Camping.fromDate,
                "toDate": c.Camping.toDate,
                "withBBQ": c.Camping.withBBQ,
                "withTent": c.Camping.withTent,
                "withMatress": c.Camping.withMatress,
                "withFood": c.Camping.withFood,
                "table": c.Camping.table,
                "pricePerson": c.Camping.pricePerson
            })
        for c in query_p:
            resp.append({
                "id": c.Reservation.id,
                "name": c.Reservation.name,
                "phone": c.Reservation.phone,
                "numPeople": c.Reservation.numPeople,
                "moreInfo": c.Reservation.moreInfo,
                "type": "Picnic",
                "added_date": c.Reservation.added_date,
                "added_by": c.Reservation.added_by,
                "date": c.Picnic.date,
                "withBBQ": c.Picnic.withBBQ,
                "table": c.Picnic.table,
                "pricePerson": c.Picnic.pricePerson
            })
        for c in query_r:
            resp.append({
                "id": c.Reservation.id,
                "name": c.Reservation.name,
                "phone": c.Reservation.phone,
                "numPeople": c.Reservation.numPeople,
                "moreInfo": c.Reservation.moreInfo,
                "type": "Resto",
                "added_date": c.Reservation.added_date,
                "added_by": c.Reservation.added_by,
                "date": c.Resto.date,
                "table": c.Resto.table
            })
        return jsonify(resp)
    return error("Send POST request")