##// END OF EJS Templates
black: reformat source
black: reformat source

File last commit:

r153:32f4b641
r153:32f4b641
Show More
forms.py
981 lines | 31.0 KiB | text/x-python | PythonLexer
project: initial commit
r0 # -*- coding: utf-8 -*-
license: change the license to Apache 2.0
r112 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # http://www.apache.org/licenses/LICENSE-2.0
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project: initial commit
r0
import wtforms
import formencode
import re
import pyramid.threadlocal
import datetime
import appenlight.lib.helpers as h
requirements: bump ziggurat_foundations to 0.8.3
r135 from ziggurat_foundations.models.services.user import UserService
from ziggurat_foundations.models.services.group import GroupService
project: initial commit
r0 from appenlight.models import DBSession
from appenlight.models.alert_channel import AlertChannel
from appenlight.models.integrations import IntegrationException
from appenlight.models.integrations.campfire import CampfireIntegration
from appenlight.models.integrations.bitbucket import BitbucketIntegration
from appenlight.models.integrations.github import GithubIntegration
from appenlight.models.integrations.flowdock import FlowdockIntegration
from appenlight.models.integrations.hipchat import HipchatIntegration
from appenlight.models.integrations.jira import JiraClient
from appenlight.models.integrations.slack import SlackIntegration
from appenlight.lib.ext_json import json
from wtforms.ext.csrf.form import SecureForm
from wtforms.compat import iteritems
from collections import defaultdict
_ = str
strip_filter = lambda x: x.strip() if x else None
uppercase_filter = lambda x: x.upper() if x else None
black: reformat source
r153 FALSE_VALUES = ("false", "", False, None)
project: initial commit
r0
class CSRFException(Exception):
pass
class ReactorForm(SecureForm):
black: reformat source
r153 def __init__(self, formdata=None, obj=None, prefix="", csrf_context=None, **kwargs):
super(ReactorForm, self).__init__(
formdata=formdata,
obj=obj,
prefix=prefix,
csrf_context=csrf_context,
**kwargs
)
project: initial commit
r0 self._csrf_context = csrf_context
def generate_csrf_token(self, csrf_context):
return csrf_context.session.get_csrf_token()
def validate_csrf_token(self, field):
request = self._csrf_context or pyramid.threadlocal.get_current_request()
black: reformat source
r153 is_from_auth_token = "auth:auth_token" in request.effective_principals
project: initial commit
r0 if is_from_auth_token:
return True
if field.data != field.current_token:
# try to save the day by using token from angular
black: reformat source
r153 if request.headers.get("X-XSRF-TOKEN") != field.current_token:
raise CSRFException("Invalid CSRF token")
project: initial commit
r0
@property
def errors_dict(self):
r_dict = defaultdict(list)
for k, errors in self.errors.items():
r_dict[k].extend([str(e) for e in errors])
return r_dict
@property
def errors_json(self):
return json.dumps(self.errors_dict)
def populate_obj(self, obj, ignore_none=False):
"""
Populates the attributes of the passed `obj` with data from the form's
fields.
:note: This is a destructive operation; Any attribute with the same name
as a field will be overridden. Use with caution.
"""
if ignore_none:
for name, field in iteritems(self._fields):
if field.data is not None:
field.populate_obj(obj, name)
else:
for name, field in iteritems(self._fields):
field.populate_obj(obj, name)
css_classes = {}
ignore_labels = {}
class SignInForm(ReactorForm):
came_from = wtforms.HiddenField()
black: reformat source
r153 sign_in_user_name = wtforms.StringField(_("User Name"))
sign_in_user_password = wtforms.PasswordField(_("Password"))
project: initial commit
r0
black: reformat source
r153 ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
black: reformat source
r153 html_attrs = {
"sign_in_user_name": {"placeholder": "Your login"},
"sign_in_user_password": {"placeholder": "Your password"},
}
project: initial commit
r0
from wtforms.widgets import html_params, HTMLString
black: reformat source
r153 def select_multi_checkbox(field, ul_class="set", **kwargs):
project: initial commit
r0 """Render a multi-checkbox widget"""
black: reformat source
r153 kwargs.setdefault("type", "checkbox")
field_id = kwargs.pop("id", field.id)
html = ["<ul %s>" % html_params(id=field_id, class_=ul_class)]
project: initial commit
r0 for value, label, checked in field.iter_choices():
black: reformat source
r153 choice_id = "%s-%s" % (field_id, value)
project: initial commit
r0 options = dict(kwargs, name=field.name, value=value, id=choice_id)
if checked:
black: reformat source
r153 options["checked"] = "checked"
html.append("<li><input %s /> " % html_params(**options))
project: initial commit
r0 html.append('<label for="%s">%s</label></li>' % (choice_id, label))
black: reformat source
r153 html.append("</ul>")
return HTMLString("".join(html))
project: initial commit
r0
black: reformat source
r153 def button_widget(field, button_cls="ButtonField btn btn-default", **kwargs):
project: initial commit
r0 """Render a button widget"""
black: reformat source
r153 kwargs.setdefault("type", "button")
field_id = kwargs.pop("id", field.id)
kwargs.setdefault("value", field.label.text)
html = [
"<button %s>%s</button>"
% (html_params(id=field_id, class_=button_cls), kwargs["value"])
]
return HTMLString("".join(html))
project: initial commit
r0
def clean_whitespace(value):
if value:
return value.strip()
return value
def found_username_validator(form, field):
requirements: bump ziggurat_foundations to 0.8.3
r135 user = UserService.by_user_name(field.data)
project: initial commit
r0 # sets user to recover in email validator
form.field_user = user
if not user:
black: reformat source
r153 raise wtforms.ValidationError("This username does not exist")
project: initial commit
r0
def found_username_email_validator(form, field):
requirements: bump ziggurat_foundations to 0.8.3
r135 user = UserService.by_email(field.data)
project: initial commit
r0 if not user:
black: reformat source
r153 raise wtforms.ValidationError("Email is incorrect")
project: initial commit
r0
def unique_username_validator(form, field):
requirements: bump ziggurat_foundations to 0.8.3
r135 user = UserService.by_user_name(field.data)
project: initial commit
r0 if user:
black: reformat source
r153 raise wtforms.ValidationError("This username already exists in system")
project: initial commit
r0
def unique_groupname_validator(form, field):
requirements: bump ziggurat_foundations to 0.8.3
r135 group = GroupService.by_group_name(field.data)
black: reformat source
r153 mod_group = getattr(form, "_modified_group", None)
project: initial commit
r0 if group and (not mod_group or mod_group.id != group.id):
black: reformat source
r153 raise wtforms.ValidationError("This group name already exists in system")
project: initial commit
r0
def unique_email_validator(form, field):
requirements: bump ziggurat_foundations to 0.8.3
r135 user = UserService.by_email(field.data)
project: initial commit
r0 if user:
black: reformat source
r153 raise wtforms.ValidationError("This email already exists in system")
project: initial commit
r0
def email_validator(form, field):
validator = formencode.validators.Email()
try:
validator.to_python(field.data)
except formencode.Invalid as e:
raise wtforms.ValidationError(e)
def unique_alert_email_validator(form, field):
q = DBSession.query(AlertChannel)
black: reformat source
r153 q = q.filter(AlertChannel.channel_name == "email")
project: initial commit
r0 q = q.filter(AlertChannel.channel_value == field.data)
email = q.first()
if email:
black: reformat source
r153 raise wtforms.ValidationError("This email already exists in alert system")
project: initial commit
r0
def blocked_email_validator(form, field):
blocked_emails = [
black: reformat source
r153 "goood-mail.org",
"shoeonlineblog.com",
"louboutinemart.com",
"guccibagshere.com",
"nikeshoesoutletforsale.com",
project: initial commit
r0 ]
black: reformat source
r153 data = field.data or ""
domain = data.split("@")[-1]
project: initial commit
r0 if domain in blocked_emails:
black: reformat source
r153 raise wtforms.ValidationError("Don't spam")
project: initial commit
r0
def old_password_validator(form, field):
black: reformat source
r153 if not UserService.check_password(field.user, field.data or ""):
raise wtforms.ValidationError("You need to enter correct password")
project: initial commit
r0
class UserRegisterForm(ReactorForm):
user_name = wtforms.StringField(
black: reformat source
r153 _("User Name"),
project: initial commit
r0 filters=[strip_filter],
validators=[
wtforms.validators.Length(min=2, max=30),
wtforms.validators.Regexp(
black: reformat source
r153 re.compile(r"^[\.\w-]+$", re.UNICODE), message="Invalid characters used"
),
project: initial commit
r0 unique_username_validator,
black: reformat source
r153 wtforms.validators.DataRequired(),
],
)
project: initial commit
r0
black: reformat source
r153 user_password = wtforms.PasswordField(
_("User Password"),
filters=[strip_filter],
validators=[
wtforms.validators.Length(min=4),
wtforms.validators.DataRequired(),
],
)
project: initial commit
r0
black: reformat source
r153 email = wtforms.StringField(
_("Email Address"),
filters=[strip_filter],
validators=[
email_validator,
unique_email_validator,
blocked_email_validator,
wtforms.validators.DataRequired(),
],
)
first_name = wtforms.HiddenField(_("First Name"))
last_name = wtforms.HiddenField(_("Last Name"))
project: initial commit
r0
black: reformat source
r153 ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
black: reformat source
r153 html_attrs = {
"user_name": {"placeholder": "Your login"},
"user_password": {"placeholder": "Your password"},
"email": {"placeholder": "Your email"},
}
project: initial commit
r0
class UserCreateForm(UserRegisterForm):
black: reformat source
r153 status = wtforms.BooleanField("User status", false_values=FALSE_VALUES)
project: initial commit
r0
class UserUpdateForm(UserCreateForm):
user_name = None
black: reformat source
r153 user_password = wtforms.PasswordField(
_("User Password"),
filters=[strip_filter],
validators=[wtforms.validators.Length(min=4), wtforms.validators.Optional()],
)
email = wtforms.StringField(
_("Email Address"),
filters=[strip_filter],
validators=[email_validator, wtforms.validators.DataRequired()],
)
project: initial commit
r0
class LostPasswordForm(ReactorForm):
black: reformat source
r153 email = wtforms.StringField(
_("Email Address"),
filters=[strip_filter],
validators=[
email_validator,
found_username_email_validator,
wtforms.validators.DataRequired(),
],
)
project: initial commit
r0
black: reformat source
r153 submit = wtforms.SubmitField(_("Reset password"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class ChangePasswordForm(ReactorForm):
old_password = wtforms.PasswordField(
black: reformat source
r153 "Old Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[old_password_validator, wtforms.validators.DataRequired()],
)
project: initial commit
r0
new_password = wtforms.PasswordField(
black: reformat source
r153 "New Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.Length(min=4),
wtforms.validators.DataRequired(),
],
)
project: initial commit
r0 new_password_confirm = wtforms.PasswordField(
black: reformat source
r153 "Confirm Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.EqualTo("new_password"),
wtforms.validators.DataRequired(),
],
)
submit = wtforms.SubmitField("Change Password")
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class CheckPasswordForm(ReactorForm):
password = wtforms.PasswordField(
black: reformat source
r153 "Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[old_password_validator, wtforms.validators.DataRequired()],
)
project: initial commit
r0
class NewPasswordForm(ReactorForm):
new_password = wtforms.PasswordField(
black: reformat source
r153 "New Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.Length(min=4),
wtforms.validators.DataRequired(),
],
)
project: initial commit
r0 new_password_confirm = wtforms.PasswordField(
black: reformat source
r153 "Confirm Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.EqualTo("new_password"),
wtforms.validators.DataRequired(),
],
)
submit = wtforms.SubmitField("Set Password")
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class CORSTextAreaField(wtforms.StringField):
"""
This field represents an HTML ``<textarea>`` and can be used to take
multi-line input.
"""
black: reformat source
r153
project: initial commit
r0 widget = wtforms.widgets.TextArea()
def process_formdata(self, valuelist):
self.data = []
if valuelist:
black: reformat source
r153 data = [x.strip() for x in valuelist[0].split("\n")]
project: initial commit
r0 for d in data:
if not d:
continue
black: reformat source
r153 if d.startswith("www."):
project: initial commit
r0 d = d[4:]
if data:
self.data.append(d)
else:
self.data = []
black: reformat source
r153 self.data = "\n".join(self.data)
project: initial commit
r0
class ApplicationCreateForm(ReactorForm):
resource_name = wtforms.StringField(
black: reformat source
r153 _("Application name"),
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.Length(min=1),
wtforms.validators.DataRequired(),
],
)
project: initial commit
r0
domains = CORSTextAreaField(
black: reformat source
r153 _("Domain names for CORS headers "),
validators=[wtforms.validators.Length(min=1), wtforms.validators.Optional()],
description="Required for Javascript error "
"tracking (one line one domain, skip http:// part)",
)
project: initial commit
r0
black: reformat source
r153 submit = wtforms.SubmitField(_("Create Application"))
project: initial commit
r0
black: reformat source
r153 ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
html_attrs = {
"resource_name": {"placeholder": "Application Name"},
"uptime_url": {"placeholder": "http://somedomain.com"},
}
project: initial commit
r0
class ApplicationUpdateForm(ApplicationCreateForm):
default_grouping = wtforms.SelectField(
black: reformat source
r153 _("Default grouping for errors"),
choices=[
("url_type", "Error Type + location"),
("url_traceback", "Traceback + location"),
("traceback_server", "Traceback + Server"),
],
default="url_traceback",
)
project: initial commit
r0
error_report_threshold = wtforms.IntegerField(
black: reformat source
r153 _("Alert on error reports"),
project: initial commit
r0 validators=[
wtforms.validators.NumberRange(min=1),
black: reformat source
r153 wtforms.validators.DataRequired(),
project: initial commit
r0 ],
black: reformat source
r153 description="Application requires to send at least this amount of "
"error reports per minute to open alert",
project: initial commit
r0 )
slow_report_threshold = wtforms.IntegerField(
black: reformat source
r153 _("Alert on slow reports"),
validators=[
wtforms.validators.NumberRange(min=1),
wtforms.validators.DataRequired(),
],
description="Application requires to send at least this amount of "
"slow reports per minute to open alert",
)
project: initial commit
r0
allow_permanent_storage = wtforms.BooleanField(
black: reformat source
r153 _("Permanent logs"),
project: initial commit
r0 false_values=FALSE_VALUES,
black: reformat source
r153 description=_("Allow permanent storage of logs in separate DB partitions"),
)
project: initial commit
r0
black: reformat source
r153 submit = wtforms.SubmitField(_("Create Application"))
project: initial commit
r0
class UserSearchSchemaForm(ReactorForm):
black: reformat source
r153 user_name = wtforms.StringField("User Name", filters=[strip_filter])
project: initial commit
r0
black: reformat source
r153 submit = wtforms.SubmitField(_("Search User"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
'<li class="user_exists"><span></span></li>'
class YesNoForm(ReactorForm):
black: reformat source
r153 no = wtforms.SubmitField("No", default="")
yes = wtforms.SubmitField("Yes", default="")
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
black: reformat source
r153 status_codes = [("", "All"), ("500", "500"), ("404", "404")]
project: initial commit
r0
black: reformat source
r153 priorities = [("", "All")]
project: initial commit
r0 for i in range(1, 11):
black: reformat source
r153 priorities.append((str(i), str(i)))
project: initial commit
r0
black: reformat source
r153 report_status_choices = [
("", "All"),
("never_reviewed", "Never revieved"),
("reviewed", "Revieved"),
("public", "Public"),
("fixed", "Fixed"),
]
project: initial commit
r0
class ReportBrowserForm(ReactorForm):
black: reformat source
r153 applications = wtforms.SelectMultipleField(
"Applications", widget=select_multi_checkbox
)
http_status = wtforms.SelectField("HTTP Status", choices=status_codes)
priority = wtforms.SelectField("Priority", choices=priorities, default="")
start_date = wtforms.DateField("Start Date")
end_date = wtforms.DateField("End Date")
error = wtforms.StringField("Error")
url_path = wtforms.StringField("URL Path")
url_domain = wtforms.StringField("URL Domain")
report_status = wtforms.SelectField(
"Report status", choices=report_status_choices, default=""
)
submit = wtforms.SubmitField(
'<span class="glyphicon glyphicon-search">' "</span> Filter results",
widget=button_widget,
)
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
slow_report_status_choices = [
("", "All"),
("never_reviewed", "Never revieved"),
("reviewed", "Revieved"),
("public", "Public"),
]
project: initial commit
r0
class BulkOperationForm(ReactorForm):
black: reformat source
r153 applications = wtforms.SelectField("Applications")
project: initial commit
r0 start_date = wtforms.DateField(
black: reformat source
r153 "Start Date",
default=lambda: datetime.datetime.utcnow() - datetime.timedelta(days=90),
)
end_date = wtforms.DateField("End Date")
project: initial commit
r0 confirm = wtforms.BooleanField(
black: reformat source
r153 "Confirm operation", validators=[wtforms.validators.DataRequired()]
)
project: initial commit
r0
class LogBrowserForm(ReactorForm):
black: reformat source
r153 applications = wtforms.SelectMultipleField(
"Applications", widget=select_multi_checkbox
)
start_date = wtforms.DateField("Start Date")
log_level = wtforms.StringField("Log level")
message = wtforms.StringField("Message")
namespace = wtforms.StringField("Namespace")
project: initial commit
r0 submit = wtforms.SubmitField(
'<span class="glyphicon glyphicon-search"></span> Filter results',
black: reformat source
r153 widget=button_widget,
)
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class CommentForm(ReactorForm):
black: reformat source
r153 body = wtforms.TextAreaField(
"Comment",
validators=[
wtforms.validators.Length(min=1),
wtforms.validators.DataRequired(),
],
)
submit = wtforms.SubmitField("Comment")
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class EmailChannelCreateForm(ReactorForm):
black: reformat source
r153 email = wtforms.StringField(
_("Email Address"),
filters=[strip_filter],
validators=[
email_validator,
unique_alert_email_validator,
wtforms.validators.DataRequired(),
],
)
submit = wtforms.SubmitField("Add email channel")
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
def gen_user_profile_form():
class UserProfileForm(ReactorForm):
email = wtforms.StringField(
black: reformat source
r153 _("Email Address"),
validators=[email_validator, wtforms.validators.DataRequired()],
)
first_name = wtforms.StringField(_("First Name"))
last_name = wtforms.StringField(_("Last Name"))
company_name = wtforms.StringField(_("Company Name"))
company_address = wtforms.TextAreaField(_("Company Address"))
zip_code = wtforms.StringField(_("ZIP code"))
city = wtforms.StringField(_("City"))
notifications = wtforms.BooleanField(
"Account notifications", false_values=FALSE_VALUES
)
submit = wtforms.SubmitField(_("Update Account"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
return UserProfileForm
class PurgeAppForm(ReactorForm):
resource_id = wtforms.HiddenField(
black: reformat source
r153 "App Id", validators=[wtforms.validators.DataRequired()]
)
days = wtforms.IntegerField("Days", validators=[wtforms.validators.DataRequired()])
project: initial commit
r0 password = wtforms.PasswordField(
black: reformat source
r153 "Admin Password",
validators=[old_password_validator, wtforms.validators.DataRequired()],
)
submit = wtforms.SubmitField(_("Purge Data"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class IntegrationRepoForm(ReactorForm):
black: reformat source
r153 host_name = wtforms.StringField("Service Host", default="")
project: initial commit
r0 user_name = wtforms.StringField(
"User Name",
filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Length(min=1),
],
)
project: initial commit
r0 repo_name = wtforms.StringField(
"Repo Name",
filters=[strip_filter],
black: reformat source
r153 validators=[
wtforms.validators.DataRequired(),
wtforms.validators.Length(min=1),
],
)
project: initial commit
r0
class IntegrationBitbucketForm(IntegrationRepoForm):
black: reformat source
r153 host_name = wtforms.StringField("Service Host", default="https://bitbucket.org")
project: initial commit
r0
def validate_user_name(self, field):
try:
request = pyramid.threadlocal.get_current_request()
client = BitbucketIntegration.create_client(
black: reformat source
r153 request, self.user_name.data, self.repo_name.data
)
project: initial commit
r0 client.get_assignees()
except IntegrationException as e:
raise wtforms.validators.ValidationError(str(e))
class IntegrationGithubForm(IntegrationRepoForm):
black: reformat source
r153 host_name = wtforms.StringField("Service Host", default="https://github.com")
project: initial commit
r0
def validate_user_name(self, field):
try:
request = pyramid.threadlocal.get_current_request()
client = GithubIntegration.create_client(
black: reformat source
r153 request, self.user_name.data, self.repo_name.data
)
project: initial commit
r0 client.get_assignees()
except IntegrationException as e:
raise wtforms.validators.ValidationError(str(e))
raise wtforms.validators.ValidationError(str(e))
def filter_rooms(data):
if data is not None:
black: reformat source
r153 rooms = data.split(",")
return ",".join([r.strip() for r in rooms])
project: initial commit
r0
class IntegrationCampfireForm(ReactorForm):
account = wtforms.StringField(
black: reformat source
r153 "Account",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0 api_token = wtforms.StringField(
black: reformat source
r153 "Api Token",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
rooms = wtforms.StringField("Room ID list", filters=[filter_rooms])
project: initial commit
r0
def validate_api_token(self, field):
try:
black: reformat source
r153 client = CampfireIntegration.create_client(
self.api_token.data, self.account.data
)
project: initial commit
r0 client.get_account()
except IntegrationException as e:
raise wtforms.validators.ValidationError(str(e))
def validate_rooms(self, field):
if not field.data:
return
black: reformat source
r153 client = CampfireIntegration.create_client(
self.api_token.data, self.account.data
)
project: initial commit
r0
try:
black: reformat source
r153 room_list = [r["id"] for r in client.get_rooms()]
project: initial commit
r0 except IntegrationException as e:
raise wtforms.validators.ValidationError(str(e))
black: reformat source
r153 rooms = field.data.split(",")
project: initial commit
r0 if len(rooms) > 3:
black: reformat source
r153 msg = "You can use up to 3 room ids"
project: initial commit
r0 raise wtforms.validators.ValidationError(msg)
if rooms:
for room_id in rooms:
if int(room_id) not in room_list:
msg = "Room %s doesn't exist"
raise wtforms.validators.ValidationError(msg % room_id)
if not room_id.strip().isdigit():
black: reformat source
r153 msg = "You must use only integers for room ids"
project: initial commit
r0 raise wtforms.validators.ValidationError(msg)
black: reformat source
r153 submit = wtforms.SubmitField(_("Connect to Campfire"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
def filter_rooms(data):
if data is not None:
black: reformat source
r153 rooms = data.split(",")
return ",".join([r.strip() for r in rooms])
project: initial commit
r0
class IntegrationHipchatForm(ReactorForm):
api_token = wtforms.StringField(
black: reformat source
r153 "Api Token",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0 rooms = wtforms.StringField(
black: reformat source
r153 "Room ID list",
project: initial commit
r0 filters=[filter_rooms],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0
def validate_rooms(self, field):
if not field.data:
return
client = HipchatIntegration.create_client(self.api_token.data)
black: reformat source
r153 rooms = field.data.split(",")
project: initial commit
r0 if len(rooms) > 3:
black: reformat source
r153 msg = "You can use up to 3 room ids"
project: initial commit
r0 raise wtforms.validators.ValidationError(msg)
if rooms:
for room_id in rooms:
if not room_id.strip().isdigit():
black: reformat source
r153 msg = "You must use only integers for room ids"
project: initial commit
r0 raise wtforms.validators.ValidationError(msg)
try:
black: reformat source
r153 client.send(
{
"message_format": "text",
"message": "testing for room existence",
"from": "AppEnlight",
"room_id": room_id,
"color": "green",
}
)
project: initial commit
r0 except IntegrationException as exc:
black: reformat source
r153 msg = "Room id: %s exception: %s"
raise wtforms.validators.ValidationError(msg % (room_id, exc))
project: initial commit
r0
class IntegrationFlowdockForm(ReactorForm):
black: reformat source
r153 api_token = wtforms.StringField(
"API Token",
filters=[strip_filter],
validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0
def validate_api_token(self, field):
try:
client = FlowdockIntegration.create_client(self.api_token.data)
registry = pyramid.threadlocal.get_current_registry()
payload = {
black: reformat source
r153 "source": registry.settings["mailing.from_name"],
"from_address": registry.settings["mailing.from_email"],
project: initial commit
r0 "subject": "Integration test",
"content": "If you can see this it was successful",
"tags": ["appenlight"],
black: reformat source
r153 "link": registry.settings["mailing.app_url"],
project: initial commit
r0 }
client.send_to_inbox(payload)
except IntegrationException as e:
raise wtforms.validators.ValidationError(str(e))
class IntegrationSlackForm(ReactorForm):
webhook_url = wtforms.StringField(
black: reformat source
r153 "Reports webhook",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0
def validate_webhook_url(self, field):
registry = pyramid.threadlocal.get_current_registry()
client = SlackIntegration.create_client(field.data)
black: reformat source
r153 link = "<%s|%s>" % (
registry.settings["mailing.app_url"],
registry.settings["mailing.from_name"],
)
project: initial commit
r0 test_data = {
refactor: fix inconsistent naming
r28 "username": "AppEnlight",
project: initial commit
r0 "icon_emoji": ":fire:",
"attachments": [
black: reformat source
r153 {
"fallback": "Testing integration channel: %s" % link,
"pretext": "Testing integration channel: %s" % link,
"color": "good",
"fields": [
{
"title": "Status",
"value": "Integration is working fine",
"short": False,
}
],
}
],
project: initial commit
r0 }
try:
client.make_request(data=test_data)
except IntegrationException as exc:
raise wtforms.validators.ValidationError(str(exc))
class IntegrationWebhooksForm(ReactorForm):
reports_webhook = wtforms.StringField(
black: reformat source
r153 "Reports webhook",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0 alerts_webhook = wtforms.StringField(
black: reformat source
r153 "Alerts webhook",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
submit = wtforms.SubmitField(_("Setup webhooks"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-primary"}
project: initial commit
r0
class IntegrationJiraForm(ReactorForm):
host_name = wtforms.StringField(
black: reformat source
r153 "Server URL",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0 user_name = wtforms.StringField(
black: reformat source
r153 "Username",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0 password = wtforms.PasswordField(
black: reformat source
r153 "Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0 project = wtforms.StringField(
black: reformat source
r153 "Project key",
project: initial commit
r0 filters=[uppercase_filter, strip_filter],
black: reformat source
r153 validators=[wtforms.validators.DataRequired()],
)
project: initial commit
r0
def validate_project(self, field):
if not field.data:
return
try:
black: reformat source
r153 client = JiraClient(
self.user_name.data,
self.password.data,
self.host_name.data,
self.project.data,
)
project: initial commit
r0 except Exception as exc:
raise wtforms.validators.ValidationError(str(exc))
room_list = [r.key.upper() for r in client.get_projects()]
if field.data.upper() not in room_list:
msg = "Project %s doesn\t exist in your Jira Instance"
raise wtforms.validators.ValidationError(msg % field.data)
def get_deletion_form(resource):
class F(ReactorForm):
application_name = wtforms.StringField(
black: reformat source
r153 "Application Name",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[wtforms.validators.AnyOf([resource.resource_name])],
)
project: initial commit
r0 resource_id = wtforms.HiddenField(default=resource.resource_id)
black: reformat source
r153 submit = wtforms.SubmitField(_("Delete my application"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-danger"}
project: initial commit
r0
return F
class ChangeApplicationOwnerForm(ReactorForm):
password = wtforms.PasswordField(
black: reformat source
r153 "Password",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[old_password_validator, wtforms.validators.DataRequired()],
)
project: initial commit
r0
user_name = wtforms.StringField(
black: reformat source
r153 "New owners username",
project: initial commit
r0 filters=[strip_filter],
black: reformat source
r153 validators=[found_username_validator, wtforms.validators.DataRequired()],
)
submit = wtforms.SubmitField(_("Transfer ownership of application"))
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-danger"}
project: initial commit
r0
def default_filename():
black: reformat source
r153 return "Invoice %s" % datetime.datetime.utcnow().strftime("%Y/%m")
project: initial commit
r0
class FileUploadForm(ReactorForm):
black: reformat source
r153 title = wtforms.StringField(
"File Title",
default=default_filename,
validators=[wtforms.validators.DataRequired()],
)
file = wtforms.FileField("File")
project: initial commit
r0
def validate_file(self, field):
black: reformat source
r153 if not hasattr(field.data, "file"):
raise wtforms.ValidationError("File is missing")
project: initial commit
r0
black: reformat source
r153 submit = wtforms.SubmitField(_("Upload"))
project: initial commit
r0
def get_partition_deletion_form(es_indices, pg_indices):
class F(ReactorForm):
black: reformat source
r153 es_index = wtforms.SelectMultipleField(
"Elasticsearch", choices=[(ix, "") for ix in es_indices]
)
pg_index = wtforms.SelectMultipleField(
"pg", choices=[(ix, "") for ix in pg_indices]
)
confirm = wtforms.TextField(
"Confirm",
filters=[uppercase_filter, strip_filter],
validators=[
wtforms.validators.AnyOf(["CONFIRM"]),
wtforms.validators.DataRequired(),
],
)
ignore_labels = ["submit"]
css_classes = {"submit": "btn btn-danger"}
project: initial commit
r0
return F
class GroupCreateForm(ReactorForm):
group_name = wtforms.StringField(
black: reformat source
r153 _("Group Name"),
project: initial commit
r0 filters=[strip_filter],
validators=[
wtforms.validators.Length(min=2, max=50),
unique_groupname_validator,
black: reformat source
r153 wtforms.validators.DataRequired(),
],
)
description = wtforms.StringField(_("Group description"))
project: initial commit
r0
black: reformat source
r153 time_choices = [(k, v["label"]) for k, v in h.time_deltas.items()]
project: initial commit
r0
class AuthTokenCreateForm(ReactorForm):
black: reformat source
r153 description = wtforms.StringField(_("Token description"))
expires = wtforms.SelectField(
"Expires",
coerce=lambda x: x,
choices=time_choices,
validators=[wtforms.validators.Optional()],
)