Example #1
0
 def get(self):
     try:
         client_data = utils.get_ninja(api="clients")
     except Exception as e:
         return response(401, message=str(e))
     else:
         return response(200, data=client_data['data'])
 def get(self, id):
     try:
         invoice_data = utils.get_ninja(api="invoices/"+id)
     except Exception as e:
         return response(200, message=str(e))
     else:
         return response(200, data=invoice_data['data'])
Example #3
0
def insert_user(self):
    random_string = uuid.uuid4()
    raw_token = '{}{}'.format(random_string, request.form['email'])
    access_token = hashlib.sha256(raw_token.encode('utf-8')).hexdigest()

    data_insert = {
        "email" : request.form['email'],
        "first_name" : request.form['first_name'],
        "last_name" : request.form['last_name'],
        "location" : request.form['location'],
        "sso_id" : access_token,
    }
    try:
        result = db.insert(table="tb_userdata", data=data_insert)
    except Exception as e:
        data = {
            "status": False,
            "error": str(e)
        }
        return response(200, message=data)
    else:
        data = {
            "status": True,
            "data": data_insert,
            "id": result
        }
        return response(200, data=data)
Example #4
0
def sigin():
    username = request.form['username']
    password = request.form['password']

    user = db.get_by_id(table="tb_user", field="username", value=username)

    if not user or not pbkdf2_sha256.verify(password, user[0]['password']):
        return response(status_code=401, data="You Not Authorized")
    else:
        random_string = uuid.uuid4()
        raw_token = '{}{}'.format(random_string, username)
        access_token = hashlib.sha256(raw_token.encode('utf-8')).hexdigest()

        userdata = db.get_by_id(table="tb_userdata",
                                field="id_userdata",
                                value=user[0]['id_userdata'])
        stored_data = {
            'id_userdata': user[0]['id_userdata'],
            'email': userdata[0]['email'],
            'username': username
        }
        dill_object = dill.dumps(stored_data)
        redis_store.set(access_token, dill_object)
        redis_store.expire(access_token, 3600)
        data = {
            'email': userdata[0]['email'],
            'Access-Token': access_token,
            'expires': 3600
        }
        return response(200, data=data)
 def get(self, id):
     try:
         client_data = utils.get_ninja(api="products/" + id)
     except Exception as e:
         return response(200, message=str(e))
     else:
         return response(200, data=client_data['data'])
Example #6
0
def user_get():
    id_userdata = get_jwt_identity()
    obj_userdata = list()
    column = db.get_columns('tb_userdata')
    try:
        results = list()
        query = "select * from tb_userdata where id_userdata='"+id_userdata+"' "
        data = db.query(query)
        rows = dbq.fetchall()
        for row in rows:
            print(row)
            results.append(dict(zip(column, row)))
    except Exception as e:
        return response(200, message=str(e))
    else:
        for i in results :
            data = {
                "id_userdata": str(i['id_userdata']),
                "email" : i['email'],
                "first_name" : i['first_name'],
                "last_name" : i['last_name'],
                "location" : i['location']
            }
            obj_userdata.append(data)
        return response(200, data=obj_userdata)
Example #7
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str, required=True)
        parser.add_argument('vat', type=str, required=True)
        parser.add_argument('id_number', type=str, required=True)
        parser.add_argument('address1', type=str, required=False)
        parser.add_argument('address2', type=str, required=False)
        parser.add_argument('city', type=str, required=False)
        parser.add_argument('state', type=str, required=False)
        parser.add_argument('postal_code', type=str, required=False)
        parser.add_argument('country_id', type=str, required=False)
        parser.add_argument('work_phone', type=str, required=True)
        parser.add_argument('email', type=str, required=True)
        parser.add_argument('first_name', type=str, required=True)
        parser.add_argument('last_name', type=str, required=True)
        args = parser.parse_args()
        client_data = {
            "name": args['name'],
            "vat_number": args['vat'],
            "id_number": args['id_number'],
            "address1": args['address1'],
            "address2": args['address2'],
            "city": args['city'],
            "state": args['state'],
            "postal_code": args['postal_code'],
            "country_id": args['country_id'],
            "work_phone": args['work_phone']
        }
        client_data_temps = utils.post_ninja("clients", client_data)
        contact_data = client_data_temps['data']['contacts']
        contact_db = db.get_by_id("contacts", "public_id",
                                  contact_data[0]['id'])[0]

        contact_data_update = {
            "where": {
                "id": str(contact_db['id'])
            },
            "data": {
                "email": args['email'],
                "first_name": args['first_name'],
                "last_name": args['last_name'],
                "phone": args['work_phone']
            }
        }

        try:
            a = db.update("contacts", contact_data_update)
        except Exception as e:
            return response(401, message=str(e))
        else:
            id = str(client_data_temps['data']['id'])
            client_data = utils.get_ninja(api="clients/" + id)['data']
        return response(200, data=client_data)
Example #8
0
    def get(self, email):
        try:
            client_data = utils.get_ninja(api="clients")
        except Exception as e:
            return response(401, message=str(e))

        data = list()
        for i in client_data['data']:
            if i['contacts'][0]['email'] == email:
                data = i

        return response(200, data=data)
Example #9
0
def update():
    id_userdata = get_jwt_identity()
    data = {
        "where":{
            "userdata_id": id_userdata
        },
        "data":{
            "email" : request.form['email'],
            "first_name" : request.form['first_name'],
            "last_name" : request.form['last_name'],
            "location" : request.form['location']
        }
    }

    try:
        db.update("tb_userdata", data=data)
    except Exception as e:
        message = {
            "status": False,
            "error": str(e)
        }
    else:
        message = {
            "status": True,
            "data": data
        }
    finally:
        return response(200, message=message)
Example #10
0
def delete():
    id_userdata = get_jwt_identity()
    try:
        db.delete(
                table="tb_userdata", 
                field='id_userdata',
                value=id_userdata
            )
    except Exception as e:
        message = {
            "status": False,
            "error": str(e)
        }
    else:
        message = "removing"

    finally:
        return response(200, message=message)
Example #11
0
def insert_login():
    password_hash = pbkdf2_sha256.hash(request.form['password'])
    data_insert = {
        "id_userdata": request.form['userdata_id'],
        "username": request.form['username'],
        "password": password_hash,
    }

    try:
        db.insert(table="tb_user", data=data_insert)
    except Exception as e:
        respon = {"status": False, "error": str(e)}
    else:
        data_insert = {
            "userdata_id": request.form['userdata_id'],
            "username": request.form['username'],
        }
        respon = {"status": True, "data": data_insert}
    return response(200, message=respon)
Example #12
0
def authorized(resp):
    access_token = resp['access_token']
    headers = {'Authorization': 'OAuth ' + access_token}
    try:
        req = utils.get_http('https://www.googleapis.com/oauth2/v1/userinfo',
                             None, headers)
    except Exception:
        return redirect(url_for('google'))

    try:
        dt_db = model.get_by_id("tb_userdata", "email", req['email'])
    except Exception as e:
        dt_db = None
    if not dt_db:
        data_save = {
            "sso_id": req['id'],
            "first_name": req['given_name'],
            "last_name": req['family_name'],
            "email": req['email'],
            "location": "",
            "picture": req['picture']
        }
        try:
            model.insert("tb_userdata", data_save)
        except Exception as e:
            print(e)
        expires_in = resp['expires_in']
        dill_object = dill.dumps(data_save)
        redis_store.set(access_token, dill_object)
        redis_store.expire(access_token, expires_in)
    else:
        expires_in = resp['expires_in']
        dill_object = dill.dumps(dt_db[0])
        redis_store.set(access_token, dill_object)
        redis_store.expire(access_token, expires_in)

    data_result = {
        "Access-Token": access_token,
        "email": req['email'],
        "expires": expires_in
    }
    return response(200, data=data_result)
Example #13
0
def github_authorized():
    resp = oauth.github.authorized_response()
    token = None
    for i in resp:
        if i == 'access_token':
            token = resp[i]
    req = utils.get_http('https://api.github.com/user?access_token=' + token,
                         None, None)
    try:
        dt_db = model.get_by_id("tb_userdata", "email", req['email'])
    except Exception as e:
        dt_db = None
    if not dt_db:
        data_save = {
            "sso_id": req['id'],
            "first_name": req['given_name'],
            "last_name": req['family_name'],
            "email": req['email'],
            "location": "",
            "picture": req['picture']
        }
        try:
            model.insert("tb_userdata", data_save)
        except Exception as e:
            print(e)
        dill_object = dill.dumps(data_save)
        redis_store.set(token, dill_object)
        redis_store.expire(token, 3600)
    else:
        dill_object = dill.dumps(dt_db[0])
        redis_store.set(token, dill_object)
        redis_store.expire(token, 3600)

    data_result = {
        "Access-Token": token,
        "email": req['email'],
        "expires": 3600
    }
    return response(200, data=data_result)
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('client_id', type=str, required=True)
        parser.add_argument('is_recurring', type=str, required=True)
        parser.add_argument('tax_rate1', type=str, required=True)
        parser.add_argument('tax_name1', type=str, required=True)
        parser.add_argument('is_amount_discount', type=str, required=False)
        parser.add_argument('service_type', type=str, required=True)
        parser.add_argument('product_id', type=str, required=True)
        args = parser.parse_args()

        try:
            get_products = utils.get_ninja("products/"+str(args['product_id']))['data']
        except Exception as e:
            print(e)
        tax_value = percentage(get_products['cost'], get_products['tax_rate1'])

        data_insert = {
            "client_id": args['client_id'],
            "is_recurring": args['is_recurring'],
            "tax_rate1": str(args['tax_rate1']),
            "tax_name1": str(args['tax_name1']),
            "is_amount_discount": str(args['is_amount_discount']),
            "custom_value1": args['service_type']
        }
        try:
            invoice_data_temps = utils.post_ninja("invoices", data_insert)['data']
        except Exception as e:
            print(e)
        get_invoice_id = db.get_by_id("invoices","public_id", invoice_data_temps['id'])[0]
        data_insert_items = {
            "account_id": "1",
            "user_id": "1",
            "invoice_id": str(get_invoice_id['id']),
            "product_id": str(args['product_id']),
            "product_key": get_products['product_key'],
            "notes": get_products['notes'],
            "custom_value1": args['service_type'],
            "tax_rate1": "0",
            "tax_name1": "",
            "tax_rate2":"0",
            "tax_name2": "",
            "discount": "0",
            "cost": str(get_products['cost']),
            "public_id": str(invoice_data_temps['id']),
            "qty":"1"
        }

        try:
            db.insert("invoice_items", data_insert_items)
        except Exception as e:
            print(e)
        else:
            invoice_updates = {
                "where":{
                    "public_id": str(invoice_data_temps['id'])
                },
                "data":{
                    "invoice_status_id": "2",
                    "amount": str(get_products['cost']+tax_value),
                    "balance": str(get_products['cost']+tax_value)
                }
            }
            try:
                db.update("invoices", data=invoice_updates)
            except Exception as e:
                print("DISINI",e)
        email_data = {
            "invoice_number":invoice_data_temps['invoice_number']
        }
        try:
            send_email = utils.post_ninja("email_invoice", email_data)
        except Exception as e:
            print(e)
        try:
            get_invoice = utils.get_ninja("invoices/"+str(invoice_data_temps['id']))
        except Exception as e:
            return response(200, message=str(e))
        else:
            return response(200, data=get_invoice)