Browse Source

Add create project functionality

master
Burathar 4 years ago
parent
commit
28b4cea3cf
  1. 23
      biscd/biscd/__init__.py
  2. 4
      biscd/biscd/config_default.yaml
  3. 7
      biscd/biscd/froms.py
  4. 26
      biscd/biscd/models/project.py
  5. 16
      biscd/biscd/routes.py
  6. 7
      biscd/biscd/templates/index.html
  7. 4
      biscd/biscd/utils.py
  8. 4
      installation-files/config_example.yaml
  9. 13
      installation-files/projects_example.yaml

23
biscd/biscd/__init__.py

@ -4,12 +4,14 @@ from logging.handlers import RotatingFileHandler
from pathlib import Path from pathlib import Path
import confuse import confuse
from .utils import ObjectView
from flask import Flask from flask import Flask
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
def read_config(application):
def read_config(): # pylint: disable=no-member
conf = confuse.Configuration('biscd', __name__) conf = confuse.Configuration('biscd', __name__)
try: try:
conf['logging'].get() conf['logging'].get()
@ -17,14 +19,16 @@ def read_config():
raise ValueError("Failed to open any configfile containing a 'logging' object. " \ raise ValueError("Failed to open any configfile containing a 'logging' object. " \
"Please make sure the module directory contains a config_default.yaml, " \ "Please make sure the module directory contains a config_default.yaml, " \
f"or /etc/{__name__}/config.yaml exists") from not_found f"or /etc/{__name__}/config.yaml exists") from not_found
conf_object = ObjectView(conf.get())
application.config.from_object(conf_object)
return conf return conf
def setup_logging(): def setup_logging(application):
# pylint: disable=no-member # 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 = logging.StreamHandler()
stream_handler.setLevel(logging.INFO) stream_handler.setLevel(logging.INFO)
app.logger.addHandler(stream_handler) application.logger.addHandler(stream_handler)
else: else:
logfile = Path(config['logging']['logfile'].as_filename()) logfile = Path(config['logging']['logfile'].as_filename())
if not logfile.parent.exists(): if not logfile.parent.exists():
@ -34,15 +38,14 @@ def setup_logging():
'%(asctime)s %(levelname)s: %(message)s ' '%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]')) '[in %(pathname)s:%(lineno)d]'))
file_handler.setLevel(logging.INFO) file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler) application.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO) application.logger.info('Biscd startup')
app.logger.info('Biscd startup')
app = Flask(__name__) app = Flask(__name__)
config = read_config() config = read_config(app)
bootstrap = Bootstrap(app) bootstrap = Bootstrap(app)
setup_logging() setup_logging(app)
from biscd import routes, errors, models from biscd import routes, errors, models

4
biscd/biscd/config_default.yaml

@ -6,7 +6,7 @@ flask_development_server:
port: 5000 port: 5000
logging: logging:
log_to_sdout: true log_to_stdout: true
logfile: info.log logfile: info.log
message: Hoi SECRET_KEY: this-should-be-very-secret

7
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')

26
biscd/biscd/models/project.py

@ -1,6 +1,5 @@
from pathlib import Path from pathlib import Path
import yaml import yaml
from flask import app
from biscd import config from biscd import config
class Project: class Project:
@ -10,16 +9,29 @@ class Project:
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.branche = 'master'
def __repr__(self): @property
return f"{self.__class__.__name__}(name={self.name})" def config_dict(self):
dictionary = self.__dict__.copy()
dictionary.pop('name')
return {self.name: dictionary}
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 @classmethod
def get(cls, name): def get(cls, name):
project = cls._get_projects_from_file().get(name) project = cls._get_projects_from_file().get(name)
if project is None: if project is None:
return None return None
return Project(project.get('name')) return cls(project.get('name'))
@classmethod @classmethod
def list(cls): def list(cls):
@ -35,3 +47,9 @@ class Project:
with open(cls._storage_file) as file: with open(cls._storage_file) as file:
projects = yaml.load(file, yaml.FullLoader).get('projects') projects = yaml.load(file, yaml.FullLoader).get('projects')
return 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)

16
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 import app
from biscd.models.project import Project from .models.project import Project
from .froms import NewProjectForm
@app.route('/', methods=['GET']) @app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET']) @app.route('/index', methods=['GET', 'POST'])
def index(): 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() project_names = Project.list()
return render_template('index.html', projects=project_names) return render_template('index.html', form=form, projects=project_names)

7
biscd/biscd/templates/index.html

@ -1,4 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %} {% block app_content %}
<h1>Biscd, what else would you like with your Gitea?</h1> <h1>Biscd, what else would you like with your Gitea?</h1>
@ -13,4 +14,10 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<div class="row">
<div class="col-md-4">
{{ wtf.quick_form(form) }}
</div>
</div>
{% endblock %} {% endblock %}

4
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_REAL_IP: {request.environ.get('HTTP_X_REAL_IP')}")
#print(f"HTTP_X_FORWARDED_FOR: {request.environ.get('HTTP_X_FORWARDED_FOR')}") #print(f"HTTP_X_FORWARDED_FOR: {request.environ.get('HTTP_X_FORWARDED_FOR')}")
return request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr) return request.environ.get('HTTP_X_FORWARDED_FOR', request.remote_addr)
class ObjectView(object):
def __init__(self, d):
self.__dict__ = d

4
installation-files/config_example.yaml

@ -6,7 +6,9 @@ flask_development_server:
port: 5000 port: 5000
logging: logging:
log_to_sdout: true log_to_stdout: true
logfile: info.log logfile: info.log
message: Hoi message: Hoi
SECRET_KEY: this-should-be-very-secret

13
installation-files/projects_example.yaml

@ -1,16 +1,15 @@
projects: projects:
- The Hunt: - The Hunt:
url: thehunt
git_repo: https://git.sciuro.org/Burathar/The-Hunt
branch: master branch: master
secret: thisissecret git_repo: https://git.sciuro.org/Burathar/The-Hunt
requirements-file: requirements.txt requirements-file: requirements.txt
secret: thisissecret
tests: tests.py tests: tests.py
url: thehunt
- Foo: - Foo:
url: bar
git_repo: https://git.sciuro.org/Burathar/The-Hunt
branch: master branch: master
secret: thisissecret git_repo: https://git.sciuro.org/Burathar/The-Hunt
requirements-file: requirements.txt requirements-file: requirements.txt
secret: thisissecret
tests: tests.py tests: tests.py
url: bar

Loading…
Cancel
Save