def edit_user(id): page = Page.query.filter_by(slug='admin').first() user = User.query.filter_by(id=id).first() form = EditUserForm() form.timezone.choices = [(t, t) for t in pytz.common_timezones] if form.validate_on_submit(): log_orig = log_change(user) user.username = form.username.data user.email = form.email.data user.avatar = form.avatar.data user.about_me = form.about_me.data user.timezone = form.timezone.data if form.password.data and user.check_password(form.password.data): user.set_password(form.new_password.data) log_change(log_orig, user, 'edited a user') db.session.commit() flash(f"User {user.username} was updated successfully!", "success") return redirect(url_for('admin.users')) form.username.data = user.username form.email.data = user.email form.avatar.data = user.avatar form.about_me.data = user.about_me form.timezone.data = user.timezone return render_template('admin/user-edit.html', form=form, tab='users', action='Edit', user=user, page=page)
def post(self, obj_id=None): self.set_object(obj_id) self.extra() current_app.logger.info( 'POSTED========================================') if self.form.validate_on_submit(): current_app.logger.info( 'VALIDATED========================================') if self.action == 'Edit': log_orig = log_change(self.obj) self.pre_post() self.form.populate_obj(self.obj) self.post_post() log_change(log_orig, self.obj, self.log_msg) else: self.pre_post() self.form.populate_obj(self.obj) self.post_post() db.session.add(self.obj) log_new(self.obj, self.log_msg) db.session.commit() self.post_submit() flash(self.success_msg, 'success') if self.redirect: return redirect(url_for(**self.redirect)) log_form(self.form) flash_form_errors(self.form) return render_template(self.template, **self.context)
def edit_tag(id): page = Page.query.filter_by(slug='admin').first() tag = Tag.query.filter_by(id=id).first() form = AddTagForm() if form.validate_on_submit(): if form.validate_tag(form.name.data, id): log_orig = log_change(tag) tag.name = form.name.data log_change(log_orig, tag, 'edited a tag') db.session.commit() flash("Tag updated successfully.", "success") return redirect(url_for('admin.tags')) else: flash("<b>Error!</b> That tag already exists.", "danger") form.name.data = tag.name return render_template('admin/tag-edit.html', form=form, tab='tags', tag=tag, action='Edit', page=page)
def edit_page(id, ver_id=None): page = Page.query.filter_by(id=id).first() was_published = page.published print(f"ANCESTORS: {page.ancestors()}") for anc in page.ancestors(): print(f"ANCESTOR: {anc}") form = AddPageForm() form.parent_id.choices = [(0, '---')] + [(p.id, f"{p.title} ({p.path})") for p in Page.query.all()] form.user_id.choices = [(u.id, u.username) for u in User.query.all()] form.notify_group.choices = [ ('', ''), ('all', 'All') ] + Subscriber.SUBSCRIPTION_CHOICES + [('discord', 'Discord Only')] for field in form: print(f"{field.name}: {field.data}") if form.validate_on_submit(): prev_parentid = page.parent_id if page.parent_id else None # Create version from current version = PageVersion( original_id=id, title=page.title, slug=page.slug, template=page.template, parent_id=prev_parentid, banner=page.banner, body=page.body, notes=page.notes, summary=page.summary, author_note=page.author_note, author_note_location=page.author_note_location, sidebar=page.sidebar, tags=page.tags, user_id=page.user_id, notify_group=page.notify_group, pub_date=page.pub_date, published=page.published, path=page.path, dir_path=page.dir_path, ) db.session.add(version) # Update page log_orig = log_change(page) parentid = form.parent_id.data if form.parent_id.data else None page.title = form.title.data page.slug = form.slug.data page.template = form.template.data page.parent_id = parentid page.banner = form.banner.data page.body = form.body.data page.notes = form.notes.data page.summary = form.summary.data page.author_note = form.author_note.data page.author_note_location = form.author_note_location.data page.sidebar = form.sidebar.data page.tags = form.tags.data page.user_id = form.user_id.data page.notify_group = form.notify_group.data page.published = form.published.data page.edit_date = datetime.utcnow() pdate = form.pub_date.data ptime = form.pub_time.data local_tz = form.timezone.data if form.timezone.data else current_user.timezone if pdate and ptime: page.set_local_pub_date(f"{pdate} {ptime}", local_tz) else: page.pub_date = None page.set_path() log_change(log_orig, page, 'edited a page') db.session.commit() if form.notify_subs.data: current_app.logger.debug(form.notify_group.data) page.notify_subscribers(form.notify_group.data) flash("Page updated successfully.", "success") Page.set_nav() return redirect(url_for('admin.edit_page', id=id)) if form.errors: flash("<b>Error!</b> Please fix the errors below.", "danger") versions = PageVersion.query.filter_by(original_id=id).order_by( desc('edit_date')).all() version = PageVersion.query.filter_by( id=ver_id).first() if ver_id else None if version: form.title.data = version.title form.slug.data = version.slug form.template.data = version.template form.parent_id.data = version.parent_id form.banner.data = version.banner form.body.data = version.body form.notes.data = version.notes form.summary.data = version.summary form.author_note.data = version.author_note form.author_note_location.data = version.author_note_location form.sidebar.data = version.sidebar form.tags.data = version.tags form.user_id.data = version.user_id form.notify_group.data = version.notify_group form.pub_date.data = version.local_pub_date(current_user.timezone) form.pub_time.data = version.local_pub_date(current_user.timezone) form.published.data = version.published else: form.title.data = page.title form.slug.data = page.slug form.template.data = page.template form.parent_id.data = page.parent_id form.banner.data = page.banner form.body.data = page.body form.notes.data = page.notes form.summary.data = page.summary form.author_note.data = page.author_note form.author_note_location.data = page.author_note_location form.sidebar.data = page.sidebar form.tags.data = page.tags form.user_id.data = page.user_id form.notify_group.data = page.notify_group form.pub_date.data = page.local_pub_date(current_user.timezone) form.pub_time.data = page.local_pub_date(current_user.timezone) form.published.data = page.published return render_template('admin/page-edit.html', form=form, tab='pages', action='Edit', edit_page=page, versions=versions, version=version, page=Page.query.filter_by(slug='admin').first())
def submit_comment(): form = AuthenticatedCommentForm( ) if current_user.is_authenticated else CommentForm() if form.validate_on_submit(): current_app.logger.debug(request.form) captcha_data = { 'secret': current_app.config['RECAPTCHA_SECRET'], 'response': request.form.get('token'), } resp = requests.post('https://www.google.com/recaptcha/api/siteverify', captcha_data) response_data = resp.json() current_app.logger.debug(response_data) if current_app.config.get('DEVELOPMENT'): response_data['success'] = True #REMOVE if response_data.get('success'): form.reply_id.data = form.reply_id.data if form.reply_id.data else None form.page_id.data = form.page_id.data if form.page_id.data else None form.product_id.data = form.product_id.data if form.product_id.data else None comment = Comment() form.populate_obj(comment) current_app.logger.debug(f'REPLY ID: {comment.reply_id}') comment.ip = request.remote_addr if current_user.is_authenticated: comment.user_id = current_user.id comment.name = current_user.display_name() comment.email = current_user.email db.session.add(comment) db.session.commit() log_new(comment, 'added a comment') flash('Comment added.', 'success') comment.notify() # notify admin if comment.email: subscriber = Subscriber.query.filter_by( email=comment.email).first() if not subscriber: subscriber = Subscriber(first_name=comment.name, email=comment.email, subscription=',Comment Replies,') if form.subscribe.data: subscriber.subscription += ','.join([ i[0] for i in current_app.config['SUBSCRIPTION_GROUPS'] ]) + ',' db.session.add(subscriber) db.session.commit() if form.subscribe.data: log_new(subscriber, 'subscribed') flash('Subscribed!', 'success') subscriber.welcome() else: log_orig = log_change(subscriber) subscriber.subscription = ',Comment Replies,' if form.subscribe.data: subscriber.subscription += ','.join([ i[0] for i in current_app.config['SUBSCRIPTION_GROUPS'] ]) + ',' log_change(log_orig, subscriber, 'updated subscriptions') db.session.commit() flash('You are already subscribed!', 'info') else: if form.subscribe.data: flash( 'You must provide an email address to subscribe. Please <a href="/subscribe">subscribe here</a> instead.', 'info') comment.notify_reply() # Notify commenter replied to else: flash( 'Unable to save comment. Recaptcha flagged you as a bot. If you are not a bot, please try submitting your comment again.', 'danger') return redirect(request.referrer)