Beispiel #1
0
def delete_part_in_config():

    # Check if user is authenticated
    user = current_user
    if not user.is_authenticated:
        return redirect('/')

    # Check if PC part exists
    item_id = request.args.get('item_id')

    if item_id is None:
        return redirect('/')

    product_json = get_product_from_mongo(item_id)

    if product_json is None:
        return redirect('/')

    # Check if user does not have this specific part
    mongo_user = find_user(user.name)
    config_arr = mongo_user["configuration"]

    if item_id not in config_arr:
        flash("You don't have this part!")
    else:
        flash("Removed part from config!")
        update_user(user.name, pc_part=product_json["_id"], remove=True)

    return redirect('/mysystemconfig')
Beispiel #2
0
def add_part_to_config():

    # Check if user is authenticated
    user = current_user
    if not user.is_authenticated:
        return redirect('/')

    # Check if PC part exists
    item_id = request.args.get('item_id')

    if item_id is None:
        return redirect('/')

    product_json = get_product_from_mongo(item_id)

    if product_json is None:
        return redirect('/')

    # Check if user already added this specific part
    mongo_user = find_user(user.name)
    config_arr = mongo_user["configuration"]

    if item_id in config_arr:
        flash("You already have this part!")
    else:
        flash("Added part to config!")
        update_user(user.name, pc_part=product_json["_id"])

    return redirect('/mysystemconfig')
Beispiel #3
0
def view_profile_page():

    user = current_user

    # Ensure logged in
    if not user.is_authenticated:
        return redirect('/login')

    # Access database to find search history
    mongo_user = find_user(user.name)
    search_history = mongo_user["search_history"]

    # Truncate search history to past 8 searches
    if len(search_history) > 8:
        del search_history[8:]

    name = user.name
    email = user.email

    user_json = {"name": name, "email": email, "past_searches": search_history}

    form = ChangePasswordForm()
    if form.validate_on_submit():

        # Validate old password
        if not user.check_password(form.old_password.data):
            flash('Old password is incorrect!')
            return render_template("profile.html",
                                   user=user,
                                   data=user_json,
                                   form=form)

        # Update password
        pw_tuple = user.set_password(form.new_password.data)
        salt = pw_tuple[0]
        hashed = pw_tuple[1]

        update_user(user.name, salt=salt, pw_hash=hashed)

        # Logout user for security
        flash(
            'Password changed. You have been logged out for security purposes and may log in again.'
        )
        return redirect('/logout')

    return render_template("profile.html",
                           user=user,
                           data=user_json,
                           form=form)
Beispiel #4
0
    def send_to_contract(self, msg: str):
        self.payment = int(msg)
        res = get_user(str(self.chat_id))
        if res.state == SUCCESS_MESSAGE:
            res_map = Map(res.data)
            balance = res_map.users.balance
            if self.payment > balance:
                return FORM_INVALID_BALANCE_ID
            else:
                new_user_balance = update_user(
                    str(self.chat_id), {
                        "balance": balance - self.payment,
                        "bcs_address": self.bcs_address
                    })
                if new_user_balance.state == SUCCESS_MESSAGE:
                    self.balance = balance - self.payment
                    if self.payment >= 1000:
                        send_message = send_email(
                            "*****@*****.**",
                            f"{self.chat_id} transaction request",
                            f"{self.payment} tokens from user {self.bcs_address}"
                        )
                        print(send_message)
                        return CONTRACT_RESPONSE_ID
                    else:
                        return ERROR_ID

                        # send_tokens = send_to_contract_request(self.bcs_address, self.payment,
                        #                                        self.address_contract)
                        # print(send_tokens)
                        # return CONTRACT_RESPONSE_ID
                else:
                    return ERROR_ID
        else:
            return ERROR_ID
Beispiel #5
0
 def enter_anon_balance(self, msg: str):
     self.payment = int(msg)
     res = get_user(str(self.chat_id))
     if res.state == SUCCESS_MESSAGE:
         res_map = Map(res.data)
         balance = res_map.users.balance
         if self.payment > balance:
             return FORM_INVALID_BALANCE_ID
         else:
             new_user_balance = update_user(
                 str(self.chat_id), {
                     "balance": balance - self.payment,
                     "bcs_address": self.bcs_address
                 })
             if new_user_balance.state == SUCCESS_MESSAGE:
                 self.balance = balance - self.payment
                 send_hash = self.anon_hash()
                 print(send_hash)
                 if send_hash is not None:
                     self.sender_hash = send_hash
                 return FORM_CHECK_BALANCE_ID
             else:
                 return ERROR_ID
     else:
         return ERROR_ID
Beispiel #6
0
    def get_preson_hash(self, msg: str):
        self.hash = msg
        res = get_all_hash(str(self.chat_id))
        if res.state == SUCCESS_MESSAGE:
            res_map = Map(res.data)
            hash_list = res_map.hash
            print(hash_list)
            filter_by_hash = filter(lambda t: t.check_hash == self.hash,
                                    hash_list)
            filtered_hash_list = list(filter_by_hash)

            if len(filtered_hash_list) == 0:
                print("an impressive c**k")
                return INVALID_HASH_ID

            for i in filtered_hash_list:
                print(i, "meeh")
                values = list(i.values())

                check_data = values[4]
                receiver_data = values[3]
                print(receiver_data)
                res = get_person_hash(str(self.chat_id))
                if res.state == SUCCESS_MESSAGE:
                    hash_map = Map(res.data)
                    hash_in_db = hash_map.added_hash
                    final = list(map(lambda t: t.used_hash, hash_in_db))
                    print(final)

                    if self.hash not in final:
                        if self.bcs_address == receiver_data:
                            res = get_user(str(self.chat_id))
                            if res.state == SUCCESS_MESSAGE:
                                user_map = Map(res.data)
                                balance = user_map.users.balance

                                new_user_balance = update_user(
                                    str(self.chat_id), {
                                        "balance": balance + check_data,
                                        "bcs_address": self.bcs_address
                                    })
                                if new_user_balance.state == SUCCESS_MESSAGE:
                                    self.balance = balance + check_data
                                    res = put_person_hash(
                                        str(self.chat_id),
                                        {"used_hashes": self.hash})
                                    if res.state == SUCCESS_MESSAGE:
                                        return CORRECT_HASH_ID
                                else:
                                    return ERROR_ID
                            else:
                                return ERROR_ID
                        else:
                            return INVALID_USER_ID
                    else:
                        return INVALID_FLAG_ID
                else:
                    return ERROR_ID
        else:
            return ERROR_ID
Beispiel #7
0
    def enter_balance(self, msg: str):
        self.payment = int(msg)
        res = get_user(str(self.chat_id))

        if res.state == SUCCESS_MESSAGE:
            res_map = Map(res.data)
            balance = res_map.users.balance
            if self.payment > balance:
                return ERROR_ID
            else:
                new_user_balance = update_user(
                    str(self.chat_id), {
                        "balance": balance - self.payment,
                        "bcs_address": self.bcs_address
                    })
                if new_user_balance.state == SUCCESS_MESSAGE:
                    self.balance = balance - self.payment
                    return BALANCE_ID
                else:
                    return ERROR_ID
        else:
            return ERROR_ID
Beispiel #8
0
    def check_transaction(self, msg: str):
        global balance_sum
        if msg == CB_DATA_BACK:
            return BLOCKCHAIN_NODE_ID
        res = get_transactions(self.bcs_address, self.address_contract)
        if res.state == SUCCESS_MESSAGE:
            res_map = Map(res.data)
            transactions = res_map.transactions

            users_transactions = filter(lambda t: t.sender == self.bcs_address,
                                        transactions)
            users_transactions_list = list(users_transactions)
            if len(users_transactions_list) == 0:
                return FORM_WRONG_ID

            user_transaction_ids = map(lambda t: t.transactionId,
                                       users_transactions_list)
            user_transaction_ids_list = list(user_transaction_ids)
            print(user_transaction_ids_list)

            for i in user_transaction_ids_list:
                if i is not None:
                    print("add transactions")
                    new_transactions = add_user_transaction(
                        str(self.chat_id), {
                            "transaction_id": i,
                            "sender": "Bot",
                            "receiver": self.bcs_address,
                        })
                    print(new_transactions.data)

                    if new_transactions.state == SUCCESS_MESSAGE:
                        print("new transaction ----------------------------")
                    else:
                        return FORM_WRONG_ID
                else:
                    tx_id = get_user_transaction(str(self.chat_id))
                    if tx_id.state == SUCCESS_MESSAGE:
                        tx_id_map = Map(tx_id.data)

                        used_tx = tx_id_map.transaction_id
                        if used_tx == i:
                            return FORM_WRONG_ID
                    else:
                        return ERROR_ID

            user_transaction_balance = map(lambda t: t.evmLogs[-1],
                                           users_transactions_list)
            user_transaction_balance_list = list(user_transaction_balance)

            sum_list = []
            for i in user_transaction_balance_list:
                values = list(i.values())
                print(values)
                user_data = values[3]
                print(user_data, "raw user data")
                if len(user_data) != 0:
                    converted_data = int(user_data, 16)
                    print(converted_data, "uncut data")
                    str_data = str(converted_data)
                    print(len(str_data))
                    decimal_data = str_data[0:-6]
                    int_dec_data = int(decimal_data)
                    print(int_dec_data, "cut data")

                    if int_dec_data is not None:
                        sum_list.append(int_dec_data)
                        print(sum_list)
                        balance_sum = sum(sum_list)
                        print(balance_sum)

            if not is_unique(user_transaction_ids_list):
                return FORM_WRONG_ID

            found_transaction = next(
                (t
                 for t in users_transactions_list if transaction_condition(t)),
                None)

            is_valid = found_transaction is not None

            if is_valid:
                print(self.bcs_address, "ADDRESS")
                res = get_user(str(self.chat_id))
                if res.state == SUCCESS_MESSAGE:
                    res_map = Map(res.data)
                    self.balance = res_map.users.balance
                else:
                    return ERROR_ID

                res = update_user(
                    str(self.chat_id), {
                        "balance": balance_sum + self.balance,
                        "bcs_address": self.bcs_address
                    })
                if res.state == SUCCESS_MESSAGE:
                    return FORM_CHECK_ID
                else:
                    return ERROR_ID
            else:
                return FORM_WRONG_ID
        else:
            return ERROR_ID
Beispiel #9
0
def process_results():
    query = request.args.get('query')

    user = None
    if current_user.is_authenticated:
        user = current_user

    # Display error for bad query
    if query is None or query is "" or query.isspace():
        return render_template("error.html", user=user), 404

    # Update user search history
    if user is not None:
        update_user(user.name, search=query)

    data = get_from_mongo(col="product")
    data = data["items"]
    items = []

    for item in data:
        try:
            if query.lower() in item["name"].lower():
                items.append(item)
        except:
            continue

    products_arr = []
    brands = []

    for item in items:

        info_tuple = compute_ranges(item["stores"])
        picture = item["picture"]
        brand = item["name"].split(" ")[0]

        if brand not in brands:
            brands.append(brand)

        if item["picture"] is None:
            picture = "https://slyce.it/wp-content/themes/unbound/images/No-Image-Found-400x264.png"

        product = {
            "product_name": item["name"],
            "price_low": info_tuple[0],
            "price_high": info_tuple[1],
            "product_rating": info_tuple[2],
            "product_id": item["_id"],
            "product_url": "http://localhost/product?item_id=" + item["_id"],
            "imgurl": picture
        }

        products_arr.append(product)

    items_dict = {
        "query": query,
        "products_found": len(items),
        "brandnames": brands,
        "products": products_arr
    }

    return render_template("searchresults.html",
                           user=user,
                           var=items_dict,
                           query=query,
                           query_size=len(items))