Esempio n. 1
0
def deploy():
    import finaldata as fd
    db.drop_all()
    db.create_all()

    Country.insertCountries(fd.getCountries())
    City.insertCities(fd.getCities())
    Type.insertTypes(fd.getTypes())
    Museum.insertMuseums(fd.getMuseums())
    Period.insertPeriods(fd.getPeriods())
Esempio n. 2
0
    def invite(self, csr, snowplow_event="use_period"):
        active_period = self.get_active_period()
        if active_period.ps.ps_name in ["Invited", "Being Served", "On Hold"]:
            raise TypeError(
                "You cannot invite a citizen that has already been invited")

        #  Calculate what Snowplow event to call.
        if (snowplow_event == "use_period"):
            if (active_period.ps.ps_name == "Waiting"):
                snowplow_event = "invitefromlist"
            else:
                snowplow_event = "invitefromhold"

        active_period.time_end = datetime.now()
        # db.session.add(active_period)

        period_state_invite = PeriodState.query.filter_by(
            ps_name="Invited").first()

        new_period = Period(sr_id=self.sr_id,
                            csr_id=csr.csr_id,
                            reception_csr_ind=csr.receptionist_ind,
                            ps_id=period_state_invite.ps_id,
                            time_start=datetime.now())

        self.periods.append(new_period)

        SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event)
Esempio n. 3
0
    def begin_service(self, csr, snowplow_event):
        active_period = self.get_active_period()

        if active_period.ps.ps_name in ["Being Served"]:
            raise TypeError(
                "You cannot begin serving a citizen that is already being served"
            )

        active_period.time_end = datetime.now()
        # db.session.add(active_period)

        period_state_being_served = PeriodState.query.filter_by(
            ps_name="Being Served").first()

        new_period = Period(sr_id=self.sr_id,
                            csr_id=csr.csr_id,
                            reception_csr_ind=csr.receptionist_ind,
                            ps_id=period_state_being_served.ps_id,
                            time_start=datetime.now())

        self.periods.append(new_period)

        #  Calculate number of active periods, for Snowplow call.
        period_count = len(self.periods)
        SnowPlow.snowplow_event(self.citizen_id,
                                csr,
                                snowplow_event,
                                period_count=period_count)
Esempio n. 4
0
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)

        user.display_name = form.display_name.data

        user.member1 = form.member1.data
        user.member2 = form.member2.data
        user.member3 = form.member3.data
        user.member4 = form.member4.data
        user.member5 = form.member5.data
        user.member6 = form.member6.data
        user.member7 = form.member7.data
        user.member8 = form.member8.data
        user.member9 = form.member9.data
        user.member10 = form.member10.data

        user.set_password(form.password.data)
        db.session.add(user)
        db.session.flush()

        game = get_game()
        db.session.add(game)
        db.session.flush()

        user.game_id = game.id

        products = Product.query.all()
        for product in products:
            initial_period = Period(
                id=f'{game.id}_{user.id}_1_{product.id}',
                game_id=game.id,
                period_number=1,
                product_id=product.id,
                products_in_stock_beginning_of_period=DEFAULT_START_PRODUCTION)
            user.periods.append(initial_period)

        initial_period_total = PeriodTotal(
            id=f'{game.id}_{user.id}_1',
            game_id=game.id,
            period_number=1,
            input_approved_by_admin=AUTO_APPROVE_RESULTS)
        initial_period_total.money_total_begining_of_period = INITIAL_CREDIT
        initial_period_total.credit_total = INITIAL_CREDIT
        user.period_total.append(initial_period_total)

        db.session.add(user)
        db.session.commit()

        flash(f'Registered new user: {user}')
        return redirect(url_for('register'))
    return render_template('register.html', title='Register', form=form)
Esempio n. 5
0
    def post(self, id):

        csr = CSR.find_by_username(g.oidc_token_info['username'])

        service_request = ServiceReq.query.filter_by(sr_id=id) \
            .join(ServiceReq.citizen, aliased=True) \
            .filter_by(office_id=csr.office_id).first_or_404()

        active_service_state = SRState.get_state_by_name("Active")
        complete_service_state = SRState.get_state_by_name("Complete")

        # Find the currently active service_request and close it
        for req in service_request.citizen.service_reqs:
            if req.sr_state_id == active_service_state.sr_state_id:
                req.sr_state_id = complete_service_state.sr_state_id
                req.finish_service(csr, clear_comments=False)
                db.session.add(req)

        # Then set the requested service to active
        service_request.sr_state_id = active_service_state.sr_state_id

        period_state_being_served = PeriodState.get_state_by_name(
            "Being Served")

        new_period = Period(sr_id=service_request.sr_id,
                            csr_id=csr.csr_id,
                            reception_csr_ind=csr.receptionist_ind,
                            ps_id=period_state_being_served.ps_id,
                            time_start=datetime.now())

        db.session.add(new_period)
        db.session.add(service_request)

        citizen_obj = Citizen.query.get(service_request.citizen_id)
        citizen_obj.service_count = citizen_obj.service_count + 1

        db.session.commit()

        SnowPlow.choose_service(service_request, csr, "additionalservice")

        citizen_result = self.citizen_schema.dump(service_request.citizen)
        socketio.emit('update_active_citizen',
                      citizen_result.data,
                      room=csr.office_id)
        result = self.service_request_schema.dump(service_request)

        return {'service_request': result.data, 'errors': result.errors}, 200
Esempio n. 6
0
    def add_to_queue(self, csr, snowplow_event):

        active_period = self.get_active_period()
        active_period.time_end = datetime.now()
        #db.session.add(active_period)

        period_state_waiting = PeriodState.query.filter_by(
            ps_name="Waiting").first()

        new_period = Period(sr_id=self.sr_id,
                            csr_id=csr.csr_id,
                            reception_csr_ind=csr.receptionist_ind,
                            ps_id=period_state_waiting.ps_id,
                            time_start=datetime.now())
        self.periods.append(new_period)

        SnowPlow.snowplow_event(self.citizen_id, csr, snowplow_event)
Esempio n. 7
0
    def place_on_hold(self, csr):
        active_period = self.get_active_period()
        active_period.time_end = datetime.now()
        # db.session.add(active_period)

        period_state_on_hold = PeriodState.query.filter_by(
            ps_name="On hold").first()

        new_period = Period(sr_id=self.sr_id,
                            csr_id=csr.csr_id,
                            reception_csr_ind=csr.receptionist_ind,
                            ps_id=period_state_on_hold.ps_id,
                            time_start=datetime.now())

        self.periods.append(new_period)

        SnowPlow.snowplow_event(self.citizen_id, csr, "hold")
Esempio n. 8
0
    def post(self):
        json_data = request.get_json()

        if not json_data:
            return {"message": "No input data received for creating service request"}, 400

        csr = CSR.find_by_username(g.oidc_token_info['username'])

        try:
            service_request = self.service_request_schema.load(json_data['service_request']).data

        except ValidationError as err:
            return {"message": err.messages}, 422
        except KeyError as err:
            print (err)
            return {"message": str(err)}

        active_sr_state = SRState.get_state_by_name("Active")
        complete_sr_state = SRState.get_state_by_name("Complete")
        citizen_state = CitizenState.query.filter_by(cs_state_name="Active").first()
        citizen = Citizen.query.get(service_request.citizen_id)
        service = Service.query.get(service_request.service_id)

        if citizen is None:
            return {"message": "No matching citizen found for citizen_id"}, 400

        if service is None:
            return {"message": "No matching service found for service_id"}, 400

        # Find the currently active service_request and close it (if it exists)
        for req in citizen.service_reqs:
            if req.sr_state_id == active_sr_state.sr_state_id:
                req.sr_state_id = complete_sr_state.sr_state_id
                req.finish_service(csr, clear_comments=False)
                db.session.add(req)

        service_request.sr_state_id = active_sr_state.sr_state_id

        # Only add ticket creation period and ticket number if it's their first service_request
        if len(citizen.service_reqs) == 0:
            period_state_ticket_creation = PeriodState.get_state_by_name("Ticket Creation")

            ticket_create_period = Period(
                csr_id=csr.csr_id,
                reception_csr_ind=csr.receptionist_ind,
                ps_id=period_state_ticket_creation.ps_id,
                time_start=citizen.get_service_start_time(),
                time_end=datetime.now()
            )
            service_request.periods.append(ticket_create_period)

            # Move start_time back 6 hours to account for DST and UTC offsets
            # It's only important that the number carries over _around_ midnight
            offset_start_time = citizen.start_time - timedelta(hours=6)

            service_count = ServiceReq.query \
                    .join(ServiceReq.citizen, aliased=True) \
                    .filter(Citizen.start_time >= offset_start_time.strftime("%Y-%m-%d")) \
                    .filter_by(office_id=csr.office_id) \
                    .join(ServiceReq.service, aliased=True) \
                    .filter_by(prefix=service.prefix) \
                    .count()

            citizen.ticket_number = service.prefix + str(service_count)
        else:
            period_state_being_served = PeriodState.get_state_by_name("Being Served")

            ticket_create_period = Period(
                csr_id=csr.csr_id,
                reception_csr_ind=csr.receptionist_ind,
                ps_id=period_state_being_served.ps_id,
                time_start=datetime.now()
            )
            service_request.periods.append(ticket_create_period)

        citizen.cs_id = citizen_state.cs_id

        #  See whether first service, or next service.
        if len(citizen.service_reqs) == 0:
            snowplow_event = "chooseservice"
            citizen.service_count = 1
        else:
            snowplow_event = "additionalservice"
            citizen.service_count = citizen.service_count + 1

        db.session.add(service_request)
        db.session.add(citizen)
        db.session.commit()

        SnowPlow.choose_service(service_request, csr, snowplow_event)

        citizen_result = self.citizen_schema.dump(citizen)
        socketio.emit('update_active_citizen', citizen_result.data, room=csr.office_id)
        result = self.service_request_schema.dump(service_request)

        return {'service_request': result.data,
                'errors': result.errors}, 201
Esempio n. 9
0
def deploy():
    '''
    Run deployment tasks.
    '''

    # migrate database to latest revision
    from flask_migrate import upgrade
    upgrade()

    # insert data
    from app.models import Color
    Color.insert_entries()

    from app.models import Permission
    Permission.insert_entries()

    from app.models import Role
    Role.insert_entries()

    from app.models import IDType
    IDType.insert_entries()

    from app.models import Gender
    Gender.insert_entries()

    from app.models import Relationship
    Relationship.insert_entries()

    from app.models import PurposeType
    PurposeType.insert_entries()

    from app.models import ReferrerType
    ReferrerType.insert_entries()

    from app.models import BookingState
    BookingState.insert_entries()

    from app.models import AssignmentScoreGrade
    AssignmentScoreGrade.insert_entries()

    from app.models import GREAWScore
    GREAWScore.insert_entries()

    from app.models import ScoreLabel
    ScoreLabel.insert_entries()

    from app.models import InvitationType
    InvitationType.insert_entries()

    from app.models import EducationType
    EducationType.insert_entries()

    from app.models import ScoreType
    ScoreType.insert_entries()

    from app.models import CourseType
    CourseType.insert_entries()

    from app.models import iPadCapacity
    iPadCapacity.insert_entries()

    from app.models import iPadState
    iPadState.insert_entries()

    from app.models import Room
    Room.insert_entries()

    from app.models import Lesson
    Lesson.insert_entries()

    from app.models import Section
    Section.insert_entries()

    from app.models import Assignment
    Assignment.insert_entries()

    from app.models import Test
    Test.insert_entries()

    from app.models import AnnouncementType
    AnnouncementType.insert_entries()

    from config import basedir
    data = raw_input(u'Enter data identifier (e.g.: initial or 20160422): ')
    datadir = os.path.join(basedir, 'data', data)
    if os.path.exists(datadir):
        from app.models import User
        User.insert_entries(data=data, basedir=basedir)

        from app.models import UserCreation
        UserCreation.insert_entries(data=data, basedir=basedir)

        from app.models import Punch
        Punch.insert_entries(data=data, basedir=basedir)

        from app.models import Tag
        Tag.insert_entries(data='initial', basedir=basedir)

        from app.models import Product
        Product.insert_entries(data='initial', basedir=basedir)

        from app.models import Course
        Course.insert_entries(data='initial', basedir=basedir)

        from app.models import Period
        Period.insert_entries(data='initial', basedir=basedir)

        from app.models import iPad
        iPad.insert_entries(data='initial', basedir=basedir)

        from app.models import iPadContent
        iPadContent.insert_entries(data='initial', basedir=basedir)

        from app.models import NotaBene
        NotaBene.insert_entries(data='initial', basedir=basedir)

        from app.models import Feed
        Feed.insert_entries(data=data, basedir=basedir)
    else:
        print u'---> Invalid data identifier: %s' % data
Esempio n. 10
0
def confirm_current_period(gameid):
    """
    Calculate results for all active games that have all their inputs approved """
    game = Game.query.filter_by(id=gameid).first_or_404()
    players = game.players.all()
    products = Product.query.all()
    periods = [
        period for player in players for period in player.periods.filter_by(
            period_number=game.current_period).all()
    ]

    periods_totals = [
        period_total for player in players
        for period_total in player.period_total.filter_by(
            period_number=game.current_period).all()
    ]

    form = None
    all_confirmed = (periods and all([p.confirmed_by_admin for p in periods])) \
        and (periods_totals and all([pt.input_approved_by_admin for pt in periods_totals]))

    if not all_confirmed:
        flash('There are unconfirmed user inputs.')
    else:
        form = ConfirmCurrentPeriodResultsForm()
        if form.validate_on_submit():
            if form.approved.data:
                game.current_period += 1
                db.session.add(game)
                db.session.flush()

                for player in players:
                    player.game_id = game.id
                    for product in products:

                        previous_period = player.periods.filter_by(
                            period_number=game.current_period - 1,
                            product_id=product.id).first()

                        # try to query period in case we come back from future
                        next_period = Period.query.filter_by(
                            id=
                            f'{game.id}_{player.id}_{game.current_period}_{product.id}'
                        ).first()

                        if not next_period:
                            next_period = Period(
                                id=
                                f'{game.id}_{player.id}_{game.current_period}_{product.id}',
                                game_id=game.id,
                                period_number=game.current_period,
                                product_id=product.id,
                                products_in_stock_beginning_of_period=
                                previous_period.products_in_stock_end_of_period
                            )
                            player.periods.append(next_period)
                        else:
                            next_period.products_in_stock_beginning_of_period = previous_period.products_in_stock_end_of_period

                    previous_period_total = player.period_total.filter_by(
                        period_number=game.current_period - 1).first()

                    next_p_total = PeriodTotal.query.filter_by(
                        id=f'{game.id}_{player.id}_{game.current_period}'
                    ).first()
                    if not next_p_total:
                        next_p_total = PeriodTotal(
                            id=f'{game.id}_{player.id}_{game.current_period}',
                            game_id=game.id,
                            period_number=game.current_period,
                            input_approved_by_admin=AUTO_APPROVE_RESULTS)

                    # financial calculations
                    next_p_total.credit_total = (
                        previous_period_total.credit_total +
                        previous_period_total.take_credit)

                    next_p_total.overdraft_total = (
                        previous_period_total.overdraft_total -
                        previous_period_total.deposit_overdraft)

                    next_p_total.money_total_begining_of_period = (
                        previous_period_total.money_total_end_of_period -
                        previous_period_total.deposit_overdraft +
                        previous_period_total.take_credit)

                    # add overdraft_change to next period money
                    if next_p_total.overdraft_total < 0:
                        overdraft_change = -next_p_total.overdraft_total
                        next_p_total.overdraft_total = 0
                        next_p_total.money_total_begining_of_period += overdraft_change

                    # if money for next_p less than 0 add to overdraft forr next_p and set money to 0
                    if next_p_total.money_total_begining_of_period < 0:
                        next_p_total.overdraft_total = (
                            next_p_total.overdraft_total -
                            next_p_total.money_total_begining_of_period)
                        next_p_total.money_total_begining_of_period = 0

                    # financial calculations

                    player.period_total.append(next_p_total)
                    db.session.add(player)
                    db.session.commit()
                flash(f'Current game period is {game.current_period}')
                flash(f'Successfully updated game state!')
                return redirect(url_for('games'))
    return render_template('confirm_game.html',
                           game=game,
                           periods=periods,
                           form=form,
                           players=players)