def validate_form(form, name): form.csrf_enabled = False form.validate() return { "form": name, "errors": [{ "name": k, "errors": [str(inner_v) for inner_v in v] } for k, v in form.errors.items()] }
def supercharge(self): data = (request.form or request.args).copy() # Ugly reverse mapping of source labels source = data.get('source') if source: for id, label in Source.get_form_choices(): if source == label: data['source'] = id form = ImportForm(data, csrf_enabled=False) form.source.choices = list(Source.get_form_choices()) form.category.choices = [(-1, '')] +\ list(Category.get_form_choices(data.get('locale') or 'en-us', True)) errors = {} if form.validate(): if form.category.data < 0: errors.update({'category': 'Missing category'}) elif not form.user.data: errors.update({'user': '******'}) elif not form.channel.data: errors.update({'channel': 'Missing channel'}) elif form.type.data != 'video': errors.update({'type': 'Type should be Video'}) elif int(form.source.data) != 1: errors.update({'source': 'Source should be YouTube'}) else: supercharge(request.form['title'], form.category.data, form.channel.data, form.user.data, form.id.data) return jsonify([]), 202 return jsonify({'error': form.errors or errors}), 400
def edit(self): """ Edit view method """ path = request.args.getlist('path') next_url = None if not path: return redirect(url_for('.index')) if len(path) > 1: next_url = url_for('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = EditForm(helpers.get_form_data()) error = False if helpers.validate_form_on_submit(form): form.process(request.form, content='') if form.validate(): try: with open(full_path, 'w') as f: f.write(request.form['content']) except IOError: flash(gettext("Error saving changes to %(name)s.", name=path), 'error') error = True else: self.on_edit_file(full_path, path) flash(gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: try: with open(full_path, 'r') as f: content = f.read() except IOError: flash(gettext("Error reading %(name)s.", name=path), 'error') error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: try: content.decode('utf8') except UnicodeDecodeError: flash(gettext("Cannot edit %(name)s.", name=path), 'error') error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error)
def accept_process(self): form = UploadAcceptForm(request.form) form.category.choices = list(Category.get_form_choices('en-us', True)) if form.validate(): metadata = dict(label=form.owner_username, **form.data) dst = self._move_video(form.path.data, 'video/%s/' % form.owner_username) metadata['path'] = dst ooyala.create_asset_in_background(dst, metadata) flash('Processing "%s"...' % form.title.data) return redirect(url_for('review.index')) return self.render('admin/upload_accept.html', form=form)
def index(self): global logger form = UnitReadForm(request.form) if logger.error is not None: flash('%s' % logger.error, 'error') logger.error = None if logger.busy_flag: if logger.error is None: flash('Working on earlier request.', 'error') return self.render('admin/home.html', form=form, return_url = '/admin/') if request.method == 'POST' and form.validate(): file_name = "%s_%s_%s.csv" % ( form.unit.data, form.date.data.strftime("%Y-%m-%d_%H-%M"), form.number.data) file_name = file_name.lower().replace(' ','_') logger.busy_flag = True try: logger.open_soc(form.unit.data.com) if logger.soc: msg = 'Writing data to file "%s".' % file_name flash(msg) logger.run = True thread.start_new_thread(logger.get_data, (file_name, form, Points.query)) return self.render('admin/home.html', form=form, return_url = '/admin/') except Exception, e: flash('%s' % e, 'error') logger.run = False logger.busy_flag = False
def edit(self): """ Edit view method """ path = request.args.getlist('path') next_url = None if not path: return redirect(url_for('.index')) if len(path) > 1: next_url = url_for('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = EditForm() error = False if request.method == 'POST': form.process(request.form, content='') if form.validate(): try: with open(full_path, 'w') as f: f.write(request.form['content']) except IOError: flash( gettext("Error saving changes to %(name)s.", name=path), 'error') error = True else: flash( gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: try: with open(full_path, 'r') as f: content = f.read() except IOError: flash(gettext("Error reading %(name)s.", name=path), 'error') error = True except: flash( gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: try: content.decode('utf8') except UnicodeDecodeError: flash(gettext("Cannot edit %(name)s.", name=path), 'error') error = True except: flash( gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error)
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist('path') if not path: return redirect(self.get_url('.index')) if len(path) > 1: next_url = self.get_url('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) if not self.is_accessible_path(path) or not self.is_file_editable( path): flash(gettext('Permission denied.')) return redirect(self._get_dir_url('.index')) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = EditForm(helpers.get_form_data()) error = False if helpers.validate_form_on_submit(form): form.process(request.form, content='') if form.validate(): try: with codecs.open(full_path, 'w', encoding='utf-8') as f: f.write(request.form['content']) except IOError: flash( gettext("Error saving changes to %(name)s.", name=path), 'error') error = True else: self.on_edit_file(full_path, path) flash( gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: try: with codecs.open(full_path, 'r', encoding='utf-8') as f: content = f.read() except IOError: flash(gettext("Error reading %(name)s.", name=path), 'error') error = True except: flash( gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error, fileExtension=fileExtension)
def validate_form(form,name): form.csrf_enabled=False form.validate() return { "form" : name, "errors" : [{ "name" : k, "errors" : [str(inner_v) for inner_v in v] } for k, v in form.errors.items()]}
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist("path") if not path: return redirect(self.get_url(".index")) if len(path) > 1: next_url = self.get_url(".edit", path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) if not self.is_accessible_path(path) or not self.is_file_editable(path): flash(gettext("Permission denied.")) return redirect(self._get_dir_url(".index")) dir_url = self._get_dir_url(".index", os.path.dirname(path)) next_url = next_url or dir_url form = EditForm(helpers.get_form_data()) error = False if helpers.validate_form_on_submit(form): form.process(request.form, content="") if form.validate(): try: with open(full_path, "w") as f: f.write(request.form["content"]) except IOError: flash(gettext("Error saving changes to %(name)s.", name=path), "error") error = True else: self.on_edit_file(full_path, path) flash(gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: try: with open(full_path, "r") as f: content = f.read() except IOError: flash(gettext("Error reading %(name)s.", name=path), "error") error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), "error") error = True else: try: content = content.decode("utf8") except UnicodeDecodeError: flash(gettext("Cannot edit %(name)s.", name=path), "error") error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), "error") error = True else: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error)
def index(self): ctx = {} data = (request.form or request.args).copy() # Ugly reverse mapping of source labels source = data.get('source') if source: for id, label in Source.get_form_choices(): if source == label: data['source'] = id form = ImportForm(data, csrf_enabled=False) form.source.choices = list(Source.get_form_choices()) form.category.choices = [(-1, '')] +\ list(Category.get_form_choices(data.get('locale') or 'en-us', True)) user_form = UserForm(data, csrf_enabled=False) ctx['user_form'] = user_form ctx['form'] = form # pre-populate from parameters if request.args.get('tag'): form.tags.data = ','.join(request.args.getlist('tag')) if request.args.get('user'): user = list(User.query.filter_by(username=request.args.get('user')).values('id', 'username')) if user: form.user.data = user[0][0] else: form.user.data = "" if request.args.get('channeltitle'): channel = list(Channel.query.filter_by( title=request.args.get('channeltitle'), owner=form.user.data).values('id', 'title')) if channel: form.channel.data = channel[0][0] else: form.channel.data = '_new:' + request.args.get('channeltitle') if request.args.get('categoryname'): for choice in form.category.choices: if choice[1] == request.args.get('categoryname'): form.category.data = choice[0] if 'source' in data: if form.commit.data and not form.user.data: if user_form.username.data: if user_form.validate(): # update import form with new user form.user.data = self._create_user(user_form) else: return self.render('admin/import.html', **ctx) else: # User and channel will be created from source data assert form.channel.data == '' if form.validate(): if form.commit.data: count, channel = self._import_videos(form) if channel and channel.id: url = '%s?id=%s' % (url_for('channel.edit_view'), channel.id) else: url = url_for('video.index_view') if form.type.data == 'playlist': url += '?flt0_0=' + form.id.data flash('Imported %d videos' % count) return redirect(url) else: ctx['import_preview'] = form.import_data form.commit.data = 'true' return self.render('admin/import.html', **ctx)
def edit(self): """ Edit view method """ next_url = None path = request.args.getlist('path') if not path: return redirect(self.get_url('.index')) if len(path) > 1: next_url = self.get_url('.edit', path=path[1:]) path = path[0] base_path, full_path, path = self._normalize_path(path) if not self.is_accessible_path(path) or not self.is_file_editable(path): flash(gettext('Permission denied.'), 'error') return redirect(self._get_dir_url('.index')) dir_url = self._get_dir_url('.index', os.path.dirname(path)) next_url = next_url or dir_url form = self.edit_form() error = False if self.validate_form(form): form.process(request.form, content='') if form.validate(): try: with open(full_path, 'w') as f: f.write(request.form['content']) except IOError: flash(gettext("Error saving changes to %(name)s.", name=path), 'error') error = True else: self.on_edit_file(full_path, path) flash(gettext("Changes to %(name)s saved successfully.", name=path)) return redirect(next_url) else: helpers.flash_errors(form, message='Failed to edit file. %(error)s') try: with open(full_path, 'rb') as f: content = f.read() except IOError: flash(gettext("Error reading %(name)s.", name=path), 'error') error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: try: content = content.decode('utf8') except UnicodeDecodeError: flash(gettext("Cannot edit %(name)s.", name=path), 'error') error = True except: flash(gettext("Unexpected error while reading from %(name)s", name=path), 'error') error = True else: form.content.data = content return self.render(self.edit_template, dir_url=dir_url, path=path, form=form, error=error)