def login(): """ Handle the login try of a user """ #In case the user is already logged in, we redirect to index if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() try: if form.validate_on_submit(): user_to_log = User.objects(username=form.username.data).first() login_user(user_to_log) user_to_log.handler_logging_successful() cust_logger.info("Logged on user {} successfully".format( user_to_log.username)) flash('Logged in successfully.') #change the identity for permissions, raising the identity changed event : identity_changed.send(current_app._get_current_object(), identity=Identity(current_user.get_id())) return redirect(url_for('index')) except Exception as e: cust_logger.exception(e) cust_logger.warning("Couldn't log on user, redirection to login page") flash('The server is experiencing troubles and failed to register you. Please retry '\ 'or contact our customer service if the problem persists') return render_template('login.html', form=form) #flash(form.errors) cust_logger.info("From failed to be validated") return render_template('login.html', form=form)
def login(): """ Handle the login try of a user """ #In case the user is already logged in, we redirect to index if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() try: if form.validate_on_submit(): user_to_log = User.objects(username=form.username.data).first() login_user(user_to_log) user_to_log.handler_logging_successful() cust_logger.info("Logged on user {} successfully".format(user_to_log.username)) flash('Logged in successfully.') #change the identity for permissions, raising the identity changed event : identity_changed.send(current_app._get_current_object(), identity=Identity(current_user.get_id())) return redirect(url_for('index')) except Exception as e: cust_logger.exception(e) cust_logger.warning("Couldn't log on user, redirection to login page") flash('The server is experiencing troubles and failed to register you. Please retry '\ 'or contact our customer service if the problem persists') return render_template('login.html', form=form) #flash(form.errors) cust_logger.info("From failed to be validated") return render_template('login.html', form=form)
def edit_real_user(): edit_real_user_form = EditRealUserForm(obj=current_user, prefix='edit_real_user') if edit_real_user_form.validate_on_submit(): current_user.name = edit_real_user_form.name.data.strip() current_user.phone = edit_real_user_form.phone.data.strip() current_user.about_me = edit_real_user_form.about_me.data.strip() app = current_app._get_current_object() if current_user.picture_url and os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], 'user', current_user.picture_url)): os.remove(os.path.join(app.config['UPLOAD_FOLDER'], 'user', current_user.picture_url)) current_user.picture_url = upload(f=edit_real_user_form.picture.data, folder='user') flash({'success': u'用户资料修改成功!'}) return render_template('auth/user-edit/real-user.html', editRealUserForm=edit_real_user_form)
def to_real(): to_real_form = ToRealForm(prefix='to_real') if to_real_form.validate_on_submit(): current_user.name = to_real_form.name.data.strip() current_user.phone = to_real_form.phone.data.strip() current_user.about_me = to_real_form.about_me.data.strip() app = current_app._get_current_object() if current_user.picture_url and \ os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], 'user', current_user.picture_url)): os.remove(os.path.join(app.config['UPLOAD_FOLDER'], 'user', current_user.picture_url)) current_user.picture_url = upload(f=to_real_form.picture.data, folder='user') flash({'success': u'申请材料已提交,请等候管理员审核。'}) return render_template('auth/role/to-real.html', toRealForm=to_real_form)
def edit_album(album_id): a = Album.query.get_or_404(album_id) if current_user != a.creator and not (current_user.is_administrator() or current_user.is_moderator()): abort(403) edit_album_form = EditAlbumForm(obj=a, prefix='edit_album') if edit_album_form.validate_on_submit(): app = current_app._get_current_object() if edit_album_form.picture.data.filename is not u'': if a.picture_url and \ os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], 'album', a.picture_url)): os.remove(os.path.join(app.config['UPLOAD_FOLDER'], 'album', a.picture_url)) a.picture_url = upload(f=edit_album_form.picture.data, folder='album') a.title = edit_album_form.title.data.strip() a.percentage = float(edit_album_form.percentage.data) a.introduction = edit_album_form.introduction.data.strip() a.confirmed = False return redirect(url_for('auth.my_albums')) return render_template('auth/albums/edit-album.html', album=a, editAlbumForm=edit_album_form)
def user(user_id): user = User.query.get_or_404(user_id) edit_user_form = EditUserForm(prefix='edit_user', obj=user) edit_user_form.role_id.choices = [(r.id, r.name) for r in Role.query.all()] if edit_user_form.validate_on_submit(): app = current_app._get_current_object() user.name = edit_user_form.name.data.strip() user.phone = edit_user_form.phone.data.strip() user.about_me = edit_user_form.about_me.data.strip() user.role_id = edit_user_form.role_id.data if edit_user_form.disabled.data == u'True': user.disabled = True elif edit_user_form.disabled.data == u'False': user.disabled = False if user.picture_url and \ os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], 'user', user.picture_url)): os.remove(os.path.join(app.config['UPLOAD_FOLDER'], 'user', user.picture_url)) user.picture_url = upload(f=edit_user_form.picture.data, folder='user') return redirect(request.args.get('next') or url_for('auth.users')) return render_template('auth/administrator/user.html', editUserForm=edit_user_form, user=user)
def controller(): """UEditor文件上传接口 config 配置文件 result 返回结果 """ app = current_app._get_current_object() mimetype = 'application/json' result = {} action = request.args.get('action') # 解析JSON格式的配置文件 with open(os.path.join(app.static_folder, 'ueditor', 'config.json')) as fp: try: # 删除 `/**/` 之间的注释 CONFIG = json.loads(re.sub(r'\/\*.*\*\/', '', fp.read())) except: CONFIG = {} if action == 'config': # 初始化时,返回配置文件给客户端 result = CONFIG elif action in ('uploadimage', 'uploadfile', 'uploadvideo'): # 图片、文件、视频上传 if action == 'uploadimage': fieldName = CONFIG.get('imageFieldName') config = { "pathFormat": CONFIG['imagePathFormat'], "maxSize": CONFIG['imageMaxSize'], "allowFiles": CONFIG['imageAllowFiles'] } elif action == 'uploadvideo': fieldName = CONFIG.get('videoFieldName') config = { "pathFormat": CONFIG['videoPathFormat'], "maxSize": CONFIG['videoMaxSize'], "allowFiles": CONFIG['videoAllowFiles'] } else: fieldName = CONFIG.get('fileFieldName') config = { "pathFormat": CONFIG['filePathFormat'], "maxSize": CONFIG['fileMaxSize'], "allowFiles": CONFIG['fileAllowFiles'] } if fieldName in request.files: field = request.files[fieldName] uploader = Uploader(field, config) result = uploader.getFileInfo() else: result['state'] = '上传接口出错' elif action in ('uploadscrawl'): # 涂鸦上传 fieldName = CONFIG.get('scrawlFieldName') config = { "pathFormat": CONFIG.get('scrawlPathFormat'), "maxSize": CONFIG.get('scrawlMaxSize'), "allowFiles": CONFIG.get('scrawlAllowFiles'), "oriName": "scrawl.png" } if fieldName in request.form: field = request.form[fieldName] uploader = Uploader(field, config, 'base64') result = uploader.getFileInfo() else: result['state'] = '上传接口出错' elif action in ('catchimage'): config = { "pathFormat": CONFIG['catcherPathFormat'], "maxSize": CONFIG['catcherMaxSize'], "allowFiles": CONFIG['catcherAllowFiles'], "oriName": "remote.png" } fieldName = CONFIG['catcherFieldName'] source = [] if fieldName in request.form: # 这里比较奇怪,远程抓图提交的表单名称不是这个 source = [] elif '%s[]' % fieldName in request.form: # 而是这个 source = request.form.getlist('%s[]' % fieldName) _list = [] for imgurl in source: uploader = Uploader(imgurl, config, 'remote') info = uploader.getFileInfo() _list.append({ 'state': info['state'], 'url': info['url'], 'original': info['original'], 'source': imgurl, }) result['state'] = 'SUCCESS' if len(_list) > 0 else 'ERROR' result['list'] = _list elif action in ('listimage'): config = { "pathFormat": CONFIG['imageManagerListPath'], "listSize": CONFIG['imageManagerListSize'], "allowFiles": CONFIG['imageManagerAllowFiles'] } lists = List(config) result = lists.getFilesInfo() elif action in ('listfile'): config = { "pathFormat": CONFIG['fileManagerListPath'], "listSize": CONFIG['fileManagerListSize'], "allowFiles": CONFIG['fileManagerAllowFiles'] } lists = List(config) result = lists.getFilesInfo() else: result['state'] = '请求地址出错' result = json.dumps(result) if 'callback' in request.args: callback = request.args.get('callback') if re.match(r'^[\w_]+$', callback): result = '%s(%s)' % (callback, result) mimetype = 'application/javascript' else: result = json.dumps({'state': 'callback参数不合法'}) res = make_response(result) res.mimetype = mimetype res.headers['Access-Control-Allow-Origin'] = '*' res.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,X_Requested_With' return res