Skip to content

Commit

Permalink
fix: orm schema
Browse files Browse the repository at this point in the history
  • Loading branch information
k0000k committed Jan 3, 2024
1 parent df3bf93 commit 5a68593
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
7 changes: 5 additions & 2 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import FastAPI
from fastapi import FastAPI, HTTPException
import uvicorn
from model.req_models import *

Expand All @@ -14,7 +14,10 @@ async def smart_campus_controller(smart_campus_req: SmartCampusReq):
현재 정상 이용 가능합니다.
"""
from smart_campus.smart_campus import smart_campus_crawling
result = smart_campus_crawling(smart_campus_req.token, smart_campus_req.student_id)
try:
result = smart_campus_crawling(smart_campus_req.token, smart_campus_req.student_id)
except:
raise HTTPException(status_code=400, detail="에러가 발생 했습니다.")
return result

@app.post("/smart-campus/auth")
Expand Down
56 changes: 29 additions & 27 deletions smart_campus/smart_campus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import sqlalchemy
from sqlalchemy import create_engine, Column, Integer, CHAR, DateTime, ForeignKey, Boolean
from sqlalchemy.orm import sessionmaker, declarative_base
from fastapi.responses import JSONResponse
import datetime
import configuration

Expand All @@ -25,7 +24,7 @@

class Course(Base):
__tablename__ = 'course'
__table_args__ = {"schema": "course"}
__table_args__ = {"schema": "daitssu"}
course_code = Column(CHAR(32), nullable=False)
id = Column(Integer, primary_key=True)
name = Column(CHAR(64))
Expand All @@ -36,10 +35,10 @@ class Course(Base):

class Video(Base):
__tablename__ = 'video'
__table_args__ = {"schema": "course"}
__table_args__ = {"schema": "daitssu"}
id = Column(Integer, primary_key=True)
name = Column(CHAR(256))
course_id = Column(Integer, ForeignKey('course.course.id'))
course_id = Column(Integer, ForeignKey('daitssu.course.id'))
due_at = Column(DateTime, default=default_date)
start_at = Column(DateTime, default=default_date)
created_at = Column(DateTime)
Expand All @@ -48,10 +47,10 @@ class Video(Base):

class Assignment(Base):
__tablename__ = 'assignment'
__table_args__ = {"schema": "course"}
__table_args__ = {"schema": "daitssu"}
id = Column(Integer, primary_key=True)
name = Column(CHAR(256))
course_id = Column(Integer, ForeignKey('course.course.id'))
course_id = Column(Integer, ForeignKey('daitssu.course.id'))
due_at = Column(DateTime, default=default_date)
start_at = Column(DateTime, default=default_date)
created_at = Column(DateTime)
Expand All @@ -60,30 +59,29 @@ class Assignment(Base):

class UserCourseRelation(Base):
__tablename__ = 'user_course_relation'
__table_args__ = {"schema": "course"}
__table_args__ = {"schema": "daitssu"}
id = Column(Integer, primary_key=True)
user_id = Column(Integer)
course_id = Column(Integer, ForeignKey('course.course.id'))
course_id = Column(Integer, ForeignKey('daitssu.course.id'))
register_status = Column(CHAR(20))
created_at = Column(DateTime)
updated_at = Column(DateTime)


class Calendar(Base):
__tablename__ = 'calendar'
__table_args__ = {"schema": "course"}
__table_args__ = {"schema": "daitssu"}
id = Column(Integer, primary_key=True)
type = Column(CHAR(32))
course = Column(CHAR(64))
course_id = Column(Integer, ForeignKey('daitssu.course.id'))
due_at = Column(DateTime,default=default_date)
name = Column(CHAR(256))
is_complete = Column(Boolean)
is_completed = Column(Boolean)
created_at = Column(DateTime)
updated_at = Column(DateTime)
user_id = Column(Integer)



class SmartCampus:
def __init__(self, session):
self.session = session
Expand Down Expand Up @@ -137,6 +135,7 @@ def get_calander_data(self, token, subject_num, user_id):

summary_url = f"https://canvas.ssu.ac.kr/learningx/api/v1/courses/{subject}/attendance_items/summary?only_use_attendance=true"
response_summary = requests.get(summary_url, headers=headers)
summary_data = ""
if response_summary.status_code == 200:
summary_data = response_summary.json().get("attendance_summaries", {})

Expand All @@ -159,9 +158,9 @@ def get_calander_data(self, token, subject_num, user_id):
break
course_name = self.session.query(Course).filter_by(course_code=str(subject_num)).first()
new_calendar_item = Calendar(type="OFFLINE_LECTURE",
course=course_name.name,
course_id=course_name.id,
due_at=schedule_time,
is_complete=status,
is_completed=status,
name=item.get("title"),
created_at=current_time,
updated_at=current_time,
Expand Down Expand Up @@ -280,6 +279,7 @@ def save_to_do_to_calendar(self, token, user_id):
data_dict = response.json() # JSON 응답을 파이썬의 딕셔너리로 변환

for module in data_dict.get("to_dos", []):

course_id = module.get("course_id")

assignment = defaultdict(list)
Expand All @@ -290,7 +290,7 @@ def save_to_do_to_calendar(self, token, user_id):
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()

# 필요한 데이터 가져오기
query = session.query(Calendar).filter_by(course=course.name)
query = session.query(Calendar).filter_by(course_id=course.id)
calendars = query.all()

# 데이터를 HashMap에 저장
Expand Down Expand Up @@ -322,9 +322,9 @@ def save_to_do_to_calendar(self, token, user_id):
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()
new_calendar_item = Calendar(
type="ASSIGNMENT",
course=course.name,
course_id=course.id,
due_at=due_at,
is_complete=False,
is_completed=False,
name=title,
created_at=current_time,
updated_at=current_time,
Expand All @@ -351,9 +351,9 @@ def save_to_do_to_calendar(self, token, user_id):
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()
new_calendar_item = Calendar(
type="VIDEO",
course=course.name,
course_id=course.id,
due_at=due_at,
is_complete=False,
is_completed=False,
name=title,
created_at=current_time,
updated_at=current_time,
Expand All @@ -380,9 +380,9 @@ def save_to_do_to_calendar(self, token, user_id):
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()
new_calendar_item = Calendar(
type="QUIZ",
course=course.name,
course_id=course.id,
due_at=due_at,
is_complete=False,
is_completed=False,
name=title,
created_at=current_time,
updated_at=current_time,
Expand All @@ -402,22 +402,22 @@ def save_to_do_to_calendar(self, token, user_id):
# 남은 데이터 처리
for assign in assignment.keys():
change_data = self.session.query(Calendar).filter_by(name=assign).first()
if change_data.is_complete == False:
change_data.is_complete = True
if change_data.is_completed == False:
change_data.is_completed = True
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()
course.updated_at = current_time

for video in video.keys():
change_data = self.session.query(Calendar).filter_by(name=video).first()
if change_data.is_complete == False:
change_data.is_complete = True
if change_data.is_completed == False:
change_data.is_completed = True
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()
course.updated_at = current_time

for quiz in quiz.keys():
change_data = self.session.query(Calendar).filter_by(name=quiz).first()
if change_data.is_complete == False:
change_data.is_complete = True
if change_data.is_completed == False:
change_data.is_completed = True
course = self.session.query(Course).filter_by(course_code=str(course_id)).first()
course.updated_at = current_time

Expand Down Expand Up @@ -450,6 +450,8 @@ def smart_campus_crawling(token, user_id):
# 모든 작업이 정상적으로 완료되면 commit 수행
session.commit()

return "Success"



if __name__ == "__main__":
Expand Down

0 comments on commit 5a68593

Please sign in to comment.