@@ -119,37 +119,63 @@ def planning_auction(auction, mapper, start, db, quick=False, lot_id=None):
119119def check_auction (auction , db , mapper ):
120120 now = get_now ()
121121 quick = os .environ .get ('SANDBOX_MODE' , False ) and u'quick' in auction .get ('submissionMethodDetails' , '' )
122- if not auction .get ('lots' ) and 'shouldStartAfter' in auction .get ('auctionPeriod' , {}) and auction ['auctionPeriod' ]['shouldStartAfter' ] > auction ['auctionPeriod' ].get ('startDate' ):
123- period = auction .get ('auctionPeriod' )
124- shouldStartAfter = max (parse_date (period .get ('shouldStartAfter' ), TZ ).astimezone (TZ ), now )
125- planned = False
126- while not planned :
127- try :
128- auctionPeriod , stream , skip_days = planning_auction (auction , mapper , shouldStartAfter , db , quick )
129- planned = True
130- except ResourceConflict :
131- planned = False
132- auctionPeriod = randomize (auctionPeriod ).isoformat ()
133- planned = 'replanned' if period .get ('startDate' ) else 'planned'
134- LOGGER .info (
135- '{} auction for auction {} to {}. Stream {}.{}' .format (
136- planned .title (), auction ['id' ], auctionPeriod , stream , skipped_days (skip_days )
137- ),
138- extra = {
139- 'MESSAGE_ID' : '{}_auction_auction' .format (planned ),
140- 'PLANNED_DATE' : auctionPeriod ,
141- 'PLANNED_STREAM' : stream ,
142- 'PLANNED_DAYS_SKIPPED' : skip_days }
122+
123+ if not auction .get ('lots' ):
124+ period = auction .get ('auctionPeriod' , {})
125+ shouldStartAfter = parse_date (period ['shouldStartAfter' ], TZ ).astimezone (TZ ) if period .get ('shouldStartAfter' ) else None
126+ start_date = parse_date (period ['startDate' ], TZ ).astimezone (TZ ) if period .get ('startDate' ) else None
127+
128+ is_needed_to_add_auction_period = bool (
129+ shouldStartAfter and
130+ (
131+ start_date is None or
132+ shouldStartAfter > start_date
133+ )
143134 )
144- return {'auctionPeriod' : {'startDate' : auctionPeriod }}
135+
136+ if shouldStartAfter and is_needed_to_add_auction_period :
137+ shouldStartAfter = max (parse_date (period .get ('shouldStartAfter' ), TZ ).astimezone (TZ ), now )
138+ planned = False
139+ while not planned :
140+ try :
141+ auctionPeriod , stream , skip_days = planning_auction (auction , mapper , shouldStartAfter , db , quick )
142+ planned = True
143+ except ResourceConflict :
144+ planned = False
145+ auctionPeriod = randomize (auctionPeriod ).isoformat ()
146+ planned = 'replanned' if period .get ('startDate' ) else 'planned'
147+ LOGGER .info (
148+ '{} auction for auction {} to {}. Stream {}.{}' .format (
149+ planned .title (), auction ['id' ], auctionPeriod , stream , skipped_days (skip_days )
150+ ),
151+ extra = {
152+ 'MESSAGE_ID' : '{}_auction_auction' .format (planned ),
153+ 'PLANNED_DATE' : auctionPeriod ,
154+ 'PLANNED_STREAM' : stream ,
155+ 'PLANNED_DAYS_SKIPPED' : skip_days }
156+ )
157+ return {'auctionPeriod' : {'startDate' : auctionPeriod }}
158+
145159 elif auction .get ('lots' ):
146160 lots = []
147161 for lot in auction .get ('lots' , []):
148- if lot ['status' ] != 'active' or 'shouldStartAfter' not in lot .get ('auctionPeriod' , {}) or lot ['auctionPeriod' ]['shouldStartAfter' ] < lot ['auctionPeriod' ].get ('startDate' ):
162+ period = lot .get ('auctionPeriod' , {})
163+ shouldStartAfter = parse_date (period ['shouldStartAfter' ], TZ ).astimezone (TZ ) if period .get (
164+ 'shouldStartAfter' ) else None
165+ start_date = parse_date (period ['startDate' ], TZ ).astimezone (TZ ) if period .get ('startDate' ) else None
166+
167+ skip_lot = bool (
168+ shouldStartAfter is None or (
169+ start_date is not None and
170+ start_date > shouldStartAfter
171+ )
172+ )
173+
174+ if lot ['status' ] != 'active' or skip_lot :
149175 lots .append ({})
150176 continue
151177 period = lot .get ('auctionPeriod' )
152- shouldStartAfter = max (parse_date ( period . get ( ' shouldStartAfter' ), TZ ). astimezone ( TZ ) , now )
178+ shouldStartAfter = max (shouldStartAfter , now )
153179 lot_id = lot ['id' ]
154180 planned = False
155181 while not planned :
0 commit comments