def session(db): truncate_all() session = Session() yield session session.close()
def delete_from_basicmodel(column_2_value): try: to_be_deleted_object = Session.query(BasicModel).filter_by( column_2=column_2_value).first() Session.delete(to_be_deleted_object) return "Succesfully deleted!!" except: Session.rollback() return "Something unexpected happened, rolledback operation"
def update_basicmodel(old_column_1_value, new_column_1_value): try: ol_baicmodel_object = Session.query(BasicModel).filter_by( column_1=old_column_1_value).first() ol_baicmodel_object.column_1 = new_column_1_value Session.commit() return "Sucessfully updated" 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()
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 is_account_exists(self, chat_id: str): session = Session() instance = session.query(AccountModel).filter_by( chat_id=chat_id, ).one_or_none() return bool(instance)
from datafetching.prereqs import get_prereqs_for_course, get_subject_codes, get_last_three_terms import json from db.connection import Session from db.models import Course import logging logging.basicConfig(filename='prereq.log', level=logging.DEBUG) # Get all courses in the database session = Session() all_courses = session.query(Course).all() # Get term codes for Fall, Spring, and Summer terms = get_last_three_terms() # Fetch prereqs for each course for course in all_courses: try: prereqs = get_prereqs_for_course(course.subject, course.num, terms) course.prereqs = prereqs except Exception as e: logging.error(e) session.commit()
from collections import deque import json from db.connection import Session from db.models import Course import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.ERROR) session = Session() def is_course_in_db(subject, num): course = session.query(Course).filter_by(subject=subject, num=num).first() return course is not None def print_prereq_dict(prereq_dict, current_node, indent): if prereq_dict['type'] == 'COURSE': print(f'{" " * indent}{prereq_dict["subject"]} {prereq_dict["num"]}') else: print(f'{" " * indent}{prereq_dict["type"]} ↴') for node in prereq_dict['members']: print_prereq_dict(node, current_node, indent+4) if current_node == prereq_dict: print(f'{" " * (indent+4)}Currently editing...') # Initialize our prereq data structures while True: prereq_dict = {} node_queue = deque() node_queue.append(prereq_dict)
class Meta: model = AccountModel sqlalchemy_session = Session() sqlalchemy_get_or_create = ('chat_id', ) sqlalchemy_session_persistence = 'commit'
from datafetching.courses import get_all_courses, get_courses_on_page from db.connection import Session from db.models import Course 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)
from collections import deque import json from db.connection import Session from db.models import Course, Curriculum, ElectiveType, Elective import logging import sys logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.ERROR) session = Session() def is_course_in_db(subject, num): course = session.query(Course).filter_by(subject=subject, num=num).first() return course is not None def is_subject_in_db(subject): course = session.query(Course).filter_by(subject=subject).first() return course is not None def is_elective_in_db(subject, num, elective_type_id): elective = session.query(Elective).filter_by( course_subject=subject, course_num=num, elective_type_id=elective_type_id).first() return elective is not None
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.')
import requests from bs4 import BeautifulSoup from db.connection import Session from db.models import Section, Course import logging session = Session() def parse_time_string(time_str: str) -> str: am_pm = time_str[-2:] hour_min = time_str[:-2].split(':') hour = int(hour_min[0]) minute = int(hour_min[1]) if am_pm.upper() == 'PM' and hour < 12: hour += 12 return f'{hour:02d}{minute:02d}' def get_sections_on_page(url: str, term: str, year: str, pageNum: int): print(f'{" " * (pageNum - 1)}Getting sections at {url} page {pageNum}...') # Load the HTML document into BeautifulSoup form_data = { 'pageNum': pageNum } r = requests.post(url, data=form_data) soup = BeautifulSoup(r.text, 'html.parser') sections = []
def get_all_basicmodel_entries(): return Session.query(BasicModel).all()