def delete_subscribe(): class PostData(TypedDict): endpoint: str channel: str if not current_user.is_role('admin'): return api_utils.response_unauthorized() post_data: PostData = request.get_json() endpoint = post_data['endpoint'] channel = post_data['channel'] if channel == 'administration': if not current_user.is_authenticated: return login.unauthorized() current_user.remove_subscription(endpoint) db.session.commit() return api_utils.response_ok() else: # FIXME: This code is not really done, but until we can send out daily menus in a consistent manner it'll # have to be this way return api_utils.response_bad_request()
def post_learning(): class PostData(TypedDict): id: str course_name_correct: bool course_type: int course_sub_type: int price_students_correct: bool price_staff_correct: bool if not current_user.is_role('learner'): return api_utils.response_unauthorized() post_data: PostData = request.get_json() datapoint = LearningDatapoint.find_by_id(int(post_data['id'])) datapoint.user_submit( current_user, { 'course_name_correct': post_data['course_name_correct'], 'course_type': post_data['course_type'], 'course_sub_type': post_data['course_sub_type'], 'price_students_correct': post_data['price_students_correct'], 'price_staff_correct': post_data['price_staff_correct'] }) db.session.commit() return jsonify({'status': 200, 'message': HTTP_STATUS_CODES[200]}), 200
def get_learning(): if not current_user.is_role('learner'): return api_utils.response_unauthorized() datapoint = LearningDatapoint.get_random(current_user) if datapoint is None: return jsonify({ 'status': 200, 'message': HTTP_STATUS_CODES[200], 'data': None }), 200 processed = json.loads(datapoint.processed_data) result = { 'id': str(datapoint.id), 'screenshot': datapoint.screenshot, 'course_name': processed['name']['nl'], 'course_type': models.CourseType[processed['course_type']].value, 'course_sub_type': models.CourseSubType[processed['course_sub_type']].value, 'price_students': processed['price_students'], 'price_staff': processed['price_staff'], } return jsonify({ 'status': 200, 'message': HTTP_STATUS_CODES[200], 'data': result }), 200
def post_trigger(): class PostData(TypedDict): trigger: str if not current_user.is_role('admin'): return api_utils.response_unauthorized() post_data: PostData = request.get_json() trigger = post_data['trigger'] if trigger == 'notification_test_error': try: raise RuntimeError('Test exception') except RuntimeError as e: notify_admins(messages.ExceptionMessage(triggers.Trigger(), e)) return api_utils.response_ok() elif trigger == 'notification_test_text': notify_admins( messages.TextMessage(triggers.Trigger(), 'Test notification')) return api_utils.response_ok() elif trigger == 'menu_update': from komidabot.komidabot import update_menus update_menus() return api_utils.response_ok() else: return api_utils.response_bad_request()
def put_subscribe(): class PostData(TypedDict): old_endpoint: str endpoint: str keys: Dict[str, str] if not current_user.is_role('admin'): return api_utils.response_unauthorized() post_data: PostData = request.get_json() old_endpoint = post_data['old_endpoint'] endpoint = post_data['endpoint'] keys = post_data['keys'] app = get_app() user: WebUser = app.user_manager.get_user( UserId(old_endpoint, web_constants.PROVIDER_ID)) # FIXME: Change internal ID of user and keys # FIXME: Change admin subscriptions as well? Need to verify this # FIXME: This code is not really done, but until we can send out daily menus in a consistent manner it'll # have to be this way return api_utils.response_bad_request()
def decorated_function(*args, **kwargs): hasrole = False for role in roles: if current_user.is_role(role): hasrole = True if not hasrole: abort(403) return f(*args, **kwargs)
def chart(): view_data = {'title': '报表-管理台'} if current_user.is_delete or not current_user.active: return redirect(url_for('online.login')) elif not current_user.can( Permission.ADMINISTER) and not current_user.is_role( Permission.ECP): return redirect(url_for('admin.posts_management')) view_data['user_cnt'] = mdb_user.db.user.count() view_data['post_cnt'] = mdb_cont.db.posts.count() return render_template('{}/home/chart.html'.format( config["theme"].ADM_THEME_NAME), view_data=view_data)
def decorated_function(*args, **kwargs): if not current_user.is_role(role): abort(403) return f(*args, **kwargs)
def user_edit_profile(uploaded_files, username, sex, addr, info): _data = {} domain = None if 'u-domain' in request.value.all and request.value.all['u-domain'].strip(): names = mdb_sys.db.audit_rules.find_one({'type':'username'}) domain = request.value.all['u-domain'].strip().replace(' ','') if len(domain)<3 or len(domain)>30: flash({'msg':'个性域名:需要3至30个字符!', 'type':'w'}) return jsonify(_data) if not re.search(r"^[a-z0-9]+$",domain): flash({'msg':'个性域名:只能是数字, 小写字母!', 'type':'w'}) return jsonify(_data) elif mdb_user.db.user_profile.find_one({'domain':domain}) or domain==str(current_user.id): flash({'msg':'此个性域名已被使用!', 'type':'w'}) return jsonify(_data) elif domain in names and not current_user.can(Permission.ADMINISTER) and not current_user.is_role(Permission.ECP): flash({'msg':'此个性域名已被使用!', 'type':'w'}) return jsonify(_data) if not username: flash({'msg':'名号不能为空!', 'type':'w'}) return jsonify(_data) user = mdb_user.db.user.find_one({"username":username}) if user and user["_id"] != current_user.id: flash({'msg':'此名号已被使用!', 'type':'w'}) return jsonify(_data) names = mdb_sys.db.audit_rules.find_one({'type':'username'}) try: t_username = username.upper() except: t_username = username if t_username in names['rule'] and not current_user.can(Permission.ADMINISTER) and not current_user.is_role(Permission.ECP): flash({'msg':'此名号已被使用!', 'type':'w'}) return jsonify(_data) if len(username.encode("gbk").decode("gbk")) > 150: flash({'msg':u'最多150字哦!','type':'w'}) return jsonify(_data) # --------------------------------------------------------------- tel = "" # 地址 _provinces = '' _city = '' _area = '' addrs = addr_f() if addr['p'].strip("string:"): for lv in addrs: if lv['id'] == addr['p'].strip("string:"): lv1 = lv _provinces = lv['text'] break if addr['c'].strip("string:"): for lv in lv1['children']: if lv['id'] == addr['c'].strip("string:"): lv2 = lv _city = lv['text'] break if addr['a'].strip("string:"): for lv in lv2['children']: if lv['id'] == addr['a'].strip("string:"): _area = lv['text'] break addr = {"provinces":_provinces} addr['city'] = _city addr['area'] = _area #性别 if sex: sex = int(sex) # 头像 bucket_name = {'b':config['upload'].AVA_B, 'domain':'avatar', 'project':'avatar'} r = img_up(uploaded_files, bucket_name) if r['url'] != -1: if r['url'] == 1: user_profile = { 'username':username, 'addr':addr, 'info':info, 'tel_num':tel, 'sex':sex } else: user_profile = { 'username':username, 'addr':addr, 'info':info, 'tel_num':tel, 'sex':sex, 'avatar_url':r['url'] } u_p = mdb_user.db.user_profile.find_one({'user_id':current_user.id}) if u_p: if not 'default' in u_p['avatar_url']['key']: img_del(u_p['avatar_url']) flash({'msg':'头像更改成功,2秒后更新!.','type':'s'}) else: user_profile = { 'username':username, 'addr':addr, 'info':info, 'tel_num':tel, 'sex':sex } mdb_user.db.user.update({"_id":current_user.id}, {"$set":{"username":username}}) if domain: user_profile['user_domain'] = domain user.domain = domain mdb_user.db.user_profile.update({'user_id':current_user.id}, {'$set':user_profile}) flash({'msg':'信息修改成功哦.','type':'s'}) return _data
def post_add(): ''' status: 0:草稿 1:发布 2:待审核 3:审核未通过 4:已删除 5:自动审核通过 :return: ''' view_data = { 'title': u'发表-{}'.format(config['title'].TITLE), 'hint': '写点东西' } view_data['permission'] = Permission.ECP view_data['help'] = mdb_cont.db.posts.find_one({ 'type': u'帮助', 'title': u'文章编辑帮助' }) subject = request.args.get('subject') if subject == "tech": view_data['hint'] = '科技有趣' elif subject == "ART": view_data['hint'] = '音乐|艺术' elif subject == "sys": if not current_user.can( Permission.ADMINISTER) and not current_user.is_role( Permission.ECP): abort(404) view_data['hint'] = '系统告示' if not subject in config['post'].SUBJECT: abort(404) else: form = EditPost(subject) if form.issue.data: if not form.title.data: flash({'type': 'w', 'msg': u'标题不能为空哦!'}) form.s_type.data = form.s_type.data form.body.data = form.body.data return render_template('post/posts/edit.html', form=form, view_data=view_data) # if current_user.can(Permission.ECP): # status = 1 # else: # status = 2 status = 1 # 封面图片 img_url = post_img(form.body.data, form.s_type.data) form.body.data = edit_img_log_claer(form.body.data, None) tag_list = request.form.getlist("boolean_l") post = { 'user_id': current_user.id, 'title': form.title.data.strip(), 'body': form.body.data, 'tag': tag_list, 'type': form.s_type.data, 'img_url': img_url, 'praise': 0, 'praise_id': [], 'pv': 0, 'pv_id': [], 'status': status, 'time': time.time(), 'update_time': time.time(), 'is_been': 1, 'subject': subject, } post_id = mdb_cont.db.posts.insert(post) post_cnt_update(current_user.id) if status == 1: flash({ 'msg': u'发表成功!首页|专栏可能延迟{}秒推出.'.format(config['cache_timeout'].POSTS), "type": 's' }) return redirect(url_for('post.show', post_id=post_id)) else: return redirect(url_for('post.preview', post_id=post_id)) elif form.draft.data: if not form.title.data: flash({'type': 'w', 'msg': u'标题不能为空哦!'}) form.s_type.data = form.s_type.data form.body.data = form.body.data return render_template('post/posts/edit.html', form=form, view_data=view_data) tag_list = request.form.getlist("boolean_l") # 封面图片 img_url = post_img(form.body.data, form.s_type.data) form.body.data = edit_img_log_claer(form.body.data, None) post = { 'user_id': current_user.id, 'title': form.title.data.strip(), 'body': form.body.data, 'tag': tag_list, 'type': form.s_type.data, 'img_url': img_url, 'praise': 0, 'praise_id': [], 'pv': 0, 'pv_id': [], 'status': 0, 'time': time.time(), 'update_time': time.time(), 'is_been': 0, 'subject': subject, } post_id = mdb_cont.db.posts.insert(post) post_cnt_update(current_user.id, ) return redirect(url_for('post.preview', post_id=post_id)) # view_data view_data['tag_s'] = [] tags = mdb_sys.db.type.find_one({'project': "post-tag", "subject": "tag"}) if tags: view_data['tag_s'] = tags["type"] view_data['tag_u'] = [] tags = mdb_cont.db.tag.find_one({'user_id': ObjectId(current_user.id)}) if tags: view_data['tag_u'] = tags['tag'] view_data['post'] = {'img_url': 'images/post_img/未分类_default.png'} return render_template('{}/post/edit.html'.format(Theme.THEME_NAME), form=form, view_data=view_data)
def post_edit(post_id): view_data = { 'title': u'编辑-{}'.format(config['title'].TITLE), 'hint': '正在编辑' } view_data['permission'] = Permission.ECP view_data['help'] = mdb_cont.db.posts.find_one({ 'type': u'帮助', 'title': u'文章编辑帮助' }) post = mdb_cont.db.posts.find_one_or_404({ '_id': ObjectId(post_id), 'user_id': current_user.id }) if post['subject'] == "tech": view_data['hint'] = '科技有趣' elif post['subject'] == "art": view_data['hint'] = '音乐|艺术' elif post['subject'] == "sys": if not current_user.is_role(Permission.ECP): abort(404) view_data['hint'] = '系统告示' form = EditPost(post['subject']) if form.issue.data: # if current_user.can(Permission.ECP): # status = 1 # else: # status = 2 status = 1 tag_list = request.form.getlist("boolean_l") # 封面图片 img_url = post_img(form.body.data, form.s_type.data) if current_user.can(Permission.ECP): form.body.data = sys_edit_img_log_claer( form.body.data, post_title=form.title.data.strip()) else: form.body.data = edit_img_log_claer(form.body.data, post_id) # tag if post['is_been']: _time = post['time'] _is_been = post['is_been'] else: _time = time.time() _is_been = 1 post = { 'title': form.title.data.strip(), 'body': form.body.data, 'status': status, 'type': form.s_type.data, 'img_url': img_url, 'time': _time, 'is_been': _is_been, 'update_time': time.time(), } if tag_list: post['tag'] = tag_list mdb_cont.db.posts.update( { '_id': ObjectId(post_id), 'user_id': current_user.id }, {'$set': post}) if status == 1: post_cnt_update(current_user.id) flash({ 'msg': u'更新成功!首页|专栏可能延迟{}秒更新.'.format(config['cache_timeout'].POSTS), "type": 's' }) return redirect(url_for('post.show', post_id=post_id)) else: return redirect(url_for('post.preview', post_id=post_id)) elif form.draft.data: status = 0 tag_list = request.form.getlist("boolean_l") # 图片 img_url = post_img(form.body.data, form.s_type.data) form.body.data = edit_img_log_claer(form.body.data, post_id) post = { 'title': form.title.data.strip(), 'body': form.body.data, 'type': form.s_type.data, 'img_url': img_url, 'status': status, 'update_time': time.time() } if tag_list: post['tag'] = tag_list post_cnt_update(current_user.id) mdb_cont.db.posts.update( { '_id': ObjectId(post_id), 'user_id': current_user.id }, {'$set': post}) return redirect(url_for('post.preview', post_id=post_id)) # view_data view_data['tag_s'] = [] tags = mdb_cont.db.tag.find_one({'user_id': 0}) if tags: view_data['tag_s'] = tags['tag'] view_data['tag_u'] = [] tags = mdb_cont.db.tag.find_one({'user_id': current_user.id}) if tags: view_data['tag_u'] = tags['tag'] #post form.title.data = post['title'] form.body.data = post['body'] form.s_type.data = post['type'] view_data['edit'] = True view_data['post'] = post # 记录图片url post_img_statis(post['body'], post['_id']) return render_template('post/posts/edit.html', form=form, view_data=view_data)