def create(full_url: str, life_period: int) -> UrlModel:
        """ Create url """
        try:
            current_date = datetime.date.today()
            expire_date = current_date + datetime.timedelta(days=life_period)

            url_hash = UrlShorterHelper.get_unique_hash_url(full_url)

            request = Request(full_url, headers={'User-Agent': 'Mozilla/5.0'})
            page = urlopen(request)
            p = parse(page)
            title = p.find(".//title").text

            url = UrlModel(full_url=full_url,
                           url_hash=url_hash,
                           expire_date=expire_date,
                           full_url_title=title)
            db.session.add(url)
            db.session.commit()

        except IntegrityError:
            db.session.rollback()
            raise ResourceExists('url already exists')

        return url
Esempio n. 2
0
    def create(qty, product_id, username, cart_number) -> dict:
        """ Create Cart """
        result: dict = {}
        try:
            data_stock = Stock.query.filter_by(product_id=product_id).first()
            if (data_stock is not None and data_stock.stock > 0):
                if ((data_stock.stock - qty) >= 0):
                    data = Cart(qty, product_id, username, 'Stock Available',
                                cart_number)
                    data.save()
                    result = {
                        'product_id': data.product_id,
                        'qty': data.qty,
                        'username': data.username,
                        'date_created': str(data.date_created),
                    }
                else:
                    result = {'message': 'Stock is not Enough'}
            else:
                result = {'message': 'Stock Unavailable'}
        except IntegrityError:
            Cart.rollback()
            raise ResourceExists('file_name already exists')

        return result
Esempio n. 3
0
    def create(username: str, avatar_url: str, password: str) -> dict:
        """ Create user """
        result: dict = {}
        try:
            user = User(
                username=username,
                avatar_url=avatar_url,
                password=UserRepository.generate_hash(password),
            )
            user.save()
            access_token = create_access_token(identity=username)
            refresh_token = create_refresh_token(identity=username)
            result = {
                "username": user.username,
                "avatar_url": user.avatar_url,
                "date_created": str(user.date_created),
                "message": "User {} was created".format(user.username),
                "access_token": access_token,
                "refresh_token": refresh_token,
            }

        except IntegrityError:
            User.rollback()
            raise ResourceExists("user already exists")

        return result
Esempio n. 4
0
    def create(data) -> dict:
        """ Create Data """
        try:

            avg_values = DataModel.compute_average_values(data)

            # save each entry separately
            for d in avg_values:
                d = DataModel(d, avg_values[d])
                d.save()

        except IntegrityError:
            DataModel.rollback()
            raise ResourceExists('hat already exists')
        return avg_values
Esempio n. 5
0
    def create(name, desc) -> dict:
        """ Create Product """
        result: dict = {}
        try:
            data = Product(name=name, desc=desc)
            data.save()
            result = {
                'name': data.name,
                'desc': data.desc,
                'date_created': str(data.date_created),
            }
        except IntegrityError:
            Product.rollback()
            raise ResourceExists('file_name already exists')

        return result
Esempio n. 6
0
    def create(username: str, avatar_url: str) -> dict:
        """ Create user """
        result: dict = {}
        try:
            user = User(username=username, avatar_url=avatar_url)
            user.save()
            result = {
                'username': user.username,
                'avatar_url': user.avatar_url,
                'date_created': str(user.date_created),
            }
        except IntegrityError:
            User.rollback()
            raise ResourceExists('user already exists')

        return result
Esempio n. 7
0
    def create(stock, product_id, remark) -> dict:
        """ Create LogStock """
        result: dict = {}
        try:
            data = LogStock(stock, product_id, remark)
            data.save()
            result = {
                'product_id': data.product_id,
                'stock': data.stock,
                'remark': data.remark,
                'date_created': str(data.date_created),
            }
        except IntegrityError:
            LogStock.rollback()
            raise ResourceExists('file_name already exists')

        return result
Esempio n. 8
0
    def create(cart_number) -> dict:
        """ Create Checkout """
        result: dict = {}
        try:
            data_cart = Cart.query.filter_by(cart_number=cart_number).all()
            result_messages = []
            for res_cart in data_cart:
                data_stock = Stock.query.filter_by(
                    product_id=res_cart.product_id).first()
                if (data_stock is not None and data_stock.stock > 0):
                    if ((data_stock.stock - res_cart.qty) >= 0):
                        current_stock = data_stock.stock - res_cart.qty
                        data_stock.stock = current_stock
                        remark = "Stock Decreased from " + str(
                            data_stock.stock) + " to " + str(current_stock)
                        data_stock.commit()
                        result = {
                            'product_id': res_cart.product_id,
                            'message': remark
                        }
                        LogStockRepository.create(current_stock,
                                                  res_cart.product_id, remark)
                    else:
                        result = {
                            'product_id': res_cart.product_id,
                            'message': 'Stock is not Enough'
                        }
                else:
                    result = {
                        'product_id': res_cart.product_id,
                        'message': 'Stock Unavailable'
                    }
                result_messages.append(result)
            data = Checkout(cart_number)
            data.save()
            result = {
                'cart_number': cart_number,
                'date_created': str(data.date_created),
                'message': result_messages
            }
        except IntegrityError:
            Checkout.rollback()
            raise ResourceExists('file_name already exists')

        return result
Esempio n. 9
0
    def update_password(username: str, password: str) -> dict:
        """ Update user password """
        result: dict = {}
        try:
            user = User.query.filter_by(username=username).first_or_404()
            if user:
                user.password = UserRepository.generate_hash(password)
                user.save()
                result = {
                    "username": user.username,
                    "message": "Password updated successfully",
                }
            else:
                result = {
                    "message":
                    "User not found. Check username spelling please."
                }
        except IntegrityError:
            User.rollback()
            raise ResourceExists("Update didn't work")

        return result
Esempio n. 10
0
    def create(name: str, password: str, nickname: str, profile: str) -> dict:
        """ Create user """
        result: dict = {}
        try:
            print(name, password, nickname, profile)
            user = User(name=name,
                        password=password,
                        nickname=nickname,
                        profile=profile)
            user.save()
            result = {
                "id": 1,
                "name": user.name,
                "password": user.password,
                "nickname": user.nickname,
                "profile": user.profile,
                "date_created": str(user.date_created),
            }
        except IntegrityError:
            User.rollback()
            raise ResourceExists("user already exists")

        return result
Esempio n. 11
0
    def create(stock, product_id) -> dict:
        """ Create Stock """
        result: dict = {}
        try:
            data = Stock.query.filter_by(product_id=product_id).first()
            if(data is None):
                data = Stock(stock, product_id)
                data.save()
            else :
                print(data)
                data.stock = stock
                data.commit()

            result = {
                'product_id': data.product_id,
                'stock': data.stock,
                'date_created': str(data.date_created),
            }
        except IntegrityError:
            Stock.rollback()
            raise ResourceExists('file_name already exists')

        return result
Esempio n. 12
0
    def delete(username: str) -> dict:
        """ Delete user """
        result: dict = {}
        try:
            user = User.query.filter_by(username=username).first_or_404()
            if user:
                user.active = False
                user.save()
                result = {
                    "username": user.username,
                    "active": user.active,
                    "date_created": str(user.date_created)
                }
            else:
                result = {
                    "message":
                    "User not found. Check username spelling please."
                }
        except IntegrityError:
            User.rollback()
            raise ResourceExists("Deletion didn't work")

        return result
Esempio n. 13
0
    def update_avatar(username: str, avatar_url: str) -> dict:
        """ Update user """
        result: dict = {}
        try:
            user = User.query.filter_by(username=username).first_or_404()
            if user:
                user.avatar_url = avatar_url
                user.save()
                result = {
                    "message": "User updated successfully",
                    "username": user.username,
                    "avatar_url": user.avatar_url,
                    "date_created": str(user.date_created)
                }
            else:
                result = {
                    "message":
                    "User not found. Check username spelling please."
                }
        except IntegrityError:
            User.rollback()
            raise ResourceExists("Update didn't work")

        return result