def course_edit(): if request.method == 'POST': course_id = request.form.get('course_id') if request.form.get('sub_id') == 'null': sub_id = None else: sub_id = int(request.form.get('sub_id')) c = course.query.filter(course.id == int(course_id)).first() c.title = request.form.get('course_title') c.item_image = request.form.get('pic') c.description = request.form.get('desc') c.status = 3 ###课程状态0: 未审核;1:已审核;2:审核中 3:已修改 relation = category_to_course.query.filter(category_to_course.course_id==course_id).order_by(desc(category_to_course.id)).first() if not relation: relation = category_to_course(course_id,int(request.form.get('category_id')),sub_id) db_session.add(relation) else: relation.category_id_first = int(request.form.get('category_id')) relation.category_id_second = sub_id db_session.commit() return 'success' try: course_id = int(request.args.get('course_id')) except Exception,e: return 'fail'
def parse(textpath): with open(textpath,'rb') as f: text = f.readlines() for line in text: if not re.match('\d.*',line): continue content = line.split('、')[1].strip() l = re.split('(-|——)', content) jitang_text = l[0].strip().strip('-').strip('——') author = l[-1].strip().strip('-').strip('——') print chardet.detect(jitang_text), chardet.detect(author) try: jitang_text = jitang_text.decode(chardet.detect(jitang_text)['encoding']) author = author.decode(chardet.detect(author)['encoding']) except Exception,e: print e print line print jitang_text print author print 'end of error' continue current = jitang(jitang_text, author) db_session.add(current)
def video_recodrd(): """ 请求链接需要提交的数据 { courseid:xx, videoid:xx, current_time:xxxx }""" user = users.query.filter(users.email == session['email']).first() videoid = int(request.form.get('videoid')) if request.method == 'POST': try: courseid = int(request.form.get('courseid')) current_time = request.form.get('current_time') record = video_record.query.filter( video_record.user_id == user.id, video_record.video_id == videoid).first() if record: record.play_time = current_time else: record = video_record(users=user, course_id=courseid, video_id=videoid, play_time=current_time) db_session.add(record) db_session.commit() return 'success' except Exception, e: logging.error(e) return 'fail'
def copy_course(cid, count): c = course.query.filter(course.status==1,course.id == cid).first() for i in xrange(2, count+2): copy_c = course(c.title, user_id=4, item_image=c.item_image) relation = category_to_course(course=copy_c,category_id_first=5) db_session.add(copy_c) db_session.add(relation) db_session.commit()
def signup(): if 'username' in session: return redirect(url_for("index.index")) if request.method == 'POST': username = request.form.get('username') passwd = request.form.get('passwd') passwd2 = request.form.get('passwd2') auth_code = request.form.get('input_auth_code') current = users.query.filter_by(email=username).first() if current: return render_template('usercontrol/signup.html', msg="该邮箱已被注册,请重新输入!") if passwd != passwd2: return render_template('usercontrol/signup.html', msg="两次输入的密码不相同!") #创建一个用户数据类 try: code = escape(session[username]) except: code = None if auth_code != code: return render_template('usercontrol/signup.html', msg="验证码错误!") #默认昵称为邮箱名去掉@xxx.com default_name = username.split('@')[0] #默认头像图片 default_pic = '/static/images/defaultpic.jpg' new = users(username, hash_string(passwd), name=default_name, head_pic=default_pic) #提交到数据库 db_session.add(new) db_session.commit() session['username'] = default_name session['email'] = username session['userid'] = new.id session['level'] = 9 session['head_pic'] = default_pic logging.info(new.id) try: #删除写入session的注册验证码 del session[username] except: pass return redirect(url_for("index.index")) return render_template('usercontrol/signup.html', msg='')
def create_category(): categorys = [u'文化艺术', u'编程开发', u'体育运动', u'工业类', u'语言学习', u'通识教育',] for c in categorys: cate= category(c, 1) db_session.add(cate) db_session.commit()
def add_collect(): email = session['email'] user = users.query.filter(users.email == email).first() course_id = int(request.form.get('course_id')) confirm = collect.query.filter(collect.user_id == user.id, collect.course_id == course_id).first() if confirm: return 'success' try: new_collect = collect(user_id=user.id, course_id=course_id) db_session.add(new_collect) db_session.commit() return 'success' except Exception, e: logging.error(e) return 'failed'
def create_course(): video_path = r'D:\web\5\*.mp4' course_name = u'Java入门教学' url_path = 'http://video.linyuling.com/5/' vs = glob.glob(video_path) c = course(course_name,user_id=1) db_session.add(c) for i, v in enumerate(vs): clip = VideoFileClip(v) du = int(clip.duration) print du path = url_path + v.split('\\')[-1] current_video = video(course_name + str(i), path, order=i+1,duration=du,course=c) db_session.add(current_video) db_session.commit()
def addNote(): if request.method == 'POST': courseid = request.form.get('courseid') videoid = request.form.get('videoid') note_text = request.form.get('note') user = users.query.filter(users.email == session['email']).first() current_note = note(note_text, users=user, course_id=courseid, video_id=videoid) current_note.str_create_date = current_note.create_date.strftime( '%Y-%m-%d %H:%M:%S') db_session.add(current_note) db_session.commit() return render_template('videoplayer/note.html', user=user, note=current_note)
def addComment(): if request.method == 'POST': courseid = request.form.get('courseid') videoid = request.form.get('videoid') comment_text = request.form.get('comment') user = users.query.filter(users.email == session['email']).first() #创建一个评论实例 com = comment(comment=comment_text, video_id=videoid, course_id=courseid, users=user) com.str_create_date = com.create_date.strftime('%Y-%m-%d %H:%M:%S') #添加数据到orm中间层 db_session.add(com) #提交数据 db_session.commit() return render_template('videoplayer/comment.html', comment=com, user=user)
def store_email(): if request.method == 'GET': try: userid = request.args.get('userid') except: userid = session['userid'] newEmail = request.args.get('newEmail') old_emails = change_email.query.filter( change_email.users_id == int(userid)).all() if old_emails: for email in old_emails: email.is_available = '0' change = change_email(email=newEmail, users_id=int(userid)) db_session.add(change) try: db_session.commit() return 'success' except Exception, e: logging.error(e) return ''
sub_id = request.form.get('sub_id') except Exception,e: return 'fail' if sub_id == 'null': sub_id = None else: sub_id = int(sub_id) #confirm2 = course_temp.query.filter(course_temp.title == course_title).first() confirm = course.query.filter(course.status==1,course.title == course_title).first() if confirm: return u'此课程名字已存在,请重新输入!' new_course = course(title=course_title, item_image=pic, description = desc, user_id = user.id) nc_relation = category_to_course(course=new_course,category_id_first=category_id,category_id_second=sub_id) db_session.add(new_course) db_session.add(nc_relation) db_session.commit() return 'success' @page.route('/course/manage/<int:courseid>') @self_required #需要验证是否为当前用户 def video_manage(courseid): session['ccid'] = courseid user = users.query.filter(users.email == session['email']).first() lessons = video.query.filter(video.course_id == int(courseid)).all() for l in lessons: l.str_create_date = l.create_date.strftime('%Y-%m-%d %H:%M:%S') c_course = course.query.filter(course.id == courseid).first()
def add_category(name, pre_id=None): #上一级分类的在category表里的id,如果是最大分类,则pre_id=None pre = category.query.filter(category.id == pre_id).first() c = category(name, level=pre.level + 1, pre_category_id=pre_id) db_session.add(c)
#db_session.add(user) #db_session.add(email) #db_session.commit() #print email.query.all() #u = users('admin', 'adminaa@localhost') #print users.__table__ #c = course(user,u"老中医按摩系列") #v = video(u'第二课:老中医教你做肩颈按摩','http://video.linyuling.com/game.mp4',course=c) #for i in range(100): # cm = note(u"于铃同学,节日快乐{}".format(i),users=user,course_id=3,video_position="00:50") # db_session.add(cm) for i in range(0): title = u"惊起却回头{}".format(i) cm = comment(users=user,course_id=3,comment=title) db_session.add(cm) #parse('jitang.txt') #text = u'我们一定要给自己提出这样的任务,第一,学习,第二,学习,第三还是学习' #j = jitang(text, u'列宁') #db_session.add(j) #rate = course_rate(8.5, u"不错的一门课程", 2, 8) #op_add_category() #db_session.add(rate) #sql_order_query = video.query.filter(video.course_id == 20) #print sql_order_query #sql_order = sql_order_query.order_by(desc(video.id)).first() #db_session.commit() #rates = course_rate.query.filter(course_rate.course_id == 8)\ #.order_by(desc(course_rate.create_date)).slice(0, 5).all()
def get_course(courseid): user = users.query.filter(users.email == session['email']).first() #每次进入课程详情页的时候,写下用户观看课程的记录 course_record = user_to_course.query.filter( user_to_course.course_id == courseid, user_to_course.user_id == user.id).first() if not course_record: add_record = user_to_course(user.id, courseid) db_session.add(add_record) logging.info('add record: %s ', add_record) #当前登录用户对这个课程的评分 current_user_rate = course_rate.query.filter( course_rate.user_id == user.id, course_rate.course_id == courseid).first() #当前课程信息 current_course = course.query.filter(course.id == int(courseid)).first() #课程所有视频信息 lessons = video.query.filter(video.course_id == int(courseid)).order_by( asc(video.order)).all() learners = db_session.query(func.count(user_to_course.id)).filter( user_to_course.course_id == courseid).first() rate_count = db_session.query(func.count(course_rate.id),func.sum(course_rate.score))\ .filter(course_rate.course_id == courseid).first() logging.info('rate people: %s, rates: %s, learners: %s', rate_count[0], rate_count[1], learners[0]) if learners: current_course.view = learners[0] if rate_count[0] > 0: current_course.score_count = rate_count[0] if rate_count[1]: current_course.score = float(rate_count[1]) / float(rate_count[0]) for v in lessons: v.format_duration = '%02d:%02d' % (int( v.duration / 60), int(v.duration % 60)) current_record = video_record.query.filter( video_record.user_id == user.id, video_record.video_id == v.id).first() if current_record and current_record.is_finished == '1': v.progress = u'{:>6}'.format('已完成') elif current_record and current_record.is_finished == '0': v.progress = '{:>6}%'.format( round(current_record.play_time / float(v.duration) * 100, 2)) else: v.progress = u'{:>6}'.format(' 未开始') rates = course_rate.query.filter(course_rate.course_id == courseid)\ .order_by(desc(course_rate.create_date)).slice(0, 5).all() for i in rates: i.str_create_date = i.create_date.strftime('%Y-%m-%d %H:%M:%S') db_session.commit() is_collect = collect.query.filter(collect.user_id == user.id, collect.course_id == courseid).first() return render_template('videoplayer/course.html', course=current_course, lessons=lessons, rates=rates, current_user_rate=current_user_rate, is_collect=is_collect)
is_collect = collect.query.filter(collect.user_id == user.id, collect.course_id == courseid).first() return render_template('videoplayer/course.html', course=current_course, lessons=lessons, rates=rates, current_user_rate=current_user_rate, is_collect=is_collect) @page.route('/course/score', methods=['GET']) @login_required def add_score(): user = users.query.filter(users.email == session['email']).first() try: course_id = int(request.args.get('course_id')) comment = unicode(unquote(request.args.get('comment'))) score = int(float(request.args.get('score')) * 2) except Exception, e: logging.error(e) return '' rate = course_rate(score, comment, course_id=course_id, user=user) db_session.add(rate) db_session.commit() return render_template('videoplayer/rate_comment.html', rate=rate)