Exemple #1
0
def getallbills():
    start = time.time()
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)
    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)

    if flag == True:
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        bills_schema = Billschema(many=True)
        dbtime = time.time()
        result = Bills.select_user_by_ownerid(owner_id)
        dur = (time.time() - dbtime) * 1000
        c.timing("dbconnect", dur)

        data = bills_schema.dumps(result)

        #File.select_file_by_billid()

        c.incr("getallbillcount")
        dur = (time.time() - start) * 1000
        c.timing("getallbilltime", dur)

        return jsonify(data)
    else:
        return custom_http_code("unauthorized", 401)
Exemple #2
0
def getallbills():
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data = request.get_json()

    flag = checkauthentication(username, passwordinfo)

    if flag == True:
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        bills_schema = Billschema(many=True)

        result = Bills.select_user_by_ownerid(owner_id)

        data = bills_schema.dumps(result)

        #File.select_file_by_billid()

        return jsonify(data)
    else:
        return custom_http_code("unauthorized", 401)
Exemple #3
0
def billcreate():
    #db.create_all()
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data = request.get_json()

    flag = checkauthentication(username, passwordinfo)

    if flag == True:
        # file = request.files['file']
        #

        data1 = request.get_json()

        list_var = data1["categories"]
        list_var = str(list_var)
        list_var = ((list_var.strip("[")).strip("]")).strip("'")
        data1["categories"] = list_var

        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()
        #user_sc = Credentialschema(many=False)
        bill_scs = Billschema(many=False)
        data = user_sc.dump(result)
        owner_id = data.get('id')
        #data=str(owner_id)
        id = str(uuid.uuid4().hex)

        load_data = bill_sc.load(data1)
        new_bill = Bills(load_data, id, owner_id)
        db.session.add(new_bill)
        db.session.commit()

        result = Bills.select_user_by_billid(id)
        print(owner_id)
        print(id)
        if result:
            print("there")
        else:
            print("empt")
        data = bill_sc.dumps(result)
        # print(result)
        # print(data)
        # # output=Credential.select_user_by_email(check_email)
        # print("done")

        attachmentfile = {}

        final = json.loads(data)
        final["attachments"] = attachmentfile
        return custom_http_code(final, 200)

    else:

        return custom_http_code("not authorized", 401)
Exemple #4
0
def getfile(billid, fileid):
    start = time.time()
    bill_id = billid
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data1 = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)
    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)

    if flag == True:  #check if user exits
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        result2 = Bills.select_user_by_billid(bill_id)
        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id2 = data2.get('owner_id')

        if owner_id == owner_id2:  #authorized against bill and user
            file_sc = File_schema_output(many=False)
            dbtime = time.time()
            result = File.select_file_by_file_id(fileid)

            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)
            print(result)
            data = file_sc.dump(result)
            print(data)
            if not result:

                c.incr("getfilecount")
                dur = (time.time() - start) * 1000
                c.timing("getfilecount", dur)

                return custom_http_code("file does not exist bad request", 404)

            c.incr("getfilecount")
            dur = (time.time() - start) * 1000
            c.timing("getfilecount", dur)

            return custom_http_code(data, 200)
        else:
            return custom_http_code('Unauthorised', 401)

    else:
        return custom_http_code('invalid login', 401)
Exemple #5
0
def deletebill(billid):
    print(billid)
    username = request.authorization.username
    passwordinfo = request.authorization.password
    # bill_sc = Billschema(many=False)
    # data = request.get_json()

    flag = checkauthentication(username, passwordinfo)
    print(billid)
    if flag == True:
        print(billid)
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')
        print(owner_id)

        result2 = Bills.select_user_by_billid(billid)
        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id_test = data2.get('owner_id')
        print(owner_id_test)
        #return "before delete"
        if owner_id == owner_id_test:
            Bills.delete_bills(billid)

            File.delete_file_by_bill(billid)

            result2 = File.select_file_by_billid(billid)
            file_sc = Fileschema(many=False)
            data2 = file_sc.dump(result2)
            file_id = data2.get('id')

            basedir = app.config['UPLOAD_FOLDER']
            filedir = basedir + file_id + "/"
            shutil.rmtree(filedir)

            return custom_http_code("deleted", 204)
        else:
            return custom_http_code("bill id invalid or not found", 404)

    else:
        return custom_http_code("unauthorized", 401)
Exemple #6
0
def deletefile(billid, fileid):
    bill_id = billid
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data1 = request.get_json()

    flag = checkauthentication(username, passwordinfo)

    if flag == True:  #check if user exits
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        result2 = Bills.select_user_by_billid(bill_id)
        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id2 = data2.get('owner_id')

        if owner_id == owner_id2:  #authorized against bill and user
            file_sc = File_schema_output(many=False)
            result = File.select_file_by_file_id(fileid)
            print(result)
            if not result:
                return custom_http_code("file does not exist", 404)

            basedir = app.config['UPLOAD_FOLDER']

            filedir = root_dir + "/" + "attachments" + "/" + fileid + "/"
            shutil.rmtree(filedir)
            File.delete_file(fileid)

            return custom_http_code(data, 204)
        else:
            return custom_http_code('Unauthorised', 401)

    else:
        return custom_http_code('invalid login', 401)
Exemple #7
0
def daycheck(days):
    username = request.authorization.username
    passwordinfo = request.authorization.password
    flag = checkauthentication(username, passwordinfo)

    if flag == True:
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        bills_schema = Billschema(many=True)
        dbtime = time.time()
        result = Bills.select_user_by_ownerid(owner_id)

        if not result:
            return custom_http_code("no bills for user", 401)

        else:

            sqs = boto3.resource('sqs', region_name='us-east-1')
            #sqs = boto3.resource('sqs',region_name='us-east-1')
            # Get the queue. This returns an SQS.Queue instance
            queue = sqs.get_queue_by_name(QueueName='lambdaQueue')
            message = {}
            message["email"] = username
            message["days"] = days
            message['owner_id'] = owner_id
            message = json.dumps(message)
            response = queue.send_message(MessageBody=message)
            id = response.get('MessageId')
            print(id)
            print(response.get('MD5OfMessageBody'))
            th = threading.Thread(target=threadFunc, args=(username, ))

            # Start the thread
            th.start()
            return custom_http_code('resquest sent to queue' + id, 200)

            # print("helo")
    else:
        return custom_http_code("invalid user")
Exemple #8
0
def deletefile(billid, fileid):
    start = time.time()
    bill_id = billid
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data1 = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)
    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)

    if flag == True:  #check if user exits
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        result2 = Bills.select_user_by_billid(bill_id)
        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id2 = data2.get('owner_id')

        if owner_id == owner_id2:  #authorized against bill and user
            file_sc = File_schema_output(many=False)

            dbtime = time.time()
            result = File.select_file_by_file_id(fileid)

            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)
            print(result)
            if not result:
                return custom_http_code("file does not exist", 404)

            filedir = root_dir + "/" + "attachments" + "/" + fileid + "/"

            bucketkey = 'fileid' + '/'
            s3 = boto3.resource("s3")
            bucketobj = s3.Bucket(bucket)
            file_key = fileid + '/'
            bucketobj.objects.filter(Prefix=file_key).delete()

            if os.path.exists(filedir):
                shutil.rmtree(filedir)
            else:
                print("file id folder noyt found")

            File.delete_file(fileid)

            c.incr("deletefilecount")
            dur = (time.time() - start) * 1000
            c.timing("deletefilecount", dur)
            return custom_http_code(data, 204)

        else:
            c.incr("deletefilecount")
            dur = (time.time() - start) * 1000
            c.timing("deletefilecount", dur)
            return custom_http_code('Unauthorised', 401)

    else:
        return custom_http_code('invalid login', 401)
Exemple #9
0
def upload_file(billId):
    start = time.time()
    bill_id = billId
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data1 = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)
    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)

    if flag == True:  #check if user exits
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        dbtime = time.time()
        result2 = Bills.select_user_by_billid(bill_id)

        dur = (time.time() - dbtime) * 1000
        c.timing("dbconnect", dur)
        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id2 = data2.get('owner_id')

        if owner_id == owner_id2:  #authorized against bill and user
            # checking  if the  request has the file part

            file = request.files['file']
            #
            if 'file' not in request.files:
                return custom_http_code('No file part in the request', 400)
            elif file.filename == '':
                return custom_http_code('No file part in the request', 400)
            elif file and allowed_file(file.filename):
                result = File.select_file_by_billid(bill_id)
                print(result)
                if result:
                    return custom_http_code(
                        "file already exists with bill delete first", 400)
                filename = secure_filename(file.filename)
                id = str(uuid.uuid4().hex)
                dir = "attachments" + "/" + id
                # os.mkdir(dir)
                target = os.path.join(root_dir, dir)
                print(target)
                if not os.path.isdir(target):
                    os.mkdir(target)
                else:
                    return custom_http_code("file already exists", 400)
                destination_folder = "/".join([target, filename])
                file.seek(0, os.SEEK_END)
                file_len = file.tell()
                img_key = hashlib.md5(file.read()).hexdigest()
                obj = file.save(destination_folder)
                #file = request.files['file']
                object_name = id + "/" + file.filename
                s3_client = boto3.client('s3')
                name = 'attachments/' + id + '/' + filename
                #fileobj= open(name,'r')
                #obj=file.save(destination_folder)
                file = request.files['file']

                dbtime = time.time()
                uploading = s3_client.upload_fileobj(file, bucket, object_name)
                #obj=file.save(destination_folder)

                dur = (time.time() - dbtime) * 1000
                c.timing("s3time", dur)

                url = 'https://s3.console.aws.amazon.com/' + bucket + "/attachments/" + id + "/" + filename
                upload_date = datetime.datetime.today().strftime('%Y-%m-%d')
                # img_key = hashlib.md5(file.read()).hexdigest()
                #     print(img_key.encode("utf-8"))

                dbtime = time.time()
                new_bill = File(id, bill_id, filename, upload_date, url,
                                file_len, img_key)
                db.create_all()
                db.session.add(new_bill)
                db.session.commit()

                dur = (time.time() - dbtime) * 1000
                c.timing("dbconnect", dur)
                #   result=Credential.query.filter_by(first_name='Jane').first()
                file_sc = File_schema_output(many=False)
                result = File.select_file_by_file_id(id)
                print(result)
                data = file_sc.dump(result)
                print(data)

                # bill_schema= Billschema(many=False)
                # data= Bills.select_user_by_billid(billid)
                #   query_result = bill_schema.dump(data)
                #file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

                c.incr("postfilecount")
                dur = (time.time() - start) * 1000
                c.timing("postfilecount", dur)
                return custom_http_code(data, 201)

            else:

                c.incr("postfilecount")
                dur = (time.time() - start) * 1000
                c.timing("postfilecount", dur)
                return custom_http_code('wrong file extension', 400)
        else:

            c.incr("postfilecount")
            dur = (time.time() - start) * 1000
            c.timing("postfilecount", dur)
            return custom_http_code('Unauthorised', 401)

    else:
        return custom_http_code('invalid login', 401)
Exemple #10
0
def getbillid(bill_id):
    start = time.time()
    username = request.authorization.username
    passwordinfo = request.authorization.password
    bill_sc = Billschema(many=False)
    data1 = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)
    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)

    if flag == True:
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')

        dbtime = time.time()
        result2 = Bills.select_user_by_billid(bill_id)
        dur = (time.time() - dbtime) * 1000
        c.timing("dbconnect", dur)

        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id2 = data2.get('owner_id')

        if owner_id == owner_id2:
            json_data = request.get_json()

            list_var = json_data["categories"]
            list_var = str(list_var)
            list_var = ((list_var.strip("[")).strip("]")).strip("'")
            json_data["categories"] = list_var

            vendor_name = json_data.get('vendor')
            bill_date = json_data.get('bill_date')
            due_date = json_data.get('due_date')
            amount_due = json_data.get('amount_due')
            categories = json_data.get('categories')
            payment_status = json_data.get('paymentStatus')

            price = json_data.get('amount_due')
            if price < 0.1 or price is None:
                return custom_http_code("amount bad request", 400)
            if payment_status != "paid" or payment_status != "due" or payment_status != "no_payment" or payment_status != "no_payment_required":
                payment_status = "due"

            dbtime = time.time()
            state = Bills.query.filter_by(id=bill_id).update(
                dict(vendor=vendor_name,
                     bill_date=bill_date,
                     due_date=due_date,
                     amount_due=amount_due,
                     categories=categories,
                     paymentStatus=payment_status))

            db.session.commit()

            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)

            bill_schema = Billschema(many=False)

            data = Bills.select_user_by_billid(bill_id)
            query_result = bill_schema.dump(data)

            #check attachment
            dbtime = time.time()
            result = File.select_file_by_billid(bill_id)
            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)

            if not result:
                attachmentfile = {}
                query_result["attachments"] = attachmentfile
                return jsonify(query_result)

            #if attachment

            print(result)
            #attachmentb results dump
            file_sc = File_schema_output(many=False)
            data = file_sc.dumps(result)

            #bill info dict
            attachmentfile = query_result

            #attachment dict
            final = json.loads(data)

            #add two dict
            attachmentfile["attachments"] = final

            c.incr("putbillcount")
            dur = (time.time() - start) * 1000
            c.timing("putbillcount", dur)
            return jsonify(attachmentfile)

        else:
            c.incr("putbillcount")
            dur = (time.time() - start) * 1000
            c.timing("putbillcount", dur)
            return custom_http_code('Unauthorised', 401)

    else:
        return custom_http_code('invalid login', 401)
Exemple #11
0
def getasinglebill(billid):
    start = time.time()

    # print(billid)
    username = request.authorization.username
    passwordinfo = request.authorization.password
    # bill_sc = Billschema(many=False)
    # data = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)

    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)

    print(billid)
    if flag == True:
        print(billid)
        dbtime = time.time()
        result = Credential.select_user_by_email(username)
        dur = (time.time() - dbtime) * 1000
        c.timing("dbconnect", dur)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')
        print(owner_id)
        dbtime = time.time()
        result2 = Bills.select_user_by_billid(billid)
        dur = (time.time() - dbtime) * 1000
        c.timing("dbconnect", dur)

        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id_test = data2.get('owner_id')
        print(owner_id_test)
        #return "before delete"
        if owner_id == owner_id_test:
            bill_schema = Billschema(many=False)

            dbtime = time.time()
            data = Bills.select_user_by_billid(billid)
            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)

            query_result = bill_schema.dumps(data)
            query_result = json.loads(query_result)

            #check attachment
            dbtime = time.time()
            result = File.select_file_by_billid(billid)
            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)

            if not result:
                attachmentfile = {}
                query_result["attachments"] = attachmentfile
                return jsonify(query_result)

            #if attachment

            print(result)
            #attachmentb results dump
            file_sc = File_schema_output(many=False)
            data = file_sc.dumps(result)

            #bill info dict
            attachmentfile = query_result

            #attachment dict
            final = json.loads(data)

            #add two dict
            attachmentfile["attachments"] = final

            c.incr("getbillcount")
            dur = (time.time() - start) * 1000
            c.timing("getbillcount", dur)
            return jsonify(attachmentfile)

        else:

            c.incr("getbillcount")
            dur = (time.time() - start) * 1000
            c.timing("getbillcount", dur)
            return custom_http_code("invalid bill id", 404)

    else:
        return custom_http_code("unzauthorized", 401)
Exemple #12
0
def deletebill(billid):
    start = time.time()
    # print(billid)
    username = request.authorization.username
    passwordinfo = request.authorization.password
    # bill_sc = Billschema(many=False)
    # data = request.get_json()
    dbtime = time.time()
    flag = checkauthentication(username, passwordinfo)
    dur = (time.time() - dbtime) * 1000
    c.timing("dbconnect", dur)
    print(billid)
    if flag == True:
        print(billid)
        result = Credential.select_user_by_email(username)
        user_sc = Credentialschema()

        data = user_sc.dump(result)
        owner_id = data.get('id')
        print(owner_id)
        dbtime = time.time()
        result2 = Bills.select_user_by_billid(billid)
        dur = (time.time() - dbtime) * 1000
        c.timing("dbconnect", dur)
        bill_sc = Billschema(many=False)

        data2 = bill_sc.dump((result2))

        owner_id_test = data2.get('owner_id')
        print(owner_id_test)
        #return "before delete"
        if owner_id == owner_id_test:

            dbtime = time.time()
            Bills.delete_bills(billid)

            result2 = File.select_file_by_billid(billid)

            dur = (time.time() - dbtime) * 1000
            c.timing("dbconnect", dur)
            file_sc = Fileschema(many=False)
            data2 = file_sc.dump(result2)
            file_id = data2.get('id')

            print(result2)

            print(data2)
            if not result2:

                c.incr("getfilecount")
                dur = (time.time() - start) * 1000
                c.timing("getfilecount", dur)

                return custom_http_code("file does not exist bad request", 404)

            #basedir=app.config['UPLOAD_FOLDER']

            filedir = root_dir + '/' + "attachments/" + file_id + "/"

            if os.path.isdir(filedir):
                shutil.rmtree(filedir)

                # filedir=root_dir+"/"+"attachments"+"/"+file_id+"/"

                bucketkey = 'fileid' + '/'
                s3 = boto3.resource("s3")
                bucketobj = s3.Bucket(bucket)
                file_key = file_id + '/'
                bucketobj.objects.filter(Prefix=file_key).delete()

            else:
                print("no attachment with bill")
            File.delete_file_by_bill(billid)

            c.incr("deletebillcount")
            dur = (time.time() - start) * 1000
            c.timing("deletebillcount", dur)
            return custom_http_code("deleted", 204)
        else:
            c.incr("deletebillcount")
            dur = (time.time() - start) * 1000
            c.timing("deletebillcount", dur)
            return custom_http_code("bill id invalid or not found", 404)

    else:
        return custom_http_code("unauthorized", 401)