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 %}