def login(user_dic, conn): """ 登录功能,登录成功,将用户信息以{'addr':[session,user_id]}的形式,放到内存中, 多线程操作,必须加锁,锁需要在主线程中生成 :param user_dic: :param conn: :return: """ user = models.User.select_one(name=user_dic['name']) if user: if user.user_type == user_dic['user_type']: if user.password == user_dic['password']: session = common.get_uuid(user_dic['name']) da.mutex.acquire() if user_dic['addr'] in da.alive_user: da.alive_user.pop(user_dic['addr']) da.alive_user[user_dic['addr']] = [session, user.id] da.mutex.release() back_dic = { 'flag': True, 'session': session, 'is_vip': user.is_vip, 'msg': 'login_success' } if user_dic['user_type'] == 'user': last_notice = user_interface.check_notice_by_count(1) back_dic['last_notice'] = last_notice else: back_dic = {'flag': False, 'msg': '密码错误'} else: back_dic = {'flag': False, 'msg': '登录类型不匹配'} else: back_dic = {'flag': False, 'msg': '用户不存在'} common.send_back(back_dic, conn)
def release_notice(user_dic,conn): """ 发布公告功能,取出字典中的公告名字,公告内容,用户id,并存入数据库 :param user_dic: :param conn: :return: """ notice = models.Notice(name=user_dic['notice_name'],content=user_dic['notice_content'],user_id=user_dic['user_id']) notice.save() back_dic = {'flag':True,'msg':'公告发布成功'} common.send_back(back_dic,conn)
def delete_movie(user_dic,conn): """ 删除视频,不是真正的删除,在视频表中的is_delete字段设为1 :param user_dic: :param conn: :return: """ movie = models.Movie.select_one(id=user_dic['movie_id']) movie.is_delete = 1 movie.update() back_dic = {'flag':True,'msg':'电影删除成功'} common.send_back(back_dic,conn)
def check_movie(user_dic,conn): """ 通过md5校验数据中该电影是否已经存在了 :param user_dic: :param conn: :return: """ movie = models.Movie.select_one(file_md5=user_dic['file_md5']) if movie: back_dic = {'flag':False,'msg':'该电影已存在'} else: back_dic = {'flag':True} common.send_back(back_dic,conn)
def register(user_dic, conn): """ 注册功能 :param user_dic: :param conn: :return: """ user = models.User.select_one(name=user_dic['name']) if user: back_dic = {'flag': False, 'msg': '用户已存在'} else: user = models.User(name=user_dic['name'], password=user_dic['password'], user_type=user_dic['user_type']) user.save() back_dic = {'flag': True, 'msg': '注册成功'} common.send_back(back_dic, conn)
def upload_movie(user_dic,conn): """ 上传视频功能 :param user_dic: :param conn: :return: """ recv_size = 0 print('------>',user_dic['file_name']) file_name = common.get_uuid(user_dic['file_name'])+user_dic['file_name']) path = os.path.join(settings.BASE_MOVIE_LIST,file_name) with open(path,'wb') as f: while recv_size < user_dic['file_size']: recv_data = conn.recv(1024) f.write(recv_data) recv_size += len(recv_data) print('%s:上传成功' %file_name) movie = models.Movie(name=file_name,path=path,if_free=user_dic['is_free'],user_id=user_dic['user_id'],file_md5=user_dic['file_md5']) movie.save() back_dic = {'flag':True,'msg':'上传成功'} common.send_back(back_dic,conn)