From e0a5f9e0fd8f2a3fca99580ca058a0ece8f08ac9 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Sun, 18 Aug 2024 17:43:24 +0000 Subject: [PATCH] Initial Commit --- .github/workflows/ci.yml | 29 ++++ .gitignore | 176 +++++++++++++++++++++++ LICENSE | 19 +++ devbox.json | 9 ++ devbox.lock | 40 ++++++ fluentciflask/__init__.py | 17 +++ fluentciflask/forms.py | 8 ++ fluentciflask/routes.py | 88 ++++++++++++ fluentciflask/templates/404.html | 21 +++ fluentciflask/templates/500.html | 21 +++ fluentciflask/templates/base.html | 59 ++++++++ fluentciflask/templates/task.html | 51 +++++++ fluentciflask/templates/task_create.html | 34 +++++ fluentciflask/templates/tasks_all.html | 31 ++++ requirements.txt | 7 + run.py | 6 + test.py | 15 ++ 17 files changed, 631 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 devbox.json create mode 100644 devbox.lock create mode 100644 fluentciflask/__init__.py create mode 100644 fluentciflask/forms.py create mode 100644 fluentciflask/routes.py create mode 100644 fluentciflask/templates/404.html create mode 100644 fluentciflask/templates/500.html create mode 100644 fluentciflask/templates/base.html create mode 100644 fluentciflask/templates/task.html create mode 100644 fluentciflask/templates/task_create.html create mode 100644 fluentciflask/templates/tasks_all.html create mode 100644 requirements.txt create mode 100644 run.py create mode 100644 test.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..23be734 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: ci +on: + push: + branches: + - main +jobs: + tasks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Run Tests + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: mongo + args: | + start + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Run tests + run: | + fluentci run --wasm devbox run pip install -r requirements.txt + devbox run python run.py & + sleep 3 + fluentci run --wasm devbox run python -m unittest + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + DB: mongodb://localhost:27017/tasks + PORT: 5000 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90f8fbb --- /dev/null +++ b/.gitignore @@ -0,0 +1,176 @@ +# Created by https://www.toptal.com/developers/gitignore/api/python +# Edit at https://www.toptal.com/developers/gitignore?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +# End of https://www.toptal.com/developers/gitignore/api/python \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0b9d25a --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023 Tsiry Sandratraina + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/devbox.json b/devbox.json new file mode 100644 index 0000000..5b63cfb --- /dev/null +++ b/devbox.json @@ -0,0 +1,9 @@ +{ + "packages": [ + "python310", + "python310Packages.pip" + ], + "shell": { + "init_hook": ". $VENV_DIR/bin/activate" + } +} \ No newline at end of file diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 0000000..04c8197 --- /dev/null +++ b/devbox.lock @@ -0,0 +1,40 @@ +{ + "lockfile_version": "1", + "packages": { + "python310": { + "plugin_version": "0.0.3", + "resolved": "github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c#python310", + "source": "nixpkg", + "systems": { + "x86_64-linux": { + "outputs": [ + { + "path": "/nix/store/s6fgyqbk8vn1014daznm5kqx90xdn86x-python3-3.10.13", + "default": true + } + ] + } + } + }, + "python310Packages.pip": { + "plugin_version": "0.0.2", + "resolved": "github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c#python310Packages.pip", + "source": "nixpkg", + "systems": { + "x86_64-linux": { + "outputs": [ + { + "path": "/nix/store/96zrmllll78k3zf6zyxyyfzpcihf1svd-python3.10-pip-23.2.1", + "default": true + }, + { + "name": "man", + "path": "/nix/store/1y54g9y4l1cifs9f2lhkyz8xwjh26qgs-python3.10-pip-23.2.1-man", + "default": true + } + ] + } + } + } + } +} diff --git a/fluentciflask/__init__.py b/fluentciflask/__init__.py new file mode 100644 index 0000000..a8c630a --- /dev/null +++ b/fluentciflask/__init__.py @@ -0,0 +1,17 @@ +from flask import Flask, flash +from flask_pymongo import PyMongo +import os + +app = Flask('__name__', template_folder="./fluentciflask/templates/") +app.config['SECRET_KEY'] = 'b6b462cf486aa21eee5719bf931a792e' +app.config["MONGO_URI"] = os.environ.get('DB') or os.environ.get('MONGODB_URI') + +try: + mongo = PyMongo(app) +except mongo.errors.ServerSelectionTimeoutError as err: + flash(f'MongoDB server is not running', 'error') + +from fluentciflask import routes + +app.register_error_handler(404, routes.page_not_found) +app.register_error_handler(500, routes.internal_server_error) diff --git a/fluentciflask/forms.py b/fluentciflask/forms.py new file mode 100644 index 0000000..cf050ab --- /dev/null +++ b/fluentciflask/forms.py @@ -0,0 +1,8 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, TextAreaField, DateTimeField, SubmitField +from wtforms.validators import DataRequired + +class CreateTaskForm(FlaskForm): + task_title = StringField('Task Title', validators=[DataRequired()]) + task_description = TextAreaField('Task Description') + task_submit = SubmitField('Create Task') \ No newline at end of file diff --git a/fluentciflask/routes.py b/fluentciflask/routes.py new file mode 100644 index 0000000..1fa4864 --- /dev/null +++ b/fluentciflask/routes.py @@ -0,0 +1,88 @@ +from flask import render_template, flash, request, redirect, url_for, jsonify +import datetime +from bson.objectid import ObjectId +from fluentciflask import app, mongo +from fluentciflask.forms import CreateTaskForm + + +@app.route('/') +@app.route('/task/all') +def tasks_all(): + tasks_data = [] + if mongo: + tasks = mongo.db.tasks.find() + if tasks: + for task in tasks: + print(task) + tasks_data.append({'task_id': task['_id'], 'task_title': task['task_title'], 'task_description': task['task_description'], + 'task_created_at': task['task_created_at']}) + return render_template('tasks_all.html', tasks= tasks_data, title='All Tasks') + + +@app.route('/task/new', methods = ['GET', 'POST']) +def task_new(): + form = CreateTaskForm() + if form.validate_on_submit(): + task_title = form.task_title.data + task_description = form.task_description.data + task_created_at = datetime.datetime.now() + mongo.db.tasks.insert_one({'task_title': task_title, 'task_description': task_description, + 'task_created_at': task_created_at}) + flash(f'Task { form.task_title.data } has been created successfully!', 'success') + return redirect(url_for('tasks_all')) + return render_template('task_create.html', title='Create New Task', form=form, operation='create') + + +@app.route('/task/', methods = ['GET']) +def task_view(task_id): + if len(task_id) == 24: + task = mongo.db.tasks.find_one({'_id': ObjectId(task_id)}) + if task: + return render_template('task.html', task = [task], title='Task: {0}'.format(task['task_title'])) + else: + return render_template('404.html') + else: + return render_template('404.html') + + +@app.route('/task//update', methods = ['GET', 'POST']) +def task_update(task_id): + if len(task_id) == 24: + task_to_update = mongo.db.tasks.find_one({'_id': ObjectId(task_id)}) + if task_to_update: + form = CreateTaskForm() + if form.validate_on_submit(): + mongo.db.tasks.update_one({"_id": ObjectId(task_id)}, + { '$set' : {"task_title": form.task_title.data, + "task_description": form.task_description.data, + "task_created_at": datetime.datetime.now() + } + }, + upsert=False) + flash(f'Task has been updated', 'success') + return redirect(url_for('task_view', task_id=ObjectId(task_id))) + elif request.method == 'GET': + form.task_title.data = task_to_update['task_title'] + form.task_description.data = task_to_update['task_description'] + form.task_submit.data = 'Update Task' + return render_template('task_create.html', title='Update task: {0}'.format(task_to_update['task_title']), + form=form, operation='update') + else: + return render_template('404.html') + + +@app.route('/task//delete', methods = ['POST']) +def task_delete(task_id): + mongo.db.tasks.remove({"_id": ObjectId(task_id)}) + flash(f'Task has been deleted sucessfully', 'success') + return redirect(url_for('tasks_all')) + + +@app.errorhandler(404) +def page_not_found(e): + return render_template('404.html'), 404 + +@app.errorhandler(500) +def internal_server_error(e): + return render_template('500.html'), 500 + diff --git a/fluentciflask/templates/404.html b/fluentciflask/templates/404.html new file mode 100644 index 0000000..d76fac0 --- /dev/null +++ b/fluentciflask/templates/404.html @@ -0,0 +1,21 @@ +{% extends 'base.html' %} + +{% block content %} +
+
+
+

+ Oops!

+

+ 404 Not Found

+
+ Sorry, Requested page not found! +
+ +
+
+
+{% endblock content %} \ No newline at end of file diff --git a/fluentciflask/templates/500.html b/fluentciflask/templates/500.html new file mode 100644 index 0000000..1872c1d --- /dev/null +++ b/fluentciflask/templates/500.html @@ -0,0 +1,21 @@ +{% extends 'tasks/base.html' %} + +{% block content %} +
+
+
+

+ Oops!

+

+ 500 Server Error

+
+ Something went wrong, Please contact support team.. !! +
+ +
+
+
+{% endblock content %} \ No newline at end of file diff --git a/fluentciflask/templates/base.html b/fluentciflask/templates/base.html new file mode 100644 index 0000000..7a92585 --- /dev/null +++ b/fluentciflask/templates/base.html @@ -0,0 +1,59 @@ + + + + + + + + + + {% if title %} + {{ title }} - Python / Flask / MongoDB / Docker Semaphore Integration + {% else %} + Python / Flask / MongoDB / Docker Semaphore Integration + {% endif %} + + + + + + + +
+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + {% for category, message in messages %} +
+ {{ message }} +
+ {% endfor %} + {% endif %} + {% endwith %} + {% block content %}{% endblock %} +
+ + + + + + + \ No newline at end of file diff --git a/fluentciflask/templates/task.html b/fluentciflask/templates/task.html new file mode 100644 index 0000000..efac8f8 --- /dev/null +++ b/fluentciflask/templates/task.html @@ -0,0 +1,51 @@ +{% extends 'base.html' %} + +{% block content %} + {% if task %} + {% for data in task %} +
+
+
{{ data.task_title }}
+

{{ data.task_description }}

+

{{ data.task_created_at }}

+ Update Task + + Go to Task list +
+
+ + {% endfor %} + {% else %} +
+
+
+

+ Got Lucky!

+

+ No tasks Found

+ +
+
+
+ {% endif %} +{% endblock content%} \ No newline at end of file diff --git a/fluentciflask/templates/task_create.html b/fluentciflask/templates/task_create.html new file mode 100644 index 0000000..faae9c0 --- /dev/null +++ b/fluentciflask/templates/task_create.html @@ -0,0 +1,34 @@ +{% extends 'base.html' %} +{% block content %} + {% if operation == 'create' %} +

Create new task

+ {% else %} +

{{ title }}

+ {% endif %} +
+
+ {{ form.hidden_tag() }} +
+ +
+ {{ form.task_title(class="form-control") }} +
+
+
+ +
+ {{ form.task_description(class="form-control") }} +
+
+
+
+ {{ form.task_submit(class="btn btn-outline-info") }} +
+
+
+
+{% endblock content %} diff --git a/fluentciflask/templates/tasks_all.html b/fluentciflask/templates/tasks_all.html new file mode 100644 index 0000000..af53197 --- /dev/null +++ b/fluentciflask/templates/tasks_all.html @@ -0,0 +1,31 @@ +{% extends 'base.html' %} + +{% block content %} + {% if tasks %} +

All tasks

+ {% for task in tasks %} +
+
+
{{ task.task_title }}
+

{{ task.task_description }}

+ View Task +
+
+ {% endfor %} + {% else %} +
+
+
+

+ Got Lucky!

+

+ No tasks Found

+ +
+
+
+ {% endif %} +{% endblock content%} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e6ca26f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +WTForms==2.2.1 +Flask_WTF==0.14.2 +pymongo==3.7.2 +Flask_PyMongo==2.2.0 +Flask==1.0.2 +dnspython==1.16.0 +werkzeug==0.16.1 diff --git a/run.py b/run.py new file mode 100644 index 0000000..823475d --- /dev/null +++ b/run.py @@ -0,0 +1,6 @@ +from fluentciflask import app +import os + +if __name__ == '__main__': + port = os.getenv('PORT',5000) + app.run(debug=True, host='0.0.0.0', port=port) diff --git a/test.py b/test.py new file mode 100644 index 0000000..f16729e --- /dev/null +++ b/test.py @@ -0,0 +1,15 @@ +from fluentciflask import app +import unittest + +class TaskTest(unittest.TestCase): + + def test_homepage(self): + tester = app.test_client(self) + response = tester.get('/', content_type='html/text') + self.assertEqual(response.status_code, 200) + response = tester.get('/task/all', content_type='html/text') + self.assertEqual(response.status_code, 200) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file