2 Commits

Author SHA1 Message Date
  Carlos Hernandez fc757db86f Add upload article endpoint 3 years ago
  Carlos Hernandez 6dc22836b5 Fix articles request bug 3 years ago

+ 2
- 1
Files-API/articles_api/__init__.py View File

@@ -7,7 +7,8 @@ def create_app(test_config=None):
7 7
     # create and configure the app
8 8
     app = Flask(__name__)
9 9
     app.config.from_mapping(
10
-        SECRET_KEY='dev')
10
+        SECRET_KEY='dev', UPLOAD_FOLDER=os.path.join(
11
+            app.root_path, "articles_files"), ALLOWED_EXTENSIONS={"txt"})
11 12
 
12 13
     if test_config is None:
13 14
         # load the instance config, if it exists, when not testing

+ 31
- 3
Files-API/articles_api/articles/articles.py View File

@@ -1,12 +1,15 @@
1 1
 import os
2
-from flask import send_from_directory, jsonify, Blueprint, current_app
3
-from flask_cors import CORS, cross_origin
4 2
 
3
+from flask import (Blueprint, current_app, flash, jsonify, redirect, request,
4
+                   send_from_directory, url_for)
5
+from flask_cors import CORS, cross_origin
6
+from werkzeug.utils import secure_filename
5 7
 
6 8
 articles_api_bp = Blueprint(
7 9
     "articles", __name__, template_folder="templates")
8 10
 
9
-cors = CORS(articles_api_bp, resources={r"/articles": {"origins": "http://localhost:5500"}})
11
+cors = CORS(articles_api_bp, resources={
12
+            r"/articles": {"origins": "http://localhost:5500"}})
10 13
 
11 14
 
12 15
 @articles_api_bp.route("/<path:article>")
@@ -27,3 +30,28 @@ def index():
27 30
         if os.path.isfile(path):
28 31
             files.append(filename)
29 32
     return jsonify(files)
33
+
34
+
35
+@articles_api_bp.route("/upload", methods=("POST",))
36
+@cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
37
+def upload_file():
38
+    if request.method == "POST":
39
+        if "file" not in request.files:
40
+            flash("No file part")
41
+            return redirect(request.url)
42
+        file = request.files["file"]
43
+        if file.filename == '':
44
+            flash("No selected file")
45
+            return redirect(request.url)
46
+        if file and allowed_file(file.filename):
47
+            filename = secure_filename(file.filename)
48
+            file.save(os.path.join(
49
+                current_app.config["UPLOAD_FOLDER"], filename))
50
+            return redirect(url_for("articles.send_text_file",
51
+                                    article=filename))
52
+
53
+
54
+def allowed_file(filename):
55
+    return '.' in filename and \
56
+           filename.rsplit('.', 1)[1].lower(
57
+           ) in current_app.config["ALLOWED_EXTENSIONS"]

+ 15
- 0
Files-API/articles_api/articles/templates/upload_article.html View File

@@ -0,0 +1,15 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<head>
4
+  <meta charset="UTF-8">
5
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+  <title>Upload Article</title>
8
+</head>
9
+<body>
10
+  <form method="post" enctype="multipart/form-data">
11
+    <input type="file" name="file">
12
+    <input type="submit" value="Upload">
13
+  </form>
14
+</body>
15
+</html>

+ 3
- 1
Files-API/requirements.txt View File

@@ -1,4 +1,5 @@
1
--e git+https://git.ccom.uprrp.edu/ccorrada/CarDieAleCa.git@5055e56314e136e6abfc24a48da9767b89da8ad8#egg=articles_api&subdirectory=Files-API
1
+-e git+https://git.ccom.uprrp.edu/ccorrada/CarDieAleCa.git@a555df819a9a4c82e58f1e2e4e0081fffbcb674f#egg=articles_api&subdirectory=Files-API
2
+autopep8==1.6.0
2 3
 backcall==0.2.0
3 4
 click==8.0.3
4 5
 decorator==5.1.0
@@ -22,6 +23,7 @@ pycodestyle==2.8.0
22 23
 pyflakes==2.4.0
23 24
 Pygments==2.10.0
24 25
 six==1.16.0
26
+toml==0.10.2
25 27
 traitlets==5.1.1
26 28
 wcwidth==0.2.5
27 29
 Werkzeug==2.0.2

+ 2
- 2
app_template/www/fetch_articles.js View File

@@ -1,10 +1,10 @@
1 1
 var articles;
2
-axios.get("136.145.231.32:8080")
2
+axios.get("http://136.145.231.32:8080/")
3 3
     .then(response => {
4 4
         articles = response.data;
5 5
         for (let i = 0; i < articles.length; i++) {
6 6
             let element = articles[i];
7
-            axios.get(`136.145.231.32:8080/${element}`)
7
+            axios.get(`http://136.145.231.32:8080/${element}`)
8 8
                 .then(response => {
9 9
                     let articlesHeading = document.querySelector("#articles-heading").parentElement;
10 10
                     let articleButton = document.createElement("button");