def post(self): if not self.request.get('Caller'): raise ValueError('No caller available for call %s' % self.request.get('CallGuid')) caller = Subscriber.get_by_key_name(self.request.get('Caller')) if self.request.get('Digits'): if not caller: # create new subscriber caller = Subscriber( key_name = str(self.request.get('Caller')), phone_number = self.request.get('Caller') ) caller.days_subscribed = int(self.request.get('Digits')) if caller.days_subscribed > app_settings.MAX_DAYS: caller.days_subscribed = app_settings.MAX_DAYS caller.call_guid = self.request.get('CallGuid') caller.zip_code = int(self.request.get('CallerZip')) caller.put() # add call scheduler as background task defer(methods.schedule_checks, caller.key().name(), caller.days_subscribed) if caller: days_subscribed = caller.days_subscribed else: days_subscribed = None self.context = { 'days_subscribed': days_subscribed, 'base_url': 'http://' + os.environ['HTTP_HOST'] } return xml_response(self, 'gather.xml', self.context)
def customerbuyconfirm(customer_id): product_c = db.session.query(Product).filter_by(id=request.args.get('productid')).first_or_404() customer_c = db.session.query(Customer).filter_by(id=customer_id).first_or_404() money = product_c.value*int(request.args.get('productbuynum')) print(money) if request.method == 'POST': print('POST') if request.args.get('pppoename') is not None and request.args.get('pppoepassword') is not None and request.args.get('productbuynum') is not None: print(request.args.get('onusn')) pppoeendtime = date.today().replace(year=date.today().year + int(request.args.get('productbuynum')), day=date.today().day + 1) print(pppoeendtime) sub = Subscriber(customerid=customer_id, onusn=request.args.get('onusn'), pppoename=request.args.get('pppoename'), pppoepassword=request.args.get('pppoepassword'), productid=product_c.id, pppoeendtime=pppoeendtime, status=1) db.session.add(sub) db.session.commit() bill = Bill(userid=current_user.id, customerid=customer_id, subscriberid=sub.id, billtime=datetime.now(), money=money, productid=product_c.id, productbuynum=int(request.args.get('productbuynum'))) db.session.add(bill) db.session.commit() db.session.add(Radcheck(username=sub.pppoename, attribute='Cleartext-Password', op=':=', value=sub.pppoepassword)) db.session.add(Radusergroup(username=sub.pppoename, groupname=product_c.id)) db.session.add(Task(name='add', userid=current_user.id, customerid=customer_id, subscriberid=sub.id, billid=bill.id, productid=product_c.id, status=1)) db.session.add( Task(name='del', userid=current_user.id, customerid=customer_id, subscriberid=sub.id, billid=bill.id, productid=product_c.id, status=0, crontime=pppoeendtime)) db.session.commit() return redirect(url_for('customer_detail', customer_id=customer_id)) return render_template('customer_buy_confirm.html', product=product_c, customer=customer_c, productbuynum=request.args.get('productbuynum'), pppoename=request.args.get('pppoename'), pppoepassword=request.args.get('pppoepassword'), onusn=request.args.get('onusn'), total=money)
def db_exists(user, twitch_id): ''' Returns whether the subscriber already exists in the database for this particular user :param user: The user who may own the particular subscriber :param twitch_id: The Twitch ID of the user ''' return SubModel.select().where(SubModel.user == user, # @UndefinedVariable SubModel.twitchid == twitch_id).exists() # @UndefinedVariable
def db_exists(user, twitch_id): ''' Returns whether the subscriber already exists in the database for this particular user :param user: The user who may own the particular subscriber :param twitch_id: The Twitch ID of the user ''' return SubModel.select().where( SubModel.user == user, # @UndefinedVariable SubModel.twitchid == twitch_id).exists() # @UndefinedVariable
def scheduled_check(phone_number, call_guid): """ deferred scheduled check - send reminder if sufficient time has passed """ caller = Subscriber.get_by_key_name(str(phone_number)) if caller.call_guid != call_guid: logging.critical("Call Guid has been reset - aborting reminder check") return if caller.last_scan < (datetime.datetime.now() - datetime.timedelta(hours=2)): import web_services.twilio web_services.twilio.sendTextMessage( caller.phone_number, app_settings.REMINDER_MSG) logging.info('Sending text message to %s' % caller.key().name()) else: logging.info('Not sending text message to %s' % caller.key().name())
def update_subs(self, user): ''' Updates the subscribers for a particular user :param user: The ActiveUser model object to update for ''' stats = (0, 0) twitch_current = Subscriber.fetch_all(user.twitchusername, user.twitchtoken) if twitch_current == None: return twitch_ids = [x['user']['_id'] for x in twitch_current] current_models = SubModel.select(SubModel.twitchid)\ .where(SubModel.user == user, SubModel.active) db_current = [x.twitchid for x in current_models] new_subs = [ x for x in twitch_current if x['user']['_id'] not in db_current ] un_subs = [x for x in db_current if x not in twitch_ids] for sub in new_subs: new_sub = SubTracker.create_sub_model(user, sub['user']) new_sub.active = 1 new_sub.save() redis = AlerterRedis() if un_subs: SubModel.update(active=False, unsubdate=datetime.now())\ .where(SubModel.twitchid << un_subs, SubModel.user == user)\ .execute() for unsub_user in SubModel.select(SubModel.username)\ .where(SubModel.twitchid << un_subs, SubModel.user == user): redis.del_subscriber(user, unsub_user.username) stats = (len(new_subs), len(un_subs)) self.stats = tuple(map(sum, zip(self.stats, stats)))
def update_subs(self, user): ''' Updates the subscribers for a particular user :param user: The ActiveUser model object to update for ''' stats = (0, 0) twitch_current = Subscriber.fetch_all(user.twitchusername, user.twitchtoken) if twitch_current == None: return twitch_ids = [x['user']['_id'] for x in twitch_current] current_models = SubModel.select(SubModel.twitchid)\ .where(SubModel.user == user, SubModel.active) db_current = [x.twitchid for x in current_models] new_subs = [x for x in twitch_current if x['user']['_id'] not in db_current] un_subs = [x for x in db_current if x not in twitch_ids] for sub in new_subs: new_sub = SubTracker.create_sub_model(user, sub['user']) new_sub.active = 1 new_sub.save() redis = AlerterRedis() if un_subs: SubModel.update(active=False, unsubdate=datetime.now())\ .where(SubModel.twitchid << un_subs, SubModel.user == user)\ .execute() for unsub_user in SubModel.select(SubModel.username)\ .where(SubModel.twitchid << un_subs, SubModel.user == user): redis.del_subscriber(user, unsub_user.username) stats = (len(new_subs), len(un_subs)) self.stats = tuple(map(sum,zip(self.stats, stats)))
def create_subscriber(channel_name, month_end_at, subscribers, notes, last_updated): subscriber = Subscriber(channel_name=channel_name, month_end_at=month_end_at, subscribers=subscribers, notes=notes, last_updated=last_updated) db.session.add(subscriber) db.session.commit() return subscriber
def schedule_checks(phone_number, days_subscribed): """ schedule checks at precise intervals throughout day for a subscriber """ caller = Subscriber.get_by_key_name(str(phone_number)) # TODO: use caller.zip_code to adjust call times for timezones now = datetime.datetime.now() if now.hour < 17: now_day = (now - datetime.timedelta(days=1)) else: now_day = now for day in range(days_subscribed): now_day += datetime.timedelta(days=1) for check_time in (13, 15, 17): check_time -= 4 #for New York. 7 for San Francisco eta = datetime.datetime(year=now_day.year, month=now_day.month,day=now_day.day, hour=check_time) if eta < now: continue defer(scheduled_check, phone_number,caller.call_guid, _eta=eta)
def create_sub_model(user, subscriber): ''' Returns a subscriber model object containing nformation given by the Twitch API JSON data or the DB if it exists :param user: The user associated with the new subscriber :param subscriber: The Twitch API JSON data for the subscriber ''' if SubTracker.db_exists(user, subscriber['_id']): return SubModel.get(SubModel.user == user, # @UndefinedVariable SubModel.twitchid == subscriber['_id']) new_sub = SubModel() new_sub.user = user new_sub.twitchid = subscriber['_id'] new_sub.username = subscriber['name'] new_sub.displayname = subscriber['display_name'] new_sub.adddate = subscriber['created_at']\ .replace('Z', '').replace('T', ' ') return new_sub
def create_sub_model(user, subscriber): ''' Returns a subscriber model object containing nformation given by the Twitch API JSON data or the DB if it exists :param user: The user associated with the new subscriber :param subscriber: The Twitch API JSON data for the subscriber ''' if SubTracker.db_exists(user, subscriber['_id']): return SubModel.get( SubModel.user == user, # @UndefinedVariable SubModel.twitchid == subscriber['_id']) new_sub = SubModel() new_sub.user = user new_sub.twitchid = subscriber['_id'] new_sub.username = subscriber['name'] new_sub.displayname = subscriber['display_name'] new_sub.adddate = subscriber['created_at']\ .replace('Z', '').replace('T', ' ') return new_sub
def setState(self, state): self._state = state def request(self): self._state.handle() if __name__ == '__main__': #myApp = Controller() #myApp.main() MyController = Controller() DisplayProfile = ConcreteStateProfile() DisplayLogin = ConcreteStateLogin() DisplayAddCourse = ConcreteStateAddCourse() context = Context() s1 = Subscriber() s1.addSubName("Tom") state = "Login" while (True): print(state) if state == "Login": context.setState(DisplayLogin) context.request() print( "-------------------------------Setting state to display Login Page ---------------------" ) state = s1.get_data() #print(f' In while loop{state} registered')