2 Commits

Author SHA1 Message Date
  Carlos Hernandez a555df819a Change GET request to server ipaddr 3 years ago
  Carlos Hernandez 5055e56314 Add popup on articles 3 years ago

+ 2
- 0
.gitignore View File

@@ -1,3 +1,5 @@
1
+.vscode
2
+
1 3
 # Byte-compiled / optimized / DLL files
2 4
 __pycache__/
3 5
 *.py[cod]

+ 2
- 0
Files-API/MANIFEST.in View File

@@ -0,0 +1,2 @@
1
+graft articles_api/articles_files
2
+global-exclude *.pyc

+ 0
- 1
Files-API/articles/article_4.txt View File

@@ -1 +0,0 @@
1
-Hello world!

+ 30
- 0
Files-API/articles_api/__init__.py View File

@@ -0,0 +1,30 @@
1
+import os
2
+
3
+from flask import Flask
4
+
5
+
6
+def create_app(test_config=None):
7
+    # create and configure the app
8
+    app = Flask(__name__)
9
+    app.config.from_mapping(
10
+        SECRET_KEY='dev')
11
+
12
+    if test_config is None:
13
+        # load the instance config, if it exists, when not testing
14
+        app.config.from_pyfile('config.py', silent=True)
15
+    else:
16
+        # load the test config if passed in
17
+        app.config.from_mapping(test_config)
18
+
19
+    # ensure the instance folder exists
20
+    try:
21
+        os.makedirs(app.instance_path)
22
+    except OSError:
23
+        pass
24
+
25
+    from articles_api.articles.articles import articles_api_bp
26
+
27
+    app.register_blueprint(articles_api_bp)
28
+    app.add_url_rule('/', endpoint="index")
29
+
30
+    return app

+ 29
- 0
Files-API/articles_api/articles/articles.py View File

@@ -0,0 +1,29 @@
1
+import os
2
+from flask import send_from_directory, jsonify, Blueprint, current_app
3
+from flask_cors import CORS, cross_origin
4
+
5
+
6
+articles_api_bp = Blueprint(
7
+    "articles", __name__, template_folder="templates")
8
+
9
+cors = CORS(articles_api_bp, resources={r"/articles": {"origins": "http://localhost:5500"}})
10
+
11
+
12
+@articles_api_bp.route("/<path:article>")
13
+@cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
14
+def send_text_file(article):
15
+    return send_from_directory(directory="articles_files", path=article)
16
+
17
+
18
+@articles_api_bp.route('/')
19
+@cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
20
+def index():
21
+    # List files in articles directory
22
+    files = list()
23
+    for filename in os.listdir(os.path.join(
24
+            current_app.root_path, "articles_files")):
25
+        path = os.path.join(os.path.join(
26
+            current_app.root_path, "articles_files"), filename)
27
+        if os.path.isfile(path):
28
+            files.append(filename)
29
+    return jsonify(files)

Files-API/articles/article_1.txt → Files-API/articles_api/articles_files/article_1.txt View File


Files-API/articles/article_2.txt → Files-API/articles_api/articles_files/article_2.txt View File


Files-API/articles/article_3.txt → Files-API/articles_api/articles_files/article_3.txt View File


+ 0
- 32
Files-API/files_api.py View File

@@ -1,32 +0,0 @@
1
-import os
2
-from flask import Flask, send_from_directory, jsonify
3
-from flask_cors import CORS, cross_origin
4
-
5
-
6
-app = Flask(__name__, static_url_path='')
7
-app.config["SECRET_KEY"] = "f82abccd93a28b6cdda4525c9afa7a30"
8
-app.config["CORS_HEADERS"] = "Content-Type"
9
-
10
-cors = CORS(app, resources={r"/articles": {"origins": "http://localhost:5500"}})
11
-
12
-
13
-@app.route("/articles/<path:article>")
14
-@cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
15
-def send_text_file(article):
16
-    return send_from_directory(directory="articles", path=article)
17
-
18
-
19
-@app.route("/articles")
20
-@cross_origin(origin="localhost", headers=["Content-Type", "Authorization"])
21
-def list_files():
22
-    files = list()
23
-    for filename in os.listdir("articles"):
24
-        path = os.path.join("articles", filename)
25
-        if os.path.isfile(path):
26
-            files.append(filename)
27
-    return jsonify(files)
28
-
29
-
30
-if __name__ == "__main__":
31
-    app.run()
32
-

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

@@ -1,3 +1,4 @@
1
+-e git+https://git.ccom.uprrp.edu/ccorrada/CarDieAleCa.git@5055e56314e136e6abfc24a48da9767b89da8ad8#egg=articles_api&subdirectory=Files-API
1 2
 backcall==0.2.0
2 3
 click==8.0.3
3 4
 decorator==5.1.0

+ 5
- 0
Files-API/run.sh View File

@@ -0,0 +1,5 @@
1
+#!/bin/sh
2
+
3
+export FLASK_APP=articles_api
4
+export FLASK_ENV=development
5
+flask run

+ 14
- 0
Files-API/setup.py View File

@@ -0,0 +1,14 @@
1
+from setuptools import find_packages, setup
2
+
3
+setup(
4
+    name="articles_api",
5
+    version="1.0.0",
6
+    packages=["articles_api",
7
+              "articles_api.articles"],
8
+    include_package_data=True,
9
+    zip_safe=False,
10
+    install_requires=[
11
+        "flask",
12
+        "flask_cors"
13
+    ]
14
+)

+ 1
- 22
app_template/www/articles.html View File

@@ -53,28 +53,7 @@
53 53
 		<script src="sortposts.js"></script>
54 54
 
55 55
 		<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
56
-		<script>
57
-			var articles;
58
-			axios.get(`http://localhost:5000/articles`)
59
-					.then(response => {
60
-						articles = response.data;
61
-
62
-						for (element of articles) {
63
-							axios.get(`http://localhost:5000/articles/${element}`)
64
-								.then(response => {
65
-									let articlesHeading = document.querySelector("#articles-heading").parentElement;
66
-									let articleDiv = document.createElement("button");
67
-									articleDiv.textContent = response.data;
68
-
69
-									articleDiv.classList.add("m-5", "article-content");
70
-									articlesHeading.appendChild(articleDiv);
71
-								})
72
-								.catch(error => console.error(error));
73
-						}
74
-					})
75
-					.catch(error => console.error(error));
76
-
77
-		</script>
56
+		<script src="fetch_articles.js"></script>
78 57
 	</head>
79 58
 	<body>
80 59
 	<header role="banner" id="fh5co-header">

+ 12
- 0
app_template/www/css/main.css View File

@@ -40,3 +40,15 @@ button.article-content {
40 40
     top: 0;
41 41
     background: linear-gradient(transparent 150px, white);
42 42
 }
43
+
44
+.modal-content {
45
+    background-color: rgb(75, 75, 75) !important;
46
+}
47
+
48
+.modal button.btn.btn-secondary {
49
+    background-color: rgb(59, 59, 59) !important;
50
+}
51
+
52
+.modal button.close {
53
+    color: rgb(255, 0, 0) !important;
54
+}

+ 46
- 0
app_template/www/fetch_articles.js View File

@@ -0,0 +1,46 @@
1
+var articles;
2
+axios.get("136.145.231.32:8080")
3
+    .then(response => {
4
+        articles = response.data;
5
+        for (let i = 0; i < articles.length; i++) {
6
+            let element = articles[i];
7
+            axios.get(`136.145.231.32:8080/${element}`)
8
+                .then(response => {
9
+                    let articlesHeading = document.querySelector("#articles-heading").parentElement;
10
+                    let articleButton = document.createElement("button");
11
+                    articleButton.textContent = response.data;
12
+
13
+                    articleButton.classList.add("m-5", "article-content");
14
+                    articleButton.setAttribute("data-toggle", "modal");
15
+                    articleButton.setAttribute("data-target", `#articleModal${i}`);
16
+
17
+                    let modal = document.createElement("div");
18
+                    modal.classList.add("modal", "fade");
19
+                    modal.setAttribute("id", `articleModal${i}`);
20
+                    modal.setAttribute("tabindex", "-1");
21
+                    modal.setAttribute("aria-labelledby", `articleModalLabel${i}`);
22
+                    modal.setAttribute("aria-hidden", "true");
23
+                    modal.innerHTML = `
24
+                        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable modal-lg">
25
+                            <div class="modal-content">
26
+                                <div class="modal-header">
27
+                                    <h5 class="modal-title" id="articleModalLabel${i}">${element}</h5>
28
+                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
29
+                                        <span aria-hidden="true">&times;</span>
30
+                                    </button>
31
+                                </div>
32
+                                <div class="modal-body">
33
+                                    ${response.data}
34
+                                </div>
35
+                                <div class="modal-footer">
36
+                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
37
+                                </div>
38
+                            </div>
39
+                        </div>`;
40
+                    document.body.appendChild(modal);
41
+                    articlesHeading.appendChild(articleButton);
42
+                })
43
+                .catch(error => console.error(error));
44
+        }
45
+    })
46
+    .catch(error => console.error(error));