Exemple #1
0
def verification_code(account_id: str, account_email: str):
    if is_none(account_id) or is_none(account_email):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        verification_query = VerificationModel.query.filter_by(
            owner_id=account_id).first()
        if not verification_query:
            return response_util.http_not_found('account id not found!')
        else:
            generated_code = random.randint(000000, 999999)
            verification_query.code = generated_code

            try:
                db.session.commit()

                mail_message = Message(
                    subject='Verification Code',
                    html=render_template('mail_template.html',
                                         full_name=verification_query.name,
                                         code=generated_code),
                    sender=('WarungKu', '*****@*****.**'),
                    recipients=[account_email])
                mail.send(mail_message)

            except HTTPException:
                return response_util.http_internal_server_error()

            token = security_util.access_token(account_id)

            return response_util.http_ok('Code verification has been sent!', {
                'token': 'Bearer {}'.format(token),
                'expired': '1 Minutes'
            })
def new_product(product: ProductModel, images):
    if is_none(product.name) or is_none(product.weight) or \
            is_none(product.weight_unit) or is_none(product.available) or \
            is_none(product.available_unit) or is_none(product.minimal_order) or \
            is_none(product.minimal_order) or is_none(product.purchase_price) or \
            is_none(product.sell_price) or is_none(product.description) or \
            is_none(product.barcode) or is_none(product.category) or \
            is_none(product.expired) or is_none(images):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        path = app.config[
            'UPLOAD_FOLDER'] = '../backend-warungku/application/static/product'

        for image in images:
            image.filename = get_unique_id() + '.jpg'
            image.save(os.path.join(path, image.filename))

            separate_path = path.replace('../backend-warungku/application/',
                                         '')
            url = 'http://127.0.0.1:5000/{}/{}'.format(separate_path,
                                                       image.filename)

            db.session.add(ProductImageModel(product_id=product.id, image=url))

        db.session.add(product)

        try:
            db.session.commit()
        except HTTPException:
            return response_util.http_internal_server_error()

        return response_util.http_ok(
            'New product has been successfully added!')
Exemple #3
0
def report_result(account_id: str, report_date: str = date_now('%d-%m-%Y')):
    if is_none(account_id):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        return response_util.http_ok(
            "Owner trade result!", {
                'date': report_date,
                'income': 10_454_000,
                'net_income': 1_609_916,
                'percentage': '+15.4%'
            })
def tip_data(category: str):
    if is_none(category):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        query_tip = TipModel.query.filter_by(category=category).limit(10).all()
        if not query_tip:
            return response_util.http_not_found('No tip found!')
        else:
            data = []
            for tip in query_tip:
                data.append({
                    'image': tip.image,
                    'title': tip.title,
                    'body': tip.body,
                    'create_at': tip.create_at
                })
            return response_util.http_ok("This is tips for {}".format(category), data)
Exemple #5
0
def verification_account(account_id: str, account_code: int):
    if is_none(account_id) or is_none(account_code):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        verification_query = VerificationModel.query.filter(
            (VerificationModel.owner_id == account_id)
            or (VerificationModel.code == account_code)).first()
        if not verification_query:
            return response_util.http_not_acceptable(
                'Wrong verification code!')
        else:
            verification_query.email = True

            try:
                db.session.commit()
            except HTTPException:
                return response_util.http_internal_server_error()

            return response_util.http_ok('Account verification success!')
Exemple #6
0
def login(owner: OwnerModel):
    if is_none(owner.email) or is_none(owner.password):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        owner_query = OwnerModel.query.filter_by(email=owner.email).first()
        if not owner_query:
            return response_util.http_not_found('Account is not registered!')
        else:
            if not security_util.verify_password(owner_query.password,
                                                 owner.password):
                return response_util.http_not_acceptable(
                    'Wrong password account!')
            else:
                verification_query = VerificationModel.query.filter(
                    (VerificationModel.owner_id == owner_query.id)
                    or VerificationModel.email).first()
                data = {'id': owner_query.id}
                if not verification_query:
                    return response_util.http_accepted(
                        'Owner login success, but account is not verified!',
                        data)
                else:
                    return response_util.http_ok('Owner login success!', data)
Exemple #7
0
def register(owner: OwnerModel):
    if is_none(owner.full_name) or is_none(owner.store[0].name) or is_none(
            owner.email) or is_none(owner.password):
        return response_util.http_bad_request('Fill all the request body!')
    else:
        owner_query = OwnerModel.query.filter_by(email=owner.email).first()
        if owner_query:
            return response_util.http_not_acceptable(
                'Owner with a same email is already exist!')
        else:
            owner.verification = [(VerificationModel(owner_id=owner.id,
                                                     name=owner.full_name,
                                                     account='OWNER'))]

            try:
                db.session.add(owner)
                db.session.add(owner.store[0])
                db.session.add(owner.verification[0])
                db.session.commit()
            except HTTPException:
                return response_util.http_internal_server_error()

        return response_util.http_created('Owner account has been created!',
                                          {'id': owner.id})