#
# Newfies-Dialer License
# http://www.newfies-dialer.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2014 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <info@star2billing.com>
#

from django.utils.safestring import mark_safe
from django.template.defaultfilters import register
from common.common_functions import word_capital
import re


def striphtml(data):
    p = re.compile(r'<.*?>')
    return mark_safe(p.sub('', data))


@register.simple_tag(name='field_html_code')
def field_html_code(field, col_class_1='col-md-6', col_class_2='col-xs-8'):
    """
    Usage: {% field_html_code field 'col-md-6' 'col-xs-8' %}
    """
    error_class = ''
    if field.errors:
        error_class = 'has-error'

    div_string = '<div class="%s">' % col_class_1
    div_string += '<div class="form-group %s">' % (error_class)
    div_string += '<div class="%s"><label class="control-label" for="%s">%s</label>%s' % (
        col_class_2, field.auto_id, word_capital(field.label), field)

    if field.errors:
        div_string += '<span class="help-block">%s</span>' % striphtml(str(field.errors)).capitalize()

    div_string += '<span class="help-block">%s</span>' % (field.help_text.capitalize())
    div_string += '</div></div></div>'

    return mark_safe(div_string)

@@ -552,16 +552,104 @@
 rt re%0A%0A%0A
+def stab(tab=1):%0A    %22%22%22%0A    create space tabulation%0A    %22%22%22%0A    return ' ' * 4 * tab%0A%0A%0A
 def stri
@@ -1035,24 +1035,26 @@
  class=%22%25s%22%3E
+%5Cn
 ' %25 col_clas
@@ -1066,32 +1066,42 @@
    div_string +=
+ stab(1) +
  '%3Cdiv class=%22fo
@@ -1113,16 +1113,18 @@
 oup %25s%22%3E
+%5Cn
 ' %25 (err
@@ -1150,16 +1150,26 @@
 tring +=
+ stab(2) +
  '%3Cdiv c
@@ -1178,16 +1178,63 @@
 ss=%22%25s%22%3E
+%5Cn' %25 col_class_2%0A    div_string += stab(3) + '
 %3Clabel c
@@ -1273,18 +1273,18 @@
 %3C/label%3E
-%25s
+%5Cn
 ' %25 (%0A  
@@ -1292,21 +1292,8 @@
     
- col_class_2,
  fie
@@ -1333,16 +1333,52 @@
 bel)
-,
+)%0A    div_string += stab(3) + '%25s%5Cn' %25
  field
-)
 %0A%0A  
@@ -1409,32 +1409,42 @@
    div_string +=
+ stab(3) +
  '%3Cspan class=%22h
@@ -1459,24 +1459,26 @@
 k%22%3E%25s%3C/span%3E
+%5Cn
 ' %25 striphtm
@@ -1529,16 +1529,26 @@
 tring +=
+ stab(3) +
  '%3Cspan 
@@ -1575,16 +1575,18 @@
 s%3C/span%3E
+%5Cn
 ' %25 (fie
@@ -1634,29 +1634,96 @@
  += 
-'%3C/div%3E%3C/div%3E
+stab(2) + '%3C/div%3E%5Cn'%0A    div_string += stab(1) + '%3C/div%3E%5Cn'%0A    div_string += '
 %3C/div%3E
+%5Cn
 '
-%0A
 %0A   

update field_html_code