Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ def create_bibtex():


@app.route("/import_citations_bibtex", methods=["POST"])
def import_from_bibtex():
bibtex = request.form.get("input_bibtex")
def import_from_bibtex(bibtex_input=None):
if bibtex_input is None:
bibtex = request.form.get("input_bibtex")
else:
bibtex = bibtex_input
try:
validate_bibtex(bibtex)
except Exception as error:
Expand All @@ -138,7 +141,26 @@ def import_from_bibtex():
return redirect("/")


# testausta varten olevat reitit

@app.route('/upload', methods=['POST'])
def upload_bibtex_file():
if 'file' not in request.files:
flash("No file given")

file = request.files['file']
if file.filename == '':
flash("No selected file")
elif file:
content = file.read()
content = content.decode('utf-8')
import_from_bibtex(content)
return redirect("/")




# testausta varten oleva reitti

if test_env:
@app.route("/reset_db")
def reset_database():
Expand Down
103 changes: 84 additions & 19 deletions src/templates/import_bibtex.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,79 @@
<div class="modal fade" id="bibtex_importing_modal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Import from BibTeX</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="import-bibtex-list" class="border rounded p-3">
<form action="/import_citations_bibtex" method="post">
<textarea rows="15" class="form-control" id="input_bibtex" name="input_bibtex"
placeholder="@inproceedings{Example1,
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Import from BibTeX</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="container">
<div class="file-drop-area border p-3">
<form action="/upload" method="post" enctype="multipart/form-data">
<input class="file-input" type="file" accept=".bib" name="file" style="display:none;">
<button type="button" class="btn btn-primary fake-btn">Choose files</button>
<span class="file-msg">or drag and drop files here</span>
<br>
<input type="submit" value="Upload" class="btn btn-success mt-2">
</form>
</div>
</div>

<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function() {
var $fileInput = $('.file-input');
var $dropArea = $('.file-drop-area');
var $fileMsg = $('.file-msg');

$('.fake-btn').click(function() {
$fileInput.click();
});

$fileInput.on('change', function() {
var file = this.files[0];
var fileName = file.name;
var fileExtension = fileName.split('.').pop().toLowerCase();

if (fileExtension !== "bib") {
alert("Only .bib files are allowed!");
this.value = ''; // Clear the input
} else {
$fileMsg.text(fileName);
}
});

// Drag and Drop functionality with validation
$dropArea.on('dragover', function(e) {
e.preventDefault();
$(this).addClass('active');
});

$dropArea.on('dragleave', function(e) {
e.preventDefault();
$(this).removeClass('active');
});

$dropArea.on('drop', function(e) {
e.preventDefault();
$(this).removeClass('active');
var files = e.originalEvent.dataTransfer.files;
var file = files[0];
var fileName = file.name;
var fileExtension = fileName.split('.').pop().toLowerCase();

if (fileExtension !== "bib") {
alert("Only .bib files are allowed!");
} else {
$fileInput.prop('files', files);
$fileMsg.text(fileName);
}
});
});
</script>
<div id="import-bibtex-list" class="border rounded p-3">
<form action="/import_citations_bibtex" method="post">
<textarea rows="15" class="form-control" id="input_bibtex" name="input_bibtex" placeholder="@inproceedings{Example1,
author = {Author1, Author2},
title = {Title1},
year = {Year1},
Expand All @@ -24,15 +88,16 @@ <h5 class="modal-title">Import from BibTeX</h5>
volume = {1},
pages = {11--22}
}"></textarea>
<div id="input_bibtex" class="form-text">
Input citations in BibTeX form as shown in example. Input should start with "@" and end with "}"
<div id="input_bibtex" class="form-text">
Input citations in BibTeX form as shown in example. Input should start with "@" and end with "}"
</div>
</div>
</div>
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-primary" name="import">Import</button>
</form>
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-primary" name="import">Import</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

Loading