예제 #1
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_vtimetable():
    current_year = datetime.now().year
    recommend = []

    if session.get_account():
        try:
            analogue = LectureAnalogue.objects(
                campus_id=session.get_account().campus_id,
                student_id=session.get_account().student_id
            ).get()

            lectures1 = Lecture.objects(students__in=[session.get_account().student_id])
            lectures2 = Lecture.objects(students__in=[analogue.target])

            lectures1 = set([l.subject_code for l in lectures1])
            lectures2 = set([l.subject_code for l in lectures2])

            lectures = lectures2 - lectures1
            
            for lecture in lectures:
                try:
                    lecture = Lecture.objects(subject_code=lecture)[0]
                    recommend.append(lecture.subject_name)
                except:
                    pass
        except:
            pass

    return render_template('module/lecture/vtimetable.html', current_year=current_year, recommend=recommend)
예제 #2
0
파일: apis.py 프로젝트: qbx2/opencampus
def lecture_students(campus_id, year, term, code):
    try:
        lecture = Lecture.objects(campus_id=campus_id,
                                  year=year, term=Lecture.term_str_to_int(term), code=code).get()
    except Lecture.DoesNotExist:
        return jsonify({
            'error': {
                'code': 'lecture_not_exist'
            }
        }), 404

    request_data = request.get_json()
    student_id = request_data.get('student_id')

    # TODO
    """
    from opencampus.module.account.models import Account
    try:
        account = Account.objects(campus_id=request.campus.id, student_id=student_id).get()
    except Account.DoesNotExist:
        return jsonify({
            'error': {
                'code': 'account_not_found'
            }
        }), 404
    """

    #if account.id not in lecture.students:
    Lecture.objects(id=lecture.id).update_one(push__students=student_id)

    return jsonify({})
예제 #3
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_auth_admin_method_cb(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)
    from opencampus.module.lecture.authmodels import LectureAuthRequestInfo
    request_info = LectureAuthRequestInfo.objects(lecture_id=lecture.id, auth_code=request.args.get('auth_code')).get()
    request_info.delete()
    Lecture.objects(id=lecture.id).update_one(push__admins=request_info.account_id)
    return '인증 완료'
예제 #4
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_vtimetable_search():
    try:
        year = int(request.args.get('year'))
        term = Lecture.term_str_to_int(request.args.get('term'))
        query = request.args.get('q', '')
    except TypeError:
        return jsonify({'error': {'code': 'type_error'}}), 400

    results = Lecture.search(request.campus.id, query=query, page=1, size=8, year=year, term=term)

    for lecture in results:
        lecture['timetable'] = []
        try:
            lecture_data = Lecture.get_lecture(int(lecture['year']), lecture['term_str'], lecture['code'])
            for time in lecture_data.timetable:
                lecture['timetable'].append({
                    'place': time.place,
                    'room': time.room,
                    'start_time': time.start_time,
                    'end_time': time.end_time
                })
        except:
            pass

    return jsonify({
        'data': results
    })
예제 #5
0
파일: apis.py 프로젝트: qbx2/opencampus
def lecture_create(campus_id):
    try:
        request_data = request.get_json()
        year = request_data.get('year')
        term = Lecture.term_str_to_int(request_data.get('term'))
        code = request_data.get('code')
        if not (year and term and code):
            raise ValueError
    except TypeError or ValueError:
        return jsonify({'error': {'code': 'invalid_request'}})

    lecture = Lecture(
        campus_id=campus_id,
        year=year,
        term=term,
        code=code
    )
    try:
        lecture.save()
    except NotUniqueError:
        return jsonify({'error': {'code': 'lecture_exist'}}), 400
    return jsonify({
        'id': str(lecture.id),
        'year': year,
        'term': Lecture.term_int_to_str(lecture.term),
        'code': lecture.code
    })
예제 #6
0
파일: apis.py 프로젝트: qbx2/opencampus
def lecture_search_api(campus_id):
    from urllib import parse

    query = request.args.get('q', '')
    page = int(request.args.get('page', 1))

    data = Lecture.search(request.campus.id, query=query, page=page)

    for lecture in data:
        lecture['timetable'] = []
        try:
            lecture_data = Lecture.get_lecture(int(lecture['year']), lecture['term_str'], lecture['code'])
            for time in lecture_data.timetable:
                lecture['timetable'].append({
                    'place': time.place,
                    'room': time.room,
                    'start_time': time.start_time,
                    'end_time': time.end_time
                })
        except:
            pass

    result = {
        'data': data,
        'paging': {}
    }

    if len(data) >= 20:
        url = list(parse.urlparse(request.url))
        query = dict(parse.parse_qs(url[4]))
        query['page'] = page + 1
        url[4] = parse.urlencode(query)
        result['paging']['next'] = parse.urlunparse(url)

    return jsonify(result)
예제 #7
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_board_list(year, term, code, board_id):
    lecture = Lecture.get_lecture(year, term, code)
    boards = LectureBoard.objects(lecture_id=lecture.id)
    board = LectureBoard.objects(lecture_id=lecture.id, board_id=board_id).get()

    return render_template('module/lecture/detail/board.html', year=year, term=term, code=code,
                           lecture=lecture, boards=boards, board=board)
예제 #8
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_auth_admin(year, term, code):
    # TODO : Remove
    lecture = Lecture.get_lecture(year, term, code)
    lecture.email = '*****@*****.**'
    lecture.phone = '+821051277004'

    return render_template('module/lecture/detail/auth_admin.html', year=year, term=term, code=code,
                           lecture=lecture)
예제 #9
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_sync(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)
    request.campus.get_gateway().sync_lecture(
        year=lecture.year,
        term=lecture.term_int_to_str(lecture.term),
        code=lecture.code
    )
    return redirect(url_for('campus.lecture_detail', year=year, term=term, code=code))
예제 #10
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_admin_administrators(year, term, code):
    from opencampus.module.account.models import Account
    lecture = Lecture.get_lecture(year, term, code)

    if request.method == 'POST':
        try:
            account = Account.objects(account_id=request.form.get('account_id')).get()
            if account.id not in lecture.admins:
                Lecture.objects(id=lecture.id).update_one(push__admins=account.id)
        except Account.DoesNotExist:
            pass

        lecture = Lecture.get_lecture(year, term, code)

    admins = [Account.objects(id=admin).get() for admin in lecture.admins]

    return render_template('module/lecture/detail/admin/administrators.html', year=year, term=term, code=code,
                           lecture=lecture, admins=admins)
예제 #11
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_search():
    query = request.args.get('q', '')
    page = int(request.args.get('page', 1))
    results = Lecture.search(request.campus.id, query=query, page=page)

    if page > 1 and len(results) < 1:
        return abort(404)

    return render_template('module/lecture/search.html', results=results, next_page=page+1, query=query)
예제 #12
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_board(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)
    boards = LectureBoard.objects(lecture_id=lecture.id)
    if len(lecture.admins) < 1:
        return redirect(url_for('campus.lecture_auth_admin', year=year, term=term, code=code))

    if len(boards) < 1:
        return redirect(url_for('campus.lecture_admin_board', year=year, term=term, code=code))

    return redirect(url_for('campus.lecture_board_list', year=year, term=term, code=code, board_id=boards[0].board_id))
예제 #13
0
파일: apis.py 프로젝트: qbx2/opencampus
def lecture_sync(campus_id, year, term, code):
    try:
        lecture = Lecture.objects(campus_id=campus_id,
                                  year=year, term=Lecture.term_str_to_int(term), code=code).get()
    except Lecture.DoesNotExist:
        return jsonify({
            'error': {
                'code': 'lecture_not_exist'
            }
        }), 404

    campus = Campus.objects(id=campus_id).get()
    campus.get_gateway().sync_lecture(
        year=lecture.year,
        term=lecture.term_int_to_str(lecture.term),
        code=lecture.code
    )

    return jsonify({})
예제 #14
0
파일: apis.py 프로젝트: qbx2/opencampus
def lecture_update(campus_id, year, term, code):
    try:
        lecture = Lecture.objects(campus_id=campus_id,
                                  year=year, term=Lecture.term_str_to_int(term), code=code).get()
    except Lecture.DoesNotExist:
        return jsonify({
            'error': {
                'code': 'lecture_not_exist'
            }
        }), 404
    request_data = request.get_json()

    lecture.type = request_data.get('type', lecture.type)
    lecture.subject_code = request_data.get('subject_code', lecture.subject_code)
    lecture.subject_name = request_data.get('subject_name', lecture.subject_name)
    lecture.credit = int(request_data.get('credit', lecture.credit))
    lecture.grade = int(request_data.get('grade', lecture.grade))
    lecture.departments = request_data.get('departments', lecture.departments)
    lecture.professors = request_data.get('professors', lecture.professors)
    lecture.tags = request_data.get('tags', lecture.tags)
    lecture.timetable_text = request_data.get('timetable_text', lecture.timetable_text)
    if request_data.get('timetable'):
        lecture.timetable = []
        for time in request_data.get('timetable'):
            lecture_time = LectureTime()
            if time.get('time'):
                lecture_time.start_time = time.get('time').get('start')
                lecture_time.end_time = time.get('time').get('end')
            if time.get('place'):
                lecture_time.place = time.get('place').get('name')
                lecture_time.room = time.get('place').get('room')

            lecture.timetable.append(lecture_time)

    lecture.email = request_data.get('email', lecture.email)
    lecture.phone = request_data.get('phone', lecture.phone)

    lecture.save()
    return jsonify({})
예제 #15
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_admin_board_create(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)

    try:
        board = LectureBoard.objects(lecture_id=lecture.id, board_id=request.form.get('board_id')).get()
    except LectureBoard.DoesNotExist:
        board = LectureBoard()

    board.lecture_id = lecture.id
    board.board_id = request.form.get('board_id')
    board.board_name = request.form.get('board_name')
    board.read_perm = request.form.get('read_perm')
    board.write_perm = request.form.get('write_perm')
    board.save()
    return jsonify({})
예제 #16
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_my():
    if not session.get_account():
        return abort(403)

    lectures = []
    c_lectures = []
    year = -1
    term = -1

    for lecture in Lecture.objects(Q(students__in=[session.get_account().student_id]) | Q(admins__in=[session.get_account().id])).\
            order_by('-year', '-term'):
        if lecture.year != year or lecture.term != term:
            lectures.append(c_lectures)
            c_lectures = []
            year = lecture.year
            term = lecture.term
        c_lectures.append(lecture)
    lectures.append(c_lectures)

    lectures = lectures[1:]
    return render_template('module/lecture/my.html', lectures=lectures)
예제 #17
0
def load_student_ids(campus_id):
    page = 0

    student_ids = []

    while True:
        lectures = Lecture.objects(campus_id=campus_id).skip(20*page).limit(20)

        if len(lectures) < 1:
            break

        print('load page %s' % page)
        for lecture in lectures:
            for student in lecture.students:
                student_ids.append(student)

        page += 1

    student_ids = list(set(student_ids))
    student_ids.sort(reverse=True)

    return student_ids
예제 #18
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_attendance(year, term, code):
    return render_template('module/lecture/detail/attendance.html', year=year, term=term, code=code,
                           lecture=Lecture.get_lecture(year, term, code))
예제 #19
0
def search(campus_id, query, page, size=24, year=None, term=None):
    from opencampus.module.lecture.models import Lecture
    results = []

    page -= 1

    if query == '':
        if not year or not term:
            query = Lecture.objects(campus_id=campus_id)
        else:
            query = Lecture.objects(campus_id=campus_id, year=year, term=term)

        for lecture in query.skip(page*size).limit(size):
            results.append({
                'year': lecture.year,
                'term_code': lecture.term,
                'term': lecture.get_term_text(),
                'code': lecture.code,
                'type': lecture.type,
                'subject_code': lecture.subject_code,
                'subject_name': lecture.subject_name,
                'credit': lecture.credit,
                'grade': lecture.grade,
                'departments': lecture.departments,
                'professors': lecture.professors,
                'tags': lecture.tags if lecture.tags else [],
                'term_str': Lecture.term_int_to_str(lecture.term)
            })
    else:
        es = Elasticsearch(hosts=app.config.get('ELASTICSEARCH_HOSTS'))

        search_body = {
            'from': page*size,
            'size': size,
            'sort': ['_score', {'year': 'desc'}, {'term_code': 'desc'}],
            'query': {
                'bool': {
                    'must': [
                        {
                            'query_string': {
                                'default_field': '_all',
                                'query': query
                            }
                        }
                    ]
                }
            }
        }

        if year and term:
            search_body['query']['bool']['must'] += [
                {
                    'term': {
                        'lecture.year': year
                    }
                },
                {
                    'term': {
                        'lecture.term_code': term
                    }
                }
            ]

        res = es.search(index=_get_index_name(campus_id), body=search_body)

        for lecture in res.get('hits').get('hits'):
            lecture = lecture.get('_source')
            lecture['term_str'] = Lecture.term_int_to_str(lecture['term_code'])
            results.append(lecture)

    return results
예제 #20
0
            campus_id=campus_id,
            student_id=student_id1
        )

    analogue_obj.point = analogue
    analogue_obj.target = student_id2
    analogue_obj.save()

    print('%s-%s %s' % (student_id1, student_id2, analogue))


campus_page = 0

while True:
    campuses = Campus.objects().skip(20*campus_page).limit(20)

    if len(campuses) < 1:
        break

    campus_page += 1

    for campus in campuses:
        campus_student_ids = load_student_ids(campus.id)

        for student_id1 in campus_student_ids:
            lecture1 = Lecture.objects(students__in=[student_id1], campus_id=campus.id)
            for student_id2 in campus_student_ids:
                lecture2 = Lecture.objects(students__in=[student_id2], campus_id=campus.id)

                check_data(campus.id, student_id1, lecture1, student_id2, lecture2)
예제 #21
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_board_show_article(year, term, code, article_id):
    return render_template('module/lecture/detail/article.html', year=year, term=term, code=code,
                           lecture=Lecture.get_lecture(year, term, code), article_id=article_id)
예제 #22
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_detail(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)
    from opencampus.module.place.models import Place
    places = Place.objects(campus_id=request.campus.id, names__in=[time_info.place for time_info in lecture.timetable])
    return render_template('module/lecture/detail/information.html', year=year, term=term, code=code,
                           lecture=lecture, places=places)
예제 #23
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_auth_admin_method(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)

    # TODO : Remove
    lecture.email = '*****@*****.**'
    lecture.phone = '+821051277004'

    if not session.get_account():
        return '<p>로그인 후 사용해주세요</p>'

    from opencampus.module.lecture.authmodels import LectureAuthRequestInfo

    if request.form.get('type') == 'email':
        if not lecture.email:
            return '강의정보에 이메일이 등록되어 있지 않습니다'

        at_index = lecture.email.find('@')

        email_view = '*' * (at_index-3) + lecture.email[at_index-3:]
        if at_index < 3:
            email_view = '*' * at_index + lecture.email[at_index:]

        try:
            from opencampus.common.sendmail import send_email
            send_email('[오픈캠퍼스] 관리자 인증 메일 - %s년 %s학기 %s(%s)' % (lecture.year, lecture.get_term_text(),
                                                                lecture.code, lecture.subject_name),
                       render_template('module/lecture/detail/sendemail.html', lecture=lecture,
                                       auth_code=LectureAuthRequestInfo.create_request(lecture.id, 'email', session.get_account().id)),
                       [lecture.email])
        except Exception as e:
            print(e)
            return '<p>메일 발송 도중 에러가 발생하였습니다</p>'

        return '<p>%s으로 이메일이 발송되었습니다.</p><p>이메일에 기재되어 있는 방법을 이용하여 인증을 진행하여 주십시오.</p>' % email_view

    if request.form.get('type') == 'ars':
        if not lecture.phone:
            return '강의정보에 전화번호가 등록되어 있지 않습니다'

        phone_view = lecture.phone[:7] + '*' * (len(lecture.phone) - 7)
        body = '<p>%s으로 ARS가 발송되었습니다.</p><p>전화를 받으신 후 아래의 인증코드를 입력하신후 # 버튼을 눌러주세요.</p>' % phone_view
        body += '<div class="well well-sm"><h2>3333</h2></div>'
        return body
    if request.form.get('type') == 'sms':
        if not lecture.phone:
            return '강의정보에 전화번호가 등록되어 있지 않거나 휴대폰 번호가 아닙니다'

        phone_view = lecture.phone[:7] + '*' * (len(lecture.phone) - 7)

        auth_code = LectureAuthRequestInfo.create_request(lecture.id, 'sms', session.get_account().id)

        from twilio.rest import TwilioRestClient
        account_sid = app.config.get('TWILIO_ACCOUNT_SID')
        auth_token = app.config.get('TWILIO_AUTH_TOKEN')
        client = TwilioRestClient(account_sid, auth_token)
        message = client.messages.create(body="[오픈캠퍼스 인증] 인증번호 %s" % auth_code,
                                         to=lecture.phone,
                                         from_="+18559731333")

        body = '<p>%s으로 SMS가 발송되었습니다.</p><p>수신된 인증코드를 입력하여 주시기 바랍니다.</p>' % phone_view
        body += '<form action="%s" method="GET">' % url_for('campus.lecture_auth_admin_method_cb', year=year, term=term, code=code)
        body += '<input type="number" name="auth_code" class="form-control input-lg" placeholder="인증번호">'
        body += '<button type="submit" class="btn btn-danger btn-lg">인증</button>'
        body += '</form>'
        return body
    if request.form.get('type') == 'manual':
        body = '<p>해당 강의 정보와 관리자임을 증빙 할 수 있는 서류 등을 첨부하여</p>'
        body += '<p><a href="mailto:[email protected]">[email protected]</a>로 보내주시기 바랍니다.</p>'
        return body

    return '비정상 접근'
예제 #24
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_board_write(year, term, code, board_id):
    return render_template('module/lecture/detail/write_article.html', year=year, term=term, code=code,
                           lecture=Lecture.get_lecture(year, term, code))
예제 #25
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_homework(year, term, code):
    return render_template('module/lecture/detail/homework.html', year=year, term=term, code=code,
                           lecture=Lecture.get_lecture(year, term, code))
예제 #26
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_admin_administrators_remove(year, term, code):
    from opencampus.module.account.models import Account
    account = Account.objects(account_id=request.form.get('account_id')).get()
    lecture = Lecture.get_lecture(year, term, code)
    Lecture.objects(id=lecture.id).update_one(pull__admins=account.id)
    return redirect(url_for('campus.lecture_admin_administrators', year=year, term=term, code=code))
예제 #27
0
파일: handlers.py 프로젝트: qbx2/opencampus
    def decorated(*args, **kwargs):
        lecture = Lecture.get_lecture(kwargs.get('year', None), kwargs.get('term', None), kwargs.get('code', None))
        if session.get_account().id not in lecture.admins:
            return abort(403)

        return f(*args, **kwargs)
예제 #28
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_admin_grade(year, term, code):
    return render_template('module/lecture/detail/admin/grade.html', year=year, term=term, code=code,
                           lecture=Lecture.get_lecture(year, term, code))
예제 #29
0
파일: handlers.py 프로젝트: qbx2/opencampus
def lecture_admin_board(year, term, code):
    lecture = Lecture.get_lecture(year, term, code)
    return render_template('module/lecture/detail/admin/board.html', year=year, term=term, code=code,
                           lecture=lecture, boards=LectureBoard.objects(lecture_id=lecture.id))