import oracledb import json import logging logging.basicConfig(level=logging.INFO) # Connect to the Oracle database try: connection = oracledb.connect( user="sisvietnam", password="sisvietnam", dsn="localhost:1521/sisvietnam" ) with connection.cursor() as cursor: # Get the ID of the 'education' tag sql_tag = "SELECT id, name FROM sis_tag WHERE name LIKE '%education%' FETCH FIRST 1 ROWS ONLY" cursor.execute(sql_tag) tag = cursor.fetchone() if not tag: logging.error("Tag 'education' not found.") else: tag_id = tag[0] tag_name = tag[1] logging.info(f"Found tag '{tag_name}' with ID {tag_id}") # Fetch the latest 10 published posts with this tag sql_posts = """ SELECT p.title, p.featured_image, p.slug, p.excerpt, p.created_date, p.content FROM sis_post p JOIN sis_post_tag pt ON p.id = pt.post_id WHERE pt.tag_id = :tag_id AND p.status = 'PUBLISHED' ORDER BY p.created_date DESC FETCH FIRST 10 ROWS ONLY """ cursor.execute(sql_posts, [tag_id]) posts = cursor.fetchall() import random import re def extract_words(html_text, num_words=100): if not html_text: return "" if hasattr(html_text, 'read'): html_text = html_text.read() text = re.sub('<[^<]+?>', ' ', html_text) words = text.split() return " ".join(words[:num_words]) + ("..." if len(words) > num_words else "") prices = ["42.400.000 VNĐ", "23.000.000 VNĐ", "32.500.000 VNĐ", "15.000.000 VNĐ", "Liên hệ"] badges = ["SAT", "SUN", "MON", "TUE", "WED", "THU", "FRI"] items = [] for post in posts: img = post[1] if post[1] else "" desc = post[3] if post[3] else extract_words(post[5], 100) date_val = post[4] date_str = date_val.strftime("%d/%m/%Y") if hasattr(date_val, 'strftime') else str(date_val) if date_val else "12/09/2026" items.append({ "title": post[0], "imageUrl": img, "linkUrl": f"/dao-tao/{post[2]}", "description": desc, "badge": random.choice(badges), "price": random.choice(prices), "dateStr": date_str }) new_group = { "slug": "chuong-trinh-khac", "name": "Chương trình khác", "layoutType": "course-card", "description": "", "className": "", "notes": f"Tự động tạo bằng Python từ thẻ {tag_name}", "items": items } # Get existing settings sql_setting = "SELECT setting_value FROM sis_setting WHERE setting_key = 'plugin_swiper_slider_data'" cursor.execute(sql_setting) setting = cursor.fetchone() if setting and setting[0]: try: # In Oracle CLOB is sometimes returned as LOB object, read it if so val = setting[0] if hasattr(val, 'read'): val = val.read() groups = json.loads(val) except Exception as e: logging.error(f"Error parsing JSON: {e}") groups = [] else: groups = [] # Remove existing 'chuong-trinh-khac' if present groups = [g for g in groups if g.get('slug') != 'chuong-trinh-khac'] # Add the new group groups.append(new_group) new_setting_value = json.dumps(groups, ensure_ascii=False) # Update the database if setting: sql_update = "UPDATE sis_setting SET setting_value = :val WHERE setting_key = 'plugin_swiper_slider_data'" cursor.execute(sql_update, [new_setting_value]) else: sql_insert = "INSERT INTO sis_setting (setting_key, setting_value) VALUES ('plugin_swiper_slider_data', :val)" cursor.execute(sql_insert, [new_setting_value]) connection.commit() logging.info("Successfully updated sis_setting with the new slider group.") except Exception as e: logging.error(f"An error occurred: {e}") finally: if 'connection' in locals(): connection.close()