def add_to_basicmodel(column_1_value, column_2_value, column_3_value): try: new_basicmodel_object = BasicModel(column_1=column_1_value, column_2=column_2_value, column_3=column_3_value) Session.add(new_basicmodel_object) Session.commit() return "Sucessfully added" except: Session.rollback() return "Something unexpected happened, rolledback operation"
def create_account_by_chat_id(self, chat_id: str, username: str): session = Session() instance = session.query(AccountModel).filter_by( chat_id=chat_id, ).one_or_none() if instance: return account_model = AccountModel(chat_id=chat_id, username=username) try: session.add(account_model) session.commit() except Exception: session.rollback()
import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.ERROR) session = Session() def get_course_from_db(subject, num) -> Course: course = session.query(Course).filter_by(subject=subject, num=num).first() return course # Fetch all courses offered from GW's course bulletin page courses = get_all_courses() # Commit courses by either updating existing rows or creating new ones print('Updating existing courses...') for course in courses: db_course = get_course_from_db(course.subject, course.num) if db_course is not None: # Update the course, comments, and credits values # Leave prereqs blank, as those are filled in manually db_course.course = course.course db_course.comments = course.comments db_course.credits = course.credits else: # If not already in db, add as a new course session.add(course) # Commit changes to db print('Commiting changes to db...') session.commit()
continue else: if not is_subject_in_db(course_subject): print( f'ERROR: No courses with subject {course_subject} exist in the database.' ) continue valid_course = True if course_num != 'ALL': new_elective = Elective(elective_type_id=elective_type.id, course_subject=course_subject, course_num=course_num) session.add(new_elective) session.commit() print('\nSuccesfully added elective.') else: new_electives = [] # Fetch all courses for the given subject subject_courses = session.query(Course).filter_by( subject=course_subject).all() num_added = 0 for course in subject_courses: if not is_elective_in_db(course.subject, course.num, elective_type.id): new_electives.append( Elective(elective_type_id=elective_type.id, course_subject=course.subject, course_num=course.num))
from datafetching.sections import get_all_sections_for_semester, get_sections_on_page from db.connection import Session from db.models import Section import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.ERROR) session = Session() sections = get_all_sections_for_semester('https://my.gwu.edu/mod/pws/subjects.cfm?campId=7&termId=202101', 'Spring', '2021') # sections = get_sections_on_page('https://my.gwu.edu/mod/pws/courses.cfm?campId=1&termId=202103&subjId=CSCI', 'Fall', '2020', 1) print('Checking for duplicate crns in database...') for section in sections: # Check if this crn already exists, if it does not, create new section existing_sec = session.query(Section).filter_by(crn=section.crn).first() if existing_sec is None: session.add(section) else: logging.error(f'CRN {section.crn} already exists in DB; skipping...') print('Committing sections to database...') # Commit new sections session.commit() print('Done.')