def delete(user, delivery, session=None): if session is None: raise ValueError if delivery.state == DeliveryState.PUBLISH.value: if RewardDepot.exist_reward(user, delivery.id): RewardDepot.del_reward(user, delivery.id) delivery.state = DeliveryState.AUTO_DELETE.value session.add(delivery) TaskDepot.del_delete_task(delivery.deadline)
def afinish(user, delivery): # charge yield RewardDepot.adeduct_reward(user, delivery.id) point_record1 = PointRecord(user_id=user.id, type='找人代送', balance=user.bright_point + user.original_point, number=str(-delivery.reward)) receiver = yield get_db_object_by_attr(User, id=delivery.ruser_id) # receipt receiver.bright_point += delivery.reward point_record2 = PointRecord(user_id=receiver.id, type='帮人代送', balance=receiver.bright_point + receiver.original_point, number='+' + str(delivery.reward)) delivery.state = DeliveryState.FINISH.value yield execute( ('add_all', [delivery, user, receiver, point_record1, point_record2])) yield execute(('commit', None)) yield first_award(user, RECEIVE_AWARD) yield add_engagement(user, RECEIVE_ENGAGEMENT) yield TaskDepot.adel_finish_task(delivery.id)
def adelete(self, user, delivery): if delivery.state < DeliveryState.RECEIVE.value and \ (yield RewardDepot.aexist_reward(user, delivery.id)): yield RewardDepot.adel_reward(user, delivery.id) if delivery.state == DeliveryState.PUBLISH.value: yield TaskDepot.adel_delete_task(delivery.deadline) delivery.state = DeliveryState.FORCE_DELETE.value yield self._asave(delivery)
def finish_job(): from xiaodi.api.mysql import session_scope, Task now = datetime.utcnow() for id_, deadline in TaskDepot.scan_finish_task(): deadline, id_ = tranform_utctime(deadline), int(id_) if now < deadline: break with session_scope() as session: delivery = session.query(Task).filter(Task.id == id_).first() user = gen_temp_object(id=delivery.puser_id) DeliveryManager.finish(user, delivery, session=session) LOG.info('auto finish task finished')
def finish(user, delivery, session=None): if session is None: raise ValueError if delivery.state > DeliveryState.FORCE_DELETE.value and \ delivery.state != DeliveryState.FINISH.value: RewardDepot.deduct_reward(user, delivery.id) point_record1 = PointRecord(user_id=user.id, type='找人代送', balance=user.bright_point + user.original_point, number=str(-delivery.reward)) receiver = session.query(User).filter( User.id == delivery.ruser_id).first() # receipt receiver.bright_point += delivery.reward point_record2 = PointRecord(user_id=receiver.id, type='帮人代送', balance=receiver.bright_point + receiver.original_point, number='+' + str(delivery.reward)) delivery.state = DeliveryState.FINISH.value session.add_all( [delivery, user, receiver, point_record1, point_record2]) TaskDepot.del_finish_task(delivery.id)
def apublish(self, user, delivery): if (yield RewardDepot.aget_available_reward(user)) < delivery.reward: raise gen.Return(forbidden_error('no enough money')) delivery.state = DeliveryState.PUBLISH.value yield self._asave(delivery) yield first_award(user, PUBLISH_AWARD) yield add_engagement(user, PUBLISH_ENGAGEMENT) yield RewardDepot.aset_reward(user, delivery.reward, delivery.id) yield TaskDepot.aadd_delete_task(delivery.deadline, delivery.id) publish_redis_event("".join( json.dumps([delivery.source.split(u'区')[0], user.id]))) raise gen.Return(delivery.id)
def asent(self, user, delivery): delivery.state = DeliveryState.SENT.value yield self._asave(delivery) time_ = datetime.utcnow() + timedelta(days=AUTO_FINISH_DELAY) yield TaskDepot.aadd_finish_task(time_, delivery.id)
def areceive(self, user, delivery): delivery.ruser_id = user.id delivery.state = DeliveryState.RECEIVE.value yield self._asave(delivery) yield TaskDepot.adel_delete_task(delivery.deadline)