#!/usr/bin/env python3
from collections import OrderedDict
from html import escape
#Forms para python3
class BaseForm:
"""The class used by all forms classes
BaseForm is the base class used for all form classes.
A form class is used for generate simple html forms, how input type, text type, hidden type, etc. PhangoField classes use this forms for generate automatically forms using GenerateAdminClass and others.
"""
def __init__(self, name, value):
"""
Args:
name (str): The html name for this form
value (str): The default value of this html form.
Attributes:
label (str): Label used in functions how show_form that generate a complete html form from a form class list
name (str): Name of the html form.
default_value (mixed): The default value of the form. Equal to value in typical html form.
css (str): Used for add css classes to the html form
type (str): Variable used for conventional html forms with html tag
field (PhangoField): Field related with this form. Used in PhangoField.
required (boolean): If form is required or not, used in functions that generate forms.
name_field_id (str): The html id for the html form. Used for html things.
help (str): A string with help text, used in functions that generate forms.
"""
self.label=name
self.name=name
self.default_value=value
self.css=''
self.type='text'
self.field=None
self.required=False
self.txt_error=''
self.error=False
self.name_field_id=self.name+'_form'
self.help=''
self.placeholder=''
def form(self):
"""Method for returm the html code of the form
"""
return ''
def show_formatted(self, value):
"""Method for show the value of form formatted
Args:
value (mixed): The value of field form
"""
return value
#Method for escape value for html input. DON'T CHANGE IF YOU DON'T KNOWN WHAT ARE YOU DOING
def setform(self, value):
"""A method for set the value in the form for escape and other things
Args:
value (mixed): The value of field form for set
"""
value=str(value)
return value.replace('"', '"').replace("'", ''')
def change_name(self, new_name):
"""A method for change the default form html name of the field form
Args:
new_name (str): The new name of the form. Always is finished with _form suffix
"""
self.name=new_name
self.name_field_id=self.name+'_form'
return ""
class SimpleTextForm(BaseForm):
"""Form for simple text
"""
def __init__(self, name, value):
super().__init__(name, value)
self.after_text=''
def form(self):
return super().form()+' '+self.after_text
class TextForm(BaseForm):
"""Form for simple text form
"""
def __init__(self, name, value):
super(TextForm, self).__init__(name, value)
def form(self):
return ''
class PasswordForm(BaseForm):
"""Form for password forms
"""
def __init__(self, name, value, show_password=False):
super(PasswordForm, self).__init__(name, value)
self.type='password'
self.show_password=show_password
def setform(self, value):
if not self.show_password:
return ""
else:
return value
class HiddenForm(BaseForm):
"""Form for hidden forms
"""
def __init__(self, name, value):
super(HiddenForm, self).__init__(name, value)
self.type='hidden'
class SelectForm(BaseForm):
"""Form for select html form
"""
def __init__(self, name, value, elements=OrderedDict()):
"""
Args:
name (str): The html name for this form
value (str): The default value of this html form
elements (OrderedDict): An ordered dict with the keys(the form value) and text label. Example, if you have a OrderedDict how {'0': 'Value selected'} in a html select form you have the next result: