async def post(self): data = await self.request.json() event = await self.request.app.objects.create(self.model, **data) if not Cache.get('PAYD_CONDITIONS'): await Rate.create_payd_conditions(self.request.app) is_payd, full_info = await event.is_payd(self.request.app) if is_payd is not None: statistics_data = { 'date': str(full_info['date']), 'partner': full_info['partner'], 'ad': full_info['ad'], 'country': full_info['country'], 'is_payd': is_payd, 'reg': event.is_reg_event, 'tutorial': event.is_tutorial_event, 'project': event.project, } # if is_payd: # sended = await self.send_to_partner(full_info) # statistics_data['postbacks'] = int(sended) await self.send_to_statistics(statistics_data) return web.Response( content_type='application/json', body=json.dumps(self.serialize(event)).encode(), )
async def is_payd(self, app): # sql = """ # SELECT * FROM event # INNER JOIN click ON (click.user_id = event.user_id) # WHERE event.user_id = {}; # """.format(self.user_id) qs = (Event.select(Event, Click).join( Click, peewee.JOIN.INNER, on=(Click.user_id == Event.user_id).alias('click'))) try: obj = await app.objects.get(qs, id=self.id) except (Event.DoesNotExist, Click.DoesNotExist): return None, {} try: payd = obj.event in Cache.get( 'PAYD_CONDITIONS', {})[obj.click.partner][obj.click.country][obj.project] except KeyError: payd = False return (payd, { 'event': obj.event, 'user_id': obj.user_id, 'date': obj.timestamp.date(), 'ad': obj.click.ad, 'country': obj.click.country, 'partner': obj.click.partner, 'project': obj.project, })