Skip to Content
Menu
This question has been flagged
1 Reply
10330 Views
    _logger = logging.getLogger(__name__)

list_type = [
    ('full', 'Full Container'),
    ('loose', 'Loose Freight')
]

list_mode = [
    ('D2D', 'CY/CY - (Door to Door)'),
    ('D2P', 'CY/CY - (Door to Port)'),
    ('P2D', 'CY/CY - (Port to Door)'),
    ('P2P-1', 'CY/CY - (Port to Port)'),
    ('P2P-2', 'CFS/CY - (Port to Port)'),
    ('P2P-3', 'CFS/CFS - (Port to Port)'),
    ('other', 'Others')
]

list_container_size = [
    ('20"C', '20 Inch Container'),
    ('40"C', '40 Inch Container'),
    ('40"HQ', '40 Inch HQ Container'),
    ('40"R', '40 Inch Refer Container'),
    ('45"C', '45 Inch Container'),
    ('45"HQ', '45 Inch HQ Container')
]

list_transit_time = []
for i in range(1,365):
    if i == 1 :
        list_transit_time.append((i,str(i) + ' Day'))
    else:
        list_transit_time.append((i,str(i) + ' Days'))


#here generating the charges key which will be act as a unique key
def _generate_unique_key(self, cr, uid, ids, field_name, arg, context=None):
    result = {}
    for record in self.browse(cr, uid, ids, context=context):
        result[record.id] = str(record.port_lading_id.id) + '-' + str(record.port_unlading_id.id) + '-' + \
                            str(record.type) + '-' + str(record.mode) + '-' + str(record.via)
    return result


class shipment_charge(osv.osv):
    _name = 'shipment.charge'
    _columns = {
        'port_lading_id': fields.many2one('shipment.port', 'Port Lading', required=True, domain=[('port_lading', '=', 1)], store=True),
        'port_unlading_id': fields.many2one('shipment.port', 'Port Unlading', required=True, domain=[('port_unlading', '=' , 1)], store=True),
        'shipment_charge_price_ids': fields.one2many ('shipment.charge.price', 'shipment_charge_id', 'Container Quote'),
        'type': fields.selection(list_type, 'Type', required=True, help="Select Full Or loose Freight"),
        'mode': fields.selection(list_mode, 'Mode', required=True, help="Mode Of Container shipment"),
        'transit_time': fields.selection(list_transit_time, 'Transit Time', required=True, help="Transit Time In days"),
        'via':  fields.char('Via', size=64, help="Route , Via"),
        #two fields are missing have to update, agent_id and supplier_id
        'expiry_date': fields.date('Expiry Date',size=30, required=True, help='expiry date of this rates/charges after this date '
                                                               'charges ma be differ.'),
        'notes': fields.text('Notes', help='Notes Extra Information'),
        'charges_key':  fields.function(_generate_unique_key,type='char', method=True, store=True,
                                        string='charges_key', help="Charges key is a unique key for charges with "
                                                                   "combination of POL, POD, Agent, Supplier, Via, "
                                                                   "Mode separated with hyphens"),
    }

    _sql_constraints = [
        ('charges_key_unique', 'unique (charges_key)','The Charges key is not Unique'),
    ]

    #here generating the charges key which will be act as a unique key
    def onchange_generate_unique_key(self, cr, uid, ids, port_lading_id, port_unlading_id):
        _logger.warning("IMPORT-LOG : in onchange function")
        charges_key = str(port_lading_id) + '-' + str (port_unlading_id)
        return {'value': {'charges_key': charges_key}}

Above is my module code , and i have a written custom function _generate_unique_key() which generates the unique charges as per

_sql_constraints = [
    ('charges_key_unique', 'unique (charges_key)','The Charges key is not Unique'),
]

but function freezes when it give _sql_constraints errors

error in log is IntegrityError: duplicate key value violates unique constraint "shipment_charge_charges_key_unique" DETAIL: Key (charges_key)=(1-3-full-D2D-Dir) already exists.

please suggest how to raise error when it counters the duplicate value

Thanks in advance

Avatar
Discard
Best Answer

Hi there,

If you want to show an alert or popup for Error/Warning, then you may try the following :

-> from openerp.tools.translate import _ (at the beginning of the file)

-> then you for validating unique constraint you can define your custom method and pass the value to it.

-> for alert/popup when error occurs, you can use :

raise osv.except_osv(_("Warning"), _("My warning Msg"))

or, if you want an error popup try this : raise osv.except_osv(_("Error!"), _("My error Msg"))

Please check this one and update me if this is useful for you or not, and if you want I can share a code snippet of mine where I've referred to a custom validation method for doing operation upon specific condition. (if.....else)

Cheers

:)

Avatar
Discard
Author

Thanks but how can i use this with

'charges_key': fields.function(_generate_unique_key,type='char', method=True, store=True,

Related Posts Replies Views Activity
0
Dec 23
1892
2
Dec 19
9021
0
Nov 19
4233
1
Jul 19
6948
0
Jul 19
2423