def test_Feederusage(self): user1 = User(username="******") feeder = Feeder() #feeder.users.append(user1) moment1 = FeedMoment(amount=2, Feeder=feeder) moment1.setFeedTime(5, 12) moment2 = FeedMoment(amount=3, Feeder=feeder) moment2.setFeedTime(14, 15) db.session.add(feeder) db.session.add(moment1) db.session.add(moment2) db.session.commit() #print(feeder.get_lastupdate()) self.assertEqual(moment2.last_updated, feeder.get_lastupdate()) #test global last update #!! current time is important! time = datetime.now().hour * 60 + datetime.now().minute print(time) if time > moment2.feed_time: self.assertEqual(feeder.get_next_moment(), moment1) else: self.assertEqual(feeder.get_next_moment(), moment2)
def add_petfeeder(): form = PetFeederRegistrationForm() if form.validate_on_submit(): feeder = Feeder.query.filter_by( secret_key=form.secret_key.data).first() if feeder is None: feeder = Feeder.query.filter_by(name=form.name.data).first() if feeder is None: new_feeder = Feeder(name=form.name.data, secret_key=form.secret_key.data) db.session.add(new_feeder) current_user.feeders.append(new_feeder) flash('new feeder created') db.session.commit() return redirect(url_for('petfeeder_index')) else: form.name.errors.append(ValidationError('name unavailable')) elif current_user.feeders.filter_by( name=form.name.data).first() is not None: form.name.errors.append( ValidationError('feeder already linked to your account')) return render_template('add_petfeeder.html', title="add feeder", form=form) else: if not feeder.name == form.name.data: form.name.errors.append( ValidationError("feeder in use with different name")) else: current_user.feeders.append(feeder) flash("existing feeder added to your account") db.session.commit() return redirect(url_for('petfeeder_index')) return render_template('add_petfeeder.html', title="add feeder", form=form)
def api_raw(hash): """ :param hash: hash of the secret key of a feeder :return: returns all feedmoments of the hashed feeder with update-timestamps between the timestamp in the url and the timestamp of the next feedmoment (both included) """ feeder = Feeder.get_feeder_from_hash(hash) if not feeder is None: time = request.args.get('last_update') try: time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S.%f') except: time = unix_to_datetime(float(time)) print(time) feedmoment = feeder.get_next_moment() moment_time = feedmoment.last_updated if moment_time > time: # TODO: encrypt return str( feeder.get_all_moments_updated_between(time, moment_time, json=False)) else: # TODO: encrypt return ''
def test_Feeder_Feedmoment(self): feeder = Feeder() moment1 = FeedMoment(amount=1, Feeder=feeder) moment2 = FeedMoment(amount=2, Feeder=feeder) db.session.add(feeder) db.session.add(moment1) db.session.add(moment2) db.session.commit() self.assertTrue(len(feeder.feed_moments.all()) == 2) self.assertTrue(feeder.feed_moments[0].amount == 1)
def api_raw_unix_nextMoment(hash): """ param: hash of secret key for feeder :return: next moment & amount this function serves as a simplified acces point, and disregards all information about update times. It hence does not facilitate caching. """ feeder = Feeder.get_feeder_from_hash(hash) if not feeder is None: return str([feeder.get_next_moment().get_raw_format()])
def test_Feeder_User(self): user1 = User(username="******") user2 = User(username="******") feeder = Feeder() feeder.users.append(user1) user2.feeders.append(feeder) db.session.add(feeder) db.session.add(user1) db.session.add(user2) db.session.commit() self.assertTrue(len(feeder.users.all()) == 2) self.assertEqual(user1.feeders[0], feeder)