1
0
mirror of https://github.com/aquatix/digimarks.git synced 2025-12-07 03:45:10 +01:00

Refactored add/edit, implemented editing a bookmark

This commit is contained in:
2016-07-22 20:37:50 +02:00
parent 1ba5b80e47
commit 8d1622ef41
2 changed files with 60 additions and 37 deletions

View File

@@ -68,13 +68,13 @@ class Bookmark(db.Model):
# Foreign key to User # Foreign key to User
userkey = CharField() userkey = CharField()
title = CharField() title = CharField(default='')
url = CharField() url = CharField()
created_date = DateTimeField(default=datetime.datetime.now) created_date = DateTimeField(default=datetime.datetime.now)
modified_date = DateTimeField(null=True) modified_date = DateTimeField(null=True)
#image = CharField(default='') #image = CharField(default='')
url_hash = CharField() url_hash = CharField(default='')
tags = CharField() tags = CharField(default='')
starred = BooleanField(default=False) starred = BooleanField(default=False)
# Website (domain) favicon # Website (domain) favicon
@@ -226,7 +226,7 @@ def editbookmark(userkey, urlhash):
# bookmark = getbyurlhash() # bookmark = getbyurlhash()
bookmark = Bookmark.get(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey) bookmark = Bookmark.get(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey)
message = request.args.get('message') message = request.args.get('message')
return render_template('edit.html', action='Edit bookmark', userkey=userkey, bookmark=bookmark, message=message) return render_template('edit.html', action='Edit bookmark', userkey=userkey, bookmark=bookmark, message=message, formaction='edit')
@app.route('/<userkey>/add') @app.route('/<userkey>/add')
@@ -236,31 +236,29 @@ def addbookmark(userkey):
return render_template('edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark) return render_template('edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark)
@app.route('/<userkey>/adding', methods=['GET', 'POST']) def updatebookmark(userkey, request, urlhash = None):
#@app.route('/<userkey>/adding') """ Add or edit bookmark """
def addingbookmark(userkey): title = request.form.get('title')
""" Add the bookmark from form submit by /add """ url = request.form.get('url')
tags = request.form.get('tags')
if request.method == 'POST':
title = request.form['title']
url = request.form['url']
tags = request.form['tags']
if 'starred' in request.form: if 'starred' in request.form:
starred = True starred = True
try: try:
request.form['starred'] request.form['starred']
except: except:
starred = False starred = False
print starred
if url: if url and not urlhash:
#bookmark = Bookmark(url=url, title=title, starred=starred, userkey=userkey) #bookmark = Bookmark(url=url, title=title, starred=starred, userkey=userkey)
bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey) bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey)
if not created: if not created:
message = 'Existing bookmark, did not overwrite with new values' message = 'Existing bookmark, did not overwrite with new values'
return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash, message=message)) return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash, message=message))
elif url:
bookmark = Bookmark.get(userkey == userkey, Bookmark.url_hash == urlhash)
# Newly created, set properties
bookmark.title = title bookmark.title = title
bookmark.url = url
bookmark.starred = starred bookmark.starred = starred
bookmark.set_tags(tags) bookmark.set_tags(tags)
bookmark.set_hash() bookmark.set_hash()
@@ -275,9 +273,30 @@ def addingbookmark(userkey):
bookmark.set_favicon() bookmark.set_favicon()
bookmark.save() bookmark.save()
#return redirect(url) return bookmark
@app.route('/<userkey>/adding', methods=['GET', 'POST'])
#@app.route('/<userkey>/adding')
def addingbookmark(userkey):
""" Add the bookmark from form submit by /add """
if request.method == 'POST':
bookmark = updatebookmark(userkey, request)
print bookmark
if type(bookmark).__name__ == 'Response':
return bookmark
return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash))
return redirect(url_for('add'))
@app.route('/<userkey>/<urlhash>/editing', methods=['GET', 'POST'])
def editingbookmark(userkey, urlhash):
""" Edit the bookmark from form submit """
if request.method == 'POST':
bookmark = updatebookmark(userkey, request, urlhash=urlhash)
return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash)) return redirect(url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash))
abort(404)
return redirect(url_for('add')) return redirect(url_for('add'))

View File

@@ -28,7 +28,11 @@
</div> </div>
{% endif %} {% endif %}
{% if formaction and formaction == 'edit' %}
<form action="{{ url_for('editingbookmark', userkey=userkey, urlhash=bookmark.url_hash) }}" method="POST">
{% else %}
<form action="{{ url_for('addingbookmark', userkey=userkey) }}" method="POST"> <form action="{{ url_for('addingbookmark', userkey=userkey) }}" method="POST">
{% endif %}
<div class="input-field col s12"> <div class="input-field col s12">
<i class="material-icons prefix">description</i> <i class="material-icons prefix">description</i>