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. 17
      installation-files/projects_example.yaml

23
biscd/biscd/__init__.py

@ -4,12 +4,14 @@ from logging.handlers import RotatingFileHandler @@ -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(): @@ -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(): @@ -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

4
biscd/biscd/config_default.yaml

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

7
biscd/biscd/froms.py

@ -0,0 +1,7 @@ @@ -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 @@ @@ -1,6 +1,5 @@
from pathlib import Path
import yaml
from flask import app
from biscd import config
class Project:
@ -10,16 +9,29 @@ class Project: @@ -10,16 +9,29 @@ class Project:
def __init__(self, name):
self.name = name
self.branche = 'master'
def __repr__(self):
return f"{self.__class__.__name__}(name={self.name})"
@property
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
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: @@ -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)

16
biscd/biscd/routes.py

@ -1,9 +1,15 @@ @@ -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)

7
biscd/biscd/templates/index.html

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

4
biscd/biscd/utils.py

@ -9,3 +9,7 @@ def get_remote_addr(): @@ -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

4
installation-files/config_example.yaml

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

17
installation-files/projects_example.yaml

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

Loading…
Cancel
Save