Beispiel #1
0
 def test_run_has_status(self):
     status = Status("Open")
     db.session.add(status)
     db.session.commit()
     run = Run(datetime.utcnow())
     run.status = status
     assert run.status.description == "Open"
Beispiel #2
0
 def test_run_calculate_cost_total(self):
     # Set up cafe with menu
     cafe = Cafe()
     db.session.add(cafe)
     db.session.commit()
     price1 = Price(cafe.id, "S")
     price1.amount = 1.4
     price1.cafe = cafe
     price2 = Price(cafe.id, "M")
     price2.amount = 2.5
     price2.cafe = cafe
     db.session.add(price1)
     db.session.add(price2)
     db.session.commit()
     # Create run and add some coffees
     run = Run(datetime.utcnow())
     db.session.add(run)
     db.session.commit()
     coffee1 = Coffee("Latte")
     coffee1.price = price1
     coffee1.run = run
     coffee2 = Coffee("Cappuccino")
     coffee2.price = price2
     coffee2.run = run
     coffee3 = Coffee("Mocha")
     coffee3.price = price1
     coffee3.run = run
     coffee3.paid = True
     db.session.add(coffee1)
     db.session.add(coffee2)
     db.session.add(coffee3)
     db.session.commit()
     # Calculate total price of run
     total = run.calculateTotalRunCost()
     assert total == 3.9
Beispiel #3
0
 def test_run_has_cafe(self):
     cafe = Cafe()
     db.session.add(cafe)
     db.session.commit()
     run = Run(datetime.utcnow())
     run.cafe = cafe
     db.session.add(run)
     db.session.commit()
     assert run in cafe.runs
Beispiel #4
0
 def test_user_has_runs(self):
     user = User()
     db.session.add(user)
     db.session.commit()
     run = Run(datetime.utcnow())
     run.fetcher = user
     assert len(user.runs) == 1
     db.session.add(run)
     db.session.commit()
     assert run in db.session
Beispiel #5
0
 def test_add_to_run(self):
     coffee = Coffee("Latte")
     run = Run(datetime.utcnow())
     coffee.run = run
     db.session.add(coffee)
     db.session.add(run)
     db.session.commit()
     assert coffee in run.coffees
Beispiel #6
0
 def test_add_run(self):
     run = Run(datetime.utcnow())
     db.session.add(run)
     db.session.commit()
     assert run in db.session
     db.session.delete(run)
     db.session.commit()
     assert run not in db.session
Beispiel #7
0
 def test_user_owes_money_to_person(self):
     # Set up cafe with menu
     cafe = Cafe()
     db.session.add(cafe)
     db.session.commit()
     price1 = Price(cafe.id, "S")
     price1.amount = 1.4
     price1.cafe = cafe
     price2 = Price(cafe.id, "M")
     price2.amount = 2.5
     price2.cafe = cafe
     db.session.add(price1)
     db.session.add(price2)
     db.session.commit()
     # Create run and add some coffees
     run = Run(datetime.utcnow())
     user1 = User()
     user2 = User()
     run.fetcher = user2
     db.session.add(user1)
     db.session.add(user2)
     db.session.add(run)
     db.session.commit()
     coffee1 = Coffee("Latte")
     coffee1.price = price1
     coffee1.run = run
     coffee2 = Coffee("Cappuccino")
     coffee2.price = price2
     coffee2.run = run
     coffee2.addict = user1
     coffee3 = Coffee("Mocha")
     coffee3.price = price1
     coffee3.run = run
     coffee3.paid = True
     coffee3.addict = user1
     db.session.add(coffee1)
     db.session.add(coffee2)
     db.session.add(coffee3)
     db.session.commit()
     amount = user1.moneyOwedPerson(user2)
     assert amount == 2.5
Beispiel #8
0
    def create_run(self, slackclient, user, channel, match):
        """Create an open run

        Args:
            slackclient: the slackclient.SlackClient object for the current
                connection to Slack.
            user: the slackclient.User object for the user who send the
                message to us.
            channel: the slackclient.Channel object for the channel the
                message was received on.
            match: the object returned by re.match (an _sre.SRE_Match object).
        """
        logger = logging.getLogger('create_run')
        logger.info('Matches: %s', pprint.pformat(match.groupdict()))
        cafeid = match.group('cafeid')
        if cafeid and cafeid.isdigit():
            cafe = Cafe.query.filter_by(id=int(cafeid)).first()
        if not cafe:
            channel.send_message('Cafe does not exist. These are the available cafes:')
            self.list_cafes(slackclient, user, channel, match=None)
            return

        pickup = match.groupdict().get('pickup', None)
        timestr = match.groupdict().get('time', None)

        # Get the person creating the run
        person = utils.get_or_create_user(user.id, self.TEAM_ID, user.name)
        logger.info('User: %s', dbuser)

        # Assume valid? Create the run
        run = Run(timestr)
        run.person = person
        run.fetcher = person
        run.cafeid = cafeid
        run.pickup = pickup
        run.modified = sydney_timezone_now()
        run.is_open = True

        db.session.add(run)
        db.session.commit()

        # Create the event
        self.write_to_events("created", "run", run.id, run.person)

        # Notify Slack
        try:
            events.run_created(run.id)
        except Exception as e:
            logging.exception('Error while trying to send notifications.')
Beispiel #9
0
 def test_run_has_coffee(self):
     run = Run(datetime.utcnow())
     db.session.add(run)
     db.session.commit()
     coffee1 = Coffee("Latte")
     coffee2 = Coffee("Cappuccino")
     coffee1.run = run
     coffee2.run = run
     db.session.add(coffee1)
     db.session.add(coffee2)
     db.session.commit()
     assert len(run.coffees) == 2
     assert run.coffees[0] == coffee1
     assert run.coffees[1] == coffee2
Beispiel #10
0
def add_run(cafeid=None):
    form = RunForm(request.form)
    users = User.query.all()
    form.person.choices = [(user.id, user.name) for user in users]
    cafes = Cafe.query.all()
    if not cafes:
        flash(
            "There are no cafes currently configured. Please add one before creating a run",
            "warning")
        return redirect(url_for("home"))
    form.cafeid.choices = [(cafe.id, cafe.name) for cafe in cafes]

    if request.method == "GET":
        if cafeid:
            form.cafeid.data = cafeid
        form.person.data = current_user.id

        # Generate a time for the run. The algorithm here is we want the
        # default run time to be at least 15 minutes in the future, and also a
        # multiple of 15 minutes (since it look nicer).
        # This means that by default, the run will be between 15 and 30 minutes
        # from the current time.
        t = sydney_timezone_now().replace(second=0, microsecond=0)
        t += datetime.timedelta(minutes=15)
        t += datetime.timedelta(
            minutes=15 -
            (t.minute % 15))  # truncate up to the nearest 15 minutes
        form.time.data = t
        form.is_open.data = True

        return render_template("runform.html",
                               form=form,
                               formtype="Add",
                               current_user=current_user)

    if form.validate_on_submit():
        # Add run
        run = Run(form.data["time"])
        person = User.query.filter_by(id=form.data["person"]).first()
        run.person = person.id
        run.fetcher = person
        run.cafeid = form.data["cafeid"]
        run.pickup = form.data["pickup"]
        run.modified = sydney_timezone_now()
        run.is_open = form.data["is_open"]

        db.session.add(run)
        db.session.commit()
        try:
            events.run_created(run.id)
        except Exception as e:
            logging.exception('Error while trying to send notifications.')
            flash(
                'Error occurred while trying to send notifications. Please tell Maddy, Elmo, or Katie.\n{}'
                .format(cgi.escape(str(e), quote=True)), "failure")
        write_to_events("created", "run", run.id)
        flash("Run added", "success")
        return redirect(url_for("view_run", runid=run.id))
    else:
        for field, errors in form.errors.items():
            flash("Error in %s: %s" % (field, "; ".join(errors)), "danger")
        return render_template("runform.html",
                               form=form,
                               formtype="Add",
                               current_user=current_user)