moved all javascript to seperate app.js file

This commit is contained in:
Alex 2018-06-01 21:37:45 +02:00
parent b24d05e77d
commit 121179cc39
7 changed files with 168 additions and 125 deletions

View File

@ -15,7 +15,7 @@ class UploadForm(FlaskForm):
title = StringField('title', validators=[InputRequired()]) title = StringField('title', validators=[InputRequired()])
author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1) author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1)
category = StringField('category', validators=[InputRequired()]) 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() file = FileField()
upload = SubmitField(label='Upload') upload = SubmitField(label='Upload')
wish = SubmitField(label='''I don't have the file, but wish I did.''') 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()]) title = StringField('title', validators=[InputRequired()])
author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1) author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1)
category = StringField('category', validators=[InputRequired()]) category = StringField('category', validators=[InputRequired()])
year_published = StringField('year published', [validators.Length(max=4)],default=None)
class SearchForm(FlaskForm): class SearchForm(FlaskForm):
choices = [('Title', 'Title'), choices = [('Title', 'Title'),

View File

@ -5,6 +5,7 @@ font-family: Helvetica;
.navigation ul{ .navigation ul{
list-style-type: none; list-style-type: none;
margin: 0; margin: 0;
margin-top: 30px;
padding: 0; padding: 0;
} }
@ -129,3 +130,25 @@ font-family:'Courier New';
.ui-tabs-vertical .ui-tabs-panel { padding: 1em; float: right; width: 40em;} .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; } #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; } #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;
}

View File

@ -30,3 +30,93 @@ $(function() {
}); //End remove row }); //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
};
}

View File

@ -18,6 +18,13 @@
{% block css %} {% endblock%} {% block css %} {% endblock%}
</head> </head>
<body> <body>
<div id="newstext">
<div class='marquee'>
<div class='marquee-text'>
Testing this marquee function
</div>
</div>
</div>
{% block header %} {% block header %}
<header> <header>
{% include "header.html" %} {% include "header.html" %}
@ -35,92 +42,13 @@
</footer> </footer>
{% block js %} {% endblock%} {% block js %} {% endblock%}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script> <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="{{ url_for("static", filename="js/app.js") }}"></script> <script src="{{ url_for("static", filename="js/app.js") }}"></script>
<script>
$( ".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
};
}
</script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#tabs" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
$( "#tabs li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );
} );
</script>
<script>
$( 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)]);
}
</script> </script>
</body> </body>
</html> </html>

View File

@ -30,10 +30,13 @@
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>
</div> </div><br>
<div class="form-group"> <div class="form-group">
{{ form.category.label }} {{ form.category(size=20, class="form-control") }} {{ form.category.label }} {{ form.category(size=20, class="form-control") }}
</div> </div><br>
<div class="form-group">
{{ form.year_published.label }} {{ form.year_published(size=4, class="form-control") }}
</div>
<br> <br>
<button type="submit" class="btn btn-primary">Update</button> <button type="submit" class="btn btn-primary">Update</button>
</form> </form>

View File

@ -8,4 +8,5 @@ This might only be one interface to this library:
</p> </p>
{% endblock %} {% endblock %}

View File

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