def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return name = self.request.get('name') interpreter = self.request.get('interpreter') comment = self.request.get('comment') song = SongNode.get_by_id(name+interpreter) if not song: self.error(404) song.comments.append(comment + ' (' + user.nickname() + ')') song.put() song_url=urllib.quote('/' + bandid + '/song/' + song.name + '/' + song.interpreter) send_notifications(song,'http://' + self.request.host + song_url, 'comment', user) self.redirect(song_url)
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.auth_users: logging.info("letting an unauthed users vote... for now") votes = self.request.get_all('votes') for songid in votes: logging.info('songid:' + songid + 'len: ' + str(len(songid))) song = SongNode.get_by_id(songid) song_url=urllib.quote('/' + bandid + '/song/' + song.name + '/' + song.interpreter) if user not in song.votes: send_notifications(song,'http://' + self.request.host + song_url, 'vote', user) song.votes.append(user) song.vote_cnt += 1 song.graduated = song.vote_cnt >= VOTES_TO_GRADUATE song.put() self.redirect('/' + bandid + '/thanks')
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.auth_users: logging.info("letting an unauthed users vote... for now") name = self.request.get('name').strip() interpreter = self.request.get('interpreter').strip() songid = name + interpreter song = SongNode.get_by_id(songid) if not song: song = SongNode(id=name+interpreter) song.name = name song.interpreter = interpreter song.vote_cnt = 0 song.comments = [] song.links = [] song.votes = [] song.graduated = False song_url=urllib.quote('/' + bandid + '/song/' + name + '/' + interpreter) unvote = self.request.get('undo', default_value=False) if user not in song.votes and not unvote: logging.info(str(user) + ' voted for ' + song.name) song.votes.append(user) song.vote_cnt += 1 send_notifications(song,'http://' + self.request.host + song_url, 'vote', user) elif user in song.votes and unvote == 'true': logging.info(str(user) + ' unvoted for ' + song.name) song.votes.remove(user) song.vote_cnt -= 1 send_notifications(song,'http://' + self.request.host + song_url, 'unvote', user) else: logging.error(str(user) + ' failed to vote/unvote for ' + song.name) return song.graduated = song.vote_cnt >= VOTES_TO_GRADUATE song.put() self.redirect(song_url)
def process_application_updated(sender, **kwargs): application = kwargs.get('application', None) stock = application.stock price = application.price application_sets = [] quantity = application.shares for _application in Application.objects.fetch_suitable_applications(application).prefetch_related(): application_sets.append((_application, min(_application.shares, quantity))) quantity -= _application.shares if quantity <= 0: break if not application_sets: return print application_sets notifications = [{ 'recipient': application.applicant.profile.user, 'verb': u'处理了', 'actor': u'系统', 'target': application, 'action': 'delete', }] for _application, share in application_sets: if application.command == Application.BUY: seller, buyer = _application, application else: seller, buyer = application, _application notifications.append({ 'actor': u'系统', 'verb': u'处理了', 'recipient': _application.applicant.profile.user, 'target': _application, 'action': 'delete', }) stock.transfer(seller, buyer, share) if quantity < 0: notifications[-1]['action'] = 'null' elif quantity > 0: notifications[0]['action'] = 'null' send_notifications(notifications) stock.update_price(price)
def run(): NOTIFICATIONS_THROTTLE = NOTIFICATIONS_TO_SEND if DRY_RUN: with open('barbora.json') as mock: payload = mock.read() response = json.loads(payload) while True: time.sleep(random.randint(60, 120)) if NOTIFICATIONS_THROTTLE == 0: NOTIFICATIONS_THROTTLE = NOTIFICATIONS_TO_SEND print(f"Going to sleep for {HOURS_TO_SLEEP} hours") notifications.send_message_to_teams(f"Going for sleep for {HOURS_TO_SLEEP} hours") time.sleep(sleep_long_as_informed) today = time.ctime() if not DRY_RUN: response = barbora.get_delivery_data() try: resp_str = json.dumps(response) except Exception as e: print(f"Was not able to parse response. Error: {e}") continue if not response: continue slots_during_period= barbora.find_free_slots(response) if not slots_during_period: continue print(f"Slot found at {today}") if not DRY_RUN: string_with_new_lines = ",".join(slots_during_period) notifications.send_notifications(f"Available time:{string_with_new_lines}") print(slots_during_period) NOTIFICATIONS_THROTTLE = NOTIFICATIONS_THROTTLE - 1
def send_notification(event_type, escrow_pubkey): """Send notification to users.""" if not escrow_pubkey or event_type not in ( events.LAUNCHED, events.COURIER_CONFIRMED, events.COURIERED, events.RECEIVED): return package = get_package(escrow_pubkey) notification_body = 'Please check your Packages archive for more details' notification_code = notifications.NOTIFICATION_CODES.get(event_type, 0) if event_type == events.LAUNCHED: notifications.send_notifications( tokens=get_active_tokens(package['recipient_pubkey']), title="You have new package {}".format(package['short_package_id']), body=notification_body, notification_code=notification_code, short_package_id=package['short_package_id']) elif event_type == events.COURIER_CONFIRMED: notifications.send_notifications( tokens=get_active_tokens(package['launcher_pubkey']), title="Courier confirmed for package {}".format(package['short_package_id']), body=notification_body, notification_code=notification_code, short_package_id=package['short_package_id']) elif event_type == events.COURIERED: notifications.send_notifications( tokens=get_active_tokens(package['recipient_pubkey']), title="Your package {} in transit".format(package['short_package_id']), body=notification_body, notification_code=notification_code, short_package_id=package['short_package_id']) elif event_type == events.RECEIVED: notifications.send_notifications( tokens=get_active_tokens(package['launcher_pubkey']), title="Your package {} delivered".format(package['short_package_id']), body=notification_body, notification_code=notification_code, short_package_id=package['short_package_id'])