def add_article(current_user): try: data = request.form files = request.files article = Article() article.title = data['title'] article.category = data['category'] article.author = data['author'] article.author_desc = data['author_desc'] article.date = datetime.strptime(str(data['date']),'%Y-%m-%d') article.image = files['image'].read() article.caption = data['caption'] article.text = data['text'] if 'chart1' in files: article.chart1 = files['chart1'].read() article.chart1_caption = data.get('chart1','') if 'chart2' in files: article.chart2 = files['chart2'].read() article.chart2_caption = data.get('chart2','') db.session.add(article) db.session.commit() return jsonify({ 'status': 'SUCCESS', 'data': {}, }) except Exception as e: print(e) return jsonify({ 'status': 'ERROR', 'data': str(e), })
def fetch_articles(): print('Fetching articles') publishers = Publisher.query.filter(Publisher.name != 'All') articles_url_association_dict = {} articles = Article.query.all() external_publishers = [ 'theguardian', 'politico', 'theverge', 'engadget', 'stuff', 'spiegel', 'sfgate', 'dailymail' ] for publisher in publishers: src = publisher.rss url = publisher.url print(f'rss: {src}, url: {url}') try: r = requests.get(src) feed = feedparser.parse(r.text) except Exception as e: print(f'Exception at 103: {e}') continue author = publisher if author and feed: try: for i, entry in enumerate(feed.entries): fetch_from_mocksite = False existing = False url = entry.link if any([i in url for i in external_publishers]): articles_url_association_dict[url] = None fetch_from_mocksite = True article = [i for i in articles if i.url == url] or None if article: article = article[0] try: img = entry.media_content[0]['url'] except Exception as e: img = None if not img: img = author.image try: terms = [] if hasattr(entry, 'tags'): terms = [i.term.lower() for i in entry.tags] category = None for c in Category: cat_name = c.value.lower() if len(cat_name) > 0: cat_name = cat_name[:-1] if cat_name in terms or cat_name in src.lower( ) or cat_name in url.lower(): category = c break if not category: category = Category(entry.category) except Exception as e: category = Category('') try: day = date.fromisoformat(entry.published) except Exception as e: day = date.today() try: desc = BeautifulSoup(entry.description, "lxml").text except: if hasattr(entry, 'description'): desc = entry.description soup = BeautifulSoup(desc, 'html.parser') desc = soup.text.strip() for img in soup.findAll('img'): img = img.get('src') break elif hasattr(entry, 'summary'): desc = entry.summary if not article: article = Article(name=entry.title, publisher=author, image=img, url=url, description=desc, date=day, category=category) else: existing = True article.image = img if not article.image else article.image article.category = category if category else article.category article.date = day if not article.date else article.date if entry.description: article.description = entry.description if fetch_from_mocksite: articles_url_association_dict[url] = article #generate article here? #gather contents in dict if not existing and not fetch_from_mocksite: db.session.add(article) print(f'fetched {author.url} articles succesfully') except Exception as e: print(f'Exception at 172: {e}') else: print('Couldnt find author with given url') print(f'URL:\n{url}') print('\n' * 3) for i in publishers: print(i) print('\n' * 3) try: print('Fetching association table') payload = {'urls': list(articles_url_association_dict.keys())} url = f'http://{PUBLISHER_DOMAIN}/check_urls' print('Asking existing article data') res = requests.post(url, json=payload) # ask existing article data data = res.json() mocksite_generate_article_data = [] print('Fetch not existing articles data') for url in data.get('not_existing', []): try: new_art_data = generate_article_data(url) except Exception as e: print(f'Exception at 193: {e}') new_art_data = None if new_art_data: header, subheader, avc, roc = new_art_data mocksite_generate_article_data.append({ 'url': url, 'header': header, 'subheader': subheader, 'avc': avc, 'roc': roc }) print('Send article generation request') mock_generate_art_url = f'http://{PUBLISHER_DOMAIN}/generate_article' resp = requests.post(mock_generate_art_url, json=mocksite_generate_article_data) resp = resp.json() print('associate urls with article objects') existing_urls = [i.url for i in articles] existing_mock_data = data.get('existing', {}) existing_mock_data = {**existing_mock_data, **resp} for url, article in articles_url_association_dict.items(): mock_url = existing_mock_data.get(url, None) if mock_url and mock_url not in existing_urls: article.url = mock_url try: db.session.add(article) except Exception as e: print(f'Exception at 219: {e}') else: try: db.session.expunge(article) except Exception as e: print(f'Exception at 224: {e}') print('done') except Exception as e: print(f'Exception at 227 {e}') db.session.commit()
product.images_album = 'P95VLN7' article = Article(randomString(10)) order = Order() order_item = OrderItem() order_item.quantity = 1 order_item.start_date = datetime.strptime('1-4-2020', '%d-%m-%Y') order_item.end_date = datetime.strptime('8-4-2020', '%d-%m-%Y') order.order_items = [] order.order_items.append(order_item) article_category = food_article category = food_category if x % 2 == 0: category = clothes_category article_category = clothes_article product.category = category article.category = article_category article.status = status db.session.add(order) db.session.add(product) db.session.add(order_item) db.session.add(article) db.session.add(max_no_products_per_month) db.session.add(max_no_free_products_adayalite_user) db.session.add(charge_per_day_if_late) db.session.add(min_duration_of_rental) db.session.add(max_duration_of_rental) db.session.add(max_no_of_vouchers) db.session.add(MAIL_USERNAME) db.session.add(MAIL_PASSWORD) db.session.add(MAIL_SERVER) db.session.add(MAIL_PORT)
def init_database(): db = AC().db # Create the database and the database table db.create_all() member = Role("member") admin = Role("admin") user = User("*****@*****.**", "1q2w3e4r") user.email_confirmed = True user2 = User("*****@*****.**", "1q2w3e4r") user3 = User("*****@*****.**", "1q2w3e4r") user4 = User("*****@*****.**", "1q2w3e4r") user.role = admin user2.email_confirmed = True user2.subscribed = True user3.email_confirmed = True user3.subscribed = True user4.email_confirmed = False max_no_products_per_month = ConfigValues('max_no_products_per_month', 20) max_no_free_products_adayalite_user = ConfigValues( 'max_no_free_products_adayalite_user', 4) max_no_of_items_per_order_adayalifestyle = ConfigValues( 'max_no_of_items_per_order_adayalifestyle', 4) max_no_products_per_order = ConfigValues('max_no_products_per_order', 4) min_duration_of_rental = ConfigValues('min_duration_of_rental', 4) max_duration_of_rental = ConfigValues('max_duration_of_rental', 7) max_no_of_vouchers = ConfigValues('max_no_of_vouchers', 2) MAIL_USERNAME = ConfigValues('MAIL_USERNAME', '*****@*****.**') MAIL_PASSWORD = ConfigValues('MAIL_PASSWORD', 'adaya1234') MAIL_SERVER = ConfigValues('MAIL_SERVER', 'smtp.gmail.com') MAIL_PORT = ConfigValues('MAIL_PORT', 465) MAIL_DEFAULT_SENDER = ConfigValues('MAIL_DEFAULT_SENDER', '*****@*****.**') EMAIL_PASSWORD_RESET_SECRET_KEY = ConfigValues( 'EMAIL_PASSWORD_RESET_SECRET_KEY', 'Thisisasecret!') SIB_KEY = ConfigValues('SIB_KEY', 'gzryVUPZHa1GW7n6') subtype = SubscriptionType(plan='Adaya Lite', price=10) subtype2 = SubscriptionType(plan='Adaya Premium', price=40) usersubscription = UserSubscription() usersubscription.user_id = 2 usersubscription.current_start_date = datetime.now() usersubscription.current_end_date = datetime.strptime( '2020-09-06 05:58:00', '%Y-%m-%d %H:%M:%S') usersubscription.subscription_type = subtype usersubscription2 = UserSubscription() usersubscription2.user_id = 3 usersubscription2.current_start_date = datetime.now() usersubscription2.current_end_date = datetime.strptime( '2020-08-06 05:58:00', '%Y-%m-%d %H:%M:%S') usersubscription2.subscription_type = subtype2 voucher = Voucher('HAO20') voucher.discount_fixed_amount = 100 voucher.product_id = 3 voucher.redeem_by = datetime.strptime('2020-8-13 00:00:00', '%Y-%m-%d %H:%M:%S') voucher2 = Voucher('LUO20') voucher2.discount_fixed_amount = 8.00 voucher2.product_id = 1 voucher2.redeem_by = datetime.strptime('2020-8-13 00:00:00', '%Y-%m-%d %H:%M:%S') for x in range(1, 11): variation = Variation('S') variation.product_id = x variation.price = 10 variation.stock = 1 variation1 = Variation('M') variation1.product_id = x variation1.price = 20 variation1.stock = 1 variation2 = Variation('L') variation2.product_id = x variation2.price = 30 variation2.stock = 1 variation3 = Variation('XL') variation3.product_id = x variation3.price = 40 variation3.stock = 1 db.session.add(variation) db.session.add(variation1) db.session.add(variation2) db.session.add(variation3) db.session.add(max_no_products_per_order) db.session.add(max_no_products_per_month) db.session.add(max_no_free_products_adayalite_user) db.session.add(max_no_of_items_per_order_adayalifestyle) db.session.add(min_duration_of_rental) db.session.add(max_duration_of_rental) db.session.add(max_no_of_vouchers) db.session.add(MAIL_USERNAME) db.session.add(MAIL_PASSWORD) db.session.add(MAIL_SERVER) db.session.add(MAIL_PORT) db.session.add(MAIL_DEFAULT_SENDER) db.session.add(EMAIL_PASSWORD_RESET_SECRET_KEY) db.session.add(SIB_KEY) db.session.add(voucher) db.session.add(voucher2) db.session.add(member) db.session.add(user) db.session.add(user2) db.session.add(user3) db.session.add(user4) db.session.add(subtype) db.session.add(subtype2) db.session.add(usersubscription) db.session.add(usersubscription2) food_category = ProductCategory('food') clothes_category = ProductCategory('cloth') food_article = ArticleCategory('food-article') clothes_article = ArticleCategory('cloth-article') status = ArticleStatus('draft') order_status = OrderStatus('completed') db.session.add(order_status) db.session.add(food_article) db.session.add(clothes_article) db.session.add(food_category) db.session.add(clothes_category) for x in range(10): product = Product('Haoluo') article = Article(randomString(10)) order = Order() order_item = OrderItem() order_item.quantity = 1 order_item.variation_id = 2 order_item.start_date = datetime.strptime('2020-4-1 00:00:00', '%Y-%m-%d %H:%M:%S') order_item.end_date = datetime.strptime('2020-4-8 00:00:00', '%Y-%m-%d %H:%M:%S') order.order_items = [] order.order_items.append(order_item) article_category = food_article category = food_category if x % 2 == 0: category = clothes_category article_category = clothes_article product.category = category article.category = article_category article.status = status db.session.add(order) db.session.add(product) db.session.add(order_item) db.session.add(article) db.session.commit() yield db db.drop_all()