From 135f199c032fdfbe6290e15043925a8987f87341 Mon Sep 17 00:00:00 2001 From: Burathar Date: Mon, 5 Apr 2021 22:42:59 +0200 Subject: [PATCH] Add project update --- biscd/biscd/errors.py | 4 +++ biscd/biscd/froms.py | 16 ++++++++- biscd/biscd/models/project.py | 4 +-- biscd/biscd/routes.py | 36 ++++++++++++++++++--- biscd/biscd/templates/400.html | 3 +- biscd/biscd/templates/401.html | 6 ++++ biscd/biscd/templates/project.html | 3 ++ biscd/biscd/templates/project_add.html | 15 --------- biscd/biscd/templates/project_settings.html | 20 ++++++++++++ installation-files/projects_example.yaml | 21 +++++++++--- 10 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 biscd/biscd/templates/401.html delete mode 100644 biscd/biscd/templates/project_add.html create mode 100644 biscd/biscd/templates/project_settings.html diff --git a/biscd/biscd/errors.py b/biscd/biscd/errors.py index 94c0d87..f88134a 100644 --- a/biscd/biscd/errors.py +++ b/biscd/biscd/errors.py @@ -5,6 +5,10 @@ from biscd import app, utils def bad_request(error): return render_template('400.html', title='400', error=error), 400 +@app.errorhandler(401) +def unauthorized_error(error): + return render_template('401.html', title='401'), 401 + @app.errorhandler(404) def not_found_error(error): return render_template('404.html', title='404'), 404 diff --git a/biscd/biscd/froms.py b/biscd/biscd/froms.py index 7ffae26..1ef00b0 100644 --- a/biscd/biscd/froms.py +++ b/biscd/biscd/froms.py @@ -1,6 +1,7 @@ from flask_wtf import FlaskForm from wtforms import SubmitField, StringField, PasswordField, BooleanField from wtforms.validators import DataRequired, Email, EqualTo, Length, ValidationError +from git import cmd as git_cmd, GitCommandError from .models import User, Project @@ -28,14 +29,27 @@ class RegistrationForm(FlaskForm): if any(user): raise ValidationError('Please use a different email adress.') -class NewProjectForm(FlaskForm): +class ProjectForm(FlaskForm): projectname = StringField('Project Name', validators=[DataRequired()]) + git_repo = StringField('Git Repo') + public = BooleanField('Public Project') submit = SubmitField('Add Project') + old_name = '' def validate_projectname(self, projectname): + if projectname.data == self.old_name: + return project = Project.get(name=projectname.data) if any(project): raise ValidationError('Please use a different projectname.') + def validate_git_repo(self, git_repo): + if not git_repo.data: + return + try: + git_cmd.Git().ls_remote(git_repo.data) + except GitCommandError as gce: + raise ValidationError('Please enter a valid git repository url') from gce + class EmptyForm(FlaskForm): submit = SubmitField('Submit') diff --git a/biscd/biscd/models/project.py b/biscd/biscd/models/project.py index ceed545..a4e94e3 100644 --- a/biscd/biscd/models/project.py +++ b/biscd/biscd/models/project.py @@ -22,12 +22,12 @@ class Project(YamlSerializable): def _yaml_object_name(cls): return 'projects' - def __init__(self, name=None, branch='master'): + def __init__(self, name=None, branch='master', git_repo = None): super().__init__() self.name = name self.branch = branch self.relative_production_path = None - self.git_repo = None + self.git_repo = git_repo self.access = {'public': False, 'owners': []} def __eq__(self, other): diff --git a/biscd/biscd/routes.py b/biscd/biscd/routes.py index 5deb937..76e36fa 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 +from .froms import ProjectForm, LoginForm, RegistrationForm @app.route('/', methods=['GET', 'POST']) @app.route('/index', methods=['GET', 'POST']) @@ -59,14 +59,40 @@ def public_projects(): @app.route('/project/add', methods=['GET', 'POST']) @login_required def project_add(): - form = NewProjectForm() + project = Project(name='') + form = ProjectForm() if form.validate_on_submit(): - project = Project(name=form.projectname.data) - project.access['owners'] = [current_user.name] + project = Project(name=form.projectname.data, git_repo=form.git_repo.data) + project.access['public'] = form.public.data + if project.access['owners'] == []: + 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) + return render_template('project_settings.html', form=form, project=project) + +@app.route('/project//settings', methods=['GET', 'POST']) +@login_required +def project_change_settings(project_name): + project = Project.first_or_404(name=project_name) + if project.user_access(current_user) != 'Owner': + abort(401) + form = ProjectForm() + form.old_name = project.name + if request.method == 'GET': + # pylint: disable=no-member + form.process() + form.public.data = project.access.get('public', False) + if form.validate_on_submit(): + project = Project(name=form.projectname.data, git_repo=form.git_repo.data) + project.access['public'] = form.public.data + if project.access['owners'] == []: + project.access['owners'] = [current_user.name] + project.save() + flash(f"{project.name} was updated!", 'success') + return redirect(url_for('project_dashboard', project_name=project.name)) + + return render_template('project_settings.html', form=form, project=project) @app.route('/project/', methods=['GET']) def project_dashboard(project_name): diff --git a/biscd/biscd/templates/400.html b/biscd/biscd/templates/400.html index 6f49eea..4fb49b0 100644 --- a/biscd/biscd/templates/400.html +++ b/biscd/biscd/templates/400.html @@ -1,10 +1,9 @@ {% extends "base.html" %} {% block app_content %} -

404 Page Not Found

+

400 Bad Request

Back

-

400 Bad Request

Error Message:

{{ error }} {% endblock %} \ No newline at end of file diff --git a/biscd/biscd/templates/401.html b/biscd/biscd/templates/401.html new file mode 100644 index 0000000..371ac59 --- /dev/null +++ b/biscd/biscd/templates/401.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} + +{% block app_content %} +

401 Unauthorized

+

Back

+{% endblock %} \ No newline at end of file diff --git a/biscd/biscd/templates/project.html b/biscd/biscd/templates/project.html index 9efeb88..8b4eeba 100644 --- a/biscd/biscd/templates/project.html +++ b/biscd/biscd/templates/project.html @@ -5,6 +5,9 @@

{{ project.name }}

{% if owner %} + + + diff --git a/biscd/biscd/templates/project_add.html b/biscd/biscd/templates/project_add.html deleted file mode 100644 index d9f1a84..0000000 --- a/biscd/biscd/templates/project_add.html +++ /dev/null @@ -1,15 +0,0 @@ -{% extends "base.html" %} -{% import 'bootstrap/wtf.html' as wtf %} - -{% block app_content %} - -
-
-
-

Add Project

-
- {{ wtf.quick_form(form, button_map={'submit': 'primary'}) }} -
-
-
-{% endblock %} diff --git a/biscd/biscd/templates/project_settings.html b/biscd/biscd/templates/project_settings.html new file mode 100644 index 0000000..8db11e5 --- /dev/null +++ b/biscd/biscd/templates/project_settings.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% import 'bootstrap/wtf.html' as wtf %} + +{% block app_content %} + +
+
+
+
+
+ {{ form.hidden_tag() }} + {{ wtf.form_field(form.projectname, class='form-control', value=project.name) }} + {{ wtf.form_field(form.git_repo, class='form-control', value=(project.git_repo or '')) }} + {{ wtf.form_field(form.public, class='form-control') }} + {{ wtf.form_field(form.submit, class='btn btn-primary', value="Update") }} +
+
+
+
+{% endblock %} diff --git a/installation-files/projects_example.yaml b/installation-files/projects_example.yaml index dd7f46a..7456e8e 100644 --- a/installation-files/projects_example.yaml +++ b/installation-files/projects_example.yaml @@ -1,18 +1,31 @@ projects: - The Hunt: + access: + owners: + - Carl + public: true + branch: master + git_repo: https://git.sciuro.org/Burathar/The-Hunt +- Foo: branch: master git_repo: https://git.sciuro.org/Burathar/The-Hunt + relative_production_path: Foo045878 requirements_file: requirements.txt secret: thisissecret tests: tests.py +- Bar: access: - public: true - owners: - - Carl -- Foo: + public: false branch: master git_repo: https://git.sciuro.org/Burathar/The-Hunt relative_production_path: Foo045878 requirements_file: requirements.txt secret: thisissecret tests: tests.py +- biscd: + access: + owners: + - Carl + public: false + branch: master + git_repo: git@git.sciuro.org:Burathar/biscd.git \ No newline at end of file