Skip to content

Commit

Permalink
fixed bug where editing record would remove its votes and date
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardhanari committed Oct 10, 2019
1 parent 87cd944 commit 22a430c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
21 changes: 11 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def commit_record():
@app.route('/update_record/<record_id>', methods=["POST"])
def update_record(record_id):
records = mongo.db.repo
# inserts new record taking values form the form on /edit
records.update({'_id': ObjectId(record_id)},
# edits existing record taking values form the form on /edit
records.update({'_id': ObjectId(record_id)}, { '$set' :
{'title': request.form.get('title'),
'url': request.form.get('url'),
'desc': request.form.get('desc'),
'category': request.form.get('category')})
'category': request.form.get('category')}})
flash('Entry updated!')
return redirect(url_for('get_records'))

Expand Down Expand Up @@ -148,17 +148,18 @@ def upvote_now(record_id):
# sorting by...
# ...date added

def sorting_by_date():
mongo.db.repo.find().sort([("date", -1), ("votes", -1)]) # newest first
mongo.db.repo.find().sort([("date", 1), ("votes", -1)]) # oldest first
def sorting_by_date_newest():
newest_records = mongo.db.repo.find().sort("date_added", -1).sort("votes", -1 ) # newest first
return redirect(url_for('get_records', records=newest_records))

return redirect(url_for('get_records'))
def sorting_by_date_oldest():
oldest_records=mongo.db.repo.find().sort([("date", 1), ("votes", -1)]) # oldest first
return redirect(url_for('get_records', records=oldest_records))

# ...votes
def sorting_by_votes():
mongo.db.repo.find().sort([("votes", 1), ("date", -1)]) # top votes first

return redirect(url_for('get_records'))
top_votes = mongo.db.repo.find().sort([("votes", 1), ("date", -1)]) # top votes first
return redirect(url_for('get_records', records=top_votes))

# if __name__ == '__main__':
# app.run(host=os.environ.get('IP'),
Expand Down
10 changes: 10 additions & 0 deletions templates/edit-record.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ <h3>Update link </h3>
<label for="category">Choose category:</label>
</div></div>

<!-- Two hidden fields to make it easy for keeping votes and date_added values
<div class="row">
<div class="input-field col s12">
<input id="date" name="date" type="text" class="hidden" value="{{ record.date_added }}" required>
</div>
<div class="input-field col s12">
<input id="votes" name="votes" type="number" class="hidden" value="{{ record.votes }}" required>
</div>
</div> -->

<div class="row">
<button class="btn waves-effect waves-light" type="submit" name="action">Update information
<i class="material-icons right">send</i>
Expand Down

0 comments on commit 22a430c

Please sign in to comment.