File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/blueprints/admin/forms.py
import logging
from collections import OrderedDict
from flask_wtf import FlaskForm
from wtforms import SelectField, StringField, BooleanField, TextAreaField, \
FloatField, DateTimeField, widgets, SelectMultipleField
from wtforms.validators import DataRequired, Length, Optional, Regexp, \
NumberRange
from wtforms_components import EmailField, IntegerField
from wtforms_alchemy import Unique
from flask_babel import lazy_gettext as _
try:
from instance import settings
LANGUAGES = settings.LANGUAGES
except ImportError:
logging.error('Ensure __init__.py and settings.py both exist in instance/')
exit(1)
except AttributeError:
from config import settings
LANGUAGES = settings.LANGUAGES
from buyercall.lib.locale import Currency
from buyercall.lib.util_wtforms import ModelForm, choices_from_dict
from buyercall.blueprints.user.models import db, User
from buyercall.blueprints.issue.models import Issue
from buyercall.blueprints.billing.models.coupon import Coupon
class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput()
class SearchForm(FlaskForm):
q = StringField(_('Search terms'), [Optional(), Length(1, 128)])
class BulkDeleteForm(FlaskForm):
SCOPE = OrderedDict([
('all_selected_products', 'All selected items'),
('all_search_results', 'All search results')
])
scope = SelectField(_('Privileges'), [DataRequired()],
choices=choices_from_dict(SCOPE, prepend_blank=False))
class UserForm(ModelForm):
username_message = _('Letters, numbers and underscores only please.')
username = StringField(validators=[
Unique(
User.username,
get_session=lambda: db.session
),
Optional(),
Length(1, 16),
Regexp('^\w+$', message=username_message)
])
firstname = StringField(_('First name'),
[Optional(),
Length(1, 128)])
lastname = StringField(_('Last name'),
[Optional(),
Length(1, 128)])
role = SelectField(_('Privileges'),
[DataRequired()],
choices=choices_from_dict(User.ROLE, prepend_blank=False))
company = StringField(_('Company'),
[Optional(),
Length(1, 256)])
active = BooleanField(_('Yes, allow this user to sign in'))
ams_2000 = BooleanField(_('Allow user AMS 2000 API access'))
ams_evolution = BooleanField(_('Allow user AMS Evolution API access'))
neo_verify = BooleanField(_('Allow user NEO Verify API access'))
ams_prequalify = BooleanField(_('Allow user AMS PreQualify API access'))
ams_analytics = BooleanField(_('Allow user AMS Analytics API access'))
locale = SelectField(_('Language preference'),
[DataRequired()],
choices=choices_from_dict(LANGUAGES))
reports_list = SelectMultipleField('Reports - Select one or multiple reports',
coerce=int)
partnership_account_group_id = SelectField(_('Account Group'),
[Optional()],
choices=[],
coerce=int)
class UserCancelSubscriptionForm(FlaskForm):
pass
class IssueForm(FlaskForm):
label = SelectField(_('What do you need help with?'), [DataRequired()],
choices=choices_from_dict(Issue.LABEL))
email = EmailField(_("What's your e-mail address?"),
[DataRequired(), Length(3, 254)])
question = TextAreaField(_("What's your question or issue?"),
[DataRequired(), Length(1, 8192)])
status = SelectField(_('What status is the issue in?'), [DataRequired()],
choices=choices_from_dict(Issue.STATUS,
prepend_blank=False))
class IssueContactForm(FlaskForm):
subject = StringField(_('Subject'), [DataRequired(), Length(1, 254)])
message = TextAreaField(_('Message to be sent'),
[DataRequired(), Length(1, 8192)])
class CouponForm(FlaskForm):
percent_off = IntegerField(_('Percent off'), [Optional(),
NumberRange(min=1, max=100)])
amount_off = FloatField(_('Amount off'), [Optional(),
NumberRange(min=0.01,
max=21474836.47)])
code = StringField(_('Code'), [DataRequired(), Length(1, 32)])
currency = SelectField(_('Currency'), [DataRequired()],
choices=choices_from_dict(Currency.TYPES,
prepend_blank=False))
duration = SelectField(_('Duration'), [DataRequired()],
choices=choices_from_dict(Coupon.DURATION,
prepend_blank=False))
duration_in_months = IntegerField(_('Duration in months'), [Optional(),
NumberRange(
min=1,
max=12)])
max_redemptions = IntegerField(_('Max Redemptions'),
[Optional(),
NumberRange(min=1)])
redeem_by = DateTimeField(_('Redeem by'), [Optional()],
format='%Y-%m-%d %H:%M:%S')
def validate(self):
if not FlaskForm.validate(self):
return False
result = True
percent_off = self.percent_off.data
amount_off = self.amount_off.data
if percent_off is None and amount_off is None:
self.percent_off.errors.append(_('Pick at least one.'))
self.amount_off.errors.append(_('Pick at least one.'))
result = False
elif percent_off and amount_off:
self.percent_off.errors.append(_('Cannot pick both.'))
self.amount_off.errors.append(_('Cannot pick both.'))
result = False
return result
class SevenHundredCreditForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_account = StringField(_('700 Credit API Account ID'), [Optional(), Length(1, 128)])
api_username = StringField(_('700 Credit API Username'), [Optional(), Length(1, 128)])
api_password = StringField(_('700 Credit API Password'), [DataRequired(), Length(1, 128)])
experian_enabled = BooleanField(_('Enable Experian As Provider'))
transunion_enabled = BooleanField(_('Enable TransUnion As Provider'))
equifax_enabled = BooleanField(_('Enable Equifax As Provider'))
class SevenHundredPrequalifyForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_account = StringField(_('700 Credit API Account ID'), [Optional(), Length(1, 128)])
api_username = StringField(_('700 Credit API Username'), [DataRequired(), Length(1, 128)])
api_password = StringField(_('700 Credit API Password'), [DataRequired(), Length(1, 128)])
experian_enabled = BooleanField(_('Enable Experian As Provider'))
transunion_enabled = BooleanField(_('Enable TransUnion As Provider'))
equifax_enabled = BooleanField(_('Enable Equifax As Provider'))
class FinservPrequalifyForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_username = StringField(_('Finserv Client ID'), [DataRequired(), Length(1, 128)])
api_password = StringField(_('Finserv Client Secret'), [DataRequired(), Length(1, 128)])
api_account = StringField(_('Finserv Dealer Code'), [DataRequired(), Length(1, 128)])
class AMSEvolutionForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_username = StringField(_('AMS Evolution Username'), [DataRequired(), Length(1, 128)])
api_password = StringField(_('AMS Evolution Password'), [DataRequired(), Length(1, 128)])
api_account = StringField(_('AMS Evolution Dealer Code'), [DataRequired(), Length(1, 128)])
api_secret = StringField(_('AMS Evolution Token'), [DataRequired(), Length(1, 128)])
api_url = StringField(_('AMS Evolution API URL'), [DataRequired(), Length(1, 128)])
class NeoVerifyForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_secret = StringField(_('AMS Evolution Token'), [DataRequired(), Length(1, 128)])
api_url = StringField(_('AMS Evolution API URL'), [DataRequired(), Length(1, 128)])
# add user form
class AddUserForm(ModelForm):
username_message = _('Letters, numbers and underscores only please.')
username = StringField('Username', validators=[
Optional(),
Length(1, 16),
Regexp('^\w+$', message=username_message)
])
firstname = StringField('First name', validators=[
DataRequired(),
Length(1, 128)
])
lastname = StringField('Last name', validators=[
Optional(),
Length(1, 128)
])
title = StringField('Title', validators=[
Optional(),
Length(1, 128)
])
department = SelectField(_('Department'),
[DataRequired()],
choices=choices_from_dict(User.DEPARTMENT, prepend_blank=False))
email = StringField('Email Address', validators=[
DataRequired(),
Length(1, 256) # Update the length as needed
])
phonenumber = StringField('Phone Number', validators=[
DataRequired(),
Length(1, 20) # Update the length as needed
])
role = SelectField(_('Privileges'),
[DataRequired()],
choices=choices_from_dict(User.ROLE, prepend_blank=False))
company = StringField('Company', validators=[
Optional(),
Length(1, 256)
])
active = BooleanField('Yes, allow this user to sign in')
ams_2000 = BooleanField('Allow user AMS 2000 API access')
ams_evolution = BooleanField('Allow user AMS Evolution API access')
neo_verify = BooleanField('Allow user NEO Verify API access')
locale = SelectField(_('Language preference'),
[DataRequired()],
choices=choices_from_dict(LANGUAGES, prepend_blank=False))
reports_list = SelectMultipleField('Reports - Select one or multiple reports', coerce=int,)
partnership_account_group_id = SelectField('Account Group', validators=[Optional()], choices=[], coerce=int)
def validate_email(self, field):
from wtforms import ValidationError
user_with_email = User.query.filter_by(email=field.data).first()
if user_with_email:
raise ValidationError('This email is already registered.')
class AmsAnalyticsForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_client_id = StringField(_('AMS Analytics Client_id'), [DataRequired(), Length(1, 128)])
api_url = StringField(_('AMS Analytics API URL'), [DataRequired(), Length(1, 128)])
class AmsPrequalifyForm(FlaskForm):
active = BooleanField(_('Integration Enabled'))
api_client_id = StringField(_('AMS Prequalify Client_id'), [DataRequired(), Length(1, 128)])
api_url = StringField(_('AMS Prequalify API URL'), [DataRequired(), Length(1, 128)])