Exemple #1
0
def mobile_synccoffee():
    # Parse JSON
    # Try to add
    # Return success or failure, with ID or error
    if request.headers["Content-Type"] == "application/json":
        coffeejson = request.get_json()
    else:
        return redirect(url_for("home"))
    try:
        print "Coffee JSON Request", coffeejson
        if "id" in coffeejson:
            coffee = Coffee.query.filter_by(id=coffeejson["id"]).first()
        else:
            coffee = Coffee(coffeejson["coffeetype"])
        person = get_person(coffeejson["person"])
        coffee.person = person.id
        coffee.addict = person
        coffee.size = coffeejson["size"]
        coffee.sugar = coffeejson["sugar"]
        coffee.run = coffeejson["run"]
        coffee.runobj = Run.query.filter_by(id=coffeejson["run"]).first()
        if "id" not in coffeejson:
            db.session.add(coffee)
        db.session.commit()
        return jsonify(msg="success",
                       id=coffee.id,
                       modified=coffee.jsondatetime("modified"))
    except:
        return jsonify(msg="error")
def add_or_update_coffee(coffee_data, coffees_updated, coffees_entered,
                         error_coffees):
    old_coffees = Coffee.query(Coffee.name == coffee_data['name'],
                               Coffee.roaster == coffee_data['roaster'],
                               Coffee.active == True).fetch()
    if old_coffees:
        if len(old_coffees) > 1:
            logging.warning(
                "Query for coffee name: {}, roaster: {} returned {} results. Result are {}"
                .format(coffee_data['name'], coffee_data['roaster'],
                        len(old_coffees), old_coffees))
        for key, value in coffee_data.iteritems():
            setattr(old_coffees[0], key, value)
        try:
            old_coffees[0].put()
            coffees_updated += 1
        except AttributeError:
            # error putting the coffee into the datastore
            error_coffees.append(coffee_data['product_page'])
    else:
        coffee = Coffee(**coffee_data)
        try:
            coffee.put()
            coffees_entered += 1
        except AttributeError:
            # error putting the coffee into the datastore
            error_coffees.append(coffee_data['product_page'])
    return [coffees_updated, coffees_entered, error_coffees]
Exemple #3
0
def add_coffee(runid=None):
    runs = Run.query.filter(Run.time >= sydney_timezone_now()).filter_by(
        is_open=True).all()
    if not runs:
        flash(
            "There are no upcoming coffee runs. Would you like to make one instead?",
            "warning")
        return redirect(url_for("home"))
    lastcoffee = Coffee.query.filter_by(addict=current_user).order_by(
        Coffee.id.desc()).first()
    form = CoffeeForm(request.form)
    form.runid.choices = [(r.id, r.time) for r in runs]
    if runid:
        run = Run.query.filter_by(id=runid).first()
        localmodified = run.time.replace(
            tzinfo=pytz.timezone("Australia/Sydney"))
        if sydney_timezone_now() > localmodified:
            flash("You can't add coffees to this run", "danger")
            return redirect(url_for("view_run", runid=runid))
        form.runid.data = runid
    users = User.query.all()
    form.person.choices = [(user.id, user.name) for user in users]
    if request.method == "GET":
        form.person.data = current_user.id
        return render_template("coffeeform.html",
                               form=form,
                               formtype="Add",
                               current_user=current_user)
    if form.validate_on_submit():
        print form.data
        coffee = Coffee(form.data["coffee"])
        person = User.query.filter_by(id=form.data["person"]).first()
        coffee.personid = person.id
        coffee.addict = person
        coffee.runid = form.data["runid"]
        run = Run.query.filter_by(id=form.data["runid"]).first()
        coffee.price = form.data["price"]
        print coffee.price
        coffee.modified = sydney_timezone_now()
        db.session.add(coffee)
        db.session.commit()
        write_to_events("created", "coffee", coffee.id)
        notify_run_owner_of_coffee(run.fetcher, person, coffee)
        flash("Coffee order added", "success")
        return redirect(url_for("view_coffee", coffeeid=coffee.id))
    else:
        for field, errors in form.errors.items():
            flash("Error in %s: %s" % (field, "; ".join(errors)), "danger")
        return render_template("coffeeform.html",
                               form=form,
                               current_user=current_user)
Exemple #4
0
def create_coffee(payload):
    body = request.get_json()
    name = body['name']
    origin = body.get('origin', None)
    roaster = body.get('roaster', None)
    description = body.get('description', None)
    brewing_method = body.get('brewing_method', None)

    new_coffee = Coffee(name=name,
                        origin=origin,
                        roaster=roaster,
                        description=description,
                        brewing_method=brewing_method)

    try:
        Coffee.insert(new_coffee)
    except:
        abort(500)

    return jsonify(success=True, coffee=new_coffee.format_long())
Exemple #5
0
 def setup(self):
     self.sirocco = Brand('Sirocco')
     self.lasemeuse = Brand('La Semeuse')
     self.brands = [self.sirocco, self.lasemeuse]
     self.coffee = Coffee(name='Mocca', brands=self.brands, quantity=5)