From 121179cc398d86a090995cf59129eb34f04794a0 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 1 Jun 2018 21:37:45 +0200 Subject: [PATCH] moved all javascript to seperate app.js file --- app/forms.py | 4 +- app/static/css/style.css | 23 ++++++++ app/static/js/app.js | 90 +++++++++++++++++++++++++++++ app/templates/base.html | 90 +++-------------------------- app/templates/edit_book_detail.html | 9 ++- app/templates/home.html | 1 + app/views.py | 74 +++++++++++------------- 7 files changed, 167 insertions(+), 124 deletions(-) diff --git a/app/forms.py b/app/forms.py index 5c84f77..f7b8a4c 100755 --- a/app/forms.py +++ b/app/forms.py @@ -15,7 +15,7 @@ class UploadForm(FlaskForm): title = StringField('title', validators=[InputRequired()]) author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1) category = StringField('category', validators=[InputRequired()]) - year_published = StringField('year published', [validators.Length(min=4, max=4)]) + year_published = StringField('year published', [validators.Length(max=4)],default=None) file = FileField() upload = SubmitField(label='Upload') wish = SubmitField(label='''I don't have the file, but wish I did.''') @@ -24,6 +24,8 @@ class EditForm(FlaskForm): title = StringField('title', validators=[InputRequired()]) author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1) category = StringField('category', validators=[InputRequired()]) + year_published = StringField('year published', [validators.Length(max=4)],default=None) + class SearchForm(FlaskForm): choices = [('Title', 'Title'), diff --git a/app/static/css/style.css b/app/static/css/style.css index 103fc59..80daf5a 100755 --- a/app/static/css/style.css +++ b/app/static/css/style.css @@ -5,6 +5,7 @@ font-family: Helvetica; .navigation ul{ list-style-type: none; margin: 0; + margin-top: 30px; padding: 0; } @@ -129,3 +130,25 @@ font-family:'Courier New'; .ui-tabs-vertical .ui-tabs-panel { padding: 1em; float: right; width: 40em;} #draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; } #droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; } + +#newstext{ + width: 100%; + margin: 0; + padding: 3px; + top:0; + left:0; + position: fixed; + font-size: 25px; + background-color: yellow; +} + +div.marquee { + white-space:no-wrap; + overflow:hidden; +} +div.marquee > div.marquee-text { + font-style: italic; + white-space:nowrap; + display:inline; + width:auto; +} diff --git a/app/static/js/app.js b/app/static/js/app.js index edc9d30..37351fc 100755 --- a/app/static/js/app.js +++ b/app/static/js/app.js @@ -30,3 +30,93 @@ $(function() { }); //End remove row }); }); + + +$( function() { + $( "#draggable" ).draggable(); + $( "#droppable" ).droppable({ + drop: function( event, ui ) { + $( this ) + .addClass( "ui-state-highlight" ) + .find( "p" ) + .html( "Dropped!" ); + } + }); +} ); + +$( "#title" ).click(function() { + generateTitle(this); +}); + +$( document ).ready(function() { + generateTitle("#title"); +}); + +function generateTitle(elem){ +var x = ["XPERIMENTAL"] +var p1 = ["POTENTIAL", "PUBLIC", "POST", "PI", "PLATFORM FOR", "PRETENTIOUS"] +var p2 = ["PIRATE", "PERFORMATIVE", "PUBLIC", "PUBLISHING", "POTENTIAL"] +var l = ["LIBRARY", "LIAISON", "LAB", "LEGALITY", "LABOUR"] + +$(elem).text(x[Math.floor(Math.random()*x.length)]+" "+p1[Math.floor(Math.random()*p1.length)]+" "+p2[Math.floor(Math.random()*p2.length)]+" "+l[Math.floor(Math.random()*l.length)]); + +} + +$( function() { +$( "#tabs" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" ); +$( "#tabs li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" ); +} ); + + +var marquee = $('div.marquee'); +marquee.each(function() { + var mar = $(this),indent = mar.width(); + mar.marquee = function() { + indent--; + mar.css('text-indent',indent); + if (indent < -1 * mar.children('div.marquee-text').width()) { + indent = mar.width(); + } + }; + mar.data('interval',setInterval(mar.marquee,1000/60)); +}); + + +$( ".no_cover" ).each(function() { + var string = $(this).attr('id') + var randomColor = colorHash(string).rgb + + $(this).css({ + 'background-color' : randomColor, + }); + +} +) + +function colorHash(inputString){ +var sum = 0; + +for(var i in inputString){ +sum += inputString.charCodeAt(i); +} + +r = ~~(('0.'+Math.sin(sum+1).toString().substr(6))*256); +g = ~~(('0.'+Math.sin(sum+2).toString().substr(6))*256); +b = ~~(('0.'+Math.sin(sum+3).toString().substr(6))*256); + +var rgb = "rgb("+r+", "+g+", "+b+")"; + +var hex = "#"; + +hex += ("00" + r.toString(16)).substr(-2,2).toUpperCase(); +hex += ("00" + g.toString(18)).substr(-2,2).toUpperCase(); +hex += ("00" + b.toString(20)).substr(-2,2).toUpperCase(); + +return { + r: r +,g: g +,b: b +,rgb: rgb +,hex: hex +}; +} diff --git a/app/templates/base.html b/app/templates/base.html index 77e1d1e..5ac4d42 100755 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -18,6 +18,13 @@ {% block css %} {% endblock%} +
+
+
+ Testing this marquee function +
+
+
{% block header %}
{% include "header.html" %} @@ -35,92 +42,13 @@ {% block js %} {% endblock%} - - - + - - - - -$(elem).text(x[Math.floor(Math.random()*x.length)]+" "+p1[Math.floor(Math.random()*p1.length)]+" "+p2[Math.floor(Math.random()*p2.length)]+" "+l[Math.floor(Math.random()*l.length)]); -} diff --git a/app/templates/edit_book_detail.html b/app/templates/edit_book_detail.html index 3447d7a..c936ff4 100755 --- a/app/templates/edit_book_detail.html +++ b/app/templates/edit_book_detail.html @@ -11,7 +11,7 @@ {{ form.csrf_token }}

{{ form.title.label }} {{ form.title(size=20, class="form-control") }}

- +

@@ -30,10 +30,13 @@ {% endfor %} - +
{{ form.category.label }} {{ form.category(size=20, class="form-control") }} -
+
+
+ {{ form.year_published.label }} {{ form.year_published(size=4, class="form-control") }} +

diff --git a/app/templates/home.html b/app/templates/home.html index b6d2605..11cd5f1 100755 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -8,4 +8,5 @@ This might only be one interface to this library:

+ {% endblock %} diff --git a/app/views.py b/app/views.py index a77b309..bdcca70 100755 --- a/app/views.py +++ b/app/views.py @@ -110,7 +110,7 @@ def remove_book_by_id(id): @app.route('/books//edit', methods=['POST', 'GET']) def edit_book_by_id(id): book_to_edit = Book.query.filter_by(id=id).first() - user_form = EditForm(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category ) + user_form = EditForm(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category, year_published= book_to_edit.year_published) if request.method == 'POST': if user_form.validate_on_submit(): @@ -118,15 +118,18 @@ def edit_book_by_id(id): title = user_form.title.data # You could also have used request.form['name'] input_authors = user_form.author.data # You could also have used request.form['email'] category = user_form.category.data + year_published = user_form.year_published.data + if year_published=="": + year_published = None # save user to database #book = Book(title, author, filename, cover, file_extension) book = Book.query.filter_by(id=id).first() - book.title = title book.category = category + book.year_published = year_published - + #authors update book.authors.clear() for i, author in enumerate(input_authors): author_name = author.get("author_name") @@ -138,7 +141,7 @@ def edit_book_by_id(id): book.authors.append(a) db.session.commit() flash("%s updated" % (title)) - return redirect(url_for('show_books')) + return redirect(url_for('show_book_by_id', id=id)) return render_template('edit_book_detail.html', book=book_to_edit, form=user_form) @@ -148,12 +151,16 @@ def add_book(): upload_form = UploadForm() if request.method == 'POST': - title = upload_form.title.data - authors = upload_form.author.data - category = upload_form.category.data - year_published = upload_form.year_published.data - if upload_form.validate_on_submit(): + #get data from form + title = upload_form.title.data + authors = upload_form.author.data + category = upload_form.category.data + year_published = upload_form.year_published.data + if year_published=="": + year_published = None + + #if upload with file if upload_form.upload.data: # check if the post request has the file part if 'file' not in request.files: @@ -174,40 +181,29 @@ def add_book(): name, file_extension = os.path.splitext(new_filename) file.save(fullpath) cover = get_cover(fullpath, name) - book = Book(title, filename, cover, file_extension, category, year_published) - db.session.add(book) - for author in authors: - author_name = author.get("author_name") - if author_name: - a = db.session.query(Author).filter_by(author_name=author_name).first() - if a == None: - a = Author(author_name=author_name) - db.session.add(a) - book.authors.append(a) - db.session.commit() - # save user to database - - flash("%s added to the library" % (title)) - return redirect(url_for('show_books')) else: flash('allowed file formats: %s' % ALLOWED_EXTENSIONS) - + #if upload without file -> wishform, with potential PDF if upload_form.wish.data: - file = open('app/uploads/potential.pdf') - filename = 'potential.pdf' - file_extension = '.pdf' + #TO DO: make pdf generator + #file = open('app/uploads/potential.pdf') + #filename = 'potential.pdf' + #file_extension = '.pdf' + filename = '' + file_extension = '' cover = '' - book = Book(title, filename, cover, file_extension, category,year_published) - db.session.add(book) - for author in authors: - author_name = author.get("author_name") - if author_name: - a = db.session.query(Author).filter_by(author_name=author_name).first() - if a == None: - a = Author(author_name=author_name) - db.session.add(a) - book.authors.append(a) - db.session.commit() + + book = Book(title, filename, cover, file_extension, category,year_published) + db.session.add(book) + for author in authors: + author_name = author.get("author_name") + if author_name: + a = db.session.query(Author).filter_by(author_name=author_name).first() + if a == None: + a = Author(author_name=author_name) + db.session.add(a) + book.authors.append(a) + db.session.commit() flash("%s added to the library" % (title)) return redirect(url_for('show_books'))