def populateProduct(product, root): for item in root.iter('description'): product.description = item.text for item in root.iter('name'): if (item.attrib["type"] == "primary"): product.name = item.attrib["value"] for item in root.iter('link'): if (item.attrib["type"] == "boardgamepublisher" and not product.publisher): product.publisher = item.attrib["value"] for item in root.iter('yearpublished'): product.release = item.attrib["value"] # Only expect to iterate over each loop once as we only expect one minplayers and # one maxplayers returned in the XML document players = '' for item in root.iter('minplayers'): players += item.attrib["value"] players += " - " for item in root.iter('maxplayers'): players += item.attrib["value"] product.player_num = players for item in root.iter('image'): product.image = "http:{}".format(item.text) session.commit()
def addreviewer(): display_name = input("Display Name: ") critic = True password = input("Password: ") reviewer = Reviewer(display_name=display_name, critic=critic, password=generate_password_hash(password)) session.add(reviewer) session.commit()
def createProducts(root): for item in root.iter('item'): product = session.query(Product).filter( Product.e_id == item.attrib["id"]).first() # Blacklist 193738 due to encoding issues with XML data back. BGG API sent back # 200 status_code while reporting an error in messaging for this if not product and item.attrib["id"] != "193738": product = Product(e_id=item.attrib["id"]) session.add(product) session.commit()
def getUser(username): user = session.query(Reviewer).filter( Reviewer.display_name == username).first() if user: return user user = Reviewer(display_name=username, critic=False, password='') session.add(user) session.commit() return user
def seedCriticReviews(): products = session.query(Product).all() shutupandsitdown = session.query(Reviewer).filter( Reviewer.display_name == "shutupandsitdown").first() dicetowerreviews = session.query(Reviewer).filter( Reviewer.display_name == "dicetowerreviews").first() eightoclock = session.query(Reviewer).filter( Reviewer.display_name == "eightoclock").first() negativenate = session.query(Reviewer).filter( Reviewer.display_name == "negativenate").first() for product in products: review10 = Review( raw_score=10, score=tenToScore(10), summary="This was an amazing game, never forget {}".format( fake.sentences(nb=1)[0]), review="This was an amazing game, never forget {}".format( fake.sentences(nb=1)[0]), product=product, reviewer=shutupandsitdown) review8 = Review( raw_score=8, score=tenToScore(8), summary="This was a great amazing game, could be better {}".format( fake.sentences(nb=1)[0]), review="This was a somewhat an amazing game, could be better {}". format(fake.sentences(nb=1)[0]), product=product, reviewer=eightoclock) review5 = Review( raw_score=5, score=tenToScore(5), summary="This was a somewhat disappointing game, could be better {}" .format(fake.sentences(nb=1)[0]), review="This was a somewhat disappointing game, could be better {}" .format(fake.sentences(nb=1)[0]), product=product, reviewer=dicetowerreviews) review1 = Review( raw_score=1, score=tenToScore(1), summary="This was a terrible game, do not purchase {}".format( fake.sentences(nb=1)[0]), review="This was a terrible game, do not purchase {}".format( fake.sentences(nb=1)[0]), product=product, reviewer=negativenate) session.add_all([review10, review5, review1]) session.commit()
def test_new_review(self): reviewer = models.Reviewer(display_name="test_user", critic=False) product = models.Product( name="Settlers of Catan", publisher="Mayfair Games", release="1995", player_num="2-4", image= "https://images-na.ssl-images-amazon.com/images/I/615H5ZMhB7L._SX425_.jpg" ) review = models.Review( raw_score="buy", score=5.0, summary="You should buy this game", review= "You should definitely buy this game. I would give it a 5.0 out of 5.0.", source="http://randomsource.com", product=product, reviewer=reviewer) product_2 = models.Product( name="Pandemic", publisher="Z-Man Games", release="2007", player_num="2-5", image= " http://25.media.tumblr.com/qgIb8tERiqn3b75revfkdxWxo1_500.jpg") review_2 = models.Review( raw_score="1", score=1.0, summary="You should not buy this game", review= "You should definitely not buy this game. I would give it a 1.0 out of 5.0.", source="http://randomsource.com", product=product_2, reviewer=reviewer) session.add_all([review, review_2]) session.commit() review_check = session.query(models.Review).all() self.assertEqual(len(review_check), 2) product_check = session.query(models.Product).all() self.assertEqual(len(product_check), 2) reviewer_check = session.query(models.Reviewer).all() self.assertEqual(len(reviewer_check), 1)
def populateReviews(product, root): for item in root.iter('comment'): if item.attrib["rating"] != "N/A": user = getUser(item.attrib["username"]) review = session.query(Review).filter( Review.reviewer == user).filter( Review.product == product).first() if review is None: review = Review(raw_score=item.attrib["rating"], score=tenToScore(item.attrib["rating"]), summary=item.attrib["value"][:140], review=item.attrib["value"][:1500], source="https://boardgamegeek.com/boardgame/{}/{}/ratings".\ format(product.e_id, product.name), product=product, reviewer=user) session.add(review) session.commit()
def seedImages(): products = session.query(Product).filter(Product.image == None).all() for product in products: product.image = "http://tincan.co.uk/sites/default/files/banners/tc-404.jpg" session.commit()
def seedPasswords(defaultPassword): users = session.query(Reviewer).filter(Reviewer.password == '').all() for user in users: user.password = generate_password_hash(defaultPassword) session.commit()