def check_content(self, c_type, content, img_uri, audio_uri): if c_type == Task.C_TYPE_IMG and img_uri == '': raise ext.ParamError(U'图片URI不能为空') if c_type == Task.C_TYPE_AUDIO and audio_uri == '': raise ext.ParamError(U'音频片URI不能为空') if c_type not in Task.ALL_C_TYPE: raise ext.ParamError(U'c_type错误')
def json_get(key, default=None, type=None, is_raise=0, must=0, obj=None): """ 由于request.json.get方法不像request.args.get方法,可以支持type参数,所以用这个方法来实现json.get的type参数 :param key: 参数名称 :param type: 类型,如 int float unicode 如果转换成功,返回转换后的值,否则抛出ParamError异常 """ json = obj or request.json or dict() try: rv = json[key] if type is not None: rv = type(rv) except KeyError: rv = default except (ValueError, TypeError) as e: if default is not None: rv = default else: if is_raise: raise ext.ParamError(u'参数%s必须是%s类型' % (key, str(type))) else: rv = default if must and rv is None: raise ext.ParamError(U'invalid %s' % key) return rv
def login(): username = utils.json_get("username") password = utils.json_get("password") if username is None: raise ext.ParamError(u'username不能为空') if password is None: raise ext.ParamError(u'password不能为空') return user_mgr.login(username, password)
def del_task(self, user_id, task_id, pwd): """删除任务""" task = self.get_db_task(task_id, pwd) if task.user_id != user_id: raise ext.ParamError(u'没有权限') if task.ddl < datetime.now(): raise ext.ParamError(u'过期任务不能删除') task.status = Task.STATUS_DEL utils.commit_db(db, e_log, u'【删除任务】task_id:%s' % task_id) return dict()
def upload(): """文件上传""" file_path = request.headers.get('FILEPATH') if file_path is None: raise ext.ParamError(U'上传文件错误') suffix = file_path.split('.')[-1] if suffix not in ('png', 'jpg', 'jpeg', 'mp3', 'silk'): raise ext.ParamError(U'不支持该文件格式') # mtype = 'mp3' if suffix == 'mp3' else "image/jpeg" f = request.files['file'] data = f.read() print 'data len', len(data) key = img_mgr.upload_file_online1('', data, suffix) return key
def reply(self, user_id, c_type, content, img_uri, audio_uri, task_id, pwd): """回复""" self.check_content(c_type, content, img_uri, audio_uri) task = self.get_db_task(task_id, pwd) if datetime.now() > task.ddl: raise ext.ParamError(u'任务已过期') # if user_id == task.user_id: # raise ext.OtherError(u'本人不能操作') if task.type != Task.TYPE_REPLY: raise ext.OtherError(U'不允许接龙') r = Reply.query.filter(Reply.user_id == user_id, Reply.task_id == task_id, Reply.status == Reply.STATUS_ON).first() if r: raise ext.OtherError(u'您已经接龙了') user_cache = user_mgr.get_cache(user_id) r = Reply( user_id=user_id, c_type=c_type, content=content, img_uri=img_uri, audio_uri=audio_uri, task_id=task_id, img_url=user_cache['img_url'], nickname=user_cache['nickname'] ) db.session.add(r) utils.commit_db(db, e_log, u'【回复】user_id:%s' % user_id) return dict()
def del_reply(self, user_id, task_id, pwd, reply_id): """删除接龙""" task = self.get_db_task(task_id, pwd) if task.ddl < datetime.now(): raise ext.ParamError(u'过期任务不能删除') reply = Reply.query.filter(Reply.id == reply_id).first() if not reply: raise ext.NotExistsError(u'接龙不存在') if reply.status != reply.STATUS_ON: raise ext.NotExistsError(U'接龙已删除') if task.id != reply.task_id: raise ext.NotExistsError(u'id异常') if reply.user_id != user_id: raise ext.ParamError(u'没有权限') reply.status = Reply.STATUS_DEL utils.commit_db(db, e_log, u'【删除任务】task_id:%s' % task_id) return dict()
def user_wxa_login(): """小程序,登录""" code = utils.form_get('code', type=str) nickname = utils.form_get('nickname', type=str) img_url = utils.form_get('img_url', type=str) if code is None: raise ext.ParamError(u'invalid code') return user_mgr.wxa_login(code, nickname, img_url)
def safe_str2datetime(dt_str, format='%Y-%m-%d'): """转换字符串到datetime格式,安全版""" if dt_str: try: dt = datetime.strptime(dt_str, format) except: raise ext.ParamError(u'日起不符合格式%s' % format) else: dt = None return dt
def get_ddl(self, ddl_type): if ddl_type == 0: ddl = datetime.now() + timedelta(1) elif ddl_type == 1: ddl = datetime.now() + timedelta(7) elif ddl_type == 2: ddl = datetime.now() + timedelta(30) else: raise ext.ParamError(u'未知的超时时间') return ddl
def add_task(): """发任务""" user_id = user_mgr.get_cur_user_id(1) title = utils.form_get('title', type=str, must=1) c_type = utils.form_get('c_type', type=int, must=1) content = utils.form_get('content', type=str, must=1) img_uri = utils.form_get('img_uri', type=str, must=1) audio_uri = utils.form_get('audio_uri', type=str, must=1) ddl = utils.form_get('ddl', type=int, must=1) type_ = utils.form_get('type', type=int, must=1) if type_ not in Task.ALL_TYPE: raise ext.ParamError(u'invalid type') return task_mgr.add_task(user_id, type_, title, c_type, content, img_uri, audio_uri, ddl)