예제 #1
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)
예제 #2
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)