Skip to content

Commit

Permalink
Make forms easier to manage
Browse files Browse the repository at this point in the history
  • Loading branch information
matejak committed Apr 4, 2024
1 parent 02f7836 commit d8dc44e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 20 deletions.
Empty file.
7 changes: 7 additions & 0 deletions estimage/plugins/base/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from flask_wtf import FlaskForm


class BaseForm(FlaskForm):
def __init__(self, ** kwargs):
self.extending_fields = []
super().__init__(** kwargs)
4 changes: 2 additions & 2 deletions estimage/plugins/crypto/forms.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from flask_wtf import FlaskForm
from ..base.forms import BaseForm
import wtforms


from ..jira import forms


class CryptoFormEnd(FlaskForm):
class CryptoFormEnd(BaseForm):
project_next = wtforms.BooleanField('Plan for the Next Iteration')
submit = wtforms.SubmitField("Import Data")

Expand Down
7 changes: 4 additions & 3 deletions estimage/plugins/demo/forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from flask_wtf import FlaskForm
import wtforms

from ..base.forms import BaseForm

class DemoForm(FlaskForm):

class DemoForm(BaseForm):
issues = wtforms.SelectMultipleField('Issues')
progress = wtforms.FloatField('Progress')
submit = wtforms.SubmitField("Next Day")


class ResetForm(FlaskForm):
class ResetForm(BaseForm):
reset = wtforms.SubmitField("Reset Simulation")
13 changes: 9 additions & 4 deletions estimage/plugins/jira/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

import cryptography.fernet
import flask
from flask_wtf import FlaskForm
import wtforms

from ..base.forms import BaseForm

class JiraFormStart(FlaskForm):

class JiraFormStart(BaseForm):
server = wtforms.StringField('Server URL', default="https://")


Expand All @@ -29,11 +30,15 @@ def encrypt_stuff(what):
return fernet.encrypt(what.encode()).decode()


class EncryptedTokenForm(FlaskForm):
class EncryptedTokenForm(BaseForm):
token = wtforms.PasswordField('Token')
store_token = wtforms.BooleanField('Store token locally for later', default=True)
encrypted_token = wtforms.HiddenField('Encrypted Token')
encrypted_meant_for_storage = wtforms.HiddenField('Store the Encrypted Token', default="no")
def __init__(self, ** kwargs):
super().__init__(** kwargs)
self.extending_fields.append(self.token)
self.extending_fields.append(self.store_token)

def validate_on_submit(self):
ret = super().validate_on_submit()
Expand All @@ -49,7 +54,7 @@ def _perform_work_with_token_encryption(self):
self.encrypted_meant_for_storage.data = "yes"


class JiraFormEnd(FlaskForm):
class JiraFormEnd(BaseForm):
retroQuery = wtforms.StringField('Retrospective Query')
projQuery = wtforms.StringField('Projective Query')
cutoffDate = wtforms.DateField("History Cutoff date")
Expand Down
6 changes: 3 additions & 3 deletions estimage/plugins/redhat_compliance/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from flask_wtf import FlaskForm
import wtforms

from ..jira import forms
from ..base.forms import BaseForm


class RedhatComplianceFormEnd(FlaskForm):
class RedhatComplianceFormEnd(BaseForm):
quarter = wtforms.StringField('Quarter String')
project_next = wtforms.BooleanField('Plan for the Next Quarter')
submit = wtforms.SubmitField("Import Data")
Expand All @@ -14,7 +14,7 @@ class RedhatComplianceForm(forms.EncryptedTokenForm, RedhatComplianceFormEnd):
pass


class RedhatComplianceRefreshForm(FlaskForm):
class RedhatComplianceRefreshForm(BaseForm):

def request_refresh_of(self, names):
self.cards.data = ",".join(names)
Expand Down
4 changes: 2 additions & 2 deletions estimage/problems/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Problem:
affected_card_name: str = ""
tags: typing.FrozenSet[str] = frozenset()

def get_formatted_description(self):
return self.description.format(formatted_task_name=self.format_task_name())
def get_formatted_description(self, formatted_task_name):
return self.description_template.format(formatted_task_name=formatted_task_name)

def format_task_name(self):
return f"'{self.affected_card_name}'"
Expand Down
13 changes: 8 additions & 5 deletions estimage/webapp/main/forms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from flask_wtf import FlaskForm
import wtforms
from wtforms import StringField, BooleanField, SubmitField, ValidationError

from ... import PluginResolver
from ...plugins.base.forms import BaseForm


class SubmitMixin:
Expand All @@ -27,7 +27,7 @@ def disable_delete_button(self):
self.delete.render_kw["disabled"] = "disabled"


class PromotionMixin(FlaskForm):
class PromotionMixin(BaseForm):
def __init__(self, id_prefix, * args, ** kwargs):
super().__init__(* args, ** kwargs)
self.i_kid_you_not.id = id_prefix + self.i_kid_you_not.id
Expand Down Expand Up @@ -65,7 +65,7 @@ def clear_to_go(self):
FIB = [0, 1, 2, 3, 5, 8, 13, 21, 34]


class NumberEstimationForm(FlaskForm, SubmitMixin, DeleteMixin):
class NumberEstimationForm(BaseForm, SubmitMixin, DeleteMixin):
optimistic = wtforms.DecimalField("Optimistic")
most_likely = wtforms.DecimalField("Most Likely")
pessimistic = wtforms.DecimalField("Pessimistic")
Expand Down Expand Up @@ -93,7 +93,7 @@ def get_all_errors(self):
return all_errors


class PointEstimationForm(FlaskForm):
class PointEstimationForm(BaseForm):
optimistic = wtforms.SelectField("Optimistic", choices=FIB)
most_likely = wtforms.SelectField("Most Likely", choices=FIB)
pessimistic = wtforms.SelectField("Pessimistic", choices=FIB)
Expand All @@ -112,7 +112,10 @@ class MultiCheckboxField(wtforms.SelectMultipleField):


@PluginResolver.class_is_extendable("ProblemForm")
class ProblemForm(FlaskForm):
class ProblemForm(BaseForm):
def __init__(self, ** kwargs):
self.extending_fields = []
super().__init__(** kwargs)

def add_problems(self, all_problems):
for p in all_problems:
Expand Down
2 changes: 1 addition & 1 deletion estimage/webapp/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def fix_problems():
form.add_problems(problem_detector.problems)
if form.validate_on_submit():
problems_cat = classifier.CATEGORIES[form.problem_category.data]
print(f"Fix {form.problems.data}: {problems_cat.solution.description}")
flask.flash(f"Fix {form.problems.data}: {problems_cat.solution.description}")
else:
flask.flash(f"Error handing over solution: {form.errors}")
return flask.redirect(
Expand Down

0 comments on commit d8dc44e

Please sign in to comment.