Beispiel #1
0
def register():
    username = request.headers.get('username', "")
    name = FINQ(request.headers.get('name', type=str).split(',')).map(
        str.strip).to_list()
    email = request.headers.get('email')
    phone = request.headers.get('phone')
    password = request.headers.get('password')
    users = FINQ(db.users())
    registrars = FINQ(db.registrars())

    if users.any(lambda u: u.username == username) or registrars.any(
            lambda u: u.username == username):
        return ErrorResult(Fail("username")).as_dict()
    if users.any(lambda u: u.email == email) or registrars.any(
            lambda u: u.email == email):
        return ErrorResult(Fail("email")).as_dict()

    registrar = db.add_user_registrar(name, username, email, phone,
                                      generate_password_hash(password))
    db.commit()
    link = registrar.create_confirmation_link()
    html = f"""<h2>Sergey's Store</h2><p>Логин: {username}</p>
<p>Имя: {' '.join([name[1], name[0], name[2]])}</p><p>Телефонный номер: {phone}</p>
<p>Для подтверждения регистрации пройдите по ссылке:</p><br><a>{app.config['SITE_ADDRESS'] + link}</a>"""
    mailer.send_html_message("Registration confirmation", [email], html)
    return {"success": True}
    def remove_item_from_cart(self, batch_id, user):
        batch = self.db.get_item_from_cart(batch_id)
        if not batch:
            raise Fail("nobatch")
        if batch.customer != user:
            raise Fail("nopermission")

        self.db.remove(batch)
        self.db.commit()
Beispiel #3
0
def auth_user(db: dbc,
              username: str,
              accessToken: str,
              permission=0) -> Result:
    user = db.get_user_by_name(username)
    if not user:
        return ErrorResult(Fail("nouser"))
    if user.accessToken != accessToken:
        return ErrorResult(Fail("noauth" if accessToken == "" else "reauth"))
    if user.permission_level < permission:
        return ErrorResult(Fail("nopermission"))

    return Success(user)
Beispiel #4
0
def get_provider_name():
    provider_id = request.args.get("providerId", -1, type=int)
    provider = db.get_provider(provider_id)

    if provider:
        return Success(provider.name) \
            .as_dict()
    return ErrorResult(Fail("noprovider")) \
        .as_dict()
Beispiel #5
0
def get_item_data():
    item_id = request.args.get("itemId", -1)

    product = db.get_product(item_id)
    if product:
        return Success({"provider_id": product.provider_id,
                        "id": product.id,
                        "description": product.description,
                        "price": product.price,
                        "name": product.name,
                        "in_stock": product.in_stock,
                        "img_count": product.img_count,
                        "category": product.category.create_category_path()}) \
            .as_dict()
    else:
        return ErrorResult(Fail("noItem")) \
            .as_dict()
Beispiel #6
0
def save_item_data(data: dict):
    product = db.get_product(data['id'])
    if product:
        provider = db.get_provider_by_name(data['provider'])
        if not provider:
            provider = db.add_provider(data['provider'])
        updated_category = db.create_category_from_path(data['category'])
        db.commit()

        product.img_count = data['img_count']
        product.name = data['name']
        product.description = data['description']
        product.price = data['price']
        product.in_stock = data['in_stock']
        product.provider_id = provider.id
        product.category_id = updated_category.id
        db.commit()
    else:
        raise Fail("noItem")
    def get_order(self, user, orderId):
        order = self.db.get_order(orderId)
        count = 0
        for batch in order.batches:
            count += batch.amount
        if order.customer != user and user.permission_level == 0:
            raise Fail("nopermission")

        batches = FINQ(order.batches).map(lambda b: self.createBatchJson(b, user.permission_level > 0)).to_list()
        dict = {
            "summary": order.summary,
            "count": count,
            "date_created": str(order.created_on.strftime("%H:%M %d/%m/%Y")),
            "batches": batches
        }
        if user.permission_level > 0:
            dict['customer_username'] = order.customer.username
            dict['customer_name'] = order.customer.name
            dict['customer_phone'] = order.customer.phone_number
        return dict
    def remove_product(self, item_id):
        product = self.db.get_product(item_id)
        if not product:
            raise Fail("noproduct")

        FINQ(product.batches) \
            .map(lambda b: b.order) \
            .filter(lambda o: FINQ(o.batches)
                    .filter(lambda b: b.product_id != item_id)
                    .none()) \
            .for_each(self.db.remove)

        if FINQ(product.category.products) \
                .filter(lambda b: b.id != item_id) \
                .none() and product.category.sub_categories:
            if product.category.parent_id != -1 and \
                    product.category.parent.sub_categories == 1:
                self.db.remove(product.category.parent)
            self.db.remove(product.category)
        self.db.remove(product)
        self.db.commit()
 def add_item_to_cart(self, item_id, user, amount):
     if amount <= 0:
         raise Fail("wrongAmount")
     self.db.add_item_to_cart(customer=user.id, product=item_id, amount=amount)
     self.db.commit()