From 28b4cea3cf29fdef9bc926f8ee3a3a4bd74508d2 Mon Sep 17 00:00:00 2001 From: Burathar Date: Sun, 7 Mar 2021 19:00:02 +0100 Subject: [PATCH] Add create project functionality --- biscd/biscd/__init__.py | 23 +++++++++++-------- biscd/biscd/config_default.yaml | 4 ++-- biscd/biscd/froms.py | 7 ++++++ biscd/biscd/models/project.py | 28 +++++++++++++++++++---- biscd/biscd/routes.py | 16 +++++++++---- biscd/biscd/templates/index.html | 7 ++++++ biscd/biscd/utils.py | 4 ++++ installation-files/config_example.yaml | 6 +++-- installation-files/projects_example.yaml | 29 ++++++++++++------------ 9 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 biscd/biscd/froms.py diff --git a/biscd/biscd/__init__.py b/biscd/biscd/__init__.py index 82c736f..26c4fb7 100644 --- a/biscd/biscd/__init__.py +++ b/biscd/biscd/__init__.py @@ -4,12 +4,14 @@ from logging.handlers import RotatingFileHandler from pathlib import Path import confuse +from .utils import ObjectView + from flask import Flask from flask_bootstrap import Bootstrap - -def read_config(): +def read_config(application): + # pylint: disable=no-member conf = confuse.Configuration('biscd', __name__) try: conf['logging'].get() @@ -17,14 +19,16 @@ def read_config(): raise ValueError("Failed to open any configfile containing a 'logging' object. " \ "Please make sure the module directory contains a config_default.yaml, " \ f"or /etc/{__name__}/config.yaml exists") from not_found + conf_object = ObjectView(conf.get()) + application.config.from_object(conf_object) return conf -def setup_logging(): +def setup_logging(application): # pylint: disable=no-member - if config['logging']['log_to_sdout'].get(bool): + if config['logging']['log_to_stdout'].get(bool): stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.INFO) - app.logger.addHandler(stream_handler) + application.logger.addHandler(stream_handler) else: logfile = Path(config['logging']['logfile'].as_filename()) if not logfile.parent.exists(): @@ -34,15 +38,14 @@ def setup_logging(): '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]')) file_handler.setLevel(logging.INFO) - app.logger.addHandler(file_handler) + application.logger.addHandler(file_handler) - app.logger.setLevel(logging.INFO) - app.logger.info('Biscd startup') + application.logger.info('Biscd startup') app = Flask(__name__) -config = read_config() +config = read_config(app) bootstrap = Bootstrap(app) -setup_logging() +setup_logging(app) from biscd import routes, errors, models diff --git a/biscd/biscd/config_default.yaml b/biscd/biscd/config_default.yaml index e792be4..19ddb7b 100644 --- a/biscd/biscd/config_default.yaml +++ b/biscd/biscd/config_default.yaml @@ -6,7 +6,7 @@ flask_development_server: port: 5000 logging: - log_to_sdout: true + log_to_stdout: true logfile: info.log -message: Hoi \ No newline at end of file +SECRET_KEY: this-should-be-very-secret diff --git a/biscd/biscd/froms.py b/biscd/biscd/froms.py new file mode 100644 index 0000000..79ae3e7 --- /dev/null +++ b/biscd/biscd/froms.py @@ -0,0 +1,7 @@ +from flask_wtf import FlaskForm +from wtforms import StringField, SubmitField +from wtforms.validators import DataRequired + +class NewProjectForm(FlaskForm): + projectname = StringField('Project Name', validators=[DataRequired()]) + submit = SubmitField('Add Project') diff --git a/biscd/biscd/models/project.py b/biscd/biscd/models/project.py index 6597d2c..a57fed2 100644 --- a/biscd/biscd/models/project.py +++ b/biscd/biscd/models/project.py @@ -1,25 +1,37 @@ from pathlib import Path import yaml -from flask import app from biscd import config class Project: _storage_file = Path(config.config_dir()) / 'projects.yaml' - + storage = {} def __init__(self, name): self.name = name + self.branche = 'master' + + @property + def config_dict(self): + dictionary = self.__dict__.copy() + dictionary.pop('name') + return {self.name: dictionary} - def __repr__(self): - return f"{self.__class__.__name__}(name={self.name})" + def save(self): + projects = self._get_projects_from_file() + if self.name in ([*project][0] for project in projects): + projects[self.name] = self.config_dict + else: + projects.append(self.config_dict) + print(projects) + self._save_projects_to_file(projects) @classmethod def get(cls, name): project = cls._get_projects_from_file().get(name) if project is None: return None - return Project(project.get('name')) + return cls(project.get('name')) @classmethod def list(cls): @@ -35,3 +47,9 @@ class Project: with open(cls._storage_file) as file: projects = yaml.load(file, yaml.FullLoader).get('projects') return projects + + @classmethod + def _save_projects_to_file(cls, projects): + projects_object = {'projects' : projects} + with open(cls._storage_file, 'w') as file: + yaml.dump(projects_object, file) diff --git a/biscd/biscd/routes.py b/biscd/biscd/routes.py index 25594d2..ad42b3b 100644 --- a/biscd/biscd/routes.py +++ b/biscd/biscd/routes.py @@ -1,9 +1,15 @@ -from flask import render_template +from flask import render_template, flash from biscd import app -from biscd.models.project import Project +from .models.project import Project +from .froms import NewProjectForm -@app.route('/', methods=['GET']) -@app.route('/index', methods=['GET']) +@app.route('/', methods=['GET', 'POST']) +@app.route('/index', methods=['GET', 'POST']) def index(): + form = NewProjectForm() + if form.validate_on_submit(): + project = Project(form.projectname.data) + project.save() + flash('You added a project!') project_names = Project.list() - return render_template('index.html', projects=project_names) + return render_template('index.html', form=form, projects=project_names) diff --git a/biscd/biscd/templates/index.html b/biscd/biscd/templates/index.html index 0725fda..f9596b3 100644 --- a/biscd/biscd/templates/index.html +++ b/biscd/biscd/templates/index.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{% import 'bootstrap/wtf.html' as wtf %} {% block app_content %}

Biscd, what else would you like with your Gitea?

@@ -13,4 +14,10 @@ + +
+
+ {{ wtf.quick_form(form) }} +
+
{% endblock %} diff --git a/biscd/biscd/utils.py b/biscd/biscd/utils.py index c96aab8..ea33ab5 100644 --- a/biscd/biscd/utils.py +++ b/biscd/biscd/utils.py @@ -9,3 +9,7 @@ def get_remote_addr(): #print(f"HTTP_X_REAL_IP: {request.environ.get('HTTP_X_REAL_IP')}") #print(f"HTTP_X_FORWARDED_FOR: {request.environ.get('HTTP_X_FORWARDED_FOR')}") return request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr) + +class ObjectView(object): + def __init__(self, d): + self.__dict__ = d diff --git a/installation-files/config_example.yaml b/installation-files/config_example.yaml index 081a894..3bd3778 100644 --- a/installation-files/config_example.yaml +++ b/installation-files/config_example.yaml @@ -6,7 +6,9 @@ flask_development_server: port: 5000 logging: - log_to_sdout: true + log_to_stdout: true logfile: info.log -message: Hoi \ No newline at end of file +message: Hoi + +SECRET_KEY: this-should-be-very-secret diff --git a/installation-files/projects_example.yaml b/installation-files/projects_example.yaml index 065e1ad..3089692 100644 --- a/installation-files/projects_example.yaml +++ b/installation-files/projects_example.yaml @@ -1,16 +1,15 @@ projects: - - The Hunt: - url: thehunt - git_repo: https://git.sciuro.org/Burathar/The-Hunt - branch: master - secret: thisissecret - requirements-file: requirements.txt - tests: tests.py - - - Foo: - url: bar - git_repo: https://git.sciuro.org/Burathar/The-Hunt - branch: master - secret: thisissecret - requirements-file: requirements.txt - tests: tests.py \ No newline at end of file +- The Hunt: + branch: master + git_repo: https://git.sciuro.org/Burathar/The-Hunt + requirements-file: requirements.txt + secret: thisissecret + tests: tests.py + url: thehunt +- Foo: + branch: master + git_repo: https://git.sciuro.org/Burathar/The-Hunt + requirements-file: requirements.txt + secret: thisissecret + tests: tests.py + url: bar