Kaynağa Gözat

started work on search backend, buggy

control 3 yıl önce
ebeveyn
işleme
24303603cf

+ 35 - 8
app/dashboards.py

@@ -9,7 +9,7 @@ from app.lib import clean_file as cf, tools
 from app.lib import collector
 from app.lib import stripe
 
-from .forms import UPForm, PicForm, CAForm, BidForm, WalletForm
+from .forms import UPForm, PicForm, CAForm, BidForm, WalletForm, SearchForm
 
 dashboards = Blueprint('dashboards', __name__)
 
@@ -18,6 +18,8 @@ dashboards = Blueprint('dashboards', __name__)
 def market():
     return_list = collector.join_art_list_table()
 
+    seform = SearchForm()
+
     if request.method == "POST":
         focus_item = request.form.get('focus_but')
         if focus_item and current_user.is_authenticated:
@@ -26,7 +28,7 @@ def market():
             db.session.commit()
             return redirect(url_for('dashboards.detail'))
 
-    return render_template('market.html', user=current_user, listings = return_list)
+    return render_template('market.html', user=current_user, listings = return_list, seform = seform)
 	
 @dashboards.route('/profile', methods=['GET', 'POST'])
 @login_required
@@ -36,6 +38,8 @@ def profile():
     form3 = WalletForm()
     user_bid_hist = collector.user_bid_hist(current_user.id)
 
+    seform = SearchForm()
+
     # Initializes wallet and fetches amount
     dispatch.init_wallet(current_user.id)
     wallet = Wallet.query.filter_by(user_id = current_user.id).first()
@@ -112,7 +116,8 @@ def profile():
             form2 = form2,
             form3 = form3,
             ubh = user_bid_hist,
-            wallet_amount = wallet_amount
+            wallet_amount = wallet_amount,
+            seform = seform
             )
 
 @dashboards.route('/create_art', methods=['GET', 'POST'])
@@ -122,6 +127,8 @@ def create():
 
     available_art = collector.check_listing()
 
+    seform = SearchForm()
+
    # check POST req
     if form.validate_on_submit():
         new_art = form.upload.data
@@ -157,18 +164,34 @@ def create():
                 flash('Listed!', category='success')
             
 
-    return render_template('create_art.html', user = current_user, form = form, av_art = available_art)
+    return render_template('create_art.html', user = current_user, form = form, av_art = available_art, seform = seform)
 	
 @dashboards.route('/search', methods=['GET', 'POST'])
 @login_required
 def search():
-    return render_template('search.html', user = current_user)
+    ##:
+    seform = SearchForm()
+
+    ##BUG: this may need to be on every view function for it to work...
+    if seform.validate_on_submit():
+        searchterm = seform.searchterm.data
+        if searchterm:
+            search_result = collector.search_art_objn(searchterm)
+            if search_result:
+                return render_template('search.html', user = current_user, seform = seform, sr = search_result)
+            search_result = collector.search_art_objc(searchterm)
+            if search_result:
+                return render_template('search.html', user = current_user, seform = seform, sr = search_result)
+                
+
+    return render_template('search.html', user = current_user, seform = seform)
 
 @dashboards.route('/detail', methods=['GET', 'POST'])
 @login_required
 def detail():
     focus = None
     form = BidForm()
+    seform = SearchForm()
 
     # Collects details of the listing based on the 
     # focus pointer of the user
@@ -215,12 +238,15 @@ def detail():
             detail = focus,
             own_uname = owner_obj.username,
             form = form,
-            ibh = item_bid_hist
+            ibh = item_bid_hist,
+            seform = seform
             )
 
 @dashboards.route('/charge', methods=['POST'])
 @login_required
 def charge():
+    seform = SearchForm()
+
     # Stripe charge POST request method
     # Amount in cents
     dbc_stripe = Stripe.query.filter_by(user_id = current_user.id).order_by(Stripe.id.desc()).first()
@@ -241,11 +267,12 @@ def charge():
     # db dispatch for wallet top up
     dispatch.top_up(current_user.id, raw_amount)
 
-    return render_template('charge.html', user = current_user)
+    return render_template('charge.html', user = current_user, seform = seform)
 
 @dashboards.route('/hashchain', methods=['GET'])
 def hashchain():
     hashchain = Hashchain.query.all()
     txlist = TX.query.all()
+    seform = SearchForm()
 
-    return render_template('hash.html', user = current_user, hashchain = hashchain, txlist = txlist)
+    return render_template('hash.html', user = current_user, hashchain = hashchain, txlist = txlist, seform = seform)

BIN
app/database.db


+ 4 - 0
app/forms.py

@@ -51,3 +51,7 @@ class MFAForm(FlaskForm):
 class WalletForm(FlaskForm):
     amount = FloatField(validators=[DataRequired()])
     submit = SubmitField('Top Up')
+
+class SearchForm(FlaskForm):
+    searchterm = StringField(validators=[DataRequired()])
+    submit = SubmitField('Search')

+ 14 - 1
app/lib/collector.py

@@ -1,4 +1,5 @@
-# Collects data the retarded way
+# Collects data from the db tables
+# the retarded way
 
 from flask_login import current_user
 from sqlalchemy import desc
@@ -90,3 +91,15 @@ def get_art_obj(filehash):
     art = Art.query.filter_by(filehash = filehash).first()
     return art
 
+def search_art_objn(artname):
+    art = Art.query.filter_by(name = artname).first()
+    print(f'DEBUG (collector: objn) art:{art}')##
+    return art
+
+def search_art_objc(creator):
+    art = Art.query.filter_by(creator = creator).all()
+    print(f'DEBUG (collector: objc) art:{art}')##
+    return art
+
+
+

+ 5 - 4
app/templates/base.html

@@ -59,11 +59,12 @@
         {% endif %}
         </ul>
         <div>
-            <form action="search" class="input-group navbar-nav-middle flash_message_size">
-                <input type="text" class="form-control" placeholder="Search Art">
+            <form action="search" name="searchterm" class="input-group navbar-nav-middle flash_message_size">
+                {{ seform.hidden_tag() }}
+                <!--<input type="text" class="form-control" placeholder="Search Art">-->
+                {{ seform.searchterm(placeholder="Search for art", class="form-control") }}
                 <div class="input-group-append">
-                    <button class="btn btn-secondary" type="button">
-                        <i class="fa fa-search">Search</i>
+                    {{ seform.submit(placeholder="Search", class="btn btn-secondary")}}
                     </button>
                 </div>
             </form>

+ 3 - 0
app/templates/search.html

@@ -12,6 +12,9 @@
                 <b></b>
             </div>
             <div class="row">
+                {% if sr %}
+                {{ sr.name }}
+                {% endif %}
                 <p id="market_item_text_left">Current Bid</b>
                 <p id="market_item_text_right">(Price) <span class="price-span">USD</span></b>
             </div>