Ejemplo n.º 1
0
    def post(self):
        """New KindToTag"""
        tag = Tag.query.filter(Tag.id == api.payload["tag_id"]).first()
        kind = Kind.query.filter(Kind.id == api.payload["kind_id"]).first()

        if not tag or not kind:
            abort(400, "Tag or kind not found")

        check_query = KindToTag.query.filter_by(kind_id=kind.id).filter_by(
            tag_id=tag.id).all()
        if len(check_query) > 0:
            abort(409, "Relation already exists")

        kind_to_tag = KindToTag(id=str(uuid.uuid4()),
                                kind=kind,
                                tag=tag,
                                amount=api.payload["amount"])
        save(kind_to_tag)

        kind.complete = (True if len(kind.kind_flavors) >= 3
                         and len(kind.kind_tags) + 1 >= 4 and kind.image_1
                         and kind.description_nl and kind.description_en else
                         False)
        save(kind)

        return kind_to_tag, 201
Ejemplo n.º 2
0
    def post(self):
        """New KindToFlavor"""
        flavor = Flavor.query.filter(
            Flavor.id == api.payload["flavor_id"]).first()
        kind = Kind.query.filter(Kind.id == api.payload["kind_id"]).first()

        if not flavor or not kind:
            abort(400, "Flavor or kind not found")

        check_query = KindToFlavor.query.filter_by(kind_id=kind.id).filter_by(
            flavor_id=flavor.id).all()
        if len(check_query) > 0:
            abort(409, "Relation already exists")

        kind_to_flavor = KindToFlavor(id=str(uuid.uuid4()),
                                      kind=kind,
                                      flavor=flavor)
        save(kind_to_flavor)

        kind.complete = (True if len(kind.kind_flavors) + 1 >= 3
                         and len(kind.kind_tags) >= 4 and kind.image_1
                         and kind.description_nl and kind.description_en else
                         False)
        save(kind)

        return kind_to_flavor, 201
Ejemplo n.º 3
0
    def post(self):
        """New KindToStrain"""
        strain = Strain.query.filter(Strain.id == api.payload["strain_id"]).first()
        kind = Kind.query.filter(Kind.id == api.payload["kind_id"]).first()

        if not strain or not kind:
            abort(400, "Strain or kind not found")

        check_query = KindToStrain.query.filter_by(kind_id=kind.id).filter_by(strain_id=strain.id).all()
        if len(check_query) > 0:
            abort(409, "Relation already exists")

        kind_to_strain = KindToStrain(id=str(uuid.uuid4()), kind=kind, strain=strain)
        save(kind_to_strain)

        kind.complete = (
            True
            if len(kind.kind_strains) + 1 >= 3
            and len(kind.kind_tags) >= 4
            and kind.image_1
            and kind.description_nl
            and kind.description_en
            else False
        )
        save(kind)

        return kind_to_strain, 201
Ejemplo n.º 4
0
    def post(self):
        """New Order"""
        payload = api.payload
        if payload.get("customer_order_id"):
            del payload["customer_order_id"]
        shop_id = payload.get("shop_id")
        if not shop_id:
            abort(400, "shop_id not in payload")

        # 5 gram check
        total_cannabis = get_price_rules_total(payload["order_info"])
        logger.info("Checked order weight", weight=total_cannabis)
        if total_cannabis > 5:
            abort(400, "MAX_5_GRAMS_ALLOWED")

        # Availability check
        unavailable_product_name = get_first_unavailable_product_name(
            payload["order_info"], shop_id)
        if unavailable_product_name:
            abort(400, f"{unavailable_product_name}, OUT_OF_STOCK")

        shop = load(Shop,
                    str(shop_id))  # also handles 404 when shop can't be found
        payload["customer_order_id"] = Order.query.filter_by(
            shop_id=str(shop.id)).count() + 1
        # Todo: recalculate total and use it as a checksum for the payload
        order = Order(id=str(uuid.uuid4()), **payload)
        save(order)
        return order, 201
Ejemplo n.º 5
0
    def post(self):
        riff_exercise_id = api.payload["riff_exercise_id"]
        """New Recent Exercise"""
        # check if current one already exists in the list
        query_result = (
            RecentRiffExercise.query.filter(RecentRiffExercise.created_by == current_user.id)
            .filter(RecentRiffExercise.riff_exercise_id == riff_exercise_id)
            .all()
        )
        if len(query_result) == 0:
            logger.info("adding recent exercise", riff_exercise_id=riff_exercise_id, user_id=current_user.id)
            riff = RecentRiffExercise(
                id=str(uuid.uuid4()), riff_exercise_id=riff_exercise_id, created_by=current_user.id
            )
            save(riff)

            # clean to ensure max recent exerciss count is 5
            logger.info("cleaning recent exercises", user_id=current_user.id)
            query_result = (
                RecentRiffExercise.query.filter(RecentRiffExercise.created_by == current_user.id)
                .order_by(desc(RecentRiffExercise.modified_at))
                .offset(5)
            )
            [db.session.delete(i) for i in query_result]

            return riff, 201
        elif len(query_result) == 1:
            logger.info("updating recent exercise", riff_exercise_id=riff_exercise_id, user_id=current_user.id)
            riff = query_result[0]
            riff.modified_at = datetime.now()
            save(riff)
            return riff, 201
        logger.error("DB integrity problem for", riff_exercise_id=riff_exercise_id, user_id=current_user.id)
        return None, 500
Ejemplo n.º 6
0
    def post(self):
        """Add new price rules to Shops"""
        price = Price.query.filter(Price.id == api.payload["price_id"]).first()
        shop = Shop.query.filter(Shop.id == api.payload["shop_id"]).first()
        kind = Kind.query.filter(Kind.id == api.payload["kind_id"]).first(
        ) if api.payload.get("kind_id") else None
        product = (Product.query.filter(
            Product.id == api.payload["product_id"]).first()
                   if api.payload.get("product_id") else None)
        category = None
        if api.payload.get("category_id"):
            category = Category.query.filter(
                Category.id == api.payload["category_id"]).first()

        if not price or not shop:
            abort(400, "Price or Shop not found")

        if (product and kind) or not product and not kind:
            abort(400, "One Cannabis or one Horeca product has to be provided")

        if kind:
            check_query = (ShopToPrice.query.filter_by(
                shop_id=shop.id).filter_by(price_id=price.id).filter_by(
                    kind_id=kind.id).all())
            if len(check_query) > 0:
                abort(409, "Relation already exists")

        if product:
            check_query = (ShopToPrice.query.filter_by(
                shop_id=shop.id).filter_by(price_id=price.id).filter_by(
                    product_id=product.id).all())
            if len(check_query) > 0:
                abort(409, "Relation already exists")

        data = api.payload
        shop_to_price = ShopToPrice(
            id=str(uuid.uuid4()),
            active=data["active"] if data.get("active") else False,
            new=data["new"] if data.get("new") else False,
            kind=kind,
            product=product,
            category=category,
            shop=shop,
            price=price,
            use_half=data["use_half"] if data.get("use_half") else False,
            use_one=data["use_one"] if data.get("use_one") else False,
            use_two_five=data["use_two_five"]
            if data.get("use_two_five") else False,
            use_five=data["use_five"] if data.get("use_five") else False,
            use_joint=data["use_joint"] if data.get("use_joint") else False,
            use_piece=data["use_piece"] if data.get("use_piece") else False,
        )
        save(shop_to_price)
        invalidateShopCache(shop_to_price.shop_id)
        return shop_to_price, 201
Ejemplo n.º 7
0
    def put(self, id):
        image_cols = [
            "image_1", "image_2", "image_3", "image_4", "image_5", "image_6"
        ]
        item = load(Kind, id)

        image = api.payload["image"]
        if image in image_cols:
            setattr(item, image, "")
            save(item)

        return item, 201
Ejemplo n.º 8
0
    def post(self):
        args = file_upload.parse_args()
        logger.warning("Ignoring files via args! (using JSON body)", args=args)

        data = request.get_json()
        backing_track = BackingTrack(id=str(uuid.uuid4()), **api.payload)

        # todo: remove approved from payload: only approve on update...

        if data.get("file") and type(data["file"]) == dict:
            name = f"{uuid.uuid4()}.mp3"
            upload_file(data["file"]["src"], name)
            backing_track.file = name
        save(backing_track)
        return backing_track, 201
Ejemplo n.º 9
0
    def post(self):
        """New RiffToTag"""
        tag = Tag.query.filter(Tag.id == api.payload["tag_id"]).first()
        riff = Riff.query.filter(Riff.id == api.payload["riff_id"]).first()

        if not tag or not riff:
            abort(400, "Tag or riff not found")

        check_query = RiffTag.query.filter_by(riff_id=riff.id).filter_by(tag_id=tag.id).all()
        if len(check_query) > 0:
            abort(409, "Relation already exists")

        riff_to_tag = RiffTag(id=str(uuid.uuid4()), riff=riff, tag=tag)
        save(riff_to_tag)

        return riff_to_tag, 201
Ejemplo n.º 10
0
    def post(self):
        """New ExerciseToTag"""
        # Todo: fix hook around me own weird id scheme...
        api.payload["riff_exercise_id"] = api.payload["exercise_id"]
        tag = Tag.query.filter(Tag.id == api.payload["tag_id"]).first()
        exercise = RiffExercise.query.filter(
            RiffExercise.id == api.payload["riff_exercise_id"]).first()

        if not tag or not exercise:
            abort(400, "Tag or exercise not found")

        check_query = RiffExerciseTag.query.filter_by(
            riff_exercise_id=exercise.id).filter_by(tag_id=tag.id).all()
        if len(check_query) > 0:
            abort(409, "Relation already exists")

        exercise_to_tag = RiffExerciseTag(id=str(uuid.uuid4()),
                                          riff_exercise=exercise,
                                          tag=tag)
        save(exercise_to_tag)

        return exercise_to_tag, 201
Ejemplo n.º 11
0
 def post(self):
     """New Shops"""
     product = Product(id=str(uuid.uuid4()), **api.payload)
     save(product)
     return product, 201
Ejemplo n.º 12
0
 def post(self):
     riff = Riff(id=str(uuid.uuid4()),
                 **api.payload,
                 created_by=str(current_user.id))
     save(riff)
     return riff, 201
Ejemplo n.º 13
0
 def post(self):
     """New Flavors"""
     flavor = Flavor(id=str(uuid.uuid4()), **api.payload)
     save(flavor)
     return flavor, 201
Ejemplo n.º 14
0
 def post(self):
     """New Table"""
     table = Table(id=str(uuid.uuid4()), **api.payload)
     save(table)
     return table, 201
Ejemplo n.º 15
0
 def post(self):
     """New Strains"""
     strain = Strain(id=str(uuid.uuid4()), **api.payload)
     save(strain)
     return strain, 201
Ejemplo n.º 16
0
 def post(self):
     """New Prices"""
     price = Price(id=str(uuid.uuid4()), **api.payload)
     save(price)
     return price, 201
Ejemplo n.º 17
0
 def post(self):
     """New Shops"""
     kind = Kind(id=str(uuid.uuid4()), **api.payload)
     save(kind)
     return kind, 201
Ejemplo n.º 18
0
 def post(self):
     """New MainCategory"""
     category = MainCategory(id=str(uuid.uuid4()), **api.payload)
     save(category)
     return category, 201
Ejemplo n.º 19
0
 def post(self):
     """New Shops"""
     shop = Shop(id=str(uuid.uuid4()), **api.payload)
     save(shop)
     return shop, 201
Ejemplo n.º 20
0
 def post(self):
     """New Tag"""
     tag = Tag(id=str(uuid.uuid4()), **api.payload)
     save(tag)
     return tag, 201