[3127] | 1 | ## Script (Python) "changeCostInPins" |
---|
| 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
| 7 | ##parameters=batch_id,old_cost |
---|
| 8 | ##title= |
---|
| 9 | ## |
---|
| 10 | # $Id: changeCostInPins.py 3132 2008-02-08 21:10:38Z henrik $ |
---|
| 11 | """ |
---|
| 12 | """ |
---|
| 13 | try: |
---|
| 14 | from Products.zdb import set_trace |
---|
| 15 | except: |
---|
| 16 | def set_trace(): |
---|
| 17 | pass |
---|
| 18 | |
---|
| 19 | mtool = context.portal_membership |
---|
| 20 | member = mtool.getAuthenticatedMember() |
---|
| 21 | if str(member) not in ('admin','joachim'): |
---|
| 22 | return |
---|
| 23 | |
---|
| 24 | import logging |
---|
| 25 | import DateTime |
---|
| 26 | logger = logging.getLogger('Skins.changeCostInPins') |
---|
| 27 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
| 28 | aq_pins = context.portal_pins.evalAdvancedQuery |
---|
| 29 | |
---|
| 30 | request = context.REQUEST |
---|
| 31 | session = request.SESSION |
---|
| 32 | batch = getattr(context.portal_url.getPortalObject().campus.pins,batch_id,None) |
---|
| 33 | if batch is None: |
---|
| 34 | logger.info('No such batch %s' % batch_id) |
---|
| 35 | new_cost = batch.getContent().cost |
---|
| 36 | prefix_batch = batch_id.replace('_','') |
---|
| 37 | query = Eq('prefix_batch',prefix_batch) |
---|
| 38 | pins = aq_pins(query) |
---|
| 39 | if len(pins) is None: |
---|
| 40 | logger.info('No pins with prefix_batch %s' % prefix_batch) |
---|
| 41 | edited = 0 |
---|
| 42 | wrong = 0 |
---|
| 43 | count = 0 |
---|
| 44 | commit_after = 50 |
---|
[3132] | 45 | #logger.info('started to change %d pins cost from %s to %s' % |
---|
| 46 | # (len(pins),old_cost,new_cost)) |
---|
[3127] | 47 | to_edit = [] |
---|
| 48 | for pin in pins: |
---|
| 49 | count += 1 |
---|
| 50 | if pin.cost != float(old_cost): |
---|
| 51 | logger.info("%s: cost %s does not match %s" % (pin.key, |
---|
| 52 | pin.cost, |
---|
| 53 | old_cost)) |
---|
| 54 | wrong += 1 |
---|
| 55 | continue |
---|
| 56 | to_edit += pin.pin, |
---|
[3132] | 57 | logger.info('%d %s pins found, start editing cost from %s to %s' % |
---|
| 58 | (len(pins),batch_id,old_cost,new_cost)) |
---|
[3127] | 59 | for pin in to_edit: |
---|
| 60 | context.portal_pins.modifyRecord(**{'pin': pin, 'cost': float(new_cost)}) |
---|
[3132] | 61 | logger.info('finished editing %d %s pins, %d had wrong cost' % (count, batch_id, |
---|
[3127] | 62 | wrong)) |
---|