Esempio n. 1
0
File: views.py Progetto: ralmn/CMAsk
def vote(id, option):

    db.session.flush()
    voteOption = VoteOption.query.filter_by(id=option).first_or_404()

    response = app.make_response(redirect(url_for('.result', id=id)))
    cookie = ('vote-' + id) in request.cookies
    if voteOption.vote.close is not None and voteOption.vote.close < datetime.datetime.now(
    ):
        return redirect(url_for('.result', id=id))
    if voteOption.vote.open is not None and voteOption.vote.open > datetime.datetime.now(
    ):
        return redirect(url_for('.view', id=id))
    if not cookie:
        voteOption.value = voteOption.value + 1
        db.session.commit()
        message = {
            "id": str(id),
            "slug": str(voteOption.slug()),
            "value": int(voteOption.value),
            'did': int(voteOption.id)
        }
        redis.publish(REDIS_CHAN, message)

        response.set_cookie('vote-' + str(id), voteOption.slug())
    return response
Esempio n. 2
0
def inbox(ws):
    while ws.socket is not None:
        gevent.sleep()
        message = ws.receive()

        if message:
            redis.publish(REDIS_CHAN, message)
Esempio n. 3
0
def inbox(ws):
    while ws.socket is not None:
        gevent.sleep()
        message = ws.receive()

        if message:
            redis.publish(REDIS_CHAN, message)
Esempio n. 4
0
def create_booking(bus_id):
    bus = Bus.query.get(bus_id)
    if not bus.booking_time_expired():
        create_booking_form = CreateBookingForm(data=request.form, bus=bus)
        if create_booking_form.validate_on_submit():
            # create booking
            grid_id = create_booking_form.grid_id.data
            pricing_id = create_booking_form.pricing_id.data
            passenger_name = create_booking_form.passenger_name.data
            passenger_telephone = create_booking_form.passenger_telephone.data
            pickup = create_booking_form.pickup.data
            pricing = Pricing.query.get(pricing_id)
            grid = Grid.query.get(grid_id)
            branch = bus.branch
            user = current_user
            if not user.is_authenticated:
                user = None

            fare = pricing.price
            stop = pricing.stop
            booking = Booking(passenger_name=passenger_name,
                              passenger_telephone=passenger_telephone,
                              seat_number=grid.number,
                              pickup=pickup,
                              fare=fare,
                              stop=stop,
                              grid_id=grid_id,
                              pricing_id=pricing_id,
                              paid=True,
                              branch=branch,
                              bus=bus,
                              created_by=user.id)

            db.session.add(booking)
            grid.booking = booking

            create_payment(booking)
            db.session.commit()
            bookings = bus.bookings
            booking_fields = Fields().booking_fields()
            bookings_json = json.loads(
                json.dumps(marshal_data(bookings, booking_fields)))
            res = json.dumps({
                "handle": "create_booking_passed",
                "data": {
                    "grid": grid.grid_dict(),
                    "bookings": bookings_json
                }
            })
            redis.publish(app.config.get("REDIS_CHAN"), res)
            flash(f'Booked Seat {grid.number}', 'success')
            return redirect(url_for('bus.get_bus', bus_id=bus.id))
        else:
            flash(f'Failed', 'danger')
            return render_template("bus/bus.html",
                                   bus=bus,
                                   create_booking_form=create_booking_form)
Esempio n. 5
0
def task():
    if 'id' in request.values:
        return task_info(request.values['id'])

    # else create new task
    task = Task()
    db.session.add(task)
    db.session.commit()
    redis.publish(IN_CHANNEL, str(task.id))
    return jsonify({"id": task.id}), 201
Esempio n. 6
0
def inbox(ws):
    """Receives incoming chat messages, inserts them into Redis."""
    while ws.socket is not None:
        # Sleep to prevent *contstant* context-switches.
        gevent.sleep(0.1)
        message = ws.receive()

        if message:
            app.logger.info(u'Inserting message: {}'.format(message))
            redis.publish(REDIS_CHAN, message)
Esempio n. 7
0
def web_socket(ws):
    socket_backend.register(ws)
    """Receives incoming messages, inserts them into Redis."""
    while not ws.closed:
        # Sleep to prevent *constant* context-switches.
        gevent.sleep(0.1)
        message = ws.receive()

        if message:
            # perform action according to message received i.e handle key
            message = parse_json_string(message)
            handle = message["handle"]
            data = message["data"]
            res = HANDLES[handle](data)
            # app.logger.info(u'Inserting message: {}'.format(message))
            redis.publish(app.config.get("REDIS_CHAN"), res)
Esempio n. 8
0
File: views.py Progetto: ralmn/CMAsk
def vote(id, option):

    db.session.flush()
    voteOption = VoteOption.query.filter_by(id=option).first_or_404()

    response = app.make_response(redirect(url_for('.result', id=id)))
    cookie = ('vote-'+id) in request.cookies
    if voteOption.vote.close is not None and voteOption.vote.close < datetime.datetime.now():
        return redirect(url_for('.result', id=id))
    if voteOption.vote.open is not None and voteOption.vote.open > datetime.datetime.now():
        return redirect(url_for('.view', id=id))
    if not cookie:
        voteOption.value = voteOption.value +1
        db.session.commit()
        message = {"id":str(id),"slug":str(voteOption.slug()),"value":int(voteOption.value), 'did':int(voteOption.id)}
        redis.publish(REDIS_CHAN, message)

        response.set_cookie('vote-' + str(id), voteOption.slug())
    return response
Esempio n. 9
0
def control(direction):
    redis.publish("direction", direction)
    return ""