def get_list(self): """ ### Request `GET /birds` Empty body. ### Response Valid status codes: - `200 OK` The body is a JSON array based on the JSON schema can be found in `get-birds-response.json`. """ birds = [] # filter by visibility where = {'visible': True} # get items sorted by date added (desc) for bird in Bird.objects(**where).order_by('-added'): birds.append(self.format_bird(bird)) return birds
def create(self, data): """ ### Request `POST /birds` The body is a JSON object based on the JSON schema can be found in `post-birds-request.json`. - If `visible` is not set, it should default to `false`. - `added` should default to today's date (in UTC) ### Response Valid status codes: - `201 Created` if the bird was successfully added - `400 Bad request` if any mandatory fields were missing or if the input JSON was invalid The body is a JSON object based on the JSON schema can be found in `post-birds-response.json`. """ # try to create a new object try: bird = Bird() bird.name = data.get('name') bird.family = data.get('family') bird.continents = data.get('continents') bird.visible = data.get('visible', False) bird.added = data.get('added', None) bird.save() bird.reload() return self.format_bird(bird) except mongoengine.ValidationError: raise web.badrequest() except AttributeError: raise web.badrequest() except ValueError: raise web.badrequest()
def load_birds(): """Load birds from birds.csv into database.""" for row in open("birds.csv"): species, scientific_name = row.rstrip().split(',') bird = Bird(species=species, scientific_name=scientific_name) # We need to add to the session or it won't ever be stored db.session.add(bird) # Once we're done, we should commit our work db.session.commit()
def setUp(self): app.config['TESTING'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False self.client = app.test_client() connect_to_db(app, db_uri='postgresql:///testdb') db.create_all() #example_data will error out atm, need to create make_users() bird = Bird(species="bluebird", scientific_name="reallyo scientifico bluebirdius") db.session.add(bird) db.session.commit()
def load_birds(): """Load birds into database.""" print("Birds") Bird.query.delete() for row in open("seed_data/birds_data.txt"): row = row.rstrip() scientific_name, common_name, species_code, image = row.split("|") bird = Bird(scientific_name=scientific_name.strip(), common_name=common_name.strip(), species_code=species_code, image=image.strip()) db.session.add(bird) db.session.commit()
def add_user_favorite(): """Adds bird to database if not already in database, Adds species code/user id pair to favorites if not already in database """ common_name = request.form.get('comName') scientific_name = request.form.get('sciName') species_code = request.form.get('speciesCode') # get bird by matching common name, could also match species code bird = Bird.query.filter(Bird.common_name == common_name).first() user_id = session['user_id'] user = User.query.get(session['user_id']) photo_id = get_photos_by_text(common_name) image_url = get_bird_pic_flickr(photo_id) image_index = image_url[0] print("PHOTO ID IS: ") print(photo_id) print("IMAGE URLS IS: ") print(image_url) print(type(image_url)) if not bird: print("Bird does not exist in db!") bird = Bird(common_name=common_name, scientific_name=scientific_name, species_code=species_code, image=image_index) db.session.add(bird) db.session.commit() print("Bird added, yay!") favorite = Favorite(user_id=session['user_id'], bird_id=bird.bird_id) db.session.add(favorite) db.session.commit() flash("Yay, added!") return redirect('/birds/' + species_code)
def game_loop(): bird = Bird(200, 200) base = Base(730) pipes = [Pipe(600)] window = pygame.display.set_mode((WN_WIDTH, WN_HIGHT)) pygame.display.set_caption('Flappy Bird') clock = pygame.time.Clock() score = 0 run = True start = True # e = True while run: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False keys_pressed = pygame.key.get_pressed() if start: draw_window(window, bird, base, pipes, score) if keys_pressed[pygame.K_SPACE]: bird.jump() start = False else: remove_pipes = [] passed = False crashed = False if base.collide(bird): crashed = True crash(window, score) for pipe in pipes: if pipe.collide(bird): crashed = True crash(window, score) break if pipe.x + pipe.PIPE_TOP.get_width() < 0: remove_pipes.append(pipe) if not pipe.passed and pipe.x < bird.x: pipe.passed = True passed = True pipe.move() # in case of collision check if player wants to replay if crashed: keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_SPACE]: main() else: # if the bird passed the pipe increment score and add new pipe if passed: score += 1 pipes.append(Pipe(600)) # delete pipes that are out of the screen for remove in remove_pipes: pipes.remove(remove) # check if the bird hit the ground if bird.x + bird.img.get_height() >= 730: pass keys_pressed = pygame.key.get_pressed() # jump while playing if keys_pressed[pygame.K_SPACE]: bird.jump() bird.move() base.move() draw_window(window, bird, base, pipes, score)