diff --git a/biscd/biscd/models/__init__.py b/biscd/biscd/models/__init__.py index 780340d..bfaad45 100644 --- a/biscd/biscd/models/__init__.py +++ b/biscd/biscd/models/__init__.py @@ -1,3 +1,4 @@ from .project import Project from .user import User from .yaml_serializable import YamlSerializable +from .utils import * diff --git a/biscd/biscd/models/project.py b/biscd/biscd/models/project.py index a9b95e1..ceed545 100644 --- a/biscd/biscd/models/project.py +++ b/biscd/biscd/models/project.py @@ -8,6 +8,7 @@ from flask import current_app as app from biscd import config from .yaml_serializable import YamlSerializable +from .utils import to_bool class Project(YamlSerializable): @@ -27,6 +28,7 @@ class Project(YamlSerializable): self.branch = branch self.relative_production_path = None self.git_repo = None + self.access = {'public': False, 'owners': []} def __eq__(self, other): """Overrides the default implementation""" @@ -101,3 +103,14 @@ class Project(YamlSerializable): full_path.mkdir(parents=True) self.relative_production_path = str(full_path.relative_to(prod_path_dir)) self.save() + + def user_access(self, user): + # pylint: disable=no-member + if not hasattr(self, 'access') or self.access is None: + return None + if hasattr(user,('name')) and user.name in self.access.get('owners', []): + return 'Owner' + public = to_bool(self.access.get('public', False)) + if public: + return 'Public' + return None diff --git a/biscd/biscd/models/utils.py b/biscd/biscd/models/utils.py new file mode 100644 index 0000000..32ee595 --- /dev/null +++ b/biscd/biscd/models/utils.py @@ -0,0 +1,6 @@ +def to_bool(value): + if isinstance(value, bool): + return value + if isinstance(value, int): + return value >= 1 + return value.lower() in ['true', '1', 't', 'y', 'yes'] diff --git a/biscd/biscd/models/yaml_serializable.py b/biscd/biscd/models/yaml_serializable.py index 75072d1..4494706 100644 --- a/biscd/biscd/models/yaml_serializable.py +++ b/biscd/biscd/models/yaml_serializable.py @@ -1,6 +1,7 @@ from abc import ABCMeta, abstractmethod from flask import abort import yaml +import inspect from .recursive_property import RecursiveProperty diff --git a/biscd/biscd/routes.py b/biscd/biscd/routes.py index c39e8f4..5deb937 100644 --- a/biscd/biscd/routes.py +++ b/biscd/biscd/routes.py @@ -4,7 +4,7 @@ from werkzeug.urls import url_parse from biscd import app from .utils import flash_result from .models import Project, User -from .froms import NewProjectForm, LoginForm, RegistrationForm, EmptyForm +from .froms import NewProjectForm, LoginForm, RegistrationForm @app.route('/', methods=['GET', 'POST']) @app.route('/index', methods=['GET', 'POST']) @@ -47,16 +47,34 @@ def register(): user = User(name=form.username.data, email=form.email.data) user.set_password(form.password.data) user.save() - flash('Congratulations, you are now a registered user!', 'info') + flash('Congratulations, you are now a registered user!', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form) +@app.route('/project/public', methods=['GET']) +def public_projects(): + projects = Project.get(access__public = True) + return render_template('projects.html', projects=projects) -@app.route('/project/', methods=['GET']) +@app.route('/project/add', methods=['GET', 'POST']) @login_required +def project_add(): + form = NewProjectForm() + if form.validate_on_submit(): + project = Project(name=form.projectname.data) + project.access['owners'] = [current_user.name] + project.save() + flash('Your project is created!', 'success') + return redirect(url_for('project_dashboard', project_name=project.name)) + return render_template('project_add.html', form=form) + +@app.route('/project/', methods=['GET']) def project_dashboard(project_name): project = Project.first_or_404(name=project_name) - return render_template('project.html', project=project) + access = project.user_access(current_user) + if access is None: + abort(404) + return render_template('project.html', project=project, owner=(access == 'Owner')) @app.route('/project//update', methods=['GET']) @login_required diff --git a/biscd/biscd/templates/base.html b/biscd/biscd/templates/base.html index d46884f..8c57327 100644 --- a/biscd/biscd/templates/base.html +++ b/biscd/biscd/templates/base.html @@ -23,9 +23,18 @@