From 533f03613115aada830c65e5a427b363b9f784ca Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Mon, 18 Jul 2016 16:29:21 +0200 Subject: [PATCH] Adding/editing bookmarks --- digimarks.py | 71 +++++++++++++++++++++++++--------------- requirements.txt | 1 + setup.py | 2 +- static/css/digimarks.css | 28 ++++++++++++++++ templates/edit.html | 48 +++++++++++++++++++++++---- 5 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 static/css/digimarks.css diff --git a/digimarks.py b/digimarks.py index 398c6a9..b4fbf20 100644 --- a/digimarks.py +++ b/digimarks.py @@ -1,7 +1,7 @@ import datetime import hashlib import os -import subprocess +import requests from utilkit import datetimeutil @@ -83,6 +83,13 @@ class Bookmark(db.Model): self.url_hash = hashlib.md5(self.url).hexdigest() + def get_title_from_source(self): + """ Request the title by requesting the source url """ + result = requests.get(self.url) + title = '' + return title + + def to_dict(self): result = { 'title': self.title, @@ -125,49 +132,61 @@ def bookmarks(userkey): @app.route('//') -def viewbookmark(urlhash): +def viewbookmark(userkey, urlhash): """ Bookmark detail view """ - # bookmark = getbyurlhash() - return render_template('viewbookmark.html', userkey=userkey) + bookmark = Bookmark.select(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey) + return render_template('viewbookmark.html', userkey=userkey, bookmark=bookmark) @app.route('///json') -def viewbookmarkjson(urlhash): +def viewbookmarkjson(userkey, urlhash): """ Serialise bookmark to json """ - # bookmark = getbyurlhash() - return bookmark + bookmark = Bookmark.select(Bookmark.url_hash == urlhash, Bookmark.userkey == userkey) + return bookmark.to_dict() -@app.route('//edit/') +@app.route('///edit') def editbookmark(userkey, urlhash): """ Bookmark edit form """ # bookmark = getbyurlhash() bookmark = Bookmark(Bookmark.url_hash==urlhash) - return render_template('edit.html', userkey=userkey) + return render_template('edit.html', action='Edit bookmark', userkey=userkey, bookmark=bookmark) @app.route('//add') def addbookmark(userkey): """ Bookmark add form """ - return render_template('edit.html', userkey=userkey) + bookmark = Bookmark() + return render_template('edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark) -@app.route('//add/') -def adding(userkey): - password = request.args.get('password') - if password != PASSWORD: +@app.route('//adding', methods=['GET', 'POST']) +#@app.route('//adding') +def addingbookmark(userkey): + """ Add the bookmark from form submit by /add """ + #password = request.args.get('password') + #if password != PASSWORD: + # abort(404) + + if request.method == 'POST': + print request + print request.form + title = request.form['title'] + print title + url = request.form['url'] + tags = request.form['tags'] + print url + if url: + bookmark = Bookmark(url=url, title=title, tags=tags, userkey=userkey) + bookmark.sethash() + #bookmark.fetch_image() + if not title: + # Title was empty, automatically fetch it from the url + bookmark.get_title_from_source() + bookmark.save() + return redirect(url) abort(404) - - url = request.args.get('url') - title = 'Temp' - tags = '' - if url: - bookmark = Bookmark(url=url, title=title, tags=tags) - bookmark.sethash() - #bookmark.fetch_image() - bookmark.save() - return redirect(url) - abort(404) + return redirect(url_for('add')) @app.route('//tags') @@ -197,4 +216,4 @@ if __name__ == '__main__': User.create_table(True) # run the application - app.run(port=9999) + app.run(port=9999, debug=True) diff --git a/requirements.txt b/requirements.txt index a10cc4a..38e95cf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ flask peewee flask-peewee +requests utilkit diff --git a/setup.py b/setup.py index 0d9a5b6..ec6de61 100644 --- a/setup.py +++ b/setup.py @@ -35,7 +35,7 @@ setup( # as a practice no need to hard code version unless you know program wont # work unless the specific versions are used - install_requires=['Flask', 'Peewee', 'Flask-Peewee', 'utilkit'], + install_requires=['Flask', 'Peewee', 'Flask-Peewee', 'requests', 'utilkit'], py_modules=['digimarks'], diff --git a/static/css/digimarks.css b/static/css/digimarks.css new file mode 100644 index 0000000..f217b2f --- /dev/null +++ b/static/css/digimarks.css @@ -0,0 +1,28 @@ + + /* label color */ + .input-field label { + color: #FFF; + } + /* label focus color */ + .input-field input[type=text]:focus + label { + color: #FFF; + } + /* label underline focus color */ + .input-field input[type=text]:focus { + border-bottom: 1px solid #FFF; + box-shadow: 0 1px 0 0 #FFF; + } + /* valid color */ + .input-field input[type=text].valid { + border-bottom: 1px solid #FFF; + box-shadow: 0 1px 0 0 #FFF; + } + /* invalid color */ + .input-field input[type=text].invalid { + border-bottom: 1px solid #FFF; + box-shadow: 0 1px 0 0 #FFF; + } + /* icon prefix focus color */ + .input-field .prefix.active { + color: #FFF; + } diff --git a/templates/edit.html b/templates/edit.html index 18d0386..9875b25 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -1,11 +1,45 @@ +{% extends "base.html" %} +{% block title %}{{ action }}{% endblock %} +{% block pageheader %}{{ action }}{% endblock %} +{% block pagecontent %} -

Edit/add bookmark

+
+
- +
+ description + + +
- - - +
+ turned_in + + +
- -
+
+ label + + +
+ +
+ {#star#} +
+ +
+
+ +
+

+
+ {# #} + +
+{% endblock %}