Exemple #1
0
    def geraHTMLContent(self, id, tipo, valor, full_text=False):
        if tipo == 'list':
            txt = ''
            for i in self.decodePickle(valor):
                txt += i + ', '

            return txt

        elif tipo == 'img':

            if id:
                return '<img width="100px" src="../form-image?id=%s">' % id
            else:
                return ''

        elif tipo == 'file':
            if id:
                arquivo = self.decodePickle(valor)
                if arquivo != '':
                    name = arquivo.get('filename', '')
                    return '<a href="../form-file?id=%s" target="_blank">%s</a><br />' % (
                        id, name)
                else:
                    return ''
            else:
                return ''

        elif tipo == 'date':
            data = self.decodePickle(valor)
            try:
                return data.strftime('%d/%m/%Y')

            except:
                return ''

        elif tipo == 'choice':
            if id:
                valor_campo = ModelsFormValues().get_Values_byID(id)
                id_form = int(self.context.forms_id)

                if valor_campo:
                    campo = ModelsFormFields().get_Fields_ByField(
                        valor_campo.fields, id_form)

                    items = campo.list_values.splitlines()
                    D = []
                    for i in items:
                        L = i.split(' | ')

                        if len(L) >= 2:
                            if L[0] == valor:
                                return L[1]

            return valor

        elif tipo == 'foreign_key':
            if id:
                valor_campo = ModelsFormValues().get_Values_byID(id)
                id_form = int(self.context.forms_id)

                if valor_campo:
                    campo = ModelsFormFields().get_Fields_ByField(
                        valor_campo.fields, id_form)

                    if campo:
                        form_ref = campo.ref_form

                        form_ref_id = form_ref.id
                        label = form_ref.campo_label
                        key = form_ref.campo_chave

                        if full_text:
                            instance = 0
                            txt = ''

                            v_campos = form_ref.fields
                            campo_busca = ModelsFormValues().store.find(
                                ModelsFormValues,
                                ModelsFormValues.fields == key,
                                ModelsFormValues.forms_id == form_ref_id,
                                ModelsFormValues.value == valor)
                            if campo_busca.count():
                                instance = campo_busca[0].instance_id

                                valores = ModelsFormValues(
                                ).get_FormValues_byForm_and_Instance(
                                    form_ref_id, instance)

                                txt = '<br/>'
                                for v_campo in v_campos:
                                    v_valor = valores.find(
                                        fields=v_campo.name_field).one()
                                    txt += '<b> %s : </b><span> %s </span><br/>' % (
                                        v_campo.title, v_valor.value)

                            return txt

                        else:
                            dados = ModelsFormValues(
                            ).get_FormValues_byForm_and_Field(
                                form_ref_id, key)
                            for item in dados:
                                if item.value == valor:
                                    dados_label = ModelsFormValues(
                                    ).get_FormValues_byForm_and_Instance_and_Field(
                                        form_ref_id, item.instance_id, label)

                                    return dados_label.value

        return valor
class VindulaViewForm(grok.View, BaseFunc):
    grok.context(IFormularioPadrao)
    grok.require('cmf.ListFolderContents')
    grok.name('view-form')  #Dados

    data_instance = []
    data_values = []
    ids_instances = []

    def get_FormValues(self, getall=True):
        id_form = int(self.context.forms_id)
        form = self.request.form

        if 'data_inicial' in form.keys():
            data_inicial = self.str2datetime(
                form.get('data_inicial')) + timedelta(days=0)
        else:
            data_inicial = self.str2datetime(self.get_data_inicial())

        if 'data_final' in form.keys():
            data_final = self.str2datetime(
                form.get('data_final')) - timedelta(days=-1)
        else:
            data_final = self.str2datetime(self.get_data_final())

        self.data_instance = ModelsFormInstance().store.find(
            ModelsFormInstance,
            ModelsFormInstance.forms_id == id_form,
            ModelsFormInstance.date_creation >= data_inicial,
            ModelsFormInstance.date_creation <= data_final,
        ).order_by(Desc(ModelsFormInstance.date_creation))

        self.ids_instances = [int(i.instance_id) for i in self.data_instance]

        L_value = []
        self.data_values = ModelsFormValues().store.find(
            ModelsFormValues, ModelsFormValues.forms_id == id_form,
            ModelsFormValues.date_creation >= data_inicial,
            ModelsFormValues.date_creation <= data_final,
            ModelsFormValues.instance_id.is_in(self.ids_instances))

        for item in self.data_instance:
            data = self.data_values.find(
                ModelsFormValues.instance_id == int(item.instance_id))
            if data.count() > 0:
                L_value.append(data)

        L = []
        for item in L_value:
            if self.checkItem(item, form):
                L.append(item)
        return L

    def get_FormValues_filtro(self):
        id_form = int(self.context.forms_id)
        return ModelsForm().get_FormValues_filtro(id_form)

    def get_Form_fields(self):
        id_form = int(self.context.forms_id)
        return ModelsFormFields().get_Fields_ByIdForm(id_form)

    def get_values_filter(self, fields=[]):
        fields_value = {}

        for field in fields:
            if field.type_fields in [
                    'text', 'bool', 'choice', 'list', 'hidden', 'radio',
                    'foreign_key', 'date'
            ]:
                fields_value[field.name_field] = []
                values = self.data_values.find(
                    ModelsFormValues.fields == field.name_field)
                for value in values:
                    if value.value not in fields_value[field.name_field]:
                        fields_value[field.name_field].append(value.value)

        #Filtro de data de criação
        fields_value['creation'] = []
        for instance in self.data_instance:
            date = instance.date_creation.strftime('%d/%m/%Y')
            if date not in fields_value['creation']:
                fields_value['creation'].append(date)
        return fields_value

    def get_Form_instance(self):
        id_form = int(self.context.forms_id)
        return ModelsFormInstance().get_Instance(id_form)

    def find_group_by(self, valores):
        L = []
        for valor in valores:
            V = valor.value
            if V and not V in L:
                L.append(V)

        return L

    def find_group_by_data(self, valores):
        L = []
        for valor in valores:
            V = valor.date_creation.strftime('%d/%m/%Y %H:%M:%S')
            if not V in L:
                L.append(V)
        return L

    def valores_b(self, all_values, campo):
        L = []
        for i in all_values:
            x = i.find(fields=campo.name_field)
            if x.count():
                x = x[0]
                L.append(x)

        return L

    def canRequestPermission(self, permissao):
        return checkPermission(permissao, self.context)

    def checkItem(self, item, form):
        for campo in form.keys():
            if campo not in [
                    'b_start', 'date_creation', 'data_final', 'data_inicial'
            ]:

                valor = form.get(campo, '')
                field = item.find(fields=self.Convert_utf8(campo)).one()

                if not valor:
                    continue
                if not field:
                    return False

                elif type(valor) == list:
                    existe = False
                    for val in valor:
                        if field:
                            if field.value == self.Convert_utf8(val):
                                existe = True
                                break

                    if not existe:
                        return False
                elif field:
                    if not field.value == self.Convert_utf8(valor):
                        return False

            elif campo == 'date_creation':
                valor = form.get(campo, '')
                if valor and item[0].instancia.date_creation.strftime(
                        '%d/%m/%Y %H:%M:%S') != valor:
                    return False

        return True

    def get_data_final(self):
        date = datetime.now()
        return date.strftime('%d/%m/%Y')

    def get_data_inicial(self):
        date = datetime.now() - timedelta(days=5)
        return date.strftime('%d/%m/%Y')

    def str2datetime(self, str):
        split_date = str.split('/')
        try:
            return datetime(int(split_date[2]), int(split_date[1]),
                            int(split_date[0]))
        except ValueError:
            return datetime.now()

    def get_data(self, item, campo):
        return item.find(fields=campo.name_field).one()
class VindulaViewForm(grok.View, BaseFunc):
    grok.context(IFormularioPadrao)
    grok.require('cmf.ListFolderContents')
    grok.name('view-form') #Dados


    data_instance = []
    data_values = []
    ids_instances = []

    def get_FormValues(self,getall=True):
        id_form = int(self.context.forms_id)
        form = self.request.form

        if 'data_inicial' in form.keys():
            data_inicial = self.str2datetime(form.get('data_inicial')) + timedelta(days=0)
        else:
            data_inicial = self.str2datetime(self.get_data_inicial())


        if 'data_final' in form.keys():
            data_final = self.str2datetime(form.get('data_final')) - timedelta(days=-1)
        else:
            data_final = self.str2datetime(self.get_data_final())

        self.data_instance = ModelsFormInstance().store.find(
            ModelsFormInstance, ModelsFormInstance.forms_id==id_form,
            ModelsFormInstance.date_creation>=data_inicial,
            ModelsFormInstance.date_creation<=data_final,
        ).order_by(Desc(ModelsFormInstance.date_creation))

        self.ids_instances = [int(i.instance_id) for i in self.data_instance]

        L_value = []
        self.data_values = ModelsFormValues().store.find(
            ModelsFormValues, 
            ModelsFormValues.forms_id == id_form,
            ModelsFormValues.date_creation >= data_inicial,
            ModelsFormValues.date_creation <= data_final,
            ModelsFormValues.instance_id.is_in(self.ids_instances))

        for item in self.data_instance: 
            data = self.data_values.find(ModelsFormValues.instance_id == int(item.instance_id))
            if data.count() > 0:
                L_value.append(data)

        L = []
        for item in L_value:
            if self.checkItem(item, form):
                L.append(item)
        return L

    def get_FormValues_filtro(self):
        id_form = int(self.context.forms_id)
        return ModelsForm().get_FormValues_filtro(id_form)

    def get_Form_fields(self):
        id_form = int(self.context.forms_id)
        return ModelsFormFields().get_Fields_ByIdForm(id_form)

    def get_values_filter(self, fields=[]):
        fields_value = {}

        for field in fields:
            if field.type_fields in ['text', 'bool', 'choice', 'list', 'hidden', 'radio', 'foreign_key', 'date']:
                fields_value[field.name_field] = []
                values = self.data_values.find(ModelsFormValues.fields==field.name_field)  
                for value in values:
                    if value.value not in fields_value[field.name_field]:
                        fields_value[field.name_field].append(value.value)

        #Filtro de data de criação
        fields_value['creation'] = []
        for instance in self.data_instance:
            date = instance.date_creation.strftime('%d/%m/%Y')
            if date not in fields_value['creation']:
                fields_value['creation'].append(date)
        return fields_value


    def get_Form_instance(self):
        id_form = int(self.context.forms_id)
        return ModelsFormInstance().get_Instance(id_form)

    def find_group_by(self, valores):
        L = []
        for valor in valores:
            V = valor.value
            if V and not V in L:
                L.append(V)

        return L

    def find_group_by_data(self, valores):
        L = []
        for valor in valores:
            V = valor.date_creation.strftime('%d/%m/%Y %H:%M:%S')
            if not V in L:
                L.append(V)
        return L

    def valores_b(self, all_values,campo):
        L = []
        for i in all_values:
            x = i.find(fields=campo.name_field)
            if x.count():
                x = x[0]
                L.append(x)

        return L


    def canRequestPermission(self,permissao):
        return checkPermission(permissao, self.context)

    def checkItem(self, item, form):
        for campo in form.keys():
            if campo not in ['b_start','date_creation','data_final','data_inicial']:

                valor = form.get(campo,'')
                field = item.find(fields=self.Convert_utf8(campo)).one()

                if not valor :
                    continue
                if not field:
                    return False

                elif type(valor) == list:
                    existe = False
                    for val in valor:
                        if field:
                            if field.value == self.Convert_utf8(val):
                                existe = True
                                break

                    if not existe:
                        return False
                elif field:
                    if not field.value == self.Convert_utf8(valor):
                        return False

            elif campo == 'date_creation':
                valor = form.get(campo,'')
                if valor and item[0].instancia.date_creation.strftime('%d/%m/%Y %H:%M:%S') != valor:
                    return False

        return True

    def get_data_final(self):
        date = datetime.now()
        return date.strftime('%d/%m/%Y')

    def get_data_inicial(self):
        date = datetime.now() - timedelta(days=5)
        return date.strftime('%d/%m/%Y')

    def str2datetime(self, str):
        split_date = str.split('/')
        try:
            return datetime(int(split_date[2]),
                            int(split_date[1]),
                            int(split_date[0]))
        except ValueError:
            return datetime.now()

    def get_data(self, item, campo):
        return item.find(fields=campo.name_field).one()
Exemple #4
0
    def geraHTMLContent(self,id,tipo,valor,full_text=False):
        if tipo == 'list':
            txt = ''
            for i in self.decodePickle(valor):
                txt += i +', '

            return txt

        elif tipo == 'img':

            if id:
                return '<img width="100px" src="../form-image?id=%s">' % id
            else:
                return ''

        elif tipo == 'file':
            if id:
                arquivo = self.decodePickle(valor)
                if arquivo != '':
                    name = arquivo.get('filename','')
                    return '<a href="../form-file?id=%s" target="_blank">%s</a><br />'%(id,name)
                else:
                    return ''
            else:
                return ''

        elif tipo == 'date':
            data = self.decodePickle(valor)
            try:
                return data.strftime('%d/%m/%Y')

            except:
                return ''


        elif tipo == 'choice':
            if id:
                valor_campo = ModelsFormValues().get_Values_byID(id)
                id_form = int(self.context.forms_id)

                if valor_campo:
                    campo = ModelsFormFields().get_Fields_ByField(valor_campo.fields,id_form)
    
                    items = campo.list_values.splitlines()
                    D=[]
                    for i in items:
                        L = i.split(' | ')
    
                        if len(L) >= 2:
                            if L[0] == valor:
                                return L[1]

            return valor

        elif tipo == 'foreign_key':
            if id:
                valor_campo = ModelsFormValues().get_Values_byID(id)
                id_form = int(self.context.forms_id)







                if valor_campo:
                    campo = ModelsFormFields().get_Fields_ByField(valor_campo.fields,id_form)

                    if campo:
                        form_ref = campo.ref_form

                        form_ref_id = form_ref.id
                        label = form_ref.campo_label
                        key = form_ref.campo_chave

                        if full_text:
                            instance = 0
                            txt = ''

                            v_campos = form_ref.fields 
                            campo_busca = ModelsFormValues().store.find(ModelsFormValues, ModelsFormValues.fields==key,
                                                                                          ModelsFormValues.forms_id==form_ref_id,
                                                                                          ModelsFormValues.value==valor
                                                                        )
                            if campo_busca.count():
                                instance = campo_busca[0].instance_id

                                valores = ModelsFormValues().get_FormValues_byForm_and_Instance(form_ref_id,instance)

                                txt = '<br/>'
                                for v_campo in v_campos:
                                    v_valor = valores.find(fields=v_campo.name_field).one()
                                    txt += '<b> %s : </b><span> %s </span><br/>' %(v_campo.title,v_valor.value)

                            return txt

                        else:
                            dados = ModelsFormValues().get_FormValues_byForm_and_Field(form_ref_id,key)
                            for item in dados:
                                if item.value == valor:
                                    dados_label = ModelsFormValues().get_FormValues_byForm_and_Instance_and_Field(form_ref_id, item.instance_id, label)

                                    return dados_label.value

        return valor