예제 #1
0
def user_login():
    if request.method == 'GET':
        return render_template('login.html')
    from models.user import USER
    import time
    returnObj = {}
    try:
        username = request.form.get('username')
        password = request.form.get('password')
        data_user = USER.objects(username=username, password=password).first()
        if data_user:
            time_now = time.strftime('%Y-%m-%d %X', time.localtime())
            USER.objects(username=username).update(LastloginTime=time_now)
            session['id'] = str(data_user.id)
            session['username'] = data_user.username
            returnObj['data'] = {}
            returnObj['info'] = {'result': 1, 'info': '登陆成功'}
            return redirect('/course')
        else:
            returnObj['data'] = {}
            returnObj['info'] = {'result': 400, 'info': '登录失败'}
            return render_template('400.html', info=returnObj['info'])
    except Exception as e:
        print('user_login:'******'data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
        return render_template('500.html', info=returnObj['info'])
예제 #2
0
def homework_put(courseId, uid):
    from models.user import USER
    import time
    returnObj = {}
    try:
        feedbackObj = {}
        id = courseId
        uid = uid
        course = request.json.get('course')
        content = request.json.get('content')
        time_now = time.strftime('%Y-%m-%d %X', time.localtime())
        feedbackObj['id'] = id
        feedbackObj['uid'] = uid
        feedbackObj['course'] = course
        feedbackObj['content'] = content
        feedbackObj['RecordTime'] = time_now
        USER.objects(id=ObjectId(session['id'])).update_one(
            push__feedbacks=feedbackObj)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 1, 'info': '提交成功'}
    except Exception as e:
        print('我的课程:', e)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
    finally:
        return jsonify(returnObj)
예제 #3
0
 def Oid_function(*args, **kwargs):
     try:
         data = USER.objects(id=session['id']).first()
         envlink = data.envlink
         for Oid, env in envlink.items():
             time_now = time.strftime('%Y-%m-%d %X', time.gmtime())
             if env[1] < time_now:
                 print('Oid clear!', Oid)
                 del envlink[Oid]
                 continue
         USER.objects(id=session['id']).update_one(envlink=envlink)
     except Exception as e:
         print("Oid_check's error:", e)
     finally:
         pass
     return f(*args, **kwargs)
예제 #4
0
def change_one():
    from models.user import USER
    returnObj = {}
    try:
        id = session.get('id')
        username = request.json.get('username')
        password = request.json.get('password')
        mobile = request.json.get('mobile')
        email = request.json.get('email')
        remark = request.json.get('remark')
        USER.objects(id=ObjectId(id)).update(username=username, password=password,\
                                       mobile=mobile, email=email, remark=remark)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 1, 'info': '更改成功'}
    except Exception as e:
        print('change_one:', e)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
    finally:
        return jsonify(returnObj)
예제 #5
0
def create_user():
    if request.method == 'GET':
        return render_template('index.html')
    from models.user import USER
    returnObj = {}
    try:
        username = request.json.get('username')
        password = request.json.get('password')
        email = request.json.get('email')
        mobile = request.json.get('mobile')
        remark = request.json.get('remark')
        courses = []
        feedbacks = []
        data = USER.objects(username=username).first()
        if not password:
            returnObj['data'] = {}
            returnObj['info'] = {'result': 400, 'info': '请输入密码'}
        elif data:
            returnObj['data'] = {}
            returnObj['info'] = {'result': 400, 'info': '用户名重复'}
        else:
            sign = USER(
                username=username,
                password=password,
                mobile=mobile,
                email=email,
                remark=remark,
                courses=courses,
                feedbacks=feedbacks
            )
            sign.save()
            returnObj['data'] = {}
            returnObj['info'] = {'result': 1, 'info': '创建成功'}
    except Exception as e:
        print('create_user:'******'data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
    finally:
        return jsonify(returnObj)
예제 #6
0
def course_me():
    from models.user import USER
    returnObj = {}
    try:
        coursesObj = []
        data_user = USER.objects(id=ObjectId(session['id'])).first()
        data_course = data_user['courses']
        for data in data_course:
            mycourse = {}
            mycourse['id'] = str(data['id'])
            mycourse['uid'] = data['uid']
            mycourse['course'] = data['course']
            coursesObj.append(mycourse)
        returnObj['data'] = {'courses': coursesObj}
        returnObj['info'] = {'result': 1, 'info': '获取成功'}
    except Exception as e:
        print('我的课程:', e)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
    finally:
        return jsonify(returnObj)
예제 #7
0
def user_me():
    from models.user import USER
    returnObj = {}
    try:
        data = {}
        username = session['username']
        data_user = USER.objects(username=username).first()
        data['_id'] = session['id']
        data['username'] = username
        data['email'] = data_user.email
        data['remark'] = data_user.remark
        data['LastloginTime'] = data_user.LastloginTime
        data['mobile'] = data_user.mobile
        returnObj['data'] = data
        returnObj['info'] = {'result': 1, 'info': '返回成功'}
    except Exception as e:
        print('user_me:', e)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
    finally:
        return jsonify(returnObj)
예제 #8
0
def course_detail(courseId):
    import hashlib
    import json
    # from websocket import create_connection
    import urllib
    import time
    import operator
    from models.course import COURSE
    from models.user import USER
    returnObj = {}
    try:
        courseId = ObjectId(courseId)
        data_course = COURSE.objects(id=courseId).first()
        courseArray = []
        courses_detail = data_course.courses
        for key, value in courses_detail.items():
            dataObj = {}
            dataObj['courseId'] = str(courseId)
            dataObj['uid'] = key
            dataObj['course'] = value['course']
            courseArray.append(dataObj)
        courseArray.sort(key=operator.itemgetter('uid'))
        returnObj['courses'] = courseArray
        uid = request.args.get('uid')
        # print(uid)
        if not uid:
            returnObj['course'] = {}
        else:
            courseObj = {}
            course = courses_detail[uid]
            courseObj['courseId'] = str(courseId)
            courseObj['uid'] = uid
            courseObj['course'] = course['course']
            courseObj['description'] = course['description']
            courseObj['datalink'] = course['datalink']
            courseObj['content'] = course['content']
            courseObj['env'] = course['env']
            returnObj['course'] = courseObj
            # print('course =', courseObj)
        envname = request.args.get('envname')
        returnObj['env'] = {}
        if envname:
            # 先检查数据库中是否有过期的环境,有就删掉
            data_user = USER.objects(id=ObjectId(session['id'])).first()
            data_Oid = data_user.envlink
            for OID in list(data_Oid.keys()):
                time_now = time.strftime('%Y-%m-%d %X', time.gmtime())
                if data_Oid[OID][1] < time_now:
                    del data_Oid[OID]
                    print('Expired Oid clear!', OID)
                    continue
            USER.objects(id=session['id']).update_one(envlink=data_Oid)
            # 利用SHA加密取前16位
            sha = hashlib.sha1()
            userid = session.get('id')
            sha.update(userid.encode('utf-8'))
            sha.update(uid.encode('utf-8'))
            Oid = sha.hexdigest()[0:16]
            print("Oid =", Oid)
            data_user = USER.objects(id=ObjectId(session['id'])).first()
            data_Oid = data_user.envlink
            # 检查某环境是否已经存在,若存在则直接调用地址
            if Oid in data_Oid.keys():
                link = data_Oid[Oid][0]
            else:
                # ws = create_connection('http://api.datadynamic.io/api/v1/instance/' + ownerid + '/eureka')
                # result = ws.recv()
                # 使用生成的Oid获取Cid
                url = 'http://api.datadynamic.io/api/v1/instance/' + Oid + '/eureka'
                try:
                    req = urllib.request.Request(url=url,
                                                 data={},
                                                 method='POST')
                    res = urllib.request.urlopen(req)
                    res = json.loads(res.read())[0]
                except urllib.error.HTTPError as e:
                    if e.reason == "Bad Request":
                        req = urllib.request.Request(url=url,
                                                     data={},
                                                     method='DELETE')
                        urllib.request.urlopen(req)
                        print('Cid clear!')
                        req = urllib.request.Request(url=url,
                                                     data={},
                                                     method='POST')
                        res = urllib.request.urlopen(req)
                        res = json.loads(res.read())[0]
                # print(res)
                Cid = res['id'][0:16]
                expires_at = res['expires_at']
                expire = expires_at[0:10] + ' ' + expires_at[11:19]
                # 拼接出环境地址存入数据库并发送至前端
                link = 'http://' + Cid + '-8888-env1.env.datadynamic.io/notebooks/Welcome.ipynb'
                # link = 'http://localhost:8888/notebooks/Welcome.ipynb'
                # expire = '2022-01-01 00:00'
                # Oid = 'localtest'
                data_Oid[Oid] = [link, expire]
                USER.objects(id=ObjectId(session['id'])).update_one(
                    envlink=data_Oid)
            returnObj['env'] = {'envname': envname, 'envlink': link}
            # print(link)
    except Exception as e:
        print('课程内容:', e)
        returnObj['data'] = {}
        returnObj['info'] = {'result': 500, 'info': '后台异常'}
    finally:
        return render_template('learn.html',
                               courses=returnObj['courses'],
                               course=returnObj['course'],
                               env=returnObj['env'])