コード例 #1
0
def add():
    if not is_admin_connected():
        abort(401)

    good = Good()
    good.nom = request.form["name"]
    good.asso = request.form["asso"]
    good.semestre = request.form["semestre"]
    good.nb_exemplaires = request.form["exemplaires"]
    good.contenance = request.form["capacity"]
    good.commentaires = request.form["comment"]

    db.session.add(good)
    db.session.commit()
    db.session.flush()
    db.session.refresh(good)

    file = request.files['file']
    if file:
        extension = file.filename.split(".")[-1]
        file.save(
            os.path.join(conf.UPLOAD_FOLDER,
                         str(good.id) + "." + extension))
        good.image_url = str(good.id) + "." + extension
        db.session.add(good)
        db.session.commit()

    flash("Écocup %s ajoutée" % good.nom, "success")

    return render_template("admin/index.html", **locals())
コード例 #2
0
def data_good(request):
    if os.environ.get("TEST", None):
        request.session["userId"] = request.GET["userId"]
    try:
        if not "userId" in request.session:
            return HttpResponse("error")
        if not "user" in request.GET:
            return HttpResponse("error")
        if not request.GET["user"] == request.session["userId"]:
            return HttpResponse("error")
        user = request.GET["user"]
        user = User.objects.get(openId=user)
        target = int(request.GET["target"])
        type = int(request.GET["type"])
        item = Good.objects.filter(user=user, target=target, type=type)
        if type == 0:
            tmp = User.objects.get(id=target)
        elif type == 1:
            tmp = Plan.objects.get(id=target)
        goods = tmp.goods
        if item.count() == 0:
            item = Good(user=user, target=target, type=type)
            item.save()
            tmp.goods = goods + 1
        else:
            tmp.goods = goods - 1
            item[0].delete()
        tmp.save()
        return HttpResponse("success")
    except:
        return HttpResponse("error")
コード例 #3
0
ファイル: goods.py プロジェクト: sharashka/vader-backend
def save_offers_to_db(db, offers: List[dict]):
    # TODO make versions for offers (probably by date)
    for offer in offers:
        vader_id = offer["vader_id"]
        good_orm = Good(vader_id=vader_id)
        parameters = [
            Parameter(good=good_orm, name=name, value=value)
            for name, value in offer["params"].items()
        ]
        good_orm.parameters = parameters
        db.add(good_orm)
    db.commit()
コード例 #4
0
ファイル: create_db_records.py プロジェクト: S-Hanin/otus_py
def main():
    with open("test_goods.json") as fh:
        goods = json.load(fh)

    for good in goods:
        good = JSDict.from_dict(good)
        good_rec = Good(name=good.name, description=good.description, price=good.price)
        for img in good.images:
            img = JSDict.from_dict(img)
            img_rec = Image(path=img.path, good=good_rec)
            db.session.add(img_rec)
        db.session.add(good_rec)
        db.session.commit()
コード例 #5
0
    def create_dub(self, bot_id, bot_2):
        try:
            telebot.TeleBot(bot_2).get_me()
        except Exception:
            return 'ERROR, go back'
        bot_2 = loads(
            BotProfile.objects(token=bot_2).first().to_json())['_id']['$oid']

        text = Texts.objects(bot_id=bot_id).first()

        if not (text is None):
            text_dict = loads(text.to_json())

            dub_text = {}

            for key in text_dict:
                if key != '_id':
                    dub_text.update({key: text_dict[key]})

            dub_text['bot_id'] = bot_2

            new_text = Texts(**dub_text)
            new_text.save()

        cities = City.objects(bot_id=bot_id)

        if cities is not None:
            for city in cities:
                if not (city is None):
                    city_dict = loads(city.to_json())

                    dub_city = {}

                    for key in city_dict:
                        if key != '_id':
                            dub_city.update({key: city_dict[key]})

                    dub_city['bot_id'] = bot_2

                    new_city = City(**dub_city)
                    new_city.save()
                    goods = Good.objects(city_id=city.id.binary.hex())

                    for good in goods:
                        if good is not None:
                            Good(name=good.name,
                                 city_id=new_city.id.binary.hex(),
                                 price=good.price,
                                 description=good.description,
                                 photo=good.photo,
                                 districts=good.districts).save()

        payment = Payment.objects(bot_id=bot_id).first()

        if not (payment is None):
            payment_dict = loads(payment.to_json())

            dub_payment = {}

            for key in payment_dict:
                if key != '_id':
                    dub_payment.update({key: payment_dict[key]})

            dub_payment['bot_id'] = bot_2

            new_payment = Payment(**dub_payment)
            new_payment.save()
        return str(bot_2)
コード例 #6
0
ファイル: create_db_records.py プロジェクト: S-Hanin/otus_py
def create_good_record(name, description, price):
    good = Good()
    good.name = name
    good.description = description
    good.price = price
    return good
コード例 #7
0
ファイル: app.py プロジェクト: DimmeGz/green_lantern
logging.basicConfig(format='%(asctime)s %(levelname)-6s %(message)s', filename="logfile.log", level=logging.INFO)

with app.app_context():
    if not database_exists(db.engine.url):
        logging.info('Database "cursor_sqlalchemy_db" does not exists')
        create_database(db.engine.url)
        logging.info('Data "cursor_sqlalchemy_db" base created')
    db.create_all()
    logging.info('Database "cursor_sqlalchemy_db" exists')

with app.app_context():
    users = get_users()
    for user in users:
        db.session.add(User(**user))
    db.session.commit()
    logging.info('Data written to table "users" in db "cursor_sqlalchemy_db" succesfuly')

with app.app_context():
    stores = get_stores()
    for store in stores:
        db.session.add(Store(**store))
    db.session.commit()
    logging.info('Data written to table "stores" in db "cursor_sqlalchemy_db" succesfuly')

with app.app_context():
    goods = get_goods()
    for good in goods:
        db.session.add(Good(**good))
    db.session.commit()
    logging.info('Data written to table "goods" in db "cursor_sqlalchemy_db" succesfuly')