Ejemplo n.º 1
0
    def get_fields_sql(self):
        def value_to_type(value):
            TYPES = {
                None: 'char',
                int: 'integer',
                str: 'char',
                float: 'float',
                bool: 'boolean',
                Decimal: 'numeric',
                datetime: 'datetime',
            }
            return TYPES.get(type(value), 'char')

        res = []
        cursor = Transaction().connection.cursor()
        cursor.execute(self.query)
        fetchall = list(cursor_dict(cursor, size=1))
        for record in fetchall:
            res = [{
                'name': key,
                'alias': key,
                'type': value_to_type(value),
            } for key, value in record.items()]
            break
        return res
    def get_context(cls, records, data):
        report_context = super(PaymentDescriptionList,
                               cls).get_context(records, data)

        pool = Pool()
        cursor = Transaction().connection.cursor()

        Company = pool.get('company.company')

        table1 = pool.get('condo.party').__table__()
        table2 = pool.get('condo.unit').__table__()

        preport = []

        for r in records:
            companies = Company.search_read(
                [
                    'OR', [('id', '=', r.company.id)],
                    [('parent', 'child_of', r.company.id)]
                ],
                order=[('party.name', 'ASC')],
                fields_names=['id', 'party.name'],
            )

            report = []

            for c in companies:
                cursor.execute(*table1.join(
                    table2, condition=table1.unit == table2.id).select(
                        table2.name,
                        table1.role,
                        where=((table1.mandate != None)
                               & (table2.company == c['id'])),
                        order_by=(Asc(table2.name), Asc(table1.role)),
                    ))
                item = {
                    'company': c['party.name'],
                    'units': list(cursor_dict(cursor, size=None))
                }
                if len(item['units']):
                    report.append(item)

            if len(report):
                item = {'reference': r.reference, 'condo': report}
                preport.append(item)

        report_context['pgroups'] = preport

        return report_context
Ejemplo n.º 3
0
    def do_update(self, action):
        pool = Pool()
        Translation = pool.get('ir.translation')

        cursor = Transaction().connection.cursor()
        lang = self.start.language.code
        translation = Translation.__table__()

        types = ['nereid_template', 'wtforms', 'nereid']
        columns = [
            translation.name.as_('name'),
            translation.res_id.as_('res_id'),
            translation.type.as_('type'),
            translation.src.as_('src'),
            translation.module.as_('module'),
            translation.comments.as_('comments'),
        ]
        cursor.execute(*(
            translation.select(
                *columns,
                where=(translation.lang == 'en_US') &
                translation.type.in_(types)) -
            translation.select(
                *columns,
                where=(translation.lang == lang) &
                translation.type.in_(types))
        ))
        to_create = []
        for row in cursor_dict(cursor):
            to_create.append({
                'name': row['name'],
                'res_id': row['res_id'],
                'lang': lang,
                'type': row['type'],
                'src': row['src'],
                'module': row['module'],
                'comments': row['comments'],
            })
        if to_create:
            with Transaction().set_user(0):
                Translation.create(to_create)
        return super(TranslationUpdate, self).do_update(action)
Ejemplo n.º 4
0
    def do_update(self, action):
        pool = Pool()
        Translation = pool.get('ir.translation')

        cursor = Transaction().connection.cursor()
        lang = self.start.language.code
        translation = Translation.__table__()

        types = ['nereid_template', 'wtforms', 'nereid']
        columns = [
            translation.name.as_('name'),
            translation.res_id.as_('res_id'),
            translation.type.as_('type'),
            translation.src.as_('src'),
            translation.module.as_('module'),
            translation.comments.as_('comments'),
        ]
        cursor.execute(*(translation.select(*columns,
                                            where=(translation.lang == 'en_US')
                                            & translation.type.in_(types)) -
                         translation.select(*columns,
                                            where=(translation.lang == lang)
                                            & translation.type.in_(types))))
        to_create = []
        for row in cursor_dict(cursor):
            to_create.append({
                'name': row['name'],
                'res_id': row['res_id'],
                'lang': lang,
                'type': row['type'],
                'src': row['src'],
                'module': row['module'],
                'comments': row['comments'],
            })
        if to_create:
            with Transaction().set_user(0):
                Translation.create(to_create)
        return super(TranslationUpdate, self).do_update(action)
Ejemplo n.º 5
0
    def read(cls, ids, fields_names=None):
        sql_table = cls.get_sql_table()
        table = cls.get_table()

        if not ids:
            return []

        cursor = Transaction().connection.cursor()
        cursor.execute(*sql_table.select(where=sql_table.id.in_(ids)))
        fetchall = list(cursor_dict(cursor))

        to_cast = {}
        for field in table.fields:
            if fields_names and not field.name in fields_names:
                continue
            cast = FIELD_TYPE_CAST[field.type]
            if cast:
                to_cast[field.name] = cast

        if to_cast:
            for record in fetchall:
                for field, cast in to_cast.items():
                    record[field] = cast(record[field])
        return fetchall
Ejemplo n.º 6
0
    def __register__(cls, module_name):
        pool = Pool()
        Move = pool.get('stock.move')
        PurchaseLine = pool.get('purchase.line')
        PurchaseRequest = pool.get('purchase.request')
        SaleLine = pool.get('sale.line')
        Location = pool.get('stock.location')
        move = Move.__table__()
        purchase_line = PurchaseLine.__table__()
        purchase_request = PurchaseRequest.__table__()
        sale_line = SaleLine.__table__()
        location = Location.__table__()
        cursor = Transaction().connection.cursor()

        super(ShipmentDrop, cls).__register__(module_name)

        # Migration from 3.6
        cursor.execute(*location.select(Count(location.id),
                                        where=(location.type == 'drop')))
        has_drop_shipment, = cursor.fetchone()

        if not has_drop_shipment:
            drop_shipment = Location(name='Migration Drop Shipment',
                                     type='drop',
                                     active=False)
            drop_shipment.save()
            drop_shipment_location = drop_shipment.id

            move_sale_query = move.join(
                purchase_line,
                condition=move.origin ==
                Concat('purchase.line,', purchase_line.id)).join(
                    purchase_request,
                    condition=purchase_request.purchase_line ==
                    purchase_line.id).join(
                        sale_line,
                        condition=sale_line.purchase_request ==
                        purchase_request.id).select(
                            move.id,
                            move.to_location,
                            sale_line.id,
                            where=move.shipment.like('stock.shipment.drop,%'))
            cursor.execute(*move_sale_query)
            move_sales = cursor.fetchall()

            for sub_move in grouped_slice(move_sales):
                sub_ids = [s[0] for s in sub_move]
                cursor.execute(*move.update(columns=[move.to_location],
                                            values=[drop_shipment_location],
                                            where=move.id.in_(sub_ids)))

            cursor.execute(*move.select(limit=1))
            moves = list(cursor_dict(cursor))
            if moves:
                move_columns = moves[0].keys()
                columns = [Column(move, c) for c in move_columns if c != 'id']
                create_move = move.insert(
                    columns=columns,
                    values=move.select(
                        *columns,
                        where=move.shipment.like('stock.shipment.drop,%')))
                cursor.execute(*create_move)

            for move_id, customer_location, line_id in move_sales:
                cursor.execute(
                    *move.update(columns=[
                        move.origin, move.from_location, move.to_location
                    ],
                                 values=[
                                     Concat('sale.line,', str(line_id)),
                                     drop_shipment_location, customer_location
                                 ],
                                 where=(move.id == move_id)))
Ejemplo n.º 7
0
    def read(cls, ids, fields_names=None):
        sql_table = cls.get_sql_table()
        table = cls.get_table()

        if not ids:
            return []

        def read_related(field_name, Target, rows, fields):
            target_ids = []
            for row in rows:
                value = row[field_name]
                if value is not None:
                    target_ids.append(value)
            return Target.read(target_ids, fields)

        def add_related(field_name, rows, targets):
            '''
            Adds 'id' and 'rec_name' of many2one/related_model fields
            Also adds 'rec_name' for the rows
            '''
            key = field_name + '.'
            for row in rows:
                value = row[field_name]
                if isinstance(value, str):
                    value = int(value.split(',', 1)[1])
                if value is not None and value >= 0:
                    row[key] = targets[value]
                    if 'rec_name' in targets[value]:
                        row['rec_name'] = targets[value]['rec_name']
                else:
                    row[key] = None
                if 'rec_name' not in row:
                    row['rec_name'] = str(row['id'])

        cursor = Transaction().connection.cursor()
        cursor.execute(*sql_table.select(where=sql_table.id.in_(ids)))
        fetchall = list(cursor_dict(cursor))

        fields_related = {
            'compilation': 'lims.interface.compilation',
            'notebook_line': 'lims.notebook.line'
            }
        for f in table.fields_:
            if f.related_model is not None:
                fields_related[f.name] = f.related_model.model

        for field in fields_related:
            Target = Pool().get(fields_related[field])
            if Target:
                targets = read_related(
                    field, Target, fetchall, ['id', 'rec_name'])
                targets = {t['id']: t for t in targets}
            else:
                targets = {}
            add_related(field, fetchall, targets)

        to_cast = {}
        for field in table.fields_:
            if fields_names and field.name not in fields_names:
                continue
            cast = FIELD_TYPE_CAST[field.type]
            if cast:
                to_cast[field.name] = cast

        if to_cast:
            for record in fetchall:
                for field, cast in to_cast.items():
                    record[field] = cast(record[field])
        return fetchall