def problem(courseId, problemId, pageNum):
    """
    use db to get its problem page
    now, it moves to just default problem page
    """
    try :
        # Get startDateOfSubmission of Problem
        try:
            startDateOfSubmission = select_problems_of_course(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = None,
                                                                                                                          courseId = courseId,
                                                                                                                          problemId = problemId)).first().\
                                                              startDateOfSubmission
        except Exception:
            startDateOfSubmission = None
            
        # are Not Access. conditions is an Administrator and endOfSubmission ago
        if SETResources().const.SERVER_ADMINISTRATOR in session[SessionResources().const.AUTHORITY]\
           or SETResources().const.COURSE_ADMINISTRATOR in session[SessionResources().const.AUTHORITY]\
           or startDateOfSubmission <= datetime.now():
            
            try:
                from GradeServer.utils.utilCodeSubmissionQuery import select_languages
                languageInfoRecords = select_languages(courseId = courseId).all()
            except Exception:
                languageInfoRecords = []
    
            try:
                problemInformation = select_problem_informations(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = None,
                                                                                                                             courseId = courseId,
                                                                                                                             problemId = problemId)).first()
            except Exception:
                problemInformation = []    
    
            browserName = request.user_agent.browser
            browserVersion = request.user_agent.version
            print browserName, browserVersion
            return render_template(HTMLResources().const.PROBLEM_HTML,
                                   SETResources = SETResources,
                                   SessionResources = SessionResources,
                                   LanguageResources = LanguageResources,
                                   courseId = courseId,
                                   problemId = problemId,
                                   problemInformation = problemInformation,
                                   problemName = problemInformation.problemName.replace(' ', ''),
                                   languageInfoRecords = languageInfoRecords,
                                   pageNum = pageNum,
                                   browserName = browserName,
                                   browserVersion = browserVersion)
    
        # Access Rejection
        else:
            flash('제출 기간이 아닙니다!!!')
            return redirect(url_for(RouteResources().const.PROBLEM_LIST,
                                    courseId = courseId,
                                    pageNum = pageNum))
    except Exception:
        return unknown_error()
def problem_list(courseId, pageNum):
    """ problem submitting page """
    try:
        # Get Last Submitted History
        lastSubmission = select_last_submissions(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = session[SessionResources().const.MEMBER_ID],
                                                                                                             courseId = courseId)).subquery()
        # Current Submission                                      
        submissions = select_current_submissions(lastSubmission).subquery()
        
        # Get Problem Informations
        problems = join_problems_names(select_problems_of_course(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = None,
                                                                                                                             courseId = courseId)).subquery()).subquery()
        # Get ProblemList Count
        try:
            count = select_count(problems.c.problemId).first().\
                                                       count
        except Exception:
            count = 0
        # Get ProblemListRecords OuterJoin
        try:
            problemListRecords = get_page_record(join_problem_lists_submissions(problems,
                                                                                submissions),
                                                 pageNum = pageNum).all()
        except Exception:
            problemListRecords = []
            
        # Get Course Information
        try:
            courseRecords = dao.query(RegisteredCourses.courseId,
                                      RegisteredCourses.courseName).\
                                filter(RegisteredCourses.courseId == courseId).\
                                first()
        except:
            courseRecords = []
        
        return render_template(HTMLResources().const.PROBLEM_LIST_HTML,
                               SETResources = SETResources,
                               SessionResources = SessionResources,
                               LanguageResources = LanguageResources,
                               courseRecords = courseRecords,
                               problemListRecords = problemListRecords,
                               pages = get_page_pointed(pageNum = pageNum,
                                                        count = count))
    except Exception:
        return unknown_error()
def submission_code(memberId, courseId, problemId, error = None):
    
    try:
        # Get endDateOfSubmission of Problem
        try:
            endDateOfSubmission = select_problems_of_course(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = None,
                                                                                                                        courseId = courseId,
                                                                                                                        problemId = problemId)).first().\
                                                            endDateOfSubmission
        except Exception:
            endDateOfSubmission = None
            
        # are Not Access. conditions is an Administrator and endOfSubmission ago
        if SETResources().const.SERVER_ADMINISTRATOR in session[SessionResources().const.AUTHORITY]\
           or SETResources().const.COURSE_ADMINISTRATOR in session[SessionResources().const.AUTHORITY]\
           or endDateOfSubmission <= datetime.now():
            
            # last Submissions Info
            lastSubmission = select_last_submissions(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = memberId,
                                                                                                                 courseId = courseId,
                                                                                                                 problemId = problemId)).subquery()
            # Code View Count Up
            update_submission_code_view_count(lastSubmission,
                                              memberCourseProblemParameter = MemberCourseProblemParameter(memberId = memberId,
                                                                                                          courseId = courseId,
                                                                                                          problemId = problemId))
            # Commit Exception
            try:
                dao.commit()
            except Exception:
                dao.rollback()
                error = get_message('updateFailed')
                
            # Problem Information (LimitedTime, LimitedMemory
            try:
                problemName = select_problem_informations(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = None,
                                                                                                                      courseId = None,
                                                                                                                      problemId = problemId)).first().\
                                                                     problemName
            except Exception:
                problemName = None
                
            # Problem Solved Users
            try:
                # last Submissions Info
                submissions = select_all_submissions(lastSubmission,
                                                     memberCourseProblemParameter = MemberCourseProblemParameter(memberId = memberId,
                                                                                                                 courseId = courseId,
                                                                                                                 problemId = problemId)).subquery()
                problemSolvedMemberRecords = select_solved_submissions(submissions).first()
            except Exception:
                problemSolvedMemberRecords = []
                
            # Submitted Files Information
            try:
                submittedFileRecords = select_submitted_files(memberCourseProblemParameter = MemberCourseProblemParameter(memberId = memberId, 
                                                                                                                          courseId = courseId,
                                                                                                                          problemId = problemId)).all()
                fileData = []
                for raw in submittedFileRecords:
                    # Open
                    filePath = raw.filePath + '/' +raw.fileName
                    file = open(filePath)
                    # Read
                    data = file.read()
                    
                    # Close
                    file.close()
                    fileData.append(data)
            except Exception:
                submittedFileRecords = []
                fileData = []
                
            return render_template(HTMLResources().const.SUBMISSION_CODE_HTML,
                                   SETResources = SETResources,
                                   SessionResources = SessionResources,
                                   LanguageResources = LanguageResources,
                                   submittedFileRecords = submittedFileRecords,
                                   fileData = fileData,
                                   problemName = problemName,
                                   problemSolvedMemberRecords = problemSolvedMemberRecords,
                                   error = error)
        #Access Rejection
        else:
            flash('코드를 볼 권한이 없습니다!!!')
            return redirect(url_for(RouteResources().const.PROBLEM_RECORD,
                                    courseId = courseId,
                                    problemId = problemId,
                                    sortCondition = OtherResources().const.RUN_TIME))
    except Exception:
        return unknown_error()