Skip to content

Commit

Permalink
Added filtered delete
Browse files Browse the repository at this point in the history
  • Loading branch information
TsimpDim committed Feb 21, 2018
1 parent 7944f4d commit f63c8cc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
37 changes: 33 additions & 4 deletions _3RStore/templates/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
<h2>Options</h2>
<div class="card">
<div class="card-body">
<!-- Delete all dropdown -->
<h3 class="card-header">Delete all resources</h3>
<div class="card-block">

<!-- Delete resources section -->
<h3 class="card-header">Delete resources</h3>
<div class="card-block d-flex flex-row justify-content-between align-items-start">
<!-- Delete all -->
<div class="dropdown">
<button class="btn btn-danger" data-toggle="dropdown" style="margin-bottom:0;">
<button class="btn btn-danger" data-toggle="dropdown">
<span class="fa fa-remove"></span>
Delete all resources
</button>
Expand All @@ -19,6 +21,33 @@ <h3 class="card-header">Delete all resources</h3>
Confirm</a></li>
</div>
</div>

<!-- Filtered delete -->
<div class="d-flex flex-column">
<form method="POST" action="fildel">
<div class="dropdown">
<button class="btn btn-danger" data-toggle="dropdown">
<span class="fa fa-remove"></span>
Filtered delete
</button>

<div class="dropdown-menu">
<button class="dropdown-item" type="submit">
<span class="fa fa-check"></span>
Confirm</button></li>
</div>
</div>

<div class="input-group">
<input list="tags" type="text" name="tags" id="search_input" class="form-control" placeholder="Tags to delete...">
<datalist id="tags">
{% for tag in tags %}
<option value="{{tag}}" class="datalist_option" ></option>
{% endfor %}
</datalist>
</div>
</form>
</div>
</div>

<!-- Sorting by time of posting -->
Expand Down
46 changes: 43 additions & 3 deletions _3RStore/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,25 @@ def options():
if session.get('logged_in'):
sort = request.cookies.get('sort')
criteria = request.cookies.get('criteria')
return render_template('options.html', sort=sort, criteria=criteria)

# Get all tags
cur = conn.cursor()
user_id = session['user_id']

cur.execute(
("""SELECT DISTINCT unnest(tags) FROM resources WHERE user_id = %s"""),
(user_id,)
)

tags_raw = cur.fetchall()

# 'Unpack' tags_raw into one array
all_tags = []
for tag_arr in tags_raw:
all_tags.append(tag_arr[0])


return render_template('options.html', sort=sort, criteria=criteria, tags=all_tags)
else:
flash('You must be logged in to access the options page', 'warning')
return redirect(url_for('login'))
Expand Down Expand Up @@ -331,8 +349,8 @@ def edit_res(user_id, re_id):
# Update the row - keep date_of_posting, re_id and user_id the same
cur = conn.cursor()
cur.execute(
("""UPDATE resources SET title=%s,link=%s,note=%s,tags=%s WHERE user_id=%s AND re_id=%s"""),
(title, link, note, tags, user_id, re_id)
("""UPDATE resources SET title=%s,link=%s,note=%s,tags=%s WHERE user_id=%s AND re_id=%s"""),
(title, link, note, tags, user_id, re_id)
)
cur.close()
conn.commit()
Expand All @@ -356,6 +374,28 @@ def delall(user_id):

return redirect(url_for('resources'))

# Filtered delete
@app.route("/fildel", methods=['POST'])
def fildel():

tags_to_del = request.form.get('tags')
user_id = session['user_id']

tags_array = '{' + tags_to_del + '}'

cur = conn.cursor()
cur.execute(
("""DELETE FROM resources WHERE user_id = %s AND tags @> %s"""),
(user_id, tags_array)
)

cur.close()
conn.commit()

flash('Resources deleted successfully', 'danger')
return redirect(url_for('options'))


# Import resources
@app.route("/import_resources", methods=['GET', 'POST'])
def import_resources():
Expand Down

0 comments on commit f63c8cc

Please sign in to comment.