def test_shopify_update_product(self): product = Product( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, name=testing_constants.NEW_PRODUCT_NAME) shop = Shop.query.filter_by( id=testing_constants.SHOPIFY_SHOP_ID).first() if not shop: shop = Shop(domain=testing_constants.SHOPIFY_SHOP_DOMAIN) db.session.add(shop) product.shop = shop db.session.add(product) db.session.commit() data = json.dumps({ 'id': testing_constants.NEW_PRODUCT_PLATFORM_ID, 'title': testing_constants.CHANGED_PRODUCT_NAME }) sha256 = base64.b64encode( hmac.new(Config.SHOPIFY_APP_SECRET, msg=data, digestmod=hashlib.sha256).digest()) response_actual = self.desktop_client.post( url_for('api.platform_shopify_update_product'), data=data, content_type="application/json", headers={ 'X-Shopify-Hmac-SHA256': sha256, 'X-Shopify-Shop-Domain': testing_constants.SHOPIFY_SHOP_DOMAIN }) self.assertEquals(response_actual.status_code, 200) product = Product.query.filter_by( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, shop=shop).first() self.assertEquals(product.name, testing_constants.CHANGED_PRODUCT_NAME) db.session.delete(product) db.session.commit()
def test_shopify_update_product(self): product = Product(platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, name=testing_constants.NEW_PRODUCT_NAME) shop = Shop.query.filter_by(id=testing_constants.SHOPIFY_SHOP_ID).first() if not shop: shop = Shop(domain=testing_constants.SHOPIFY_SHOP_DOMAIN) db.session.add(shop) product.shop = shop db.session.add(product) db.session.commit() data = json.dumps({ 'id': testing_constants.NEW_PRODUCT_PLATFORM_ID, 'title': testing_constants.CHANGED_PRODUCT_NAME }) sha256 = base64.b64encode(hmac.new(Config.SHOPIFY_APP_SECRET, msg=data, digestmod=hashlib.sha256).digest()) response_actual = self.desktop_client.post(url_for('api.platform_shopify_update_product'), data=data, content_type="application/json", headers={ 'X-Shopify-Hmac-SHA256': sha256, 'X-Shopify-Shop-Domain': testing_constants.SHOPIFY_SHOP_DOMAIN}) self.assertEquals(response_actual.status_code, 200) product = Product.query.filter_by(platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, shop=shop).first() self.assertEquals(product.name, testing_constants.CHANGED_PRODUCT_NAME) db.session.delete(product) db.session.commit()
def test_fetch_new_and_updated_update_product_0_name_product_1_status_product_2_description(self): # setup with existing product 0, 1,2 shop = Shop() product0 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID, name='dark side', # change active=True, short_description=testing_constants.MAGENTO_PRODUCT_0_DESCRIPTION) product1 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_1_NAME, active=False, # change short_description=testing_constants.MAGENTO_PRODUCT_1_DESCRIPTION) product2 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_2_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_2_NAME, active=False, short_description='oh oh') # change shop.products.append(product0) shop.products.append(product1) shop.products.append(product2) db.session.add(shop) db.session.commit() api = magento_api.API() current_shop_products = Product.query.filter_by(shop_id=shop.id).all() new_and_updated = api.fetch_new_and_updated_products(current_shop_products) # test self.assertEquals(len(new_and_updated), 3) self.assertEquals(new_and_updated[0].platform_product_id, testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID) self.assertEquals(new_and_updated[0].name, testing_constants.MAGENTO_PRODUCT_0_NAME) self.assertIsNone(getattr(new_and_updated[0], 'urlkey', None)) self.assertEquals(new_and_updated[0].short_description, testing_constants.MAGENTO_PRODUCT_0_DESCRIPTION) self.assertTrue(new_and_updated[0].active) self.assertEquals(new_and_updated[1].platform_product_id, testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID) self.assertEquals(new_and_updated[1].name, testing_constants.MAGENTO_PRODUCT_1_NAME) self.assertIsNone(getattr(new_and_updated[1], 'urlkey', None)) self.assertEquals(new_and_updated[1].short_description, testing_constants.MAGENTO_PRODUCT_1_DESCRIPTION) self.assertTrue(new_and_updated[1].active) self.assertEquals(new_and_updated[2].platform_product_id, testing_constants.MAGENTO_PRODUCT_2_PLATFORM_PRODUCT_ID) self.assertEquals(new_and_updated[2].name, testing_constants.MAGENTO_PRODUCT_2_NAME) self.assertIsNone(getattr(new_and_updated[2], 'urlkey', None)) self.assertEquals(new_and_updated[2].short_description, testing_constants.MAGENTO_PRODUCT_2_DESCRIPTION) self.assertFalse(new_and_updated[2].active) # tear down db.session.delete(shop) db.session.delete(product0) db.session.delete(product1) db.session.delete(product2) db.session.commit()
def delete_product(product_id) -> redirect: query = Product().fetch_product(product_id, current_user.get_id()) if not query: flash(['Something went wrong. Please try again']) return redirect(url_for('dashboard.products')) Product().delete(product_id) flash(['Product Successfully deleted']) return redirect(url_for('dashboard.products'))
def product_updater(): """Makes new product insertion in database""" # setting the logger logger = logging.getLogger(__name__) # emptying the database i = 0 # for each category in category list for elem in dt.CAT_LIST: # Replace some characters with others, so we can use it in API elem = elem.replace(" ", "-") elem = unidecode(elem) # get id list using Requester class category = Category.objects.get(category_name=elem) id_list = Requester(elem).product_id_list # for each product id in id list for product_id in id_list: # gather product data with Requester class product_data = Product_data( Requester.product_data_requester(product_id)) # create a product in database product = Product( product_name=product_data.name, product_url=product_data.url, product_img=product_data.img, product_nutriscore=product_data.nutriscore, product_category_id=category, ) # create nutriments data affiliated to product nutriments = Nutriments( nutriments_product_id=product, nutriments_kj=product_data.energy_kj, nutriments_kcal=product_data.energy_kcal, nutriments_lipids=product_data.lipids, nutriments_fat=product_data.fat, nutriments_carbohydrates=product_data.carbohydrates, nutriments_sugar=product_data.sugar, nutriments_protein=product_data.protein, nutriments_salt=product_data.salt, nutriments_sodium=product_data.sodium, ) # save if ok try: with transaction.atomic(): product.save() nutriments.save() i += 1 print(str(i) + " produit(s) ajouté(s)") # report error if not ok except DatabaseError as prod_error: logger.error(prod_error) pass
def edit_product(product_id): product = Product().fetch_product(product_id, current_user.get_id()) form = ProductForm() for item in Category().fetch_user_categories(): form.categories.choices.append( [item.category_name, item.category_name]) for item in Attachment().fetch_user_attachments(): form.attachment_file.choices.append( [item.attachment_filename, item.original_attachment_filename]) if not product: flash(['Something went wrong. Please try again later']) return redirect(url_for('dashboard.products')) form.product_name.data = product.name if product.product_type == 'item': linked_product_items = ProductItem().fetch_active_product_items( product.product_items) form.items.data = ''.join(linked_product_items) else: form.attachment_file.data = product.product_attachment.attachment_filename form.stock.data = product.stock form.price.data = float(product.price) return render_template('/dashboard/edit-product.html', product=product, form=form, flashed_message=get_flashed_messages(), attachments=Attachment().fetch_user_attachments())
def payment_complete(order_id): ## this needs to be rewritten to take into account stripe and crypto payments token = request.args.get('token') payer_id = request.args.get('PayerID') order = Order().fetch_order(order_id) if not order: return abort(404) if order.payment.transaction_id != token: return abort(404) if order.status != 'Paid': capture_paypal_order(token) check_order = get_paypal_order(token) if check_order['payment_status'] == 'COMPLETED': order.status = 'Paid' deliver_goods = Product().deliver_product(order.product_id, order_id, order.quantity, order.customer.email) leave_feedback.apply_async(args=[ order.customer.email, User().fetch_user_supply_uuid(order.user).username, order_id, order.order_hash ], countdown=120) if not order.customer.email: order.customer.email = check_order['buyer_email'] order.update() return redirect(url_for('order.track_order', order_id=order_id))
def create_coupon() -> render_template: form = CouponForm() for item in Product().fetch_user_products(): form.product_list.choices.append([item.id, item.name]) return render_template('/dashboard/create-coupon.html', form=form)
def create_category(): form = CategoryForm() for item in Product().fetch_user_products(): form.linked_products.choices.append([item.id, item.name]) return render_template('/dashboard/create-category.html', form=form)
def test_YOTPO_import_reviews_verified_review(self): self.refresh_db() p = Product(shop_id=self.yotpo_shop.id, name=testing_constants.NEW_PRODUCT_NAME) user_legacy = UserLegacy(name=testing_constants.ORDER_USER_NAME, email=testing_constants.ORDER_USER_EMAIL) o = Order(user_legacy=user_legacy, shop=self.yotpo_shop, status=Constants.ORDER_STATUS_NOTIFIED) o.products.append(p) db.session.add(p) db.session.add(o) db.session.commit() before_count = len(Review.query.all()) self.yotpo_importer.import_reviews( testing_constants.YOTPO_REVIEWS_CSV_FILEPATH) after_count = len(Review.query.all()) r = Review.query.filter_by(user_legacy=user_legacy).first() self.assertEqual(after_count, before_count + 2) self.assertNotEqual(r, None) self.assertEqual(r.body, testing_constants.NEW_REVIEW_BODY) self.assertEqual(r.product, p) self.assertEqual(r.star_rating, 4) self.assertEqual( r.created_ts, parse( testing_constants.YOTPO_REVIEW_TIMESTAMP).replace(tzinfo=None)) self.assertEqual(r.youtube_video, None) self.assertTrue(r.verified_review)
def test_shopify_delete_product(self): product = Product( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, name=testing_constants.NEW_PRODUCT_NAME, shop_id=testing_constants.SHOPIFY_SHOP_ID) db.session.add(product) db.session.commit() data = json.dumps({ 'id': testing_constants.NEW_PRODUCT_PLATFORM_ID, }) sha256 = base64.b64encode( hmac.new(Config.SHOPIFY_APP_SECRET, msg=data, digestmod=hashlib.sha256).digest()) response_actual = self.desktop_client.post( url_for('api.platform_shopify_delete_product'), data=data, headers={ 'X-Shopify-Hmac-SHA256': sha256, 'X-Shopify-Shop-Domain': testing_constants.SHOPIFY_SHOP_DOMAIN }) self.assertEquals(response_actual.status_code, 200) product = Product.query.filter_by( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, shop_id=testing_constants.SHOPIFY_SHOP_ID).first() self.assertIsNone(product)
def test_shopify_fulfill_order(self): product = Product(name=testing_constants.NEW_PRODUCT_NAME) order = Order( platform_order_id=testing_constants.NEW_ORDER_PLATFORM_ID, shop_id=testing_constants.SHOPIFY_SHOP_ID, user=self.reviewer_user) order.products.append(product) db.session.add(order) db.session.commit() data = json.dumps({ 'order_id': testing_constants.NEW_ORDER_PLATFORM_ID, 'tracking_number': testing_constants.ORDER_TRACKING_NUMBER, 'created_at': testing_constants.ORDER_SHIPPED_AT }) sha256 = base64.b64encode( hmac.new(Config.SHOPIFY_APP_SECRET, msg=data, digestmod=hashlib.sha256).digest()) response_actual = self.desktop_client.post( url_for('api.platform_shopify_fulfill_order'), data=data, headers={ 'X-Shopify-Hmac-SHA256': sha256, 'X-Shopify-Shop-Domain': testing_constants.SHOPIFY_SHOP_DOMAIN }) self.assertEquals(response_actual.status_code, 200) order = Order.query.filter_by( platform_order_id=testing_constants.NEW_ORDER_PLATFORM_ID, shop_id=testing_constants.SHOPIFY_SHOP_ID).first() self.assertEquals(order.status, Constants.ORDER_STATUS_NOTIFIED) db.session.delete(order) db.session.commit()
def SaveProduct(self, item): # 保存地区信息 zone = Zone() zone.name = item["zone"][0] zone.save() # 保存分类信息 category = Category() category.category = item["category"][0] category.save() # 保存产品信息 product = Product() product.name = item["product"][0] product.zone = zone product.category = category product.save() # 保存产品详细信息 product_detail = ProductDetail() product_detail.product = product product_detail.price = float(item["price"][0]) # 这里有问题,需要优化 try: product_detail.price = float(item["price"][0]) product_detail.star = item["star"][0] except Exception, e: product_detail.price = float(item["price"]) product_detail.star = 0
def coupons() -> render_template: form = CouponForm() for item in Product().fetch_user_products(): form.product_list.choices.append([item.id, item.name]) return render_template('/dashboard/coupons.html', flashed_message=get_flashed_messages(), form=form, all_coupons=Coupon().get_all_coupons())
def product_page(username, product_id): form = PurchaseProduct() user = User().fetch_user_supply_username(username) product = Product().fetch_product(product_id, user.uuid) if (not product) or (product.user != user.uuid): return abort(404) form.product_id.data = product.id return render_template('/shop/product-page.html', product=product, user=user, form=form, flashed_message=get_flashed_messages())
def SaveProduct(self, item): # 保存地区信息 zone = Zone() zone.name = item["zone"][0] zone.save() # 保存分类信息 category = Category() category.category = item["category"][0] category.save() # 保存产品信息 product = Product() product.name = item["product"][0] product.zone = zone product.category = category product.save() # 保存产品详细信息 product_detail = ProductDetail() product_detail.product = product product_detail.price = float(item["price"][0]) # 这里有问题,需要优化 try: product_detail.price = float(item["price"][0]) product_detail.star = item["star"][0] except Exception, e : product_detail.price = float(item["price"]) product_detail.star = 0
def track_order(order_id) -> render_template: fetch_order = Order().fetch_order(order_id) if not fetch_order: return abort(404) return render_template( '/shop/order.html', order=fetch_order, flashed_message=get_flashed_messages(), user=User().fetch_user_supply_uuid(fetch_order.user), product=Product().fetch_product(fetch_order.product_id, fetch_order.user))
def test_fetch_new_and_updated_no_new_or_updated(self): # setup with existing product 0, 1,2 shop = Shop() product0 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_0_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_0_DESCRIPTION) product1 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_1_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_1_DESCRIPTION) product2 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_2_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_2_NAME, active=False, short_description=testing_constants.MAGENTO_PRODUCT_2_DESCRIPTION) shop.products.append(product0) shop.products.append(product1) shop.products.append(product2) db.session.add(shop) db.session.commit() api = magento_api.API() current_shop_products = Product.query.filter_by(shop_id=shop.id).all() new_and_updated = api.fetch_new_and_updated_products(current_shop_products) # test self.assertEquals(len(new_and_updated), 0) # tear down db.session.delete(shop) db.session.delete(product0) db.session.delete(product1) db.session.delete(product2) db.session.commit()
def test_shopify_create_order(self): product = Product( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, name=testing_constants.NEW_PRODUCT_NAME, shop_id=testing_constants.SHOPIFY_SHOP_ID) db.session.add(product) db.session.commit() data = json.dumps({ 'id': testing_constants.NEW_ORDER_PLATFORM_ID, 'browser_ip': testing_constants.NEW_ORDER_BROWSER_IP, 'customer': { 'email': testing_constants.ORDER_USER_EMAIL }, 'line_items': [{ 'product_id': testing_constants.NEW_PRODUCT_PLATFORM_ID }] }) sha256 = base64.b64encode( hmac.new(Config.SHOPIFY_APP_SECRET, msg=data, digestmod=hashlib.sha256).digest()) response_actual = self.desktop_client.post( url_for('api.platform_shopify_create_order'), data=data, headers={ 'X-Shopify-Hmac-SHA256': sha256, 'X-Shopify-Shop-Domain': testing_constants.SHOPIFY_SHOP_DOMAIN }) self.assertEquals(response_actual.status_code, 201) product = Product.query.filter_by( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, shop_id=testing_constants.SHOPIFY_SHOP_ID).first() order = Order.query.filter_by( platform_order_id=testing_constants.NEW_ORDER_PLATFORM_ID, shop_id=testing_constants.SHOPIFY_SHOP_ID).first() self.assertEquals(order.user_legacy.email, testing_constants.ORDER_USER_EMAIL) self.assertEquals(order.browser_ip, testing_constants.NEW_ORDER_BROWSER_IP) self.assertIn(product, order.products) db.session.delete(order) db.session.delete(product) db.session.commit()
def submit_coupon() -> redirect: form = CouponForm() for item in Product().fetch_user_products(): form.product_list.choices.append([item.id, item.name]) if not form.validate_on_submit(): flash(list(form.errors.values())[0]) return redirect(url_for('dashboard.coupons')) if not Coupon().add_coupon(request.form): flash( ['Coupon already exists. Please choose a different coupon code.']) return redirect(url_for('dashboard.coupons')) flash(['Coupon successfully added']) return redirect(url_for('dashboard.coupons'))
def add_category() -> redirect: form = CategoryForm() for item in Product().fetch_user_products(): form.linked_products.choices.append([item.id, item.name]) if not form.validate_on_submit(): flash(list(form.errors.values())[0]) return redirect(url_for('dashboard.categories')) new_category = Category().add(request.form) if not new_category: flash(['Category already exists']) return redirect(url_for('dashboard.categories')) flash(['Category Successfully Added']) return redirect(url_for('dashboard.categories'))
def submit_new_product() -> redirect: form = ProductForm() attachments = Attachment().fetch_user_attachments() for item in Category().fetch_user_categories(): form.categories.choices.append( [item.category_name, item.category_name]) for item in attachments: form.attachment_file.choices.append( [item.attachment_filename, item.original_attachment_filename]) if not form.validate_on_submit(): flash(list(form.errors.values())[0]) return redirect(url_for('dashboard.create_product')) Product().add(request) flash(['Product Successfully Created']) return redirect(url_for('dashboard.products'))
def edit_category(category_id) -> render_template: form = CategoryForm() category = Category().fetch_category_by_id(category_id) if not cat: return abort(404) form.linked_products.data = [] for item in Product().fetch_user_products: form.linked_products.choices.append([item.id, item.name]) for product_cat in ProductCategory().fetch_linked_product_categories( item.id): if product_cat.category_name == category.category_name: form.linked_products.data.append(item.id) form.name.data = cat.category_name return render_template('/dashboard/edit-category.html', form=form, category=category)
def test_shopify_uninstall_app(self): shop = Shop.query.filter_by( id=testing_constants.SHOPIFY_SHOP_ID).first() shop.owner = self.shop_owner_user product = Product( platform_product_id=testing_constants.NEW_PRODUCT_PLATFORM_ID, name=testing_constants.NEW_PRODUCT_NAME, shop_id=testing_constants.SHOPIFY_SHOP_ID) order = Order(user_id=1, shop_id=testing_constants.SHOPIFY_SHOP_ID) order.products.append(product) db.session.add(order) db.session.commit() order.ship() order.set_notifications() # emails are sent automatically anyway, just check it happened self.assertEquals(len(self.outbox), 1) data = json.dumps({}) sha256 = base64.b64encode( hmac.new(Config.SHOPIFY_APP_SECRET, msg=data, digestmod=hashlib.sha256).digest()) response_actual = self.desktop_client.post( url_for('api.platform_shopify_app_uninstalled'), data=data, headers={ 'X-Shopify-Hmac-SHA256': sha256, 'X-Shopify-Shop-Domain': testing_constants.SHOPIFY_SHOP_DOMAIN }) self.assertEquals(response_actual.status_code, 200) shop = Shop.query.filter_by( id=testing_constants.SHOPIFY_SHOP_ID).first() shop_customer = shop.owner.customer[0] self.assertFalse(shop_customer.active) self.assertEqual(len(shop_customer.subscription), 0) # make sure tasks are revoked db.session.delete(order) db.session.delete(product) db.session.commit()
def update_product(product_id): form = ProductForm() attachments = Attachment().fetch_user_attachments() for item in Category().fetch_user_categories(): form.categories.choices.append( [item.category_name, item.category_name]) for item in attachments: form.attachment_file.choices.append( [item.attachment_filename, item.original_attachment_filename]) if not form.validate_on_submit(): flash(list(form.errors.values())[0]) return redirect( url_for('dashboard.edit_product', product_id=product_id)) if not Product().update(request, product_id): flash(['Something went wrong. Please try again later']) return redirect(url_for('dashboard.products')) flash(['Product Successfully Updated']) return redirect(url_for('dashboard.edit_product', product_id=product_id))
def test_fetch_new_and_updated_products_with_current_products_0(self): # setup with existing product 0 shop = Shop() product = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_0_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_0_DESCRIPTION) shop.products.append(product) db.session.add(shop) db.session.commit() # Fetch api = magento_api.API() current_shop_products = Product.query.filter_by(shop_id=shop.id).all() new_and_updated = api.fetch_new_and_updated_products(current_shop_products) # Test self.assertEquals(len(new_and_updated), 2) self.assertEquals(new_and_updated[0].platform_product_id, testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID) self.assertEquals(new_and_updated[0].name, testing_constants.MAGENTO_PRODUCT_1_NAME) self.assertEquals(new_and_updated[0].urlkey, testing_constants.MAGENTO_PRODUCT_1_URL_KEY) self.assertEquals(new_and_updated[0].short_description, testing_constants.MAGENTO_PRODUCT_1_DESCRIPTION) self.assertTrue(new_and_updated[0].active) self.assertEquals(new_and_updated[1].platform_product_id, testing_constants.MAGENTO_PRODUCT_2_PLATFORM_PRODUCT_ID) self.assertEquals(new_and_updated[1].name, testing_constants.MAGENTO_PRODUCT_2_NAME) self.assertEquals(new_and_updated[1].urlkey, testing_constants.MAGENTO_PRODUCT_2_URL_KEY) self.assertEquals(new_and_updated[1].short_description, testing_constants.MAGENTO_PRODUCT_2_DESCRIPTION) self.assertFalse(new_and_updated[1].active) # tear down db.session.delete(shop) db.session.delete(product) db.session.commit()
def test_SHOPIFY_import_reviews(self): self.refresh_db() p = Product(shop_id=self.shopify_shop.id, name=testing_constants.NEW_PRODUCT_NAME) db.session.add(p) db.session.commit() before_count = len(Review.query.all()) self.shopify_importer.import_reviews( testing_constants.SHOPIFY_REVIEWS_CSV_FILEPATH) after_count = len(Review.query.all()) u = UserLegacy.query.filter_by( email=testing_constants.ORDER_USER_EMAIL).first() r = Review.query.filter_by(user_legacy=u).first() self.assertEqual(after_count, before_count + 3) self.assertNotEqual(r, None) self.assertEqual(r.body, testing_constants.NEW_REVIEW_BODY) self.assertEqual(r.product, p) self.assertEqual(r.star_rating, 4) self.assertEqual( r.created_ts, parse(testing_constants.SHOPIFY_REVIEW_TIMESTAMP).replace( tzinfo=None)) self.assertEqual(r.youtube_video, None) self.assertFalse(r.verified_review)
def test_fetch_new_and_updated_orders_already_exist(self): # setup with existing product 0,1,2 user_shop_owner = User(email=testing_constants.NEW_USER_EMAIL) Customer(user=user_shop_owner) shop = Shop(name=testing_constants.MAGENTO_SHOP_NAME) shop.owner = user_shop_owner product0 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_0_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_0_DESCRIPTION) product1 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_1_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_1_DESCRIPTION) product2 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_2_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_2_NAME, active=False, short_description=testing_constants.MAGENTO_PRODUCT_2_DESCRIPTION) order0_user_legacy = UserLegacy(email=testing_constants.MAGENTO_ORDER_0_EMAIL, name=testing_constants.MAGENTO_ORDER_0_NAME) order0 = Order(platform_order_id=testing_constants.MAGENTO_ORDER_0_PLATFORM_ORDER_ID, status=Constants.ORDER_STATUS_PURCHASED, purchase_timestamp=testing_constants.MAGENTO_ORDER_0_CREATED, user_legacy=order0_user_legacy) order0.products.append(product0) order1_user_legacy = UserLegacy(email=testing_constants.MAGENTO_ORDER_1_EMAIL, name=testing_constants.MAGENTO_ORDER_1_NAME) order1 = Order(platform_order_id=testing_constants.MAGENTO_ORDER_1_PLATFORM_ORDER_ID, status=Constants.ORDER_STATUS_NOTIFIED, purchase_timestamp=testing_constants.MAGENTO_ORDER_1_CREATED, shipment_timestamp=testing_constants.MAGENTO_ORDER_1_SHIPPED, user_legacy=order1_user_legacy) order1.products.append(product0) order1.products.append(product1) order2_user_legacy = UserLegacy(email=testing_constants.MAGENTO_ORDER_2_EMAIL, name=testing_constants.MAGENTO_ORDER_2_NAME) order2 = Order(platform_order_id=testing_constants.MAGENTO_ORDER_2_PLATFORM_ORDER_ID, status=Constants.ORDER_STATUS_PURCHASED, purchase_timestamp=testing_constants.MAGENTO_ORDER_2_CREATED, user_legacy=order2_user_legacy) shop.products.append(product0) shop.products.append(product1) shop.products.append(product2) shop.orders.append(order0) shop.orders.append(order1) shop.orders.append(order2) db.session.add(shop) db.session.commit() # test api = magento_api.API() current_orders = Order.query.filter_by(shop_id=shop.id).all() new_and_updated = api.fetch_new_and_updated_orders(current_orders, shop) self.assertEquals(len(new_and_updated), 0) # tear down user = User.query.filter_by(email=testing_constants.NEW_USER_EMAIL).first() customer = user.customer[0] shop = Shop.query.filter_by(name=testing_constants.MAGENTO_SHOP_NAME).first() orders = Order.query.all() for product in shop.products: db.session.delete(product) db.session.delete(user) db.session.delete(customer) db.session.delete(shop) db.session.delete(shop) for order in orders: for review_request in order.review_requests: db.session.delete(review_request) db.session.delete(order) db.session.commit()
def products() -> render_template: return render_template('/dashboard/products.html', flashed_message=get_flashed_messages(), prods=Product().fetch_user_products())
def update_coupon(coupon_id) -> redirect: for item in Product().fetch_user_products(): form.product_list.choices.append([item.id, item.name])
def test_fetch_new_and_updated_orders_already_exist_but_not_shipped(self): # setup with existing product 0,1,2 user_shop_owner = User(email=testing_constants.NEW_USER_EMAIL) Customer(user=user_shop_owner) shop = Shop(name=testing_constants.MAGENTO_SHOP_NAME) shop.owner = user_shop_owner product0 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_0_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_0_DESCRIPTION) product1 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_1_NAME, active=True, short_description=testing_constants.MAGENTO_PRODUCT_1_DESCRIPTION) product2 = Product(platform_product_id=testing_constants.MAGENTO_PRODUCT_2_PLATFORM_PRODUCT_ID, name=testing_constants.MAGENTO_PRODUCT_2_NAME, active=False, short_description=testing_constants.MAGENTO_PRODUCT_2_DESCRIPTION) order0_user_legacy = UserLegacy(email=testing_constants.MAGENTO_ORDER_0_EMAIL, name=testing_constants.MAGENTO_ORDER_0_NAME) order0 = Order(platform_order_id=testing_constants.MAGENTO_ORDER_0_PLATFORM_ORDER_ID, status=Constants.ORDER_STATUS_PURCHASED, purchase_timestamp=testing_constants.MAGENTO_ORDER_0_CREATED, user_legacy=order0_user_legacy) order0.products.append(product0) order1_user_legacy = UserLegacy(email=testing_constants.MAGENTO_ORDER_1_EMAIL, name=testing_constants.MAGENTO_ORDER_1_NAME) order1 = Order(platform_order_id=testing_constants.MAGENTO_ORDER_1_PLATFORM_ORDER_ID, status=Constants.ORDER_STATUS_PURCHASED, purchase_timestamp=testing_constants.MAGENTO_ORDER_1_CREATED, user_legacy=order1_user_legacy) order1.products.append(product0) order1.products.append(product1) order2_user_legacy = UserLegacy(email=testing_constants.MAGENTO_ORDER_2_EMAIL, name=testing_constants.MAGENTO_ORDER_2_NAME) order2 = Order(platform_order_id=testing_constants.MAGENTO_ORDER_2_PLATFORM_ORDER_ID, status=Constants.ORDER_STATUS_PURCHASED, purchase_timestamp=testing_constants.MAGENTO_ORDER_2_CREATED, user_legacy=order2_user_legacy) shop.products.append(product0) shop.products.append(product1) shop.products.append(product2) shop.orders.append(order0) shop.orders.append(order1) shop.orders.append(order2) db.session.add(shop) db.session.commit() # test api = magento_api.API() current_orders = Order.query.filter_by(shop_id=shop.id).all() new_and_updated = api.fetch_new_and_updated_orders(current_orders, shop) self.assertEquals(len(new_and_updated), 1) db.session.add_all(new_and_updated) db.session.commit() self.assertEquals(len(self.outbox), 1) self.assertEquals(len(self.outbox[0].send_to), 1) self.assertEquals(self.outbox[0].send_to.pop(), testing_constants.MAGENTO_ORDER_1_EMAIL) self.assertEquals(self.outbox[0].subject, Constants.DEFAULT_REVIEW_SUBJECT % (testing_constants.MAGENTO_ORDER_1_NAME.split()[0], testing_constants.MAGENTO_SHOP_NAME)) self.assertTrue(testing_constants.MAGENTO_ORDER_1_NAME.split()[0] in self.outbox[0].body) self.assertTrue(testing_constants.MAGENTO_PRODUCT_0_NAME in self.outbox[0].body) self.assertTrue(testing_constants.MAGENTO_PRODUCT_1_NAME in self.outbox[0].body) self.assertTrue(testing_constants.MAGENTO_SHOP_NAME in self.outbox[0].body) self.assertEquals(new_and_updated[0].platform_order_id, testing_constants.MAGENTO_ORDER_1_PLATFORM_ORDER_ID) self.assertEquals(new_and_updated[0].status, Constants.ORDER_STATUS_NOTIFIED) self.assertEquals(new_and_updated[0].purchase_timestamp, datetime.datetime.strptime(testing_constants.MAGENTO_ORDER_1_CREATED, '%Y-%m-%d %H:%M:%S')) self.assertEquals(new_and_updated[0].shipment_timestamp, datetime.datetime.strptime(testing_constants.MAGENTO_ORDER_1_SHIPPED, '%Y-%m-%d %H:%M:%S')) self.assertEquals(len(new_and_updated[0].products), 2) self.assertIsNotNone(new_and_updated[0].products[0]) self.assertEquals(new_and_updated[0].products[0].platform_product_id, testing_constants.MAGENTO_PRODUCT_0_PLATFORM_PRODUCT_ID) self.assertIsNotNone(new_and_updated[0].products[1]) self.assertEquals(new_and_updated[0].products[1].platform_product_id, testing_constants.MAGENTO_PRODUCT_1_PLATFORM_PRODUCT_ID) self.assertEquals(len(new_and_updated[0].tasks), 2) self.assertEquals(new_and_updated[0].tasks[0].method, 'notify_for_review') self.assertEquals(str(new_and_updated[0].tasks[0].eta), '2015-12-19 12:00:00') self.assertEquals(new_and_updated[0].tasks[1].method, 'send_email') self.assertEquals(str(new_and_updated[0].tasks[1].eta), '2015-12-19 12:00:00') self.assertIsNotNone(new_and_updated[0].user_legacy) self.assertEquals(new_and_updated[0].user_legacy.email, testing_constants.MAGENTO_ORDER_1_EMAIL) self.assertEquals(new_and_updated[0].user_legacy.name, testing_constants.MAGENTO_ORDER_1_NAME) # tear down user = User.query.filter_by(email=testing_constants.NEW_USER_EMAIL).first() customer = user.customer[0] shop = Shop.query.filter_by(name=testing_constants.MAGENTO_SHOP_NAME).first() orders = Order.query.all() for product in shop.products: db.session.delete(product) db.session.delete(user) db.session.delete(customer) db.session.delete(shop) db.session.delete(shop) for order in orders: for review_request in order.review_requests: db.session.delete(review_request) db.session.delete(order) db.session.commit()